refactor(cdp): centralize execution context identity parsing

This commit is contained in:
morluto committed 2026-10-26 13:47:04 +08:00
1 parent 60daca427d
commit 7ccecbcb00
5 files changed
+14 -34

No files matched your search

+3 -9
View File
@@ -11,6 +11,7 @@ import { WEB_RUNTIME_LIMITS } from "../domain/webRuntime.js";
import { safeResponseMetadata } from "./CdpSafeMetadata.js";
import {
allowedSanitizedUrl,
executionContextKey,
isHttpUrl,
numberValue,
admitRecords,
@@ -27,7 +28,6 @@ import {
import {
consolePrimitive,
exclusionReasonForUrl,
executionContextKey,
initiatorLocation,
integerOrNull,
isJsonMediaType,
@@ -46,15 +46,9 @@ export const handleExecutionContextCreated = (
params: UnknownRecord,
): void => {
const context = recordValue(params.context);
const identifier = numberValue(context?.id);
const key = executionContextKey(context?.id);
const frameId = cdpStringValue(recordValue(context?.auxData)?.frameId);
if (
identifier === undefined ||
!Number.isSafeInteger(identifier) ||
frameId === undefined
)
return;
const key = String(identifier);
if (key === null || frameId === undefined) return;
// New contexts may reuse an id. Scripts already parsed keep the frame stored on them.
state.executionContextFrames.set(key, frameId);
};
-7
View File
@@ -202,13 +202,6 @@ export const integerOrNull = (value: unknown): number | null => {
return number === undefined ? null : Math.max(0, Math.trunc(number));
};
export const executionContextKey = (value: unknown): string | null => {
const identifier = numberValue(value);
return identifier !== undefined && Number.isSafeInteger(identifier)
? String(identifier)
: null;
};
export const isJsonContentType = (
headers: UnknownRecord | undefined,
): boolean => {
+7
View File
@@ -31,6 +31,13 @@ export const cdpStringValue = (value: unknown): string | undefined =>
export const numberValue = (value: unknown): number | undefined =>
typeof value === "number" && Number.isFinite(value) ? value : undefined;
export const executionContextKey = (value: unknown): string | null => {
const identifier = numberValue(value);
return identifier !== undefined && Number.isSafeInteger(identifier)
? String(identifier)
: null;
};
/** An omitted resource size is unknown; a supplied invalid size is malformed. */
export const resourceContentSize = (
value: unknown,
+1 -8
View File
@@ -4,7 +4,7 @@ import { jsonValueByteLength } from "../domain/jsonValueByteLength.js";
import { CDP_CAPTURE_SCRIPT_METADATA_LIMITS } from "./CdpCaptureEventTypes.js";
import { CdpCaptureCompleteness } from "./CdpCaptureCompleteness.js";
import {
numberValue,
executionContextKey,
recordValue,
scriptMetadataValues,
cdpStringValue,
@@ -129,10 +129,3 @@ const retainExecutionContext = (
if (contextKey === null || frameId === undefined || frameId === "") return;
frames.set(contextKey, frameId);
};
const executionContextKey = (value: unknown): string | null => {
const identifier = numberValue(value);
return identifier !== undefined && Number.isSafeInteger(identifier)
? String(identifier)
: null;
};
+3 -10
View File
@@ -24,7 +24,7 @@ import { analysisErrorWithCleanupFailure } from "../domain/analysisErrorCleanup.
import type { BrowserObservationOperation } from "../domain/browserObservationErrors.js";
import { err, ok, type Result } from "../domain/result.js";
import {
numberValue,
executionContextKey,
recordValue,
cdpStringValue,
scriptMetadataValues,
@@ -651,7 +651,7 @@ const ingestScript = (event: CdpEvent, state: CaptureState): void => {
const metadata = scriptMetadataValues(value);
const draft: ScriptDraft = {
rawUrl,
executionContextKey: contextKey(value?.executionContextId),
executionContextKey: executionContextKey(value?.executionContextId),
cdpHash: metadata.hash,
length: metadata.length,
isModule: metadata.isModule,
@@ -680,7 +680,7 @@ const ingestContext = (event: CdpEvent, state: CaptureState): void => {
event.method === "Runtime.executionContextCreated"
? recordValue(parameters?.context)
: parameters;
const key = contextKey(
const key = executionContextKey(
event.method === "Runtime.executionContextCreated"
? runtimeContext?.id
: runtimeContext?.executionContextId,
@@ -778,13 +778,6 @@ const retainedMetadataBytes = (value: ScriptDraft | ContextDraft): number => {
);
};
const contextKey = (value: unknown): string | null => {
const identifier = numberValue(value);
return identifier !== undefined && Number.isSafeInteger(identifier)
? String(identifier)
: null;
};
const providerError = (
cause: unknown,
operation: BrowserObservationOperation,