fix(evidence): preserve literal keys and response body ownership

Retain exact empty and computed literal keys in JavaScript observations. Bind CDP response bodies to request occurrences, account for late responses, and validate the target after final progress callbacks.
This commit is contained in:
morluto
2026-10-12 16:17:29 +08:00
parent 79156e5bed
commit d1dcebce2c
10 changed files with 576 additions and 211 deletions
+72 -10
View File
@@ -7,11 +7,18 @@ import type { CdpCaptureEventsState } from "./CdpCaptureEventState.js";
import type { CapturedScript, NetworkState } from "./CdpCaptureEventTypes.js";
import * as handlers from "./CdpCaptureEventHandlers.js";
import { isJsonMediaType } from "./CdpCaptureEventHelpers.js";
import { recordValue } from "./CdpCaptureValues.js";
import { cdpStringValue, recordValue } from "./CdpCaptureValues.js";
export interface ResponseBodyCandidate {
readonly requestId: string;
readonly occurrence: number;
}
/** Event accumulator that validates and normalizes payloads at ingestion. */
export class CdpCaptureEvents implements CdpCaptureEventsState {
#scriptMetadataBudgetExclusionsRecorded = false;
readonly #networkRequestOccurrences = new Map<string, number>();
readonly #responseBodyExaminedOccurrences = new Map<string, number>();
readonly scripts = new Map<string, CapturedScript>();
readonly scriptMetadataBudgetOmissionsById = new Map<string, true>();
rejectedScriptMetadataBudgetCount = 0;
@@ -64,6 +71,8 @@ export class CdpCaptureEvents implements CdpCaptureEventsState {
this.executionContextFrames.clear();
this.network.clear();
this.networkRequestTimestamps.clear();
this.#networkRequestOccurrences.clear();
this.#responseBodyExaminedOccurrences.clear();
this.malformedRedirectRequestIds.clear();
this.webSocketConnectionsById.clear();
this.console.length = 0;
@@ -128,9 +137,18 @@ export class CdpCaptureEvents implements CdpCaptureEventsState {
case "Debugger.scriptParsed":
handlers.handleScriptParsed(this, params);
break;
case "Network.requestWillBeSent":
case "Network.requestWillBeSent": {
const requestId = cdpStringValue(params.requestId);
const previous =
requestId === undefined ? undefined : this.network.get(requestId);
handlers.handleRequestWillBeSent(this, params);
if (requestId !== undefined && this.network.get(requestId) !== previous)
this.#networkRequestOccurrences.set(
requestId,
(this.#networkRequestOccurrences.get(requestId) ?? 0) + 1,
);
break;
}
case "Network.responseReceived":
handlers.handleResponseReceived(this, params);
break;
@@ -165,24 +183,68 @@ export class CdpCaptureEvents implements CdpCaptureEventsState {
: null;
}
responseBodyRequestIds(): readonly string[] {
responseBodyRequests(): readonly ResponseBodyCandidate[] {
if (!this.input.include_json_body_shapes) return [];
return [...this.network.values()]
.filter(
(request) => !this.malformedRedirectRequestIds.has(request.request_id),
)
.filter((request) => isJsonMediaType(request.mime_type))
.map((request) => request.request_id);
.flatMap((request) => {
const occurrence = this.#networkRequestOccurrences.get(
request.request_id,
);
return occurrence === undefined
? []
: [{ requestId: request.request_id, occurrence }];
});
}
responseBodyUnavailable(requestId: string): void {
if (this.malformedRedirectRequestIds.has(requestId)) return;
bodyShapes.updateResponseBodyShape(this, requestId, null);
currentResponseBodyRequest(
candidate: ResponseBodyCandidate,
): NetworkState | undefined {
if (
candidate.occurrence !==
this.#networkRequestOccurrences.get(candidate.requestId)
)
return undefined;
return this.network.get(candidate.requestId);
}
responseBodyUnavailable(candidate: ResponseBodyCandidate): void {
if (this.currentResponseBodyRequest(candidate) === undefined) return;
this.#responseBodyExaminedOccurrences.set(
candidate.requestId,
candidate.occurrence,
);
if (this.malformedRedirectRequestIds.has(candidate.requestId)) return;
bodyShapes.updateResponseBodyShape(this, candidate.requestId, null);
this.completeness.unavailable("json_body_shapes");
}
ingestResponseBody(requestId: string, value: unknown): void {
if (this.malformedRedirectRequestIds.has(requestId)) return;
bodyShapes.ingestResponseBodyShape(this, requestId, value);
/**
* Mark eligible response bodies that arrived after the fetch pass was
* enumerated.
*/
settleUnexaminedResponseBodies(): void {
for (const candidate of this.responseBodyRequests()) {
const request = this.currentResponseBodyRequest(candidate);
if (
request?.body_shapes.response === null &&
this.#responseBodyExaminedOccurrences.get(candidate.requestId) !==
candidate.occurrence
)
this.responseBodyUnavailable(candidate);
}
}
ingestResponseBody(candidate: ResponseBodyCandidate, value: unknown): void {
if (this.currentResponseBodyRequest(candidate) === undefined) return;
this.#responseBodyExaminedOccurrences.set(
candidate.requestId,
candidate.occurrence,
);
if (this.malformedRedirectRequestIds.has(candidate.requestId)) return;
bodyShapes.ingestResponseBodyShape(this, candidate.requestId, value);
}
}