perf(javascript): index artifact package directory lookups (#13327)

This commit is contained in:
morluto authored and GitHub committed 2026-11-22 04:38:43 +00:00
1 parent 11a75a5c19
commit 4b86932527
6 files changed
+99 -113

No files matched your search

@@ -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(),
@@ -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) => {
@@ -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;
@@ -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<string, JavaScriptArtifactFile>(
@@ -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({