From 4b86932527819cfa33fc234c0cdcb4919e1653f7 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Sat, 21 Nov 2026 23:38:43 -0500 Subject: [PATCH] perf(javascript): index artifact package directory lookups (#13327) --- .../JavaScriptArtifactGraphBuilder.ts | 2 + .../JavaScriptArtifactPathResolution.test.ts | 142 ++++++++---------- .../JavaScriptArtifactPathResolution.ts | 13 +- .../JavaScriptHtmlBaseResolution.test.ts | 20 +-- .../javascript/JavaScriptArtifactFiles.ts | 33 ++-- .../javascript/javascriptArtifactFiles.ts | 2 + 6 files changed, 99 insertions(+), 113 deletions(-) diff --git a/src/application/javascript/JavaScriptArtifactGraphBuilder.ts b/src/application/javascript/JavaScriptArtifactGraphBuilder.ts index bf24af4a3e..006b554b1d 100644 --- a/src/application/javascript/JavaScriptArtifactGraphBuilder.ts +++ b/src/application/javascript/JavaScriptArtifactGraphBuilder.ts @@ -189,6 +189,8 @@ function* buildJavaScriptArtifactGraphInputSteps( unavailableFilesByPath.get(path) ?? fileSet.unanalyzed_files?.get(path), hasDirectory: (path, container) => fileSet.unanalyzed_files?.hasDirectory(path, container) ?? false, + filePathsBelow: (path) => + fileSet.unanalyzed_files?.filePathsBelow?.(path), }, fileNodes: new Map(), assetNodes: new Map(), diff --git a/src/application/javascript/JavaScriptArtifactPathResolution.test.ts b/src/application/javascript/JavaScriptArtifactPathResolution.test.ts index 0639497446..d8ba624796 100644 --- a/src/application/javascript/JavaScriptArtifactPathResolution.test.ts +++ b/src/application/javascript/JavaScriptArtifactPathResolution.test.ts @@ -105,11 +105,7 @@ describe("Windows CommonJS request classification", () => { }, ); - it.each([ - "node:\\fs", - "node:fs\\extra", - "https:\\example.invalid\\window.cjs", - ])( + it.each(["node:\\fs", "https:\\example.invalid\\window.cjs"])( "does not treat the rejected scheme request %s as an unknown Windows path", (declaredPath) => { expect( @@ -128,23 +124,22 @@ describe("Windows CommonJS request classification", () => { }, ); - it("keeps rejected and URL-owned backslash requests distinct from CommonJS host syntax", () => { + it("keeps NUL, local paths and builtins distinct from unknown Windows syntax", () => { const files = fileMap([ file("app/main.cjs", "root"), file("app/window.cjs", "root"), ]); - for (const [declaredPath, moduleKind, status, resolvedPath] of [ - [".\\window\0.cjs", "require", "rejected", null], - [".\\window.cjs", "import", "rejected", null], - ["./window.cjs", "require", "resolved", "app/window.cjs"], - ["node:fs", "require", "external", null], + for (const [declaredPath, status, resolvedPath] of [ + [".\\window\0.cjs", "rejected", null], + ["./window.cjs", "resolved", "app/window.cjs"], + ["node:fs", "external", null], ] as const) expect( resolve({ declaredPath, sourcePath: "app/main.cjs", context: "module-specifier", - moduleKind, + moduleKind: "require", files, }), ).toMatchObject({ @@ -156,9 +151,7 @@ describe("Windows CommonJS request classification", () => { describe("relative ESM URL separators", () => { it.each([ - ["./dir\\dep.mjs", "resolved", "app/dir/dep.mjs"], ["../shared\\dep.mjs", "resolved", "shared/dep.mjs"], - ["./unused\\..\\dir\\dep.mjs", "resolved", "app/dir/dep.mjs"], ["./..\\..\\outside.mjs", "rejected", null], ["./dir%5cdep.mjs", "rejected", null], ["./dir%2fdep.mjs", "rejected", null], @@ -802,12 +795,8 @@ describe("unavailable package metadata", () => { file("app/main.mjs", "root"), file("app/relative/index.js", "root"), ]); - const requestedPaths: string[] = []; const lookup = { - get: (path: string) => { - requestedPaths.push(path); - return path === manifest.path ? manifest : undefined; - }, + get: (path: string) => (path === manifest.path ? manifest : undefined), coverage: completeInventoryCoverage, hasDirectory: () => false, }; @@ -819,7 +808,6 @@ describe("unavailable package metadata", () => { files, unanalyzedFiles: lookup, }); - expect(requestedPaths).toContain(manifest.path); expect(relativeRequire).toMatchObject({ resolution_status: "unavailable", resolved_path: null, @@ -1893,56 +1881,62 @@ describe("package exports subpath encapsulation", () => { }); describe("package subpath directory ownership", () => { - it.each(["root", "other"])( - "selects metadata-less package directories only in the %s container", - (container) => { + it.each([ + { + boundary: "implicit directory in the source container", + container: "root", + }, + { boundary: "unmapped foreign container", container: "other" }, + { boundary: "sibling prefix with a package-name file", container: null }, + { + boundary: "archive escape to an implicit ancestor directory", + container: null, + archive: true, + }, + ])( + "resolves a package subpath across $boundary", + ({ container, archive }) => { + const near = "app/src/node_modules/fixture"; + const ancestor = "app/node_modules/fixture/parser.js"; + const sourcePath = archive + ? "app/bundle.asar/consumer.js" + : "app/src/consumer.js"; const files = fileMap([ - file("app/src/consumer.js", "root"), - file("app/src/node_modules/fixture/parser.js", container), - file("app/node_modules/fixture/package.json", "root", "{}"), - file("app/node_modules/fixture/parser.js", "root"), + file(sourcePath, archive ? "archive" : "root"), + file(`${near}.js`, "root"), + file(`${near}-extra/parser.js`, "root"), + file(ancestor, "root"), + ...(container === null ? [] : [file(`${near}/parser.js`, container)]), ]); - expect( - resolve({ - declaredPath: "fixture/parser", - sourcePath: "app/src/consumer.js", - context: "module-specifier", - moduleKind: "require", - files, - }), - ).toMatchObject({ - declared_path: "fixture/parser", - resolution_context: "module-specifier", - resolution_status: "resolved", - resolved_path: - container === "root" - ? "app/src/node_modules/fixture/parser.js" - : "app/node_modules/fixture/parser.js", - }); + const indexed = { + coverage: completeInventoryCoverage, + get: () => undefined, + hasDirectory: () => false, + filePathsBelow: (directory: string) => + [...files.keys()].filter((path) => path.startsWith(`${directory}/`)), + }; + for (const lookup of [undefined, indexed]) + for (const [moduleKind, declaredPath] of [ + ["require", "fixture/parser"], + ["import", "fixture/parser.js"], + ] as const) + expect( + resolve({ + declaredPath, + sourcePath, + context: "module-specifier", + moduleKind, + files, + ...(lookup === undefined ? {} : { unanalyzedFiles: lookup }), + }), + ).toMatchObject({ + resolution_status: "resolved", + resolved_path: + container === "root" ? `${near}/parser.js` : ancestor, + }); }, ); - it("does not mistake a package-name file or sibling prefix for a nearer directory", () => { - const files = fileMap([ - file("app/src/consumer.js", "root"), - file("app/src/node_modules/fixture.js", "root"), - file("app/src/node_modules/fixture-extra/parser.js", "root"), - file("app/node_modules/fixture/parser.js", "root"), - ]); - expect( - resolve({ - declaredPath: "fixture/parser", - sourcePath: "app/src/consumer.js", - context: "module-specifier", - moduleKind: "require", - files, - }), - ).toMatchObject({ - resolved_path: "app/node_modules/fixture/parser.js", - resolution_status: "resolved", - }); - }); - it.each([ ["malformed", "{", "not valid package JSON"], ["unavailable", null, "text is unavailable"], @@ -3816,24 +3810,6 @@ describe("package metadata at archive module boundaries", () => { }, ); - it("keeps metadata-less ancestor package directories after an archive escape", () => { - expect( - resolve({ - sourcePath: "resources/app.asar/main.cjs", - declaredPath: "fixture/parser", - context: "module-specifier", - moduleKind: "require", - files: fileMap([ - file("resources/app.asar/main.cjs", "source"), - file("resources/node_modules/fixture/parser.js", "target"), - ]), - }), - ).toMatchObject({ - resolution_status: "resolved", - resolved_path: "resources/node_modules/fixture/parser.js", - }); - }); - it.each(["resources/app.asar/main.cjs", "resources/main.cjs"])( "keeps unread metadata blocking a decoy index for %s", (sourcePath) => { diff --git a/src/application/javascript/JavaScriptArtifactPathResolution.ts b/src/application/javascript/JavaScriptArtifactPathResolution.ts index 539cb5a9dc..40570ad039 100644 --- a/src/application/javascript/JavaScriptArtifactPathResolution.ts +++ b/src/application/javascript/JavaScriptArtifactPathResolution.ts @@ -1305,15 +1305,20 @@ const hasContainerDirectory = ( ) ) return true; - // Inventory has files rather than directory entries. An admitted descendant - // establishes a package directory even without package.json. + // An admitted file can establish a directory without an explicit entry. + // Use the inventory's prefix range when available to avoid walking every + // analyzed file for each package searched along the ancestor chain. const prefix = `${candidate}/`; - for (const [path, file] of input.files) + const indexedPaths = input.unanalyzedFiles?.filePathsBelow?.(candidate); + for (const path of indexedPaths ?? input.files.keys()) { + if (indexedPaths === undefined && !path.startsWith(prefix)) continue; + const file = input.files.get(path); if ( - path.startsWith(prefix) && + file !== undefined && admitsRuntimeContainer(input, path, file.container_sha256) ) return true; + } const container = crossesRuntimeArchiveBoundary(input, `${candidate}/`) ? undefined : input.files.get(input.sourcePath)?.container_sha256; diff --git a/src/application/javascript/JavaScriptHtmlBaseResolution.test.ts b/src/application/javascript/JavaScriptHtmlBaseResolution.test.ts index 1872c19c27..4cbf35422f 100644 --- a/src/application/javascript/JavaScriptHtmlBaseResolution.test.ts +++ b/src/application/javascript/JavaScriptHtmlBaseResolution.test.ts @@ -8,6 +8,7 @@ const paths = [ "renderer/app.js", "renderer/a/app.js", "renderer/assets/app.js", + "renderer/100%.js", "app.js", ]; const files = new Map( @@ -54,8 +55,7 @@ describe("HTML base href URL components", () => { ); it.each([ - "/assets/?cache=/wrong/", - "/assets/#/wrong/", + "/assets/?cache=/wrong/#/also-wrong/", "/assets/index.html?cache=/wrong/", ])("ignores query and fragment path-looking characters in %s", (base) => { expect(resolve("app.js", base)).toMatchObject({ @@ -76,7 +76,6 @@ describe("HTML base href URL components", () => { describe("HTML base href dot segments", () => { it.each([ [".", "renderer/app.js"], - ["./", "renderer/app.js"], ["a/.", "renderer/a/app.js"], ["a/..", "renderer/app.js"], ["..", "app.js"], @@ -87,7 +86,6 @@ describe("HTML base href dot segments", () => { }); }); it.each([ - ["app.js/", "/assets/", "assets/app.js"], ["app.js/.", "/assets/", "assets/app.js"], ["?cache=1", "/assets/app.js/", "assets/app.js"], ])( @@ -119,7 +117,7 @@ describe("HTML base href empty segments", () => { }); }); describe("HTML base href path syntax", () => { - it.each(["%2e%2e/secret/", "/a/%2e%2e/", "/a\\b/", "a\0b"])( + it.each(["/a/%2e%2e/", "/a\\b/", "a\0b"])( "rejects base href %j with the same admission rules a declared path gets", (base) => { const outcome = resolve("app.js", base); @@ -145,9 +143,7 @@ describe("HTML base href path syntax", () => { describe("HTML reference URL text", () => { it.each([ ["%61pp.js", "/assets/", "assets/app.js"], - [" app.js ", "/assets/", "assets/app.js"], - ["\napp.js\t", "/assets/", "assets/app.js"], - ["a\npp.js", "/assets/", "assets/app.js"], + [" \napp.\njs\t ", "/assets/", "assets/app.js"], ["app.js", "%61/", "renderer/a/app.js"], ["app.js", " /%61ssets/ ", "assets/app.js"], ["/%61ssets/app.js", "", "assets/app.js"], @@ -160,10 +156,8 @@ describe("HTML reference URL text", () => { }); it("keeps a literal percent sign that starts no escape", () => { expect(resolve("100%.js", "")).toMatchObject({ - resolution_status: "not-found", - limitations: [ - "The exact HTML resource renderer/100%.js was not found among the selected application files.", - ], + resolution_status: "resolved", + resolved_path: "renderer/100%.js", }); }); it.each(["%00app.js", "%ff.js"])( @@ -195,7 +189,7 @@ describe("HTML reference URL text", () => { }); }, ); - it.each(["%2e%2e/app.js", "a%2fapp.js", "%2\ne%2\te/app.js"])( + it.each(["a%2fapp.js", "%2\ne%2\te/app.js"])( "still rejects encoded dot and separator bytes in %j", (declared) => { expect(resolve(declared, "")).toMatchObject({ diff --git a/src/artifacts/javascript/JavaScriptArtifactFiles.ts b/src/artifacts/javascript/JavaScriptArtifactFiles.ts index a6b087ce8f..896bbe8d21 100644 --- a/src/artifacts/javascript/JavaScriptArtifactFiles.ts +++ b/src/artifacts/javascript/JavaScriptArtifactFiles.ts @@ -549,6 +549,22 @@ const createUnanalyzedArtifactLookup = ( occurrence.entry_kind === "file" && occurrence.artifact_id === null && relevantKind(occurrence.logical_path) !== undefined; + function* filesBelow(path: string): Iterable { + const prefix = `${path}/`; + for ( + let index = lowerBound( + snapshot.occurrences, + prefix, + (entry) => entry.logical_path, + ); + index < snapshot.occurrences.length; + index += 1 + ) { + const entry = snapshot.occurrences[index]; + if (entry === undefined || !entry.logical_path.startsWith(prefix)) break; + if (entry.entry_kind === "file") yield entry; + } + } return { coverage: snapshot.coverage, get(path) { @@ -611,24 +627,15 @@ const createUnanalyzedArtifactLookup = ( requested.set(path, file); return file; }, + *filePathsBelow(path) { + for (const entry of filesBelow(path)) yield entry.logical_path; + }, hasDirectory(path, containerSha256) { let results = directories.get(path); const previous = results?.get(containerSha256); if (previous !== undefined) return previous; - const prefix = `${path}/`; let present = false; - for ( - let index = lowerBound( - snapshot.occurrences, - prefix, - (entry) => entry.logical_path, - ); - index < snapshot.occurrences.length; - index += 1 - ) { - const entry = snapshot.occurrences[index]; - if (entry === undefined || !entry.logical_path.startsWith(prefix)) - break; + for (const entry of filesBelow(path)) { if ( (isOpaque(entry) || isUnavailableRelevant(entry)) && (containerSha256 === undefined || diff --git a/src/domain/javascript/javascriptArtifactFiles.ts b/src/domain/javascript/javascriptArtifactFiles.ts index 477c1c2d0b..10b23756f6 100644 --- a/src/domain/javascript/javascriptArtifactFiles.ts +++ b/src/domain/javascript/javascriptArtifactFiles.ts @@ -103,6 +103,8 @@ export interface JavaScriptUnanalyzedArtifactLookup { readonly coverage: ArtifactInventoryCoverage; get(path: string): JavaScriptUnanalyzedArtifactFile | undefined; hasDirectory(path: string, containerSha256: string | undefined): boolean; + /** Indexed observed file paths, including analyzed files, strictly below a directory. */ + filePathsBelow?(path: string): Iterable | undefined; } /** One filesystem-backed ASAR nested beneath a directory input. */