Skip to content

Commit 961eb95

Browse files
committed
perf(secrets): lazy-load provider env var exports
1 parent 8efbe8c commit 961eb95

2 files changed

Lines changed: 71 additions & 6 deletions

File tree

src/secrets/provider-env-vars.dynamic.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,33 @@ describe("provider env vars dynamic manifest metadata", () => {
5151
expect(mod.listKnownSecretEnvVarNames()).toContain("FIREWORKS_ALT_API_KEY");
5252
});
5353

54+
it("keeps lazy manifest-backed exports cold until accessed and resolves them once", async () => {
55+
loadPluginManifestRegistry.mockReturnValue({
56+
plugins: [
57+
{
58+
id: "external-fireworks",
59+
origin: "global",
60+
providerAuthEnvVars: {
61+
fireworks: ["FIREWORKS_ALT_API_KEY"],
62+
},
63+
},
64+
],
65+
diagnostics: [],
66+
});
67+
68+
const mod = await import("./provider-env-vars.js");
69+
70+
expect(loadPluginManifestRegistry).not.toHaveBeenCalled();
71+
expect(mod.PROVIDER_ENV_VARS.fireworks).toEqual(["FIREWORKS_ALT_API_KEY"]);
72+
expect(mod.PROVIDER_AUTH_ENV_VAR_CANDIDATES.fireworks).toEqual(["FIREWORKS_ALT_API_KEY"]);
73+
const initialLoads = loadPluginManifestRegistry.mock.calls.length;
74+
expect(initialLoads).toBeGreaterThan(0);
75+
76+
void mod.PROVIDER_ENV_VARS.fireworks;
77+
void mod.PROVIDER_AUTH_ENV_VAR_CANDIDATES.fireworks;
78+
expect(loadPluginManifestRegistry).toHaveBeenCalledTimes(initialLoads);
79+
});
80+
5481
it("keeps workspace plugin env vars in default lookups", async () => {
5582
loadPluginManifestRegistry.mockReturnValue({
5683
plugins: [

src/secrets/provider-env-vars.ts

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,16 +163,56 @@ export function resolveProviderEnvVars(
163163
};
164164
}
165165

166+
function createLazyReadonlyRecord(
167+
resolve: () => Record<string, readonly string[]>,
168+
): Record<string, readonly string[]> {
169+
let cached: Record<string, readonly string[]> | undefined;
170+
const getResolved = (): Record<string, readonly string[]> => {
171+
cached ??= resolve();
172+
return cached;
173+
};
174+
175+
return new Proxy({} as Record<string, readonly string[]>, {
176+
get(_target, prop) {
177+
if (typeof prop !== "string") {
178+
return undefined;
179+
}
180+
return getResolved()[prop];
181+
},
182+
has(_target, prop) {
183+
return typeof prop === "string" && Object.hasOwn(getResolved(), prop);
184+
},
185+
ownKeys() {
186+
return Reflect.ownKeys(getResolved());
187+
},
188+
getOwnPropertyDescriptor(_target, prop) {
189+
if (typeof prop !== "string") {
190+
return undefined;
191+
}
192+
const value = getResolved()[prop];
193+
if (value === undefined) {
194+
return undefined;
195+
}
196+
return {
197+
configurable: true,
198+
enumerable: true,
199+
value,
200+
writable: false,
201+
};
202+
},
203+
});
204+
}
205+
166206
/**
167207
* Provider auth env candidates used by generic auth resolution.
168208
*
169209
* Order matters: the first non-empty value wins for helpers such as
170210
* `resolveEnvApiKey()`. Bundled providers source this from plugin manifest
171211
* metadata so auth probes do not need to load plugin runtime.
172212
*/
173-
export const PROVIDER_AUTH_ENV_VAR_CANDIDATES: Record<string, readonly string[]> = {
174-
...resolveProviderAuthEnvVarCandidates(),
175-
};
213+
export const PROVIDER_AUTH_ENV_VAR_CANDIDATES = createLazyReadonlyRecord(() =>
214+
resolveProviderAuthEnvVarCandidates(),
215+
);
176216

177217
/**
178218
* Provider env vars used for setup/default secret refs and broad secret
@@ -183,9 +223,7 @@ export const PROVIDER_AUTH_ENV_VAR_CANDIDATES: Record<string, readonly string[]>
183223
* is only for true core/non-plugin providers and a few setup-specific ordering
184224
* overrides where generic onboarding wants a different preferred env var.
185225
*/
186-
export const PROVIDER_ENV_VARS: Record<string, readonly string[]> = {
187-
...resolveProviderEnvVars(),
188-
};
226+
export const PROVIDER_ENV_VARS = createLazyReadonlyRecord(() => resolveProviderEnvVars());
189227

190228
export function getProviderEnvVars(
191229
providerId: string,

0 commit comments

Comments
 (0)