perf: reduce gateway runtime discovery overhead

This commit is contained in:
Peter Steinberger
2026-05-28 15:47:50 +01:00
parent 7b5f0c23e5
commit 68e6f03fd9
6 changed files with 161 additions and 50 deletions

View File

@@ -456,6 +456,53 @@ describe("getCachedPluginModuleLoader", () => {
});
});
it("lets native require handle compiled plugin SDK aliases before source-transform fallback", async () => {
const fromSourceTransformer = vi.fn();
const createJiti = vi.fn(() => fromSourceTransformer);
const nativeStub = vi.fn((target: string) => ({
ok: true as const,
moduleExport: { loadedFrom: target },
}));
vi.doMock("./native-module-require.js", () => ({
isJavaScriptModulePath: (p: string) =>
p.endsWith(".js") || p.endsWith(".mjs") || p.endsWith(".cjs"),
tryNativeRequireJavaScriptModule: nativeStub,
}));
const { getCachedPluginModuleLoader, getPluginModuleLoaderStats } = await importFreshModule<
typeof import("./plugin-module-loader-cache.js")
>(import.meta.url, "./plugin-module-loader-cache.js?scope=native-require-plugin-sdk-alias");
const cache = new Map();
const loader = getCachedPluginModuleLoader({
cache,
modulePath: "/repo/dist/extensions/demo/api.js",
importerUrl: "file:///repo/src/plugins/public-surface-loader.ts",
loaderFilename: "file:///repo/src/plugins/public-surface-loader.ts",
aliasMap: {
"openclaw/plugin-sdk": "/repo/dist/plugin-sdk/root-alias.cjs",
"openclaw/plugin-sdk/core": "/repo/dist/plugin-sdk/core.js",
},
createLoader: asPluginModuleLoaderFactory(createJiti),
});
const result = loader("/repo/dist/extensions/demo/api.js") as { loadedFrom: string };
expect(result.loadedFrom).toBe("/repo/dist/extensions/demo/api.js");
expect(createJiti).not.toHaveBeenCalled();
expect(fromSourceTransformer).not.toHaveBeenCalled();
expectNativeOptions(nativeStub, "/repo/dist/extensions/demo/api.js");
const options = callArg(nativeStub, 0, 1, "native options") as {
aliasMap?: Record<string, string>;
};
expect(options.aliasMap?.["openclaw/plugin-sdk/core"]).toBe("/repo/dist/plugin-sdk/core.js");
expectStats(getPluginModuleLoaderStats(), {
calls: 1,
nativeHits: 1,
nativeMisses: 0,
sourceTransformFallbacks: 0,
sourceTransformForced: 0,
});
});
it("does not source-transform fallback after native loading reaches a missing dependency", async () => {
const fromSourceTransformer = vi.fn();
const createJiti = vi.fn(() => fromSourceTransformer);