2026-10-09 22:45:18 +08:00
|
|
|
import type { WebPageInspection } from "../domain/browserObservationSchemas.js";
|
2026-07-18 03:42:35 +00:00
|
|
|
import {
|
2026-10-17 15:51:23 +08:00
|
|
|
createCapturedWebScriptReconciler,
|
2026-07-18 03:42:35 +00:00
|
|
|
stableWebScriptKey,
|
|
|
|
|
} from "../domain/webInventory.js";
|
|
|
|
|
import type { CapturedResource } from "./CdpCaptureDocuments.js";
|
|
|
|
|
import type { CdpCaptureEvents } from "./CdpCaptureEvents.js";
|
|
|
|
|
import type { CapturedScript } from "./CdpCaptureEventTypes.js";
|
|
|
|
|
import {
|
2026-10-09 20:02:52 +08:00
|
|
|
requiredScriptSource,
|
2026-07-18 03:42:35 +00:00
|
|
|
sourceExcluded,
|
|
|
|
|
sourceResult,
|
|
|
|
|
} from "./CdpCaptureValues.js";
|
|
|
|
|
import type { CaptureContext } from "./CdpPageCapture.js";
|
|
|
|
|
import type { WebSourceMapRequest } from "./WebSourceMapFetcher.js";
|
2026-10-11 10:10:01 -07:00
|
|
|
import { compareUnicodeCodePoints } from "../domain/unicodeCodePointOrder.js";
|
2026-10-19 13:22:54 +08:00
|
|
|
import { AnalysisCancelledError } from "../domain/analysisErrorCore.js";
|
|
|
|
|
import type { BrowserObservationOperation } from "../domain/browserObservationErrors.js";
|
2026-10-22 05:47:38 +08:00
|
|
|
import { selectedCdpCommandTimeoutMs } from "../domain/browserDeadline.js";
|
2026-10-19 13:22:54 +08:00
|
|
|
import {
|
|
|
|
|
CDP_SCRIPT_SOURCE_BYTES_OMISSION_REASON,
|
2026-10-22 05:47:38 +08:00
|
|
|
cdpScriptSourceDeadlineOmissionReason,
|
2026-10-19 13:22:54 +08:00
|
|
|
CdpScriptSourceBudget,
|
|
|
|
|
} from "./CdpScriptSourceBudget.js";
|
2026-07-18 03:42:35 +00:00
|
|
|
|
|
|
|
|
export interface CapturedScripts {
|
|
|
|
|
readonly inventory: WebPageInspection["scripts"];
|
|
|
|
|
readonly sourceMapRequests: readonly WebSourceMapRequest[];
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-23 05:38:30 +08:00
|
|
|
export interface CapturedScriptSnapshot extends CapturedScripts {
|
|
|
|
|
readonly scriptIds: ReadonlySet<string>;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-18 03:42:35 +00:00
|
|
|
export interface CaptureScriptsOptions {
|
|
|
|
|
readonly context: CaptureContext;
|
|
|
|
|
readonly events: CdpCaptureEvents;
|
|
|
|
|
readonly rawResources: readonly CapturedResource[];
|
|
|
|
|
readonly resources: WebPageInspection["resources"];
|
|
|
|
|
readonly frameIds: ReadonlySet<string>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface ScriptDraftState {
|
|
|
|
|
readonly context: CaptureContext;
|
|
|
|
|
readonly events: CdpCaptureEvents;
|
|
|
|
|
readonly frameIds: ReadonlySet<string>;
|
2026-10-17 15:51:23 +08:00
|
|
|
readonly reconcileResource: ReturnType<
|
|
|
|
|
typeof createCapturedWebScriptReconciler
|
|
|
|
|
>;
|
2026-07-18 03:42:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type ScriptDraftItem = Omit<
|
|
|
|
|
WebPageInspection["scripts"]["items"][number],
|
|
|
|
|
"script_key"
|
|
|
|
|
>;
|
|
|
|
|
|
|
|
|
|
export const captureScripts = async (
|
|
|
|
|
options: CaptureScriptsOptions,
|
2026-10-23 05:38:30 +08:00
|
|
|
): Promise<CapturedScriptSnapshot> => {
|
2026-07-18 03:42:35 +00:00
|
|
|
const { context, events, rawResources, resources, frameIds } = options;
|
2026-10-17 15:51:23 +08:00
|
|
|
let state: ScriptDraftState | undefined;
|
2026-07-18 03:42:35 +00:00
|
|
|
const drafts: {
|
|
|
|
|
readonly item: ScriptDraftItem;
|
|
|
|
|
readonly sourceMapRawUrl: string | null;
|
|
|
|
|
}[] = [];
|
2026-10-23 05:38:30 +08:00
|
|
|
const scripts = new Map(events.scripts);
|
2026-10-22 05:47:38 +08:00
|
|
|
const commandTimeoutMs = selectedCdpCommandTimeoutMs(
|
|
|
|
|
context.input.command_timeout_ms,
|
|
|
|
|
);
|
|
|
|
|
const deadlineOmission =
|
|
|
|
|
cdpScriptSourceDeadlineOmissionReason(commandTimeoutMs);
|
2026-10-19 13:22:54 +08:00
|
|
|
const sourceBudget =
|
2026-10-23 05:38:30 +08:00
|
|
|
context.input.include_script_sources && scripts.size > 0
|
2026-10-22 05:47:38 +08:00
|
|
|
? new CdpScriptSourceBudget(context.signal, commandTimeoutMs)
|
2026-10-19 13:22:54 +08:00
|
|
|
: undefined;
|
|
|
|
|
try {
|
2026-10-23 05:38:30 +08:00
|
|
|
for (const script of scripts.values()) {
|
2026-10-19 13:22:54 +08:00
|
|
|
state ??= {
|
|
|
|
|
context,
|
|
|
|
|
events,
|
|
|
|
|
frameIds,
|
|
|
|
|
reconcileResource: createCapturedWebScriptReconciler(
|
|
|
|
|
rawResources,
|
|
|
|
|
resources,
|
|
|
|
|
),
|
|
|
|
|
};
|
|
|
|
|
let source: WebPageInspection["scripts"]["items"][number]["source"] =
|
|
|
|
|
context.input.include_script_sources
|
2026-10-22 05:47:38 +08:00
|
|
|
? sourceExcluded(deadlineOmission)
|
2026-10-19 13:22:54 +08:00
|
|
|
: sourceExcluded("source capture was not selected");
|
|
|
|
|
if (
|
|
|
|
|
context.input.include_script_sources &&
|
|
|
|
|
isSourceDeadlineExpired(sourceBudget)
|
|
|
|
|
) {
|
|
|
|
|
events.completeness.exclude(
|
|
|
|
|
"script_sources",
|
|
|
|
|
"resource_budget_exhausted",
|
|
|
|
|
);
|
|
|
|
|
} else if (
|
|
|
|
|
context.input.include_script_sources &&
|
|
|
|
|
sourceBudget !== undefined
|
|
|
|
|
) {
|
|
|
|
|
try {
|
|
|
|
|
source = await captureScriptSource(script, state, sourceBudget);
|
|
|
|
|
} catch (cause: unknown) {
|
|
|
|
|
throwIfCallerCancelled(context.signal, context.operation);
|
|
|
|
|
if (isSourceDeadlineExpired(sourceBudget))
|
|
|
|
|
events.completeness.exclude(
|
|
|
|
|
"script_sources",
|
|
|
|
|
"resource_budget_exhausted",
|
|
|
|
|
);
|
|
|
|
|
else throw cause;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
drafts.push(buildScriptDraft(script, state, source));
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
sourceBudget?.dispose();
|
2026-10-17 15:51:23 +08:00
|
|
|
}
|
2026-07-18 03:42:35 +00:00
|
|
|
if (!context.input.include_script_sources)
|
2026-10-23 05:38:30 +08:00
|
|
|
events.completeness.exclude("script_sources", "not_approved", scripts.size);
|
|
|
|
|
return {
|
|
|
|
|
...keyAndBuildInventory(drafts, scripts.size),
|
|
|
|
|
scriptIds: new Set(scripts.keys()),
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/** Settle once after the last CDP read, before normalizing completeness. */
|
|
|
|
|
export const finalizeScriptInventory = (
|
|
|
|
|
snapshot: CapturedScriptSnapshot,
|
|
|
|
|
events: CdpCaptureEvents,
|
|
|
|
|
): WebPageInspection["scripts"] => {
|
|
|
|
|
let lateCount = 0;
|
|
|
|
|
for (const scriptId of events.scripts.keys())
|
|
|
|
|
if (!snapshot.scriptIds.has(scriptId)) lateCount += 1;
|
|
|
|
|
if (lateCount > 0) events.completeness.drop("scripts", lateCount);
|
|
|
|
|
return { ...snapshot.inventory, total: snapshot.inventory.total + lateCount };
|
2026-07-18 03:42:35 +00:00
|
|
|
};
|
|
|
|
|
|
2026-10-19 13:22:54 +08:00
|
|
|
const buildScriptDraft = (
|
2026-07-18 03:42:35 +00:00
|
|
|
script: CapturedScript,
|
|
|
|
|
state: ScriptDraftState,
|
2026-10-19 13:22:54 +08:00
|
|
|
source: WebPageInspection["scripts"]["items"][number]["source"],
|
|
|
|
|
): {
|
2026-07-18 03:42:35 +00:00
|
|
|
readonly item: ScriptDraftItem;
|
|
|
|
|
readonly sourceMapRawUrl: string | null;
|
2026-10-19 13:22:54 +08:00
|
|
|
} => {
|
2026-07-18 03:42:35 +00:00
|
|
|
const inventoryScript = {
|
|
|
|
|
frame_id: state.events.frameForScript(script, state.frameIds),
|
|
|
|
|
url: script.url,
|
|
|
|
|
cdp_hash: script.hash,
|
|
|
|
|
length: script.length,
|
|
|
|
|
is_module: script.isModule,
|
|
|
|
|
language: script.language,
|
|
|
|
|
source_map_url: script.sourceMapUrl,
|
|
|
|
|
};
|
|
|
|
|
return {
|
|
|
|
|
item: {
|
|
|
|
|
...inventoryScript,
|
|
|
|
|
origin: script.origin,
|
2026-10-17 15:51:23 +08:00
|
|
|
resource_reconciliation: state.reconcileResource({
|
|
|
|
|
...inventoryScript,
|
|
|
|
|
rawUrl: script.rawUrl,
|
|
|
|
|
}),
|
2026-07-18 03:42:35 +00:00
|
|
|
source,
|
|
|
|
|
},
|
|
|
|
|
sourceMapRawUrl: script.sourceMapRawUrl,
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const captureScriptSource = async (
|
|
|
|
|
script: CapturedScript,
|
|
|
|
|
state: ScriptDraftState,
|
2026-10-19 13:22:54 +08:00
|
|
|
sourceBudget: CdpScriptSourceBudget,
|
2026-07-18 03:42:35 +00:00
|
|
|
): Promise<WebPageInspection["scripts"]["items"][number]["source"]> => {
|
2026-09-29 13:56:49 +08:00
|
|
|
const { context } = state;
|
2026-10-19 13:22:54 +08:00
|
|
|
const response = await context.connection.send(
|
|
|
|
|
"Debugger.getScriptSource",
|
|
|
|
|
{ scriptId: script.scriptId },
|
|
|
|
|
context.sessionId,
|
|
|
|
|
sourceBudget.signal,
|
2026-07-18 03:42:35 +00:00
|
|
|
);
|
2026-10-19 13:22:54 +08:00
|
|
|
throwIfCallerCancelled(context.signal, context.operation);
|
|
|
|
|
const content = requiredScriptSource(response, context.operation);
|
|
|
|
|
if (sourceBudget.timedOut) {
|
|
|
|
|
state.events.completeness.exclude(
|
|
|
|
|
"script_sources",
|
|
|
|
|
"resource_budget_exhausted",
|
|
|
|
|
);
|
2026-10-22 05:47:38 +08:00
|
|
|
return sourceExcluded(
|
|
|
|
|
cdpScriptSourceDeadlineOmissionReason(sourceBudget.commandTimeoutMs),
|
|
|
|
|
);
|
2026-10-19 13:22:54 +08:00
|
|
|
}
|
|
|
|
|
if (!sourceBudget.admit(content)) {
|
|
|
|
|
state.events.completeness.exclude(
|
|
|
|
|
"script_sources",
|
|
|
|
|
"resource_budget_exhausted",
|
|
|
|
|
);
|
|
|
|
|
return sourceExcluded(CDP_SCRIPT_SOURCE_BYTES_OMISSION_REASON);
|
|
|
|
|
}
|
2026-07-18 03:42:35 +00:00
|
|
|
return sourceResult(content);
|
|
|
|
|
};
|
|
|
|
|
|
2026-10-19 13:22:54 +08:00
|
|
|
const throwIfCallerCancelled = (
|
|
|
|
|
signal: AbortSignal | undefined,
|
|
|
|
|
operation: BrowserObservationOperation,
|
|
|
|
|
): void => {
|
|
|
|
|
if (signal?.aborted === true)
|
|
|
|
|
throw new AnalysisCancelledError(operation, { cause: signal.reason });
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const isSourceDeadlineExpired = (
|
|
|
|
|
sourceBudget: CdpScriptSourceBudget | undefined,
|
|
|
|
|
): boolean => sourceBudget?.timedOut === true;
|
|
|
|
|
|
2026-07-18 03:42:35 +00:00
|
|
|
const keyAndBuildInventory = (
|
|
|
|
|
drafts: readonly {
|
|
|
|
|
readonly item: ScriptDraftItem;
|
|
|
|
|
readonly sourceMapRawUrl: string | null;
|
|
|
|
|
}[],
|
|
|
|
|
scriptCount: number,
|
|
|
|
|
): CapturedScripts => {
|
|
|
|
|
const keyed = drafts.map((draft) => ({
|
|
|
|
|
...draft,
|
|
|
|
|
base: stableWebScriptKey(draft.item),
|
|
|
|
|
}));
|
|
|
|
|
keyed.sort(
|
|
|
|
|
(left, right) =>
|
2026-10-11 10:10:01 -07:00
|
|
|
compareUnicodeCodePoints(left.base, right.base) ||
|
|
|
|
|
compareUnicodeCodePoints(
|
|
|
|
|
sourceDigest(left.item.source),
|
2026-07-18 03:42:35 +00:00
|
|
|
sourceDigest(right.item.source),
|
|
|
|
|
) ||
|
2026-10-11 10:10:01 -07:00
|
|
|
compareUnicodeCodePoints(
|
|
|
|
|
left.item.frame_id ?? "",
|
|
|
|
|
right.item.frame_id ?? "",
|
|
|
|
|
),
|
2026-07-18 03:42:35 +00:00
|
|
|
);
|
|
|
|
|
const totals = new Map<string, number>();
|
|
|
|
|
for (const { base } of keyed) totals.set(base, (totals.get(base) ?? 0) + 1);
|
|
|
|
|
const seen = new Map<string, number>();
|
|
|
|
|
const sourceMapRequests: WebSourceMapRequest[] = [];
|
|
|
|
|
const items = keyed.map(({ base, item, sourceMapRawUrl }) => {
|
|
|
|
|
const occurrence = (seen.get(base) ?? 0) + 1;
|
|
|
|
|
seen.set(base, occurrence);
|
|
|
|
|
const scriptKey =
|
|
|
|
|
totals.get(base) === 1 ? base : `${base}_${String(occurrence)}`;
|
|
|
|
|
if (item.source_map_url !== null && sourceMapRawUrl !== null)
|
|
|
|
|
sourceMapRequests.push({
|
|
|
|
|
scriptKey,
|
|
|
|
|
declaredUrl: item.source_map_url,
|
|
|
|
|
fetchUrl: sourceMapRawUrl,
|
|
|
|
|
});
|
|
|
|
|
return { script_key: scriptKey, ...item };
|
|
|
|
|
});
|
|
|
|
|
return {
|
|
|
|
|
inventory: { total: scriptCount, items },
|
|
|
|
|
sourceMapRequests,
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const sourceDigest = (
|
|
|
|
|
source: WebPageInspection["scripts"]["items"][number]["source"],
|
|
|
|
|
): string => (source.included ? source.artifact.sha256 : source.reason);
|