fix(browser): bound script metadata and preserve omissions
This commit is contained in:
@@ -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 = (
|
||||
|
||||
Reference in New Issue
Block a user