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
@@ -239,9 +239,9 @@ const semanticValue = (
if (node.kind === "ipc-channel")
return properties.resolution === "dynamic"
? null
: (stringValue(properties.channel) ?? firstLabel(node));
: (nonEmptyString(properties.channel) ?? firstLabel(node));
if (node.kind === "context-bridge-api")
return firstLabel(node) ?? stringValue(properties.api_name);
return firstLabel(node) ?? nonEmptyString(properties.api_name);
if (node.kind === "native-export")
return (
joined(
@@ -258,8 +258,8 @@ const semanticValue = (
].includes(node.kind)
)
return joined(properties.declared_path, properties.resolved_path);
if (node.kind === "package") return stringValue(properties.name);
return canonicalPath(node) ?? stringValue(properties.path);
if (node.kind === "package") return nonEmptyString(properties.name);
return canonicalPath(node) ?? nonEmptyString(properties.path);
};
const canonicalPath = (node: ApplicationNode): string | null =>
@@ -286,11 +286,11 @@ const uniqueStringProperty = (
};
const joined = (...values: readonly unknown[]): string | null => {
const strings = values.map(stringValue);
const strings = values.map(nonEmptyString);
return strings.some((value) => value === null) ? null : strings.join("\0");
};
const stringValue = (value: unknown): string | null =>
const nonEmptyString = (value: unknown): string | null =>
typeof value === "string" && value !== "" ? value : null;
const stringList = (value: unknown): string[] =>
+1 -3
View File
@@ -4,6 +4,7 @@ import * as t from "@babel/types";
import { compareCodePoints } from "./canonicalOrdering.js";
import { propertyName } from "./javascriptAstValues.js";
import { stringValue } from "./javascriptStaticAnalysisHelpers.js";
/** Static CommonJS export names. */
export interface StaticExports {
@@ -123,6 +124,3 @@ const memberPath = (node: t.Node | null | undefined): string => {
}
return "";
};
const stringValue = (node: t.Node | null | undefined): string | undefined =>
t.isStringLiteral(node) ? node.value : undefined;
+57
View File
@@ -0,0 +1,57 @@
import { describe, expect, it } from "vitest";
import { flattenSourceMapLeaves, isVersion3Map } from "./sourceMapEnvelope.js";
const leaf = {
version: 3,
mappings: "AAAA",
sources: ["app.ts"],
names: [],
};
describe("source-map envelope walk", () => {
it("accepts version-3 maps and rejects other shapes", () => {
expect(isVersion3Map(leaf)).toBe(true);
expect(isVersion3Map({ version: 2, mappings: "AAAA" })).toBe(false);
expect(isVersion3Map(null)).toBe(false);
expect(isVersion3Map([{ version: 3 }])).toBe(false);
});
it("collects indexed section leaves without validating offsets by default", () => {
const root = {
version: 3,
sections: [
{ offset: { line: 0, column: 0 }, map: leaf },
{ offset: { line: 10, column: 0 }, map: { ...leaf, sources: [] } },
],
};
expect(flattenSourceMapLeaves(root)).toHaveLength(2);
});
it("rejects non-map sections and enforces offsets when requested", () => {
const missing = {
version: 3,
sections: [{ offset: { line: 0, column: 0 } }],
};
expect(flattenSourceMapLeaves(missing)).toBeUndefined();
const badOffset = {
version: 3,
sections: [{ offset: { line: -1, column: 0 }, map: leaf }],
};
expect(
flattenSourceMapLeaves(badOffset, { validateOffsets: true }),
).toBeUndefined();
expect(flattenSourceMapLeaves(badOffset)).toHaveLength(1);
});
it("rejects non-version-3 roots and nested maps", () => {
expect(flattenSourceMapLeaves({ version: 3 })).toHaveLength(1);
expect(flattenSourceMapLeaves({ mappings: "AAAA" })).toBeUndefined();
expect(
flattenSourceMapLeaves({
version: 3,
sections: [{ offset: { line: 0, column: 0 }, map: { version: 2 } }],
}),
).toBeUndefined();
});
});
+58
View File
@@ -0,0 +1,58 @@
/**
* Structural walk over indexed source-map envelopes. Both the browser
* source-map fetcher and the artifact source-map reader accept version-3
* maps with recursively indexed `sections`; this module owns that walk so
* the two validators cannot drift. Leaf content checks (mappings, sources,
* names, contents) stay with the callers, which enforce different
* strictness.
*/
type SourceMapRecord = Readonly<Record<string, unknown>>;
const isRecord = (value: unknown): value is SourceMapRecord =>
typeof value === "object" && value !== null && !Array.isArray(value);
/** Whether a parsed value has the version-3 map shape. */
export const isVersion3Map = (value: unknown): value is SourceMapRecord =>
isRecord(value) && value.version === 3;
const isValidOffset = (value: unknown): boolean =>
typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
export interface FlattenSourceMapOptions {
/** Require each section offset to carry safe-integer line/column. */
readonly validateOffsets?: boolean;
}
/**
* Collect leaf maps from an indexed source-map envelope, or undefined when
* the envelope is not a version-3 map. A map carrying neither `mappings`
* nor `sections` yields itself as the single leaf; callers decide whether
* that leaf is valid content.
*/
export const flattenSourceMapLeaves = (
root: unknown,
options?: FlattenSourceMapOptions,
): readonly SourceMapRecord[] | undefined => {
if (!isVersion3Map(root)) return undefined;
const leaves: SourceMapRecord[] = [];
const pending: unknown[] = [root];
while (pending.length > 0) {
const map = pending.pop();
if (!isVersion3Map(map)) return undefined;
if (Array.isArray(map.sections)) {
for (const section of map.sections) {
if (!isRecord(section) || !isRecord(section.map)) return undefined;
if (options?.validateOffsets === true) {
if (!isRecord(section.offset)) return undefined;
if (
!isValidOffset(section.offset.line) ||
!isValidOffset(section.offset.column)
)
return undefined;
}
pending.push(section.map);
}
} else leaves.push(map);
}
return leaves;
};