mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 09:50:42 +00:00
perf: cache startup package metadata
This commit is contained in:
@@ -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();
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user