fix(inspector): bound retained metadata and preserve cancellation

This commit is contained in:
morluto
2026-10-09 05:15:23 +08:00
parent f983c718c3
commit e714fb027a
9 changed files with 296 additions and 76 deletions
@@ -175,9 +175,10 @@ describe("complete Inspector script hashes", () => {
scriptHashes,
});
try {
const result = await new V8InspectorProvider().observe(
observeInput(fake.endpoint, fake.targetId, "node"),
);
const result = await new V8InspectorProvider().observe({
...observeInput(fake.endpoint, fake.targetId, "node"),
observation_ms: 1_000,
});
if (!result.ok) throw result.error;
expect(result.value.scripts.items).toHaveLength(scriptHashes.length);
expect(
@@ -212,7 +213,7 @@ describe("undeclared V8 runtime role", () => {
});
});
describe("passive V8 Inspector evidence", () => {
describe("V8 Inspector script retention", () => {
test("retains authorized script locations longer than the former byte ceiling", async () => {
const longUrl = `https://example.test/${"a".repeat(20_000)}`;
const fake = await startFakeV8Inspector({
@@ -261,7 +262,69 @@ describe("passive V8 Inspector evidence", () => {
await fake.close();
}
});
});
describe("bounded V8 Inspector capture", () => {
test("bounds aggregate script and context metadata while retaining a useful partial capture", async () => {
const fixture = await runtimeFixture();
const url = pathToFileURL(fixture.entry).href;
const scriptHashes = Array.from(
{ length: 2_300 },
(_, index) => `${"h".repeat(4_096)}${String(index)}`,
);
const fake = await startFakeV8Inspector({
targetUrl: url,
scriptUrls: scriptHashes.map(() => url),
scriptHashes,
contextTransitionCount: 2_000,
});
try {
const result = await new V8InspectorProvider().observe({
...observeInput(fake.endpoint, fake.targetId, "node"),
observation_ms: 1_000,
});
if (!result.ok) throw result.error;
expect(result.value.capture.truncated).toBe(true);
expect(result.value.capture.truncation_reasons).toEqual([
"retained_metadata_budget_exceeded",
]);
expect(result.value.capture.events_dropped).toBeGreaterThan(0);
expect(result.value.capture.metadata_bytes_retained).toBeLessThanOrEqual(
8 * 1024 * 1024,
);
expect(result.value.capture.events_observed).toBeGreaterThan(
result.value.capture.events_retained,
);
expect(result.value.execution_contexts).toHaveLength(1);
} finally {
await fake.close();
}
});
test("rejects a CDP event larger than the configured transport payload budget", async () => {
const fixture = await runtimeFixture();
const fake = await startFakeV8Inspector({
targetUrl: pathToFileURL(fixture.entry).href,
oversizedEventBytes: 16 * 1024 * 1024,
});
try {
const result = await new V8InspectorProvider().observe(
observeInput(fake.endpoint, fake.targetId, "node"),
);
expect(result.ok).toBe(false);
if (result.ok) return;
expect(result.error).toMatchObject({
_tag: "BrowserObservationError",
reason: "payload_limit",
});
expect(result.error.userMessage).toContain("protocol budget");
} finally {
await fake.close();
}
});
});
describe("passive V8 Inspector evidence", () => {
test("produces deterministic Evidence through the service without grants", async () => {
const fixture = await runtimeFixture();
const fake = await startFakeV8Inspector({
+80 -41
View File
@@ -14,6 +14,8 @@ interface FakeV8InspectorOptions {
readonly runtimeProduct?: string;
readonly scriptUrls?: readonly string[];
readonly scriptHashes?: readonly string[];
readonly oversizedEventBytes?: number;
readonly contextTransitionCount?: number;
readonly additionalTargetUrl?: string;
readonly additionalTargetCount?: number;
readonly closeOnMethod?: string;
@@ -94,47 +96,7 @@ export const startFakeV8Inspector = async (
);
});
webSockets.on("connection", (socket: WebSocket) => {
sockets.add(socket);
socket.on("close", () => sockets.delete(socket));
socket.on("message", (raw) => {
const command = parseCommand(raw.toString());
commands.push(command);
if (options.closeOnMethod === command.method) {
socket.close();
return;
}
socket.send(JSON.stringify({ id: command.id, result: {} }));
if (command.method === "Runtime.enable")
socket.send(
JSON.stringify({
method: "Runtime.executionContextCreated",
params: {
context: {
id: 1,
origin: "",
name: "node[fixture]",
},
},
}),
);
if (command.method === "Debugger.enable")
for (const [index, url] of (
options.scriptUrls ?? [options.targetUrl]
).entries())
socket.send(
JSON.stringify({
method: "Debugger.scriptParsed",
params: {
scriptId: String(index + 1),
url,
executionContextId: 1,
hash: options.scriptHashes?.[index] ?? `hash-${String(index)}`,
length: 100 + index,
isModule: index % 2 === 0,
},
}),
);
});
registerInspectorSocket(socket, options, commands, sockets);
});
await new Promise<void>((resolve, reject) => {
http.once("error", reject);
@@ -160,6 +122,83 @@ export const startFakeV8Inspector = async (
};
};
const registerInspectorSocket = (
socket: WebSocket,
options: FakeV8InspectorOptions,
commands: InspectorCommand[],
sockets: Set<WebSocket>,
): void => {
sockets.add(socket);
socket.on("close", () => sockets.delete(socket));
socket.on("message", (raw) => {
const command = parseCommand(raw.toString());
commands.push(command);
if (options.closeOnMethod === command.method) {
socket.close();
return;
}
socket.send(JSON.stringify({ id: command.id, result: {} }));
respondToInspectorCommand(socket, command, options);
});
};
const respondToInspectorCommand = (
socket: WebSocket,
command: InspectorCommand,
options: FakeV8InspectorOptions,
): void => {
if (command.method === "Runtime.enable") sendRuntimeEvents(socket, options);
if (command.method === "Debugger.enable") sendScriptEvents(socket, options);
};
const sendRuntimeEvents = (
socket: WebSocket,
options: FakeV8InspectorOptions,
): void => {
socket.send(
JSON.stringify({
method: "Runtime.executionContextCreated",
params: { context: { id: 1, origin: "", name: "node[fixture]" } },
}),
);
for (let index = 0; index < (options.contextTransitionCount ?? 0); index += 1)
socket.send(
JSON.stringify({
method: "Runtime.executionContextDestroyed",
params: { executionContextId: 1 },
}),
);
if (options.oversizedEventBytes !== undefined)
socket.send(
JSON.stringify({
method: "Runtime.noisyFixtureEvent",
params: { padding: "x".repeat(options.oversizedEventBytes) },
}),
);
};
const sendScriptEvents = (
socket: WebSocket,
options: FakeV8InspectorOptions,
): void => {
for (const [index, url] of (
options.scriptUrls ?? [options.targetUrl]
).entries())
socket.send(
JSON.stringify({
method: "Debugger.scriptParsed",
params: {
scriptId: String(index + 1),
url,
executionContextId: 1,
hash: options.scriptHashes?.[index] ?? `hash-${String(index)}`,
length: 100 + index,
isModule: index % 2 === 0,
},
}),
);
};
const target = (
id: string,
url: string,