Files
rea/src/browser/execution/CdpRuntimeRetention.ts
T
morluto 680c83f88c feat(errors): carry the value of Go, Rust, IL2CPP, sigrok, Electron fuse, CDP, projection and assetutil allowances (#11645)
Each of these fixed allowances now passes limit and unit, traced to the constant or parse refusal that produced it. The Electron fuse, CDP runtime retention and assetutil names no longer embed the value.
2026-11-16 09:40:58 +08:00

62 lines
1.9 KiB
TypeScript

import { AnalysisResourceConstraintError } from "../../domain/analysisErrorCore.js";
import { Budget } from "../../domain/budget.js";
import { fixedAllowanceOptions } from "../../domain/fixedAllowanceRemediation.js";
import { WEB_RUNTIME_LIMITS } from "../../domain/webRuntime.js";
type RuntimeRetentionKind = "metadata" | "requests" | "sources";
const retentionLimit = (kind: RuntimeRetentionKind): number =>
kind === "sources"
? WEB_RUNTIME_LIMITS.sourceBytes
: WEB_RUNTIME_LIMITS.retainedEventBytes;
const retentionExceeded = (
operation: string,
kind: RuntimeRetentionKind,
retainedBytes: number,
): AnalysisResourceConstraintError => {
const limit = retentionLimit(kind);
const allowance = `Complete browser runtime ${kind} retention`;
return new AnalysisResourceConstraintError(
operation,
"capacity",
`${allowance} (${String(limit)} bytes) exceeded; no partial runtime result is returned.`,
{
boundary: "browser-runtime-retention",
kind,
retained_bytes: retainedBytes,
limit_bytes: limit,
},
fixedAllowanceOptions(allowance, "live", { limit, unit: "bytes" }),
);
};
/** Enforce the retained runtime representation, separately from wire payloads. */
export const assertRuntimeRetention = (
operation: string,
kind: RuntimeRetentionKind,
retainedBytes: number,
): void => {
if (retainedBytes > retentionLimit(kind))
throw retentionExceeded(operation, kind, retainedBytes);
};
/** Cumulative retention of one runtime kind across a capture. */
export class RuntimeRetention {
readonly #bytes: Budget;
constructor(
readonly operation: string,
readonly kind: RuntimeRetentionKind,
) {
this.#bytes = new Budget(retentionLimit(kind));
}
/** Charge `bytes` before retaining them, or throw the retention failure. */
charge(bytes: number): void {
this.#bytes.charge(bytes, () =>
retentionExceeded(this.operation, this.kind, this.#bytes.used + bytes),
);
}
}