mirror of
https://github.com/morluto/rea.git
synced 2026-12-02 13:11:18 -05:00
5f702dd6cc
Normalize parsed facts at their owning boundaries across PE exports and certificates, ELF section names, GPU bundles, IL2CPP generic types, dyld slice selection, source maps, environment replay, and JavaScript mapped arguments. Preserve partial facts and explicit unsupported cases; clarify the corresponding documentation. Validation: 831 affected tests passed with one existing skip across 23 files; build, typecheck/lint, documentation, formatting, and 2133-file test discovery passed. Format controls are synthetic; real Unity, GPU, and ARM-device execution is unverified. Fixes #6709 Fixes #6688 Fixes #6719 Fixes #6718 Fixes #6704 Fixes #6703 Fixes #6692 Fixes #6723 Fixes #6687 Fixes #6726 Fixes #6696 Fixes #6691 Fixes #6727
193 lines
6.4 KiB
TypeScript
193 lines
6.4 KiB
TypeScript
import { posix } from "node:path";
|
|
import { domainToASCII, fileURLToPath, pathToFileURL } from "node:url";
|
|
import { TraceMap } from "@jridgewell/trace-mapping";
|
|
|
|
import { z } from "zod";
|
|
|
|
/** Map-scoped source declaration and its path interpretation for matching. */
|
|
export const javascriptSourceMapReferenceSchema = z.strictObject({
|
|
source_name: z.string(),
|
|
source_root: z.string().nullable(),
|
|
map_path: z.string().min(1),
|
|
resolution: z.discriminatedUnion("kind", [
|
|
z.strictObject({
|
|
kind: z.enum(["artifact-relative", "suffix"]),
|
|
path: z.string().min(1),
|
|
}),
|
|
z.strictObject({
|
|
kind: z.literal("unresolved"),
|
|
reason: z.string().min(1),
|
|
}),
|
|
]),
|
|
});
|
|
|
|
/** Typed source-map context retained through graph serialization. */
|
|
export type JavaScriptSourceMapReference = z.infer<
|
|
typeof javascriptSourceMapReferenceSchema
|
|
>;
|
|
|
|
/** Interpret declarations together once at their source-map ingestion boundary. */
|
|
export const resolveJavaScriptSourceMapReferences = (
|
|
sourceNames: readonly (string | null)[],
|
|
sourceRoot: string | null,
|
|
mapPath: string,
|
|
): (JavaScriptSourceMapReference | null)[] => {
|
|
if (sourceNames.length === 0) return [];
|
|
const resolvedSources = resolveSourceMapUrls(
|
|
sourceNames.map((sourceName) => sourceName ?? ""),
|
|
sourceRoot,
|
|
"",
|
|
);
|
|
return sourceNames.map((sourceName, index) => {
|
|
if (sourceName === null) return null;
|
|
const reference = resolvedSources[index];
|
|
if (reference === undefined)
|
|
throw new TypeError(
|
|
`TraceMap did not resolve source declaration ${String(index)}`,
|
|
);
|
|
return {
|
|
source_name: sourceName,
|
|
source_root: sourceRoot,
|
|
map_path: mapPath,
|
|
resolution: resolveJavaScriptSourceMapPath(reference, mapPath),
|
|
};
|
|
});
|
|
};
|
|
|
|
/** Resolve one source declaration with ECMA-426's WHATWG URL parsing rule. */
|
|
export const resolveJavaScriptSourceMapUrl = (
|
|
source: string | null,
|
|
sourceRoot: string | null | undefined,
|
|
mapUrl: string,
|
|
): string | null => {
|
|
if (source === null) return null;
|
|
return (
|
|
URL.parse(sourceMapReference(source, sourceRoot), mapUrl)?.href ?? null
|
|
);
|
|
};
|
|
|
|
const sourceMapReference = (
|
|
source: string,
|
|
sourceRoot: string | null | undefined,
|
|
): string => {
|
|
if (sourceRoot == null) return source;
|
|
return `${sourceRoot}${sourceRoot.endsWith("/") ? "" : "/"}${source}`;
|
|
};
|
|
|
|
const resolveSourceMapUrls = (
|
|
sources: string[],
|
|
sourceRoot: string | null | undefined,
|
|
mapUrl: string,
|
|
): readonly string[] => {
|
|
// ECMA-426 prefixes each source when sourceRoot is non-null, adding `/`
|
|
// only when the root does not already end with one. Do this before handing
|
|
// the references to TraceMap so its URL resolver cannot duplicate a slash.
|
|
return new TraceMap(
|
|
{
|
|
version: 3,
|
|
names: [],
|
|
sources: sources.map((source) => sourceMapReference(source, sourceRoot)),
|
|
mappings: "",
|
|
},
|
|
mapUrl,
|
|
).resolvedSources;
|
|
};
|
|
|
|
/** Resolve a source-map URL reference without changing its recorded identity. */
|
|
const resolveJavaScriptSourceMapPath = (
|
|
source: string,
|
|
mapPath: string,
|
|
): JavaScriptSourceMapReference["resolution"] => {
|
|
const portable = source.replaceAll("\\", "/");
|
|
const windowsPath = /^[a-z]:\//iu.test(portable) || source.startsWith("\\\\");
|
|
// Preserve parent traversal rather than letting URL resolution clamp at '/'.
|
|
const anchor = `/${"source/".repeat(portable.split("/").length + 1)}`;
|
|
const directory = posix.join(anchor, posix.dirname(mapPath));
|
|
const base = pathToFileURL(`${directory}/`, { windows: false });
|
|
const absolute = windowsPath ? windowsFileUrl(source) : URL.parse(source);
|
|
const resolved = windowsPath ? absolute : URL.parse(source, base);
|
|
if (resolved === null)
|
|
return {
|
|
kind: "unresolved",
|
|
reason: "Source reference is not a valid URL or filesystem path.",
|
|
};
|
|
if (!resolved.pathname.startsWith("/"))
|
|
return {
|
|
kind: "unresolved",
|
|
reason:
|
|
"Source reference uses a non-hierarchical URL without a file pathname.",
|
|
};
|
|
if (resolved.pathname.endsWith("/"))
|
|
return {
|
|
kind: "unresolved",
|
|
reason: "Source reference names a directory rather than a file.",
|
|
};
|
|
// Reject separators in declarations, not escaped literal names in the owned base.
|
|
const declaredPath = windowsPath
|
|
? (absolute?.pathname ?? "")
|
|
: (URL.parse(source, "file:///")?.pathname ?? "");
|
|
if (/%(?:2f|5c)/iu.test(declaredPath))
|
|
return {
|
|
kind: "unresolved",
|
|
reason: "Source reference contains an encoded path separator.",
|
|
};
|
|
const relative =
|
|
absolute === null
|
|
? !portable.startsWith("/")
|
|
: absolute.href !== resolved.href;
|
|
const path = decodedUrlPath(resolved);
|
|
if (path === null)
|
|
return {
|
|
kind: "unresolved",
|
|
reason:
|
|
"Source reference has malformed URL encoding or an invalid file URL.",
|
|
};
|
|
const value = relative
|
|
? posix.relative(anchor, path)
|
|
: path.replace(/^\/+/, "");
|
|
if (value === "" || value === ".." || value.startsWith("../"))
|
|
return {
|
|
kind: "unresolved",
|
|
reason: "Source reference resolves outside the inventoried artifact.",
|
|
};
|
|
return { path: value, kind: relative ? "artifact-relative" : "suffix" };
|
|
};
|
|
|
|
// Reject URL delimiters/escapes before IDNA can truncate or decode the host.
|
|
const unusableHost = /[\0\t\n\r #/:<>?@[\\\]^|%]/u;
|
|
|
|
// URL/IDNA validation also rejects Unicode hosts that normalize to empty.
|
|
const carriesUsableHost = (host: string): boolean =>
|
|
!unusableHost.test(host) && domainToASCII(host) !== "";
|
|
|
|
const windowsFileUrl = (path: string): URL | null => {
|
|
// A malformed UNC host aborts pathToFileURL() on the Node builds this
|
|
// package admits, so decline the reference before handing the path over
|
|
// rather than trusting a TypeError that never arrives.
|
|
if (
|
|
path.startsWith("\\\\") &&
|
|
!carriesUsableHost(path.slice(2).split("\\")[0] ?? "")
|
|
)
|
|
return null;
|
|
try {
|
|
return pathToFileURL(path, { windows: true });
|
|
} catch (cause: unknown) {
|
|
if (cause instanceof TypeError) return null;
|
|
throw cause;
|
|
}
|
|
};
|
|
|
|
const decodedUrlPath = (url: URL): string | null => {
|
|
try {
|
|
if (url.protocol === "file:") {
|
|
const windows = url.hostname !== "" && url.hostname !== "localhost";
|
|
const path = fileURLToPath(url, { windows });
|
|
return windows ? path.replaceAll("\\", "/") : path;
|
|
}
|
|
return `${url.host}${decodeURIComponent(url.pathname)}`;
|
|
} catch (cause: unknown) {
|
|
if (cause instanceof URIError || cause instanceof TypeError) return null;
|
|
throw cause;
|
|
}
|
|
};
|