mirror of
https://github.com/morluto/rea.git
synced 2026-11-22 06:25:44 +00:00
refactor(domain): charge resource allowances through one Budget owner (#11293)
This commit is contained in:
1 parent
c931ba43ab
commit
3d67404132
11 files changed
+215
-96
No files matched your search
@@ -1,3 +1,4 @@
|
||||
import { Budget } from "../domain/budget.js";
|
||||
import type { FileHandle } from "node:fs/promises";
|
||||
import { ArtifactReaderFailure } from "./ArtifactReader.js";
|
||||
import { readArtifactFileBytes } from "./ArtifactFileRange.js";
|
||||
@@ -39,15 +40,6 @@ const directoryLimit = (): ArtifactReaderFailure =>
|
||||
{ boundary: "memory" },
|
||||
);
|
||||
|
||||
/** Reserve buffers, JS rows and escaped path projections before constructing them. */
|
||||
class UnityFsDirectoryBudget {
|
||||
#remaining = DIRECTORY_BUDGET_BYTES;
|
||||
reserve(bytes: number): void {
|
||||
if (!Number.isSafeInteger(bytes) || bytes < 0 || bytes > this.#remaining)
|
||||
throw directoryLimit();
|
||||
this.#remaining -= bytes;
|
||||
}
|
||||
}
|
||||
/** Signature, version, two bounded strings and the fixed tail; reads are bounded. */
|
||||
const HEADER_PREFIX_MAX = 8 + 4 + UNITYFS_HEADER_STRING_MAX * 2 + 20;
|
||||
const UTF8 = strictUtf8Decoder("preserve");
|
||||
@@ -258,7 +250,7 @@ export const decodeUnityFsLz4 = async (
|
||||
|
||||
const parseBlocks = async (
|
||||
table: Buffer,
|
||||
budget: UnityFsDirectoryBudget,
|
||||
budget: Budget,
|
||||
signal?: AbortSignal,
|
||||
): Promise<UnityFsBlock[]> => {
|
||||
if (table.length < 24)
|
||||
@@ -267,7 +259,7 @@ const parseBlocks = async (
|
||||
if (20 + blockCount * 10 + 4 > table.length)
|
||||
throw unityFsFormatFailure("declared block count exceeds the table");
|
||||
// Includes array slots, numeric fields and the retained lookup representation.
|
||||
budget.reserve(blockCount * 256);
|
||||
budget.charge(blockCount * 256, directoryLimit);
|
||||
const blocks: UnityFsBlock[] = [];
|
||||
let cursor = 20;
|
||||
let compressedStart = 0;
|
||||
@@ -327,7 +319,7 @@ const parseBlocks = async (
|
||||
const parseNodes = async (
|
||||
table: Buffer,
|
||||
blocks: readonly UnityFsBlock[],
|
||||
budget: UnityFsDirectoryBudget,
|
||||
budget: Budget,
|
||||
signal?: AbortSignal,
|
||||
): Promise<UnityFsNode[]> => {
|
||||
let cursor = 20 + blocks.length * 10;
|
||||
@@ -338,7 +330,7 @@ const parseNodes = async (
|
||||
if (cursor + nodeCount * 21 > table.length)
|
||||
throw unityFsFormatFailure("declared node count exceeds the table");
|
||||
// Covers parsed rows plus the reader's owned entries/map; paths are charged separately.
|
||||
budget.reserve(nodeCount * 512);
|
||||
budget.charge(nodeCount * 512, directoryLimit);
|
||||
const streamSize =
|
||||
blocks.length === 0
|
||||
? 0
|
||||
@@ -357,7 +349,7 @@ const parseNodes = async (
|
||||
const nameEnd = table.indexOf(0, cursor);
|
||||
if (nameEnd === -1) throw unityFsFormatFailure("node path is unterminated");
|
||||
// UTF-16 plus repeated JSON-escaped source/canonical path projections.
|
||||
budget.reserve((nameEnd - cursor) * 24);
|
||||
budget.charge((nameEnd - cursor) * 24, directoryLimit);
|
||||
let path: string;
|
||||
try {
|
||||
path = UTF8.decode(table.subarray(cursor, nameEnd));
|
||||
@@ -428,10 +420,10 @@ export const readUnityFsBlocksInfo = async (
|
||||
header.uncompressedBlocksInfoSize > MAX_TABLE_BYTES
|
||||
)
|
||||
throw directoryLimit();
|
||||
const budget = new UnityFsDirectoryBudget();
|
||||
budget.reserve(header.compressedBlocksInfoSize);
|
||||
const budget = new Budget(DIRECTORY_BUDGET_BYTES);
|
||||
budget.charge(header.compressedBlocksInfoSize, directoryLimit);
|
||||
if (header.compression !== UNITYFS_COMPRESSION_NONE)
|
||||
budget.reserve(header.uncompressedBlocksInfoSize);
|
||||
budget.charge(header.uncompressedBlocksInfoSize, directoryLimit);
|
||||
const compressed = await readArtifactFileBytes(
|
||||
handle,
|
||||
layout.blocksInfoOffset,
|
||||
|
||||
Reference in new issue
Block a user