refactor(domain): extract source-map envelope walk and disambiguate stringValue (#828)

Move the indexed sections walk shared by the browser fetcher and
the artifact reader into domain/sourceMapEnvelope with the
fetchers stricter offset validation as an option; both validators
keep their leaf strictness. Rename the browser CDP stringValue to
cdpStringValue across its 18 files, fold the fingerprint duplicate
into the static helper, and rename the version-keys shadow to
nonEmptyString.
This commit is contained in:
morluto
2026-10-07 11:23:37 +08:00
committed by GitHub
parent 6dd719eaf6
commit 64ebdf4501
24 changed files with 300 additions and 200 deletions
+9 -27
View File
@@ -14,6 +14,10 @@ import type {
import { webSourceMapsSchema } from "../domain/webBundleAnalysis.js";
import { createWebTextArtifact } from "../domain/webContentArtifact.js";
import { safeParseJson } from "../domain/safeJson.js";
import {
flattenSourceMapLeaves,
isVersion3Map,
} from "../domain/sourceMapEnvelope.js";
export interface WebSourceMapRequest {
readonly scriptKey: string;
@@ -278,32 +282,13 @@ const validSourceMapEnvelope = (text: string): boolean => {
const parsedResult = safeParseJson(text);
if (!parsedResult.ok) return false;
const parsed: unknown = parsedResult.value;
if (!isRecord(parsed) || parsed.version !== 3) return false;
if (typeof parsed.mappings === "string") return validSourceMapLeaf(parsed);
if (!Array.isArray(parsed.sections)) return false;
const pending: unknown[] = [...parsed.sections];
while (pending.length > 0) {
const section = pending.pop();
if (
!isRecord(section) ||
!isRecord(section.offset) ||
!validSourceMapOffset(section.offset.line) ||
!validSourceMapOffset(section.offset.column) ||
!("map" in section)
)
return false;
const map = section.map;
if (!isRecord(map) || map.version !== 3) return false;
if (Array.isArray(map.sections))
for (const child of map.sections) pending.push(child);
else if (!validSourceMapLeaf(map)) return false;
}
return true;
if (isVersion3Map(parsed) && typeof parsed.mappings === "string")
return validSourceMapLeaf(parsed);
const leaves = flattenSourceMapLeaves(parsed, { validateOffsets: true });
if (leaves === undefined) return false;
return leaves.every(validSourceMapLeaf);
};
const validSourceMapOffset = (value: unknown): boolean =>
typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
const validSourceMapLeaf = (map: Readonly<Record<string, unknown>>): boolean =>
typeof map.mappings === "string" &&
Array.isArray(map.sources) &&
@@ -380,9 +365,6 @@ const sourceMediaType = (source: string | null): string =>
? "text/typescript"
: "text/javascript";
const isRecord = (value: unknown): value is Readonly<Record<string, unknown>> =>
typeof value === "object" && value !== null && !Array.isArray(value);
const originalDependency = (
node: t.Node,
): {