mirror of
https://github.com/morluto/rea.git
synced 2026-12-05 10:34:41 +00:00
* fix(filesystem): close file handles only through the owned-handle owners Three sites still closed a fs/promises FileHandle in a finally block, where a failed close replaced the operation's own failure or result: the agentenv runner head read, the Linux cgroup kernel-file read and the JavaScript analysis transfer writer. They now use withOwnedFileHandle, which retains the operation outcome alongside a failed close. A lint rule, rea/no-raw-file-handle-close, refuses close() on a handle opened from fs/promises or declared as FileHandle, and fs close/closeSync on a descriptor, in src outside src/filesystem/Owned*. * fix(lint): distinguish owned handle class fields * fix(filesystem): retain diagnostic file cleanup owners * test(hopper): retry after confirmed application exit * fix(filesystem): settle doctor peers, sample cgroup outside admission, and bind lint this Desktop doctor waits for every client inspection before reporting a retained close failure. Ghidra memory cgroup sampling no longer passes the resource scope's admission gate, so a pending owner cannot mask the provider failure; failed closes are still retained for session cleanup. The raw FileHandle close rule resolves this through arrow functions only, treats static blocks as static, and keeps #name distinct from constructor parameter properties. * fix(ghidra): join in-flight cgroup samples before session cleanup ArtifactResourceScope.track runs optional work outside the admission gate while cleanup drains still join it, so a cancelled request's final retry cannot finish before a sample retains a failed close. * fix(filesystem): join work tracked during a cleanup drain and report doctor file cleanup as typed errors A cleanup retry now repeats until no tracked work is active and every retained owner has had one attempt, so diagnostic work admitted while an earlier owner is releasing is joined. Doctor reports a retained runner or configuration file close as a ProviderCleanupError bound to its lease instead of rethrowing a raw file failure, so the error document keeps the retained file. * fix(desktop): read the agentenv runner head through the artifact file owner The runner read retained a failed close under one lease while the reported failure carried another, so a successful cleanup retry left the doctor error reporting an already-closed file. withArtifactFileHandle binds one lease to both.
551 lines
21 KiB
TypeScript
551 lines
21 KiB
TypeScript
import { createHash } from "node:crypto";
|
|
import { createReadStream } from "node:fs";
|
|
import { open, stat } from "node:fs/promises";
|
|
import { z } from "zod";
|
|
import { AnalysisResourceConstraintError } from "../../domain/analysisErrorCore.js";
|
|
import type { JsonValue } from "../../domain/jsonValue.js";
|
|
import { safeParseUtf8Json } from "../../domain/safeJson.js";
|
|
|
|
import type { JavaScriptArtifactFile } from "../../domain/javascript/javascriptArtifactFiles.js";
|
|
import type { JavaScriptModuleSemanticIr } from "../../domain/javascript/javascriptModuleSemanticIr.js";
|
|
import {
|
|
JAVASCRIPT_PARENT_HEAP_NODE_OPTIONS,
|
|
JAVASCRIPT_ANALYSIS_TRANSFER_RECORD_BYTES,
|
|
JAVASCRIPT_SEMANTIC_ITEM_MAXIMUM,
|
|
} from "../../domain/javascript/javascriptAnalysisResourceControls.js";
|
|
import type {
|
|
JavaScriptSemanticFileProjection,
|
|
JavaScriptSemanticProjectionCapacity,
|
|
} from "../../domain/javascript/javascriptSemanticFileProjection.js";
|
|
import { digestSchema } from "../../domain/digests.js";
|
|
import {
|
|
javaScriptWorkerGraphRecordSchema,
|
|
javaScriptWorkerModuleHeaderSchema,
|
|
javaScriptWorkerModuleRecordSchema,
|
|
javaScriptWorkerProjectionHeaderSchema,
|
|
} from "./JavaScriptAnalysisWorkerSchemas.js";
|
|
import { OwnedFileHandle } from "../../filesystem/OwnedFileHandle.js";
|
|
import { withOwnedFileHandle } from "../../filesystem/OwnedRegularFile.js";
|
|
|
|
// Budgets bound transfer representation, independently of source length.
|
|
const FRAME_BYTES = JAVASCRIPT_ANALYSIS_TRANSFER_RECORD_BYTES;
|
|
const TRANSFER_BYTES = 512 * 1024 * 1024;
|
|
const MAX_TRANSFER_BYTES = 2 * 1024 * 1024 * 1024;
|
|
const BATCH_BYTES = 1024 * 1024;
|
|
const recordSchema = z.discriminatedUnion("kind", [
|
|
z.strictObject({
|
|
kind: z.literal("projection-header"),
|
|
value: javaScriptWorkerProjectionHeaderSchema,
|
|
}),
|
|
z.strictObject({
|
|
kind: z.literal("module-header"),
|
|
value: javaScriptWorkerModuleHeaderSchema,
|
|
}),
|
|
...javaScriptWorkerGraphRecordSchema.options,
|
|
...javaScriptWorkerModuleRecordSchema.options,
|
|
]);
|
|
|
|
/** Complete-file coordinates authenticated before adopting any worker facts. */
|
|
export const javaScriptAnalysisTransferDescriptorSchema = z.strictObject({
|
|
bytes: z.int().nonnegative().max(MAX_TRANSFER_BYTES),
|
|
sha256: digestSchema,
|
|
records: z.int().nonnegative(),
|
|
module_bytes: z.int().nonnegative(),
|
|
module_records: z.int().nonnegative(),
|
|
graph_bytes: z.int().nonnegative(),
|
|
graph_records: z.int().nonnegative(),
|
|
});
|
|
type Descriptor = z.output<typeof javaScriptAnalysisTransferDescriptorSchema>;
|
|
|
|
/** Conservative retained-object and sealing expansion for validated transfer rows. */
|
|
export const javaScriptTransferRetentionBytes = (
|
|
bytes: number,
|
|
records: number,
|
|
): number => bytes * 8 + records * 64;
|
|
|
|
/** Larger streamed files require corresponding parent admission capacity. */
|
|
export const javaScriptTransferByteBudget = (
|
|
resultBudgetBytes: number,
|
|
): number =>
|
|
Math.min(
|
|
MAX_TRANSFER_BYTES,
|
|
Math.max(TRANSFER_BYTES, Math.floor(resultBudgetBytes / 8)),
|
|
);
|
|
|
|
/**
|
|
* The record ceiling and the 2 GiB file maximum are fixed. The effective file
|
|
* ceiling follows the parent offered result budget and can rise until that maximum.
|
|
*/
|
|
export const javaScriptTransferCeilingRemediation = (
|
|
transferFileBytes: number | undefined,
|
|
): string => {
|
|
const recordBytes = String(JAVASCRIPT_ANALYSIS_TRANSFER_RECORD_BYTES);
|
|
const maximumBytes = String(MAX_TRANSFER_BYTES);
|
|
const fixed = `The ${recordBytes}-byte transfer record ceiling and the ${maximumBytes}-byte transfer file maximum have no runtime override. Raising max_heap_mb does not raise either fixed ceiling. A request that exceeds the record ceiling or that maximum fails the same way when repeated unchanged.`;
|
|
const adjustable = `${JAVASCRIPT_PARENT_HEAP_NODE_OPTIONS} to raise the effective file ceiling up to ${maximumBytes} bytes. Selecting fewer source_paths can also raise this effective file allowance, because earlier retained results and the reserve for remaining sources reduce the offered budget. Neither change raises the record ceiling or the maximum.`;
|
|
if (
|
|
transferFileBytes !== undefined &&
|
|
transferFileBytes >= MAX_TRANSFER_BYTES
|
|
)
|
|
return `${fixed} The effective transfer file ceiling is the ${maximumBytes}-byte maximum. Selecting fewer source_paths does not raise the record ceiling or that maximum.`;
|
|
const effective =
|
|
transferFileBytes === undefined
|
|
? "The effective transfer file ceiling follows the parent offered result budget, which follows the CLI/MCP process heap, and can be lower than the maximum."
|
|
: `The effective transfer file ceiling is ${String(transferFileBytes)} bytes. It follows the parent offered result budget, which follows the CLI/MCP process heap.`;
|
|
return `${fixed} ${effective} ${adjustable}`;
|
|
};
|
|
|
|
/** Reattach transfer recovery after a worker protocol frame drops remediation. */
|
|
export const javaScriptForwardedTransferRemediation = (
|
|
limits: Readonly<Record<string, JsonValue>> | null,
|
|
): string | undefined => {
|
|
const transferFileBytes = limits?.transfer_file_bytes;
|
|
if (
|
|
typeof limits?.transfer_record_bytes !== "number" &&
|
|
typeof transferFileBytes !== "number"
|
|
)
|
|
return undefined;
|
|
return javaScriptTransferCeilingRemediation(
|
|
typeof transferFileBytes === "number" ? transferFileBytes : undefined,
|
|
);
|
|
};
|
|
|
|
const javaScriptTransferCeilingReason = (
|
|
recordBytes: number,
|
|
transferFileBytes: number,
|
|
): string =>
|
|
recordBytes > FRAME_BYTES
|
|
? `A complete JavaScript fact exceeds the ${String(FRAME_BYTES)}-byte transfer record ceiling. This release has no runtime override.`
|
|
: transferFileBytes >= MAX_TRANSFER_BYTES
|
|
? `Complete JavaScript file facts exceed the ${String(MAX_TRANSFER_BYTES)}-byte transfer file maximum. This release has no runtime override.`
|
|
: `Complete JavaScript file facts exceed the effective transfer file ceiling of ${String(transferFileBytes)} bytes (maximum ${String(MAX_TRANSFER_BYTES)} bytes). That effective ceiling follows the parent offered result budget and can rise until the maximum. The record ceiling and that maximum have no runtime override.`;
|
|
|
|
const assertTransferByteBudget = (transferBytes: number): void => {
|
|
if (
|
|
!Number.isSafeInteger(transferBytes) ||
|
|
transferBytes < 1 ||
|
|
transferBytes > MAX_TRANSFER_BYTES
|
|
)
|
|
throw new RangeError(
|
|
"JavaScript transfer requires a valid complete-file byte budget",
|
|
);
|
|
};
|
|
|
|
const encodeTransferRecord = (
|
|
input: unknown,
|
|
previousBytes: number,
|
|
transferBytes: number,
|
|
) => {
|
|
const record = recordSchema.parse(input);
|
|
const line = `${JSON.stringify(record)}\n`;
|
|
const size = Buffer.byteLength(line);
|
|
if (size > FRAME_BYTES || previousBytes + size > transferBytes)
|
|
throw new AnalysisResourceConstraintError(
|
|
"analyze_javascript_application",
|
|
"transport",
|
|
javaScriptTransferCeilingReason(size, transferBytes),
|
|
{
|
|
transfer_record_bytes: FRAME_BYTES,
|
|
transfer_file_bytes: transferBytes,
|
|
record_bytes: size,
|
|
transfer_bytes_at_least: previousBytes + size,
|
|
},
|
|
{
|
|
remediationAction: javaScriptTransferCeilingRemediation(transferBytes),
|
|
},
|
|
);
|
|
return { line, size, kind: record.kind };
|
|
};
|
|
|
|
function* moduleTransferRecords(
|
|
module: JavaScriptModuleSemanticIr,
|
|
): Generator<unknown> {
|
|
yield {
|
|
kind: "module-header",
|
|
value: { coverage: module.coverage, limitations: module.limitations },
|
|
};
|
|
for (const value of module.scopes) yield { kind: "scope", value };
|
|
for (const value of module.bindings) yield { kind: "binding", value };
|
|
for (const value of module.callables) yield { kind: "callable", value };
|
|
for (const value of module.moduleLinks) yield { kind: "module-link", value };
|
|
}
|
|
|
|
/** Measure actual validated module rows before allocating an unadmittable graph. */
|
|
export const measureJavaScriptModuleTransfer = (
|
|
module: JavaScriptModuleSemanticIr,
|
|
transferBytes = TRANSFER_BYTES,
|
|
) => {
|
|
assertTransferByteBudget(transferBytes);
|
|
let bytes = 0;
|
|
let records = 0;
|
|
for (const record of moduleTransferRecords(module)) {
|
|
bytes += encodeTransferRecord(record, bytes, transferBytes).size;
|
|
records += 1;
|
|
}
|
|
return {
|
|
bytes,
|
|
records,
|
|
retentionBytes: javaScriptTransferRetentionBytes(bytes, records),
|
|
};
|
|
};
|
|
|
|
/** Encode validated facts in bounded records, without serializing a whole graph or IR. */
|
|
export const writeJavaScriptAnalysisTransfer = async (
|
|
path: string,
|
|
module: JavaScriptModuleSemanticIr,
|
|
projection: JavaScriptSemanticFileProjection,
|
|
transferBytes = TRANSFER_BYTES,
|
|
): Promise<Descriptor> => {
|
|
assertTransferByteBudget(transferBytes);
|
|
const owner = new OwnedFileHandle(await open(path, "wx", 0o600));
|
|
const output = owner.handle;
|
|
const digest = createHash("sha256");
|
|
let bytes = 0;
|
|
let records = 0;
|
|
let moduleBytes = 0;
|
|
let moduleRecords = 0;
|
|
let graphBytes = 0;
|
|
let graphRecords = 0;
|
|
let batch: string[] = [];
|
|
let batchBytes = 0;
|
|
const flush = async (): Promise<void> => {
|
|
if (batch.length === 0) return;
|
|
const buffer = Buffer.from(batch.join(""));
|
|
// FileHandle.write can be short; writeFile completes the buffer.
|
|
await output.writeFile(buffer);
|
|
digest.update(buffer);
|
|
batch = [];
|
|
batchBytes = 0;
|
|
};
|
|
const append = async (input: unknown): Promise<void> => {
|
|
const { line, size, kind } = encodeTransferRecord(
|
|
input,
|
|
bytes,
|
|
transferBytes,
|
|
);
|
|
bytes += size;
|
|
records += 1;
|
|
if (
|
|
["module-header", "scope", "binding", "callable", "module-link"].includes(
|
|
kind,
|
|
)
|
|
) {
|
|
moduleBytes += size;
|
|
moduleRecords += 1;
|
|
} else {
|
|
graphBytes += size;
|
|
graphRecords += 1;
|
|
}
|
|
batch.push(line);
|
|
batchBytes += size;
|
|
if (batchBytes >= BATCH_BYTES) await flush();
|
|
};
|
|
return withOwnedFileHandle(path, owner, {}, async () => {
|
|
await append({
|
|
kind: "projection-header",
|
|
value: {
|
|
roots: projection.roots,
|
|
nodesTruncated: projection.nodesTruncated,
|
|
...(projection.omittedUnknownOccurrences === undefined
|
|
? {}
|
|
: {
|
|
omittedUnknownOccurrences: projection.omittedUnknownOccurrences,
|
|
}),
|
|
},
|
|
});
|
|
for (const record of moduleTransferRecords(module)) await append(record);
|
|
for (const value of projection.evidenceContexts)
|
|
await append({ kind: "evidence-context", value });
|
|
for (const value of projection.nodes) await append({ kind: "node", value });
|
|
for (const value of projection.relations)
|
|
await append({ kind: "relation", value });
|
|
for (const value of projection.unknowns)
|
|
await append({ kind: "unknown", value });
|
|
for (const value of projection.fingerprints)
|
|
await append({ kind: "fingerprint", value });
|
|
await flush();
|
|
return {
|
|
bytes,
|
|
sha256: digest.digest("hex"),
|
|
records,
|
|
module_bytes: moduleBytes,
|
|
module_records: moduleRecords,
|
|
graph_bytes: graphBytes,
|
|
graph_records: graphRecords,
|
|
};
|
|
});
|
|
};
|
|
|
|
/** Decode producer records into realm-local, source-bound facts; sealing follows in the consumer. */
|
|
export function readJavaScriptAnalysisTransfer(
|
|
path: string,
|
|
descriptor: Descriptor,
|
|
source: Pick<JavaScriptArtifactFile, "path" | "sha256">,
|
|
signal?: AbortSignal,
|
|
): Promise<{
|
|
readonly module: JavaScriptModuleSemanticIr;
|
|
readonly projection: JavaScriptSemanticFileProjection;
|
|
}>;
|
|
/** Validate the transfer while retaining only module facts when graph admission fails. */
|
|
export function readJavaScriptAnalysisTransfer(
|
|
path: string,
|
|
descriptor: Descriptor,
|
|
source: Pick<JavaScriptArtifactFile, "path" | "sha256">,
|
|
signal: AbortSignal | undefined,
|
|
capacity: JavaScriptSemanticProjectionCapacity | null,
|
|
): Promise<{
|
|
readonly module: JavaScriptModuleSemanticIr;
|
|
readonly projection: JavaScriptSemanticFileProjection | null;
|
|
}>;
|
|
export async function readJavaScriptAnalysisTransfer(
|
|
path: string,
|
|
descriptor: Descriptor,
|
|
source: Pick<JavaScriptArtifactFile, "path" | "sha256">,
|
|
signal?: AbortSignal,
|
|
capacity: JavaScriptSemanticProjectionCapacity | null = {
|
|
nodes: JAVASCRIPT_SEMANTIC_ITEM_MAXIMUM,
|
|
unknowns: JAVASCRIPT_SEMANTIC_ITEM_MAXIMUM,
|
|
},
|
|
): Promise<{
|
|
readonly module: JavaScriptModuleSemanticIr;
|
|
readonly projection: JavaScriptSemanticFileProjection | null;
|
|
}> {
|
|
if (
|
|
capacity !== null &&
|
|
[capacity.nodes, capacity.unknowns].some(
|
|
(value) =>
|
|
!Number.isSafeInteger(value) ||
|
|
value < 0 ||
|
|
value > JAVASCRIPT_SEMANTIC_ITEM_MAXIMUM,
|
|
)
|
|
)
|
|
throw new RangeError(
|
|
"JavaScript transfer requires valid retained capacities",
|
|
);
|
|
const expected = javaScriptAnalysisTransferDescriptorSchema.parse(descriptor);
|
|
if ((await stat(path)).size !== expected.bytes)
|
|
throw new TypeError(
|
|
"JavaScript analysis transfer byte count does not match",
|
|
);
|
|
const scopes: JavaScriptModuleSemanticIr["scopes"][number][] = [];
|
|
const bindings: JavaScriptModuleSemanticIr["bindings"][number][] = [];
|
|
const callables: JavaScriptModuleSemanticIr["callables"][number][] = [];
|
|
const moduleLinks: JavaScriptModuleSemanticIr["moduleLinks"][number][] = [];
|
|
const evidenceContexts: JavaScriptSemanticFileProjection["evidenceContexts"][number][] =
|
|
[];
|
|
const nodes: JavaScriptSemanticFileProjection["nodes"][number][] = [];
|
|
const relations: JavaScriptSemanticFileProjection["relations"][number][] = [];
|
|
const unknowns: JavaScriptSemanticFileProjection["unknowns"][number][] = [];
|
|
const fingerprints: JavaScriptSemanticFileProjection["fingerprints"][number][] =
|
|
[];
|
|
let projectionHeader:
|
|
| z.output<typeof javaScriptWorkerProjectionHeaderSchema>
|
|
| undefined;
|
|
let moduleHeader:
|
|
| z.output<typeof javaScriptWorkerModuleHeaderSchema>
|
|
| undefined;
|
|
const identities = new Map<string, Set<string>>();
|
|
const retainIdentity = (kind: string, identifier: string): void => {
|
|
let seen = identities.get(kind);
|
|
if (seen === undefined) {
|
|
seen = new Set();
|
|
identities.set(kind, seen);
|
|
}
|
|
if (seen.has(identifier))
|
|
throw new TypeError(
|
|
`Duplicate JavaScript transfer ${kind} identity: ${identifier}`,
|
|
);
|
|
seen.add(identifier);
|
|
};
|
|
let records = 0;
|
|
let moduleBytes = 0;
|
|
let moduleRecords = 0;
|
|
let graphBytes = 0;
|
|
let graphRecords = 0;
|
|
const digest = createHash("sha256");
|
|
const stream = createReadStream(path, {
|
|
highWaterMark: 64 * 1024,
|
|
...(signal === undefined ? {} : { signal }),
|
|
});
|
|
let bytes = 0;
|
|
let pending: Buffer[] = [];
|
|
let pendingBytes = 0;
|
|
try {
|
|
for await (const chunk of stream) {
|
|
signal?.throwIfAborted();
|
|
if (!Buffer.isBuffer(chunk))
|
|
throw new TypeError("JavaScript transfer stream is not bytes");
|
|
bytes += chunk.length;
|
|
if (bytes > expected.bytes)
|
|
throw new RangeError("JavaScript transfer grew while being read");
|
|
digest.update(chunk);
|
|
let start = 0;
|
|
let newline: number;
|
|
while ((newline = chunk.indexOf(0x0a, start)) >= 0) {
|
|
const fragment = chunk.subarray(start, newline);
|
|
const lineBytes = pendingBytes + fragment.length;
|
|
if (lineBytes + 1 > FRAME_BYTES)
|
|
throw new RangeError(
|
|
"JavaScript transfer record exceeds its byte budget",
|
|
);
|
|
// Authenticate physical bytes and frame before decoding. Borrow read
|
|
// windows until one bounded record is complete, then copy it once.
|
|
const line =
|
|
pending.length === 0
|
|
? fragment
|
|
: Buffer.concat([...pending, fragment], lineBytes);
|
|
pending = [];
|
|
pendingBytes = 0;
|
|
start = newline + 1;
|
|
const decoded = safeParseUtf8Json(line);
|
|
if (!decoded.ok) throw decoded.cause;
|
|
const record = recordSchema.parse(decoded.value);
|
|
if (
|
|
[
|
|
"module-header",
|
|
"scope",
|
|
"binding",
|
|
"callable",
|
|
"module-link",
|
|
].includes(record.kind)
|
|
) {
|
|
moduleBytes += lineBytes + 1;
|
|
moduleRecords += 1;
|
|
} else {
|
|
graphBytes += lineBytes + 1;
|
|
graphRecords += 1;
|
|
}
|
|
if (record.kind === "projection-header") {
|
|
if (records !== 0 || projectionHeader !== undefined)
|
|
throw new TypeError(
|
|
"Duplicate or late JavaScript projection header",
|
|
);
|
|
projectionHeader = record.value;
|
|
} else if (record.kind === "module-header") {
|
|
if (records !== 1 || moduleHeader !== undefined)
|
|
throw new TypeError("Duplicate or late JavaScript module header");
|
|
moduleHeader = record.value;
|
|
} else {
|
|
if (projectionHeader === undefined || moduleHeader === undefined)
|
|
throw new TypeError(
|
|
"JavaScript transfer facts precede their headers",
|
|
);
|
|
if (
|
|
capacity === null &&
|
|
[
|
|
"evidence-context",
|
|
"node",
|
|
"relation",
|
|
"unknown",
|
|
"fingerprint",
|
|
].includes(record.kind)
|
|
) {
|
|
if (
|
|
record.kind === "node" &&
|
|
(record.value.identity.artifact_sha256 !== source.sha256 ||
|
|
record.value.identity.module_path !== source.path)
|
|
)
|
|
throw new TypeError(
|
|
"JavaScript transfer node belongs to a different artifact source",
|
|
);
|
|
records += 1;
|
|
continue;
|
|
}
|
|
switch (record.kind) {
|
|
case "scope":
|
|
retainIdentity(record.kind, record.value.scopeId);
|
|
scopes.push(record.value);
|
|
break;
|
|
case "binding":
|
|
retainIdentity(record.kind, record.value.bindingId);
|
|
bindings.push(record.value);
|
|
break;
|
|
case "callable":
|
|
retainIdentity(record.kind, record.value.callableId);
|
|
callables.push(record.value);
|
|
break;
|
|
case "module-link":
|
|
moduleLinks.push(record.value);
|
|
break;
|
|
case "evidence-context":
|
|
retainIdentity(record.kind, record.value.context_id);
|
|
evidenceContexts.push(record.value);
|
|
break;
|
|
case "node": {
|
|
retainIdentity(record.kind, record.value.node_id);
|
|
if (
|
|
record.value.identity.artifact_sha256 !== source.sha256 ||
|
|
record.value.identity.module_path !== source.path
|
|
)
|
|
throw new TypeError(
|
|
"JavaScript transfer node belongs to a different artifact source",
|
|
);
|
|
if (capacity !== null && nodes.length >= capacity.nodes)
|
|
throw new TypeError(
|
|
"JavaScript transfer nodes exceed the offered capacity",
|
|
);
|
|
nodes.push(record.value);
|
|
break;
|
|
}
|
|
case "relation":
|
|
retainIdentity(record.kind, record.value.relation_id);
|
|
relations.push(record.value);
|
|
break;
|
|
case "unknown":
|
|
retainIdentity(record.kind, record.value.unknown_id);
|
|
if (capacity !== null && unknowns.length >= capacity.unknowns)
|
|
throw new TypeError(
|
|
"JavaScript transfer unknowns exceed the offered capacity",
|
|
);
|
|
unknowns.push(record.value);
|
|
break;
|
|
case "fingerprint":
|
|
retainIdentity(record.kind, record.value.fingerprint_id);
|
|
fingerprints.push(record.value);
|
|
break;
|
|
}
|
|
}
|
|
records += 1;
|
|
}
|
|
const tail = chunk.subarray(start);
|
|
pendingBytes += tail.length;
|
|
if (pendingBytes + 1 > FRAME_BYTES)
|
|
throw new RangeError(
|
|
"Unterminated JavaScript transfer record exceeds its byte budget",
|
|
);
|
|
if (tail.length > 0) pending.push(tail);
|
|
}
|
|
} finally {
|
|
stream.destroy();
|
|
}
|
|
if (
|
|
pendingBytes !== 0 ||
|
|
records !== expected.records ||
|
|
bytes !== expected.bytes ||
|
|
moduleBytes !== expected.module_bytes ||
|
|
moduleRecords !== expected.module_records ||
|
|
graphBytes !== expected.graph_bytes ||
|
|
graphRecords !== expected.graph_records ||
|
|
digest.digest("hex") !== expected.sha256
|
|
)
|
|
throw new TypeError(
|
|
"JavaScript analysis transfer is incomplete or its digest does not match",
|
|
);
|
|
if (projectionHeader === undefined || moduleHeader === undefined)
|
|
throw new TypeError("JavaScript analysis transfer is missing its headers");
|
|
return {
|
|
module: { ...moduleHeader, scopes, bindings, callables, moduleLinks },
|
|
projection:
|
|
capacity !== null
|
|
? {
|
|
...projectionHeader,
|
|
evidenceContexts,
|
|
nodes,
|
|
relations,
|
|
unknowns,
|
|
fingerprints,
|
|
}
|
|
: null,
|
|
};
|
|
}
|