Files
rea/src/cliJavaScriptRuntimeCommands.ts
T
morluto f6905719c3 fix(browser): bound capture work and preserve observation gaps
Own service-worker CDP registrations and retry failed teardown. Admit redaction and network metadata against remaining capture capacity, retain explicit omission reasons, and preserve source URL, redirect ancestry, caller-selected content, and historical capture values. Forward CLI cancellation through provider cleanup.

Reviewed fixes: #2263, #2637, #2767, #2845, #2859, #2863, #2872, #2874, #2876, #2879, #2955, #2957, #2961, #3033.

Validation: 25 current event/network source tests passed; earlier focused browser lanes passed 201 source tests and 176 boundary tests with two skips, plus 35 Python tests. Real browser/Windows lanes were not rerun. Playwright allocation before admission remains unbounded by these retention limits.
2026-10-14 08:31:29 +08:00

129 lines
4.2 KiB
TypeScript

import { Cli, z } from "incur";
import {
listJavaScriptRuntimeTargets,
observeJavaScriptRuntime,
} from "./application/javascript/JavaScriptRuntimeObservationService.js";
import { createJavaScriptRuntimeObservationProvider } from "./composition/javascriptRuntimeObservation.js";
import { CLI_COMMANDS } from "./cliCommandNames.js";
import { logCliCommand } from "./cliLogging.js";
import {
javascriptRuntimeKindSchema,
listJavaScriptRuntimeTargetsInputSchema,
observeJavaScriptRuntimeInputSchema,
} from "./domain/javascript/javascriptRuntimeObservation.js";
import { analysisInputErrorFromIssues } from "./domain/inputIssueProjection.js";
import { projectAnalysisError } from "./domain/analysisErrorProjection.js";
import type { JsonValue } from "./domain/jsonValue.js";
import type { Logger } from "pino";
import { withCommandCancellation } from "./cli/commandCancellation.js";
const observeOptionsSchema = z.object({
runtimeKind: javascriptRuntimeKindSchema
.optional()
.describe(
"Declared target role; Inspector cannot authenticate the Electron role",
),
observationMs: z
.number()
.int()
.min(0)
.default(100)
.describe("Observation window in milliseconds"),
});
/** Register CLI equivalents of passive V8 Inspector tools. */
export const registerJavaScriptRuntimeObservationCommands = (
cli: ReturnType<typeof Cli.create>,
logger: Logger,
): void => {
cli.command(CLI_COMMANDS.listJavaScriptRuntimeTargets, {
description: "List Node/Electron V8 Inspector targets",
args: z.object({
endpoint: z.string().describe("Literal-loopback Inspector endpoint"),
}),
run: ({ args }) =>
withCommandCancellation((signal) =>
logCliCommand(
logger,
CLI_COMMANDS.listJavaScriptRuntimeTargets,
async () => {
const context = runtimeContext();
const input = {
inspector_endpoint: args.endpoint,
};
const parsed =
listJavaScriptRuntimeTargetsInputSchema.safeParse(input);
if (!parsed.success)
return cliError(
analysisInputErrorFromIssues(
"list_javascript_runtime_targets",
parsed.error.issues,
input,
),
);
const result = await listJavaScriptRuntimeTargets(
context.provider,
parsed.data,
{ signal },
);
return result.ok ? result.value : cliError(result.error);
},
),
),
});
cli.command(CLI_COMMANDS.observeJavaScriptRuntime, {
description: "Passively observe one exact Node/Electron Inspector target",
args: z.object({
endpoint: z.string().describe("Literal-loopback Inspector endpoint"),
targetId: z
.string()
.describe("Target from list-javascript-runtime-targets"),
}),
options: observeOptionsSchema,
run: ({ args, options }) =>
withCommandCancellation((signal) =>
logCliCommand(
logger,
CLI_COMMANDS.observeJavaScriptRuntime,
async () => {
const context = runtimeContext();
const input = {
inspector_endpoint: args.endpoint,
target_id: args.targetId,
runtime_kind: options.runtimeKind,
observation_ms: options.observationMs,
};
const parsed = observeJavaScriptRuntimeInputSchema.safeParse(input);
if (!parsed.success)
return cliError(
analysisInputErrorFromIssues(
"observe_javascript_runtime",
parsed.error.issues,
input,
),
);
const result = await observeJavaScriptRuntime(
context.provider,
parsed.data,
{ signal },
);
return result.ok ? result.value : cliError(result.error);
},
),
),
});
};
const runtimeContext = () => ({
provider: createJavaScriptRuntimeObservationProvider(),
});
const cliError = (
error: Parameters<typeof projectAnalysisError>[0],
): JsonValue => ({
error: "JavaScript runtime observation failed",
...projectAnalysisError(error),
});