fix(browser): bound script metadata and preserve omissions
This commit is contained in:
@@ -65,6 +65,7 @@ export class CdpCaptureCompleteness {
|
||||
reason === "invalid_protocol_value"
|
||||
)
|
||||
this.#unavailable.add(section);
|
||||
if (reason === "resource_budget_exhausted") this.#truncated.add(section);
|
||||
const key = `${section}\0${reason}`;
|
||||
const existing = this.#excluded.get(key);
|
||||
const combined =
|
||||
|
||||
@@ -26,7 +26,11 @@ import {
|
||||
isMainFrameNavigation,
|
||||
} from "./CdpCaptureEventHelpers.js";
|
||||
import type { CdpCaptureEventsState } from "./CdpCaptureEventState.js";
|
||||
import type { CapturedScript, NetworkState } from "./CdpCaptureEventTypes.js";
|
||||
import {
|
||||
CDP_CAPTURE_SCRIPT_METADATA_LIMITS,
|
||||
type CapturedScript,
|
||||
type NetworkState,
|
||||
} from "./CdpCaptureEventTypes.js";
|
||||
|
||||
export const handleExecutionContextCreated = (
|
||||
state: CdpCaptureEventsState,
|
||||
@@ -64,12 +68,18 @@ export const handleScriptParsed = (
|
||||
state.completeness.exclude("scripts", "invalid_protocol_value");
|
||||
return;
|
||||
}
|
||||
const replacedBytes = state.scriptMetadataBytesById.get(scriptId) ?? 0;
|
||||
state.scriptMetadataBudgetOmissionsById.delete(scriptId);
|
||||
if (replacedBytes > 0) {
|
||||
state.scripts.delete(scriptId);
|
||||
state.scriptMetadataBytesById.delete(scriptId);
|
||||
state.retainedScriptMetadataBytes -= replacedBytes;
|
||||
}
|
||||
if (sanitized === undefined) {
|
||||
state.completeness.exclude("scripts", exclusionReasonForUrl(rawUrl));
|
||||
return;
|
||||
}
|
||||
const sourceMap = sourceMapForScript(params.sourceMapURL, rawUrl, state);
|
||||
const script: CapturedScript = {
|
||||
const baseScript = {
|
||||
scriptId,
|
||||
rawUrl,
|
||||
url: sanitized.url,
|
||||
@@ -84,21 +94,79 @@ export const handleScriptParsed = (
|
||||
),
|
||||
isModule: params.isModule === true,
|
||||
language: cdpStringValue(params.scriptLanguage) ?? null,
|
||||
sourceMapUrl: sourceMap.sanitized,
|
||||
sourceMapRawUrl: sourceMap.raw,
|
||||
executionContextKey: executionContextKey(params.executionContextId),
|
||||
};
|
||||
const baseBytes = scriptMetadataStringBytes(baseScript);
|
||||
const sourceMapDeclared =
|
||||
typeof params.sourceMapURL === "string" && params.sourceMapURL.length > 0;
|
||||
if (
|
||||
state.scripts.size >= CDP_CAPTURE_SCRIPT_METADATA_LIMITS.scripts ||
|
||||
baseBytes >
|
||||
CDP_CAPTURE_SCRIPT_METADATA_LIMITS.retainedBytes -
|
||||
state.retainedScriptMetadataBytes
|
||||
) {
|
||||
state.rejectedScriptMetadataBudgetCount += 1;
|
||||
if (sourceMapDeclared) state.rejectedSourceMapMetadataBudgetCount += 1;
|
||||
return;
|
||||
}
|
||||
const sourceMap = sourceMapForScript(
|
||||
params.sourceMapURL,
|
||||
rawUrl,
|
||||
state,
|
||||
CDP_CAPTURE_SCRIPT_METADATA_LIMITS.retainedBytes -
|
||||
state.retainedScriptMetadataBytes -
|
||||
baseBytes,
|
||||
);
|
||||
const { budgetExhausted, ...sourceMapFields } = sourceMap;
|
||||
const script: CapturedScript = { ...baseScript, ...sourceMapFields };
|
||||
if (budgetExhausted)
|
||||
state.scriptMetadataBudgetOmissionsById.set(scriptId, true);
|
||||
const retainedBytes =
|
||||
baseBytes +
|
||||
stringByteLength(sourceMap.sourceMapUrl) +
|
||||
stringByteLength(sourceMap.sourceMapRawUrl);
|
||||
state.retainedScriptMetadataBytes += retainedBytes;
|
||||
state.scriptMetadataBytesById.set(scriptId, retainedBytes);
|
||||
state.scripts.set(scriptId, script);
|
||||
};
|
||||
|
||||
const scriptMetadataStringBytes = (script: {
|
||||
readonly scriptId: string;
|
||||
readonly rawUrl: string;
|
||||
readonly url: string;
|
||||
readonly origin: string | null;
|
||||
readonly hash: string;
|
||||
readonly language: string | null;
|
||||
readonly executionContextKey: string | null;
|
||||
}): number =>
|
||||
stringByteLength(script.scriptId) +
|
||||
stringByteLength(script.rawUrl) +
|
||||
stringByteLength(script.url) +
|
||||
stringByteLength(script.origin) +
|
||||
stringByteLength(script.hash) +
|
||||
stringByteLength(script.language) +
|
||||
stringByteLength(script.executionContextKey);
|
||||
|
||||
const stringByteLength = (value: string | null): number =>
|
||||
value === null ? 0 : Buffer.byteLength(value, "utf8");
|
||||
|
||||
const sourceMapForScript = (
|
||||
value: unknown,
|
||||
scriptUrl: string,
|
||||
state: CdpCaptureEventsState,
|
||||
): { readonly sanitized: string | null; readonly raw: string | null } => {
|
||||
remainingBytes: number,
|
||||
): {
|
||||
readonly sourceMapUrl: string | null;
|
||||
readonly sourceMapRawUrl: string | null;
|
||||
readonly budgetExhausted: boolean;
|
||||
} => {
|
||||
const declaredUrl = cdpStringValue(value);
|
||||
if (declaredUrl === undefined || declaredUrl === "")
|
||||
return { sanitized: null, raw: null };
|
||||
return {
|
||||
sourceMapUrl: null,
|
||||
sourceMapRawUrl: null,
|
||||
budgetExhausted: false,
|
||||
};
|
||||
let rawUrl: string;
|
||||
try {
|
||||
rawUrl = new URL(declaredUrl, scriptUrl).href;
|
||||
@@ -106,12 +174,36 @@ const sourceMapForScript = (
|
||||
// Unparseable URLs are recorded as unsupported, not as failures.
|
||||
void cause;
|
||||
state.completeness.exclude("source_maps", "unsupported_url");
|
||||
return { sanitized: null, raw: null };
|
||||
return {
|
||||
sourceMapUrl: null,
|
||||
sourceMapRawUrl: null,
|
||||
budgetExhausted: false,
|
||||
};
|
||||
}
|
||||
const sanitized = allowedSanitizedUrl(rawUrl, state.allowedOrigins);
|
||||
if (sanitized !== undefined) return { sanitized: sanitized.url, raw: rawUrl };
|
||||
if (sanitized !== undefined) {
|
||||
if (
|
||||
stringByteLength(rawUrl) + stringByteLength(sanitized.url) >
|
||||
remainingBytes
|
||||
) {
|
||||
return {
|
||||
sourceMapUrl: null,
|
||||
sourceMapRawUrl: null,
|
||||
budgetExhausted: true,
|
||||
};
|
||||
}
|
||||
return {
|
||||
sourceMapUrl: sanitized.url,
|
||||
sourceMapRawUrl: rawUrl,
|
||||
budgetExhausted: false,
|
||||
};
|
||||
}
|
||||
state.completeness.exclude("source_maps", exclusionReasonForUrl(rawUrl));
|
||||
return { sanitized: null, raw: null };
|
||||
return {
|
||||
sourceMapUrl: null,
|
||||
sourceMapRawUrl: null,
|
||||
budgetExhausted: false,
|
||||
};
|
||||
};
|
||||
|
||||
export const handleRequestWillBeSent = (
|
||||
|
||||
@@ -9,6 +9,11 @@ export interface CdpCaptureEventsState {
|
||||
readonly input: InspectWebPageInput;
|
||||
readonly allowedOrigins: ReadonlySet<string>;
|
||||
readonly scripts: Map<string, CapturedScript>;
|
||||
readonly scriptMetadataBudgetOmissionsById: Map<string, true>;
|
||||
rejectedScriptMetadataBudgetCount: number;
|
||||
rejectedSourceMapMetadataBudgetCount: number;
|
||||
readonly scriptMetadataBytesById: Map<string, number>;
|
||||
retainedScriptMetadataBytes: number;
|
||||
readonly executionContextFrames: Map<string, string>;
|
||||
readonly network: Map<string, NetworkState>;
|
||||
readonly networkRequestTimestamps: Map<string, number>;
|
||||
|
||||
@@ -15,3 +15,9 @@ export interface CapturedScript {
|
||||
}
|
||||
|
||||
export type NetworkState = WebPageInspection["network"]["requests"][number];
|
||||
|
||||
/** Bounds retained CDP script identities independently from source text capture. */
|
||||
export const CDP_CAPTURE_SCRIPT_METADATA_LIMITS = {
|
||||
retainedBytes: 8 * 1024 * 1024,
|
||||
scripts: 50_000,
|
||||
} as const;
|
||||
|
||||
@@ -15,7 +15,13 @@ export { type CapturedScript } from "./CdpCaptureEventTypes.js";
|
||||
|
||||
/** Event accumulator that validates and normalizes payloads at ingestion. */
|
||||
export class CdpCaptureEvents implements CdpCaptureEventsState {
|
||||
#scriptMetadataBudgetExclusionsRecorded = false;
|
||||
readonly scripts = new Map<string, CapturedScript>();
|
||||
readonly scriptMetadataBudgetOmissionsById = new Map<string, true>();
|
||||
rejectedScriptMetadataBudgetCount = 0;
|
||||
rejectedSourceMapMetadataBudgetCount = 0;
|
||||
readonly scriptMetadataBytesById = new Map<string, number>();
|
||||
retainedScriptMetadataBytes = 0;
|
||||
readonly executionContextFrames = new Map<string, string>();
|
||||
readonly network = new Map<string, NetworkState>();
|
||||
readonly networkRequestTimestamps = new Map<string, number>();
|
||||
@@ -51,6 +57,12 @@ export class CdpCaptureEvents implements CdpCaptureEventsState {
|
||||
this.navigationDuringCapture = false;
|
||||
this.committedDocument = undefined;
|
||||
this.scripts.clear();
|
||||
this.scriptMetadataBudgetOmissionsById.clear();
|
||||
this.rejectedScriptMetadataBudgetCount = 0;
|
||||
this.rejectedSourceMapMetadataBudgetCount = 0;
|
||||
this.scriptMetadataBytesById.clear();
|
||||
this.retainedScriptMetadataBytes = 0;
|
||||
this.#scriptMetadataBudgetExclusionsRecorded = false;
|
||||
this.executionContextFrames.clear();
|
||||
this.network.clear();
|
||||
this.networkRequestTimestamps.clear();
|
||||
@@ -81,6 +93,28 @@ export class CdpCaptureEvents implements CdpCaptureEventsState {
|
||||
this.committedDocument = undefined;
|
||||
}
|
||||
|
||||
/** Record final script metadata omissions after all scriptParsed replacements. */
|
||||
recordScriptMetadataBudgetExclusions(): void {
|
||||
if (this.#scriptMetadataBudgetExclusionsRecorded) return;
|
||||
this.#scriptMetadataBudgetExclusionsRecorded = true;
|
||||
const omittedScripts = this.rejectedScriptMetadataBudgetCount;
|
||||
const omittedSourceMaps =
|
||||
this.rejectedSourceMapMetadataBudgetCount +
|
||||
this.scriptMetadataBudgetOmissionsById.size;
|
||||
if (omittedScripts > 0)
|
||||
this.completeness.exclude(
|
||||
"scripts",
|
||||
"resource_budget_exhausted",
|
||||
omittedScripts,
|
||||
);
|
||||
if (omittedSourceMaps > 0)
|
||||
this.completeness.exclude(
|
||||
"source_maps",
|
||||
"resource_budget_exhausted",
|
||||
omittedSourceMaps,
|
||||
);
|
||||
}
|
||||
|
||||
ingest(event: CdpEvent): void {
|
||||
const params = recordValue(event.params);
|
||||
if (params === undefined) return;
|
||||
|
||||
@@ -162,6 +162,7 @@ const captureAuthorizedPage = async (
|
||||
if (state.events.navigationDuringCapture || completedUrl !== attachedUrl)
|
||||
throw new BrowserObservationError("inspect_web_page", "target_changed");
|
||||
await report(context.progress, 3, "Normalizing browser evidence");
|
||||
state.events.recordScriptMetadataBudgetExclusions();
|
||||
return {
|
||||
inspection: normalizedInspection(state, {
|
||||
attachedUrl,
|
||||
|
||||
@@ -36,6 +36,7 @@ const browserExclusionReasonSchema = z.enum([
|
||||
"out_of_target_scope",
|
||||
"provider_unavailable",
|
||||
"invalid_protocol_value",
|
||||
"resource_budget_exhausted",
|
||||
]);
|
||||
export type BrowserExclusionReason = z.infer<
|
||||
typeof browserExclusionReasonSchema
|
||||
|
||||
@@ -17,12 +17,19 @@ export const buildWebBundleAnalysis = (
|
||||
const unavailable = inspection.scripts.items
|
||||
.filter((script) => !script.source.included)
|
||||
.map(({ script_key }) => script_key);
|
||||
const omittedScripts = captureBudgetOmission(inspection, "scripts");
|
||||
const omittedSourceMaps =
|
||||
sourceMaps.status === "not_requested"
|
||||
? undefined
|
||||
: captureBudgetOmission(inspection, "source_maps");
|
||||
const sourceMapIncomplete =
|
||||
sourceMaps.status === "partial" || sourceMaps.status === "unavailable";
|
||||
const partial =
|
||||
accumulator.parseFailures > 0 ||
|
||||
unavailable.length > 0 ||
|
||||
sourceMapIncomplete;
|
||||
sourceMapIncomplete ||
|
||||
omittedScripts !== undefined ||
|
||||
omittedSourceMaps !== undefined;
|
||||
return webBundleAnalysisSchema.parse({
|
||||
capture: buildCaptureObservation(inspection, sourceScripts),
|
||||
observations: {
|
||||
@@ -41,6 +48,8 @@ export const buildWebBundleAnalysis = (
|
||||
unavailable,
|
||||
parseFailures: accumulator.parseFailures,
|
||||
sourceMaps,
|
||||
omittedScripts,
|
||||
omittedSourceMaps,
|
||||
}),
|
||||
completeness: buildCompleteness({
|
||||
partial,
|
||||
@@ -83,9 +92,20 @@ interface UnknownsInput {
|
||||
readonly unavailable: string[];
|
||||
readonly parseFailures: number;
|
||||
readonly sourceMaps: WebBundleAnalysis["observations"]["source_maps"];
|
||||
readonly omittedScripts: number | null | undefined;
|
||||
readonly omittedSourceMaps: number | null | undefined;
|
||||
}
|
||||
|
||||
const buildUnknowns = (input: UnknownsInput): WebBundleAnalysis["unknowns"] => [
|
||||
...(input.omittedScripts === undefined
|
||||
? []
|
||||
: [
|
||||
{
|
||||
dimension: "script_inventory",
|
||||
reason: captureBudgetReason("script records", input.omittedScripts),
|
||||
affected_script_keys: [],
|
||||
},
|
||||
]),
|
||||
...(input.unavailable.length === 0
|
||||
? []
|
||||
: [
|
||||
@@ -106,6 +126,18 @@ const buildUnknowns = (input: UnknownsInput): WebBundleAnalysis["unknowns"] => [
|
||||
),
|
||||
},
|
||||
]),
|
||||
...(input.omittedSourceMaps === undefined
|
||||
? []
|
||||
: [
|
||||
{
|
||||
dimension: "source_maps" as const,
|
||||
reason: captureBudgetReason(
|
||||
"source-map declarations",
|
||||
input.omittedSourceMaps,
|
||||
),
|
||||
affected_script_keys: [],
|
||||
},
|
||||
]),
|
||||
...(input.sourceMaps.status === "not_requested" ||
|
||||
input.sourceMaps.status === "included"
|
||||
? []
|
||||
@@ -123,6 +155,25 @@ const buildUnknowns = (input: UnknownsInput): WebBundleAnalysis["unknowns"] => [
|
||||
]),
|
||||
];
|
||||
|
||||
const captureBudgetOmission = (
|
||||
inspection: WebPageInspection,
|
||||
section: "scripts" | "source_maps",
|
||||
): number | null | undefined => {
|
||||
const exclusions = inspection.completeness.excluded.filter(
|
||||
(exclusion) =>
|
||||
exclusion.section === section &&
|
||||
exclusion.reason === "resource_budget_exhausted",
|
||||
);
|
||||
if (exclusions.length === 0) return undefined;
|
||||
if (exclusions.some(({ count }) => count === null)) return null;
|
||||
return exclusions.reduce((sum, { count }) => sum + (count ?? 0), 0);
|
||||
};
|
||||
|
||||
const captureBudgetReason = (label: string, count: number | null): string =>
|
||||
count === null
|
||||
? `Capture omitted an unknown number of ${label} because its resource budget was exhausted.`
|
||||
: `Capture omitted ${String(count)} ${label} because its resource budget was exhausted.`;
|
||||
|
||||
interface CompletenessInput {
|
||||
readonly partial: boolean;
|
||||
readonly parsedScripts: number;
|
||||
|
||||
Reference in New Issue
Block a user