2026-10-07 05:24:32 +08:00
|
|
|
import type { BrowserScenarioCapture } from "../../domain/browserScenarioCapture.js";
|
|
|
|
|
import type { BrowserScenarioEvent } from "../../domain/browserScenarioCaptureValues.js";
|
|
|
|
|
import type { BrowserNetworkBody } from "../../domain/browserNetworkEvidence.js";
|
|
|
|
|
import type { CapturedWebScript } from "../../domain/webScriptExport.js";
|
|
|
|
|
|
|
|
|
|
type RequestEvent = Extract<BrowserScenarioEvent, { kind: "request" }>;
|
|
|
|
|
type ResponseEvent = Extract<BrowserScenarioEvent, { kind: "response" }>;
|
|
|
|
|
type ContentEvent = Extract<BrowserScenarioEvent, { kind: "network-content" }>;
|
|
|
|
|
|
|
|
|
|
interface ResponseIndex {
|
|
|
|
|
readonly responses: Map<string, ResponseEvent[]>;
|
|
|
|
|
readonly contents: Map<number, ContentEvent[]>;
|
|
|
|
|
readonly requests: Map<string, number>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Join script response bytes only through explicit transaction and event identity. */
|
|
|
|
|
export const scenarioScripts = (
|
|
|
|
|
capture: BrowserScenarioCapture,
|
|
|
|
|
): CapturedWebScript[] => {
|
|
|
|
|
const index = indexResponses(capture.events.items);
|
|
|
|
|
return capture.events.items.flatMap((request): CapturedWebScript[] => {
|
|
|
|
|
if (request.kind !== "request" || request.resource_type !== "script")
|
|
|
|
|
return [];
|
|
|
|
|
return [projectRequest(request, index)];
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const indexResponses = (
|
|
|
|
|
events: readonly BrowserScenarioEvent[],
|
|
|
|
|
): ResponseIndex => {
|
|
|
|
|
const index: ResponseIndex = {
|
|
|
|
|
responses: new Map(),
|
|
|
|
|
contents: new Map(),
|
|
|
|
|
requests: new Map(),
|
|
|
|
|
};
|
|
|
|
|
for (const event of events) {
|
2026-10-09 23:17:03 +08:00
|
|
|
if (event.kind === "request")
|
2026-10-07 05:24:32 +08:00
|
|
|
index.requests.set(
|
|
|
|
|
event.transaction_id,
|
|
|
|
|
(index.requests.get(event.transaction_id) ?? 0) + 1,
|
|
|
|
|
);
|
2026-10-09 23:17:03 +08:00
|
|
|
if (event.kind === "response")
|
2026-10-07 05:24:32 +08:00
|
|
|
append(index.responses, event.transaction_id, event);
|
|
|
|
|
if (event.kind === "network-content" && event.phase === "response")
|
|
|
|
|
append(index.contents, event.source_event_sequence, event);
|
|
|
|
|
}
|
|
|
|
|
return index;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const projectRequest = (
|
|
|
|
|
request: RequestEvent,
|
|
|
|
|
index: ResponseIndex,
|
|
|
|
|
): CapturedWebScript => {
|
|
|
|
|
const id = request.transaction_id;
|
2026-10-09 23:17:03 +08:00
|
|
|
const matches = index.responses.get(id) ?? [];
|
2026-10-07 05:24:32 +08:00
|
|
|
const response = matches.length === 1 ? matches[0] : undefined;
|
|
|
|
|
const bodies =
|
|
|
|
|
response === undefined ? [] : (index.contents.get(response.sequence) ?? []);
|
|
|
|
|
const body = bodies.length === 1 ? bodies[0]?.body : undefined;
|
|
|
|
|
const ambiguous =
|
2026-10-09 23:17:03 +08:00
|
|
|
index.requests.get(id) !== 1 || matches.length > 1 || bodies.length > 1;
|
2026-10-07 05:24:32 +08:00
|
|
|
return {
|
|
|
|
|
source: {
|
|
|
|
|
kind: "scenario-response",
|
2026-10-09 23:17:03 +08:00
|
|
|
transaction_id: id,
|
2026-10-07 05:24:32 +08:00
|
|
|
request_sequence: request.sequence,
|
|
|
|
|
response_sequence: ambiguous ? null : (response?.sequence ?? null),
|
|
|
|
|
status: ambiguous ? null : (response?.status ?? null),
|
|
|
|
|
},
|
|
|
|
|
url: request.url.url,
|
2026-10-09 23:17:03 +08:00
|
|
|
content: projectBody(body, ambiguous),
|
2026-10-07 05:24:32 +08:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const unavailable = (
|
|
|
|
|
reason: string,
|
|
|
|
|
message: string,
|
|
|
|
|
): CapturedWebScript["content"] => ({ state: "unavailable", reason, message });
|
|
|
|
|
|
|
|
|
|
const projectBody = (
|
|
|
|
|
body: BrowserNetworkBody | undefined,
|
|
|
|
|
ambiguous: boolean,
|
|
|
|
|
): CapturedWebScript["content"] => {
|
|
|
|
|
if (ambiguous)
|
|
|
|
|
return unavailable(
|
|
|
|
|
"ambiguous-transaction",
|
|
|
|
|
"The capture does not identify one request, response, and content record for this transaction.",
|
|
|
|
|
);
|
|
|
|
|
if (body === undefined)
|
|
|
|
|
return unavailable(
|
2026-10-09 23:17:03 +08:00
|
|
|
"response-content-not-retained",
|
2026-10-07 05:24:32 +08:00
|
|
|
"No selected, completed script response bytes were retained for this request.",
|
|
|
|
|
);
|
|
|
|
|
if (body.state === "unavailable")
|
|
|
|
|
return unavailable(body.reason, body.message);
|
|
|
|
|
if (body.state !== "captured")
|
|
|
|
|
return unavailable(
|
|
|
|
|
body.state,
|
|
|
|
|
"No selected, completed script response bytes were retained for this request.",
|
|
|
|
|
);
|
2026-10-12 14:07:58 +08:00
|
|
|
if (
|
|
|
|
|
body.representation !== "playwright-response-body-bytes" &&
|
|
|
|
|
body.representation !== "browser-decoded-response-bytes"
|
|
|
|
|
)
|
2026-10-07 05:24:32 +08:00
|
|
|
return unavailable(
|
|
|
|
|
"unsupported-body-representation",
|
|
|
|
|
`Captured representation ${body.representation} is not script response bytes.`,
|
|
|
|
|
);
|
|
|
|
|
return {
|
|
|
|
|
state: "captured",
|
|
|
|
|
bytes: Buffer.from(body.content, "base64"),
|
|
|
|
|
sha256: body.sha256,
|
|
|
|
|
media_type: body.media_type,
|
|
|
|
|
redacted: body.redacted,
|
|
|
|
|
representation: body.representation,
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const append = <Key, Value>(
|
|
|
|
|
map: Map<Key, Value[]>,
|
|
|
|
|
key: Key,
|
|
|
|
|
value: Value,
|
|
|
|
|
): void => {
|
|
|
|
|
const existing = map.get(key);
|
|
|
|
|
if (existing === undefined) map.set(key, [value]);
|
|
|
|
|
else existing.push(value);
|
|
|
|
|
};
|