perf: cache startup package metadata

This commit is contained in:
Peter Steinberger
2026-05-02 16:11:00 +01:00
parent 5b063c2d83
commit ad0d87d881
15 changed files with 439 additions and 23 deletions

View File

@@ -3,6 +3,7 @@ import path from "node:path";
import { fileURLToPath } from "node:url";
const STARTUP_METADATA_FILE = "cli-startup-metadata.json";
const startupMetadataByPath = new Map<string, Record<string, unknown> | null>();
function resolveStartupMetadataPathCandidates(moduleUrl: string): string[] {
const moduleDir = path.dirname(fileURLToPath(moduleUrl));
@@ -14,10 +15,20 @@ function resolveStartupMetadataPathCandidates(moduleUrl: string): string[] {
export function readCliStartupMetadata(moduleUrl: string): Record<string, unknown> | null {
for (const metadataPath of resolveStartupMetadataPathCandidates(moduleUrl)) {
const cached = startupMetadataByPath.get(metadataPath);
if (cached !== undefined) {
if (cached) {
return cached;
}
continue;
}
try {
return JSON.parse(fs.readFileSync(metadataPath, "utf8")) as Record<string, unknown>;
const parsed = JSON.parse(fs.readFileSync(metadataPath, "utf8")) as Record<string, unknown>;
startupMetadataByPath.set(metadataPath, parsed);
return parsed;
} catch {
// Try the next bundled/source layout before falling back to dynamic startup work.
startupMetadataByPath.set(metadataPath, null);
}
}
return null;
@@ -25,4 +36,7 @@ export function readCliStartupMetadata(moduleUrl: string): Record<string, unknow
export const __testing = {
resolveStartupMetadataPathCandidates,
clearStartupMetadataCache(): void {
startupMetadataByPath.clear();
},
};