hanzio 2.0.0 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{index-wjyb9bsd.js → index-hyt6c56h.js} +39 -12
- package/dist/index-ktynw3vs.js +37 -0
- package/dist/{index-181e6vfw.js → index-w882zry3.js} +1 -1
- package/dist/secrets/core.d.ts +7 -0
- package/dist/secrets/index.d.ts +3 -1
- package/dist/secrets/public-config.d.ts +7 -0
- package/dist/secrets/vite.d.ts +1 -0
- package/dist/secrets/worker.d.ts +1 -0
- package/dist/src/secrets/index.js +13 -7
- package/dist/src/secrets/vite.js +5 -1
- package/dist/src/secrets/worker.js +8 -4
- package/package.json +1 -1
- package/src/secrets/core.ts +72 -16
- package/src/secrets/index.ts +16 -4
- package/src/secrets/public-config.ts +47 -0
- package/src/secrets/vite.ts +2 -0
- package/src/secrets/worker.ts +2 -0
- package/dist/index-srt06xd2.js +0 -15
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
SecretLoadError2
|
|
3
|
-
} from "./index-
|
|
3
|
+
} from "./index-ktynw3vs.js";
|
|
4
4
|
|
|
5
5
|
// src/secrets/environment.ts
|
|
6
6
|
function getProcessEnvironment() {
|
|
@@ -92,9 +92,10 @@ function readOverrides(keys, env) {
|
|
|
92
92
|
return value ? [[key, value]] : [];
|
|
93
93
|
}));
|
|
94
94
|
}
|
|
95
|
-
|
|
95
|
+
function createSecretSet2(keys, options, environmentVariables) {
|
|
96
96
|
const keySet = [...new Set(keys)];
|
|
97
|
-
|
|
97
|
+
let environment;
|
|
98
|
+
let initialized = false;
|
|
98
99
|
const logger = options.logger === false ? undefined : options.logger ?? defaultLogger;
|
|
99
100
|
let cached;
|
|
100
101
|
let parsed;
|
|
@@ -105,7 +106,7 @@ async function defineSecretSet2(keys, options, environmentVariables) {
|
|
|
105
106
|
logger?.[level](message, attributes);
|
|
106
107
|
} catch {}
|
|
107
108
|
};
|
|
108
|
-
const loadSnapshot = async (request, signal) => {
|
|
109
|
+
const loadSnapshot = async (request, signal, environment) => {
|
|
109
110
|
const local = readOverrides(keySet, environmentVariables);
|
|
110
111
|
const missing = keySet.filter((key) => !Object.hasOwn(local, key));
|
|
111
112
|
let loaderSource = options.loader.source ?? "custom loader";
|
|
@@ -171,6 +172,7 @@ async function defineSecretSet2(keys, options, environmentVariables) {
|
|
|
171
172
|
cached = next;
|
|
172
173
|
parsed = nextParsed;
|
|
173
174
|
sources = nextSources;
|
|
175
|
+
initialized = true;
|
|
174
176
|
};
|
|
175
177
|
const load = (request = {}) => {
|
|
176
178
|
if (inFlight) {
|
|
@@ -179,10 +181,12 @@ async function defineSecretSet2(keys, options, environmentVariables) {
|
|
|
179
181
|
inFlight = (async () => {
|
|
180
182
|
const start = Date.now();
|
|
181
183
|
try {
|
|
184
|
+
environment ??= resolveSecretEnvironment(options.environment, environmentVariables);
|
|
185
|
+
const resolvedEnvironment = environment;
|
|
182
186
|
await withSecretDeadline({
|
|
183
187
|
timeoutMs: request.timeoutMs ?? options.timeoutMs,
|
|
184
188
|
signal: request.signal ?? options.signal
|
|
185
|
-
}, (signal) => loadSnapshot(request, signal));
|
|
189
|
+
}, (signal) => loadSnapshot(request, signal, resolvedEnvironment));
|
|
186
190
|
const sourceCounts = new Map;
|
|
187
191
|
for (const key of keySet) {
|
|
188
192
|
const source = sources[key];
|
|
@@ -190,7 +194,7 @@ async function defineSecretSet2(keys, options, environmentVariables) {
|
|
|
190
194
|
}
|
|
191
195
|
const counts = Object.fromEntries(sourceCounts);
|
|
192
196
|
log("info", "Loaded secrets", {
|
|
193
|
-
environment,
|
|
197
|
+
environment: resolvedEnvironment,
|
|
194
198
|
count: keySet.length,
|
|
195
199
|
durationMs: Date.now() - start,
|
|
196
200
|
source: Object.keys(counts).join(" + ") || "none",
|
|
@@ -199,7 +203,7 @@ async function defineSecretSet2(keys, options, environmentVariables) {
|
|
|
199
203
|
} catch (error) {
|
|
200
204
|
const safe = error instanceof SecretLoadError2 ? error : new SecretLoadError2("LOADER", "Secret loading failed.");
|
|
201
205
|
log("error", "Secret loading failed", {
|
|
202
|
-
environment,
|
|
206
|
+
environment: environment ?? "unresolved",
|
|
203
207
|
count: keySet.length,
|
|
204
208
|
durationMs: Date.now() - start,
|
|
205
209
|
code: safe.code
|
|
@@ -211,24 +215,47 @@ async function defineSecretSet2(keys, options, environmentVariables) {
|
|
|
211
215
|
});
|
|
212
216
|
return inFlight;
|
|
213
217
|
};
|
|
218
|
+
const assertLoaded = () => {
|
|
219
|
+
if (!initialized) {
|
|
220
|
+
throw new SecretLoadError2("CONFIGURATION", "Secret set has not been loaded. Await load() before reading values.");
|
|
221
|
+
}
|
|
222
|
+
};
|
|
214
223
|
const secretSet = {
|
|
215
224
|
secret(key) {
|
|
225
|
+
assertLoaded();
|
|
216
226
|
if (!Object.hasOwn(cached, key)) {
|
|
217
227
|
throw new SecretLoadError2("MISSING_SECRETS", `Secret ${key} not found`);
|
|
218
228
|
}
|
|
219
229
|
return cached[key];
|
|
220
230
|
},
|
|
221
|
-
secrets
|
|
222
|
-
|
|
223
|
-
|
|
231
|
+
secrets() {
|
|
232
|
+
assertLoaded();
|
|
233
|
+
return { ...cached };
|
|
234
|
+
},
|
|
235
|
+
parsed() {
|
|
236
|
+
assertLoaded();
|
|
237
|
+
return structuredClone(parsed);
|
|
238
|
+
},
|
|
239
|
+
sources() {
|
|
240
|
+
assertLoaded();
|
|
241
|
+
return { ...sources };
|
|
242
|
+
},
|
|
243
|
+
async load(request) {
|
|
244
|
+
if (!initialized) {
|
|
245
|
+
await load(request);
|
|
246
|
+
}
|
|
247
|
+
return secretSet;
|
|
248
|
+
},
|
|
224
249
|
async reload(request) {
|
|
225
250
|
await load(request);
|
|
226
251
|
return secretSet;
|
|
227
252
|
}
|
|
228
253
|
};
|
|
229
|
-
await load();
|
|
230
254
|
return secretSet;
|
|
231
255
|
}
|
|
256
|
+
async function defineSecretSet2(keys, options, environmentVariables) {
|
|
257
|
+
return createSecretSet2(keys, options, environmentVariables).load();
|
|
258
|
+
}
|
|
232
259
|
|
|
233
260
|
// src/secrets/worker-loader.ts
|
|
234
261
|
function cloudflareWorkerEnvLoader2(workerEnv) {
|
|
@@ -260,4 +287,4 @@ function workerBindingToString(value) {
|
|
|
260
287
|
return String(value);
|
|
261
288
|
}
|
|
262
289
|
|
|
263
|
-
export { withSecretDeadline, getProcessEnvironment, resolveSecretEnvironment, defineSecretSet2, cloudflareWorkerEnvLoader2 };
|
|
290
|
+
export { withSecretDeadline, getProcessEnvironment, resolveSecretEnvironment, createSecretSet2, defineSecretSet2, cloudflareWorkerEnvLoader2 };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// src/secrets/errors.ts
|
|
2
|
+
class SecretLoadError2 extends Error {
|
|
3
|
+
code;
|
|
4
|
+
operation;
|
|
5
|
+
status;
|
|
6
|
+
constructor(code, message, details = {}) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.name = "SecretLoadError";
|
|
9
|
+
this.code = code;
|
|
10
|
+
this.operation = details.operation;
|
|
11
|
+
this.status = details.status;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// src/secrets/public-config.ts
|
|
16
|
+
function readPublicConfig2(keys, values) {
|
|
17
|
+
const snapshot = Object.fromEntries(keys.map((key) => [
|
|
18
|
+
key,
|
|
19
|
+
Object.hasOwn(values, key) ? values[key] : undefined
|
|
20
|
+
]));
|
|
21
|
+
const missing = keys.filter((key) => typeof snapshot[key] !== "string" || !snapshot[key]);
|
|
22
|
+
if (missing.length) {
|
|
23
|
+
throw new SecretLoadError2("MISSING_SECRETS", `Missing required public configuration: ${missing.join(", ")}`);
|
|
24
|
+
}
|
|
25
|
+
const configured = snapshot;
|
|
26
|
+
return {
|
|
27
|
+
secret(key) {
|
|
28
|
+
if (!Object.hasOwn(configured, key)) {
|
|
29
|
+
throw new SecretLoadError2("CONFIGURATION", `Public key ${key} is not configured.`);
|
|
30
|
+
}
|
|
31
|
+
return configured[key];
|
|
32
|
+
},
|
|
33
|
+
secrets: () => ({ ...configured })
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export { SecretLoadError2, readPublicConfig2 };
|
package/dist/secrets/core.d.ts
CHANGED
|
@@ -44,4 +44,11 @@ export type SecretSet<K extends string, Parsed = Record<K, string>> = {
|
|
|
44
44
|
sources: () => Record<K, string>;
|
|
45
45
|
reload: (options?: SecretRequestOptions) => Promise<SecretSet<K, Parsed>>;
|
|
46
46
|
};
|
|
47
|
+
/** A declaration that must be loaded explicitly before its values can be read. */
|
|
48
|
+
export type LazySecretSet<K extends string, Parsed = Record<K, string>> = SecretSet<K, Parsed> & {
|
|
49
|
+
/** Initialize once. Concurrent calls share a load; use reload to refresh. */
|
|
50
|
+
load: (options?: SecretRequestOptions) => Promise<LazySecretSet<K, Parsed>>;
|
|
51
|
+
};
|
|
52
|
+
export declare function createSecretSet<const Keys extends readonly string[], Parsed = Record<Keys[number], string>>(keys: Keys, options: CustomLoaderDefineSecretSetOptions<Keys[number], Parsed>, environmentVariables?: EnvironmentVariables): LazySecretSet<Keys[number], Parsed>;
|
|
53
|
+
/** Compatibility API: declares and loads a complete snapshot before returning. */
|
|
47
54
|
export declare function defineSecretSet<const Keys extends readonly string[], Parsed = Record<Keys[number], string>>(keys: Keys, options: CustomLoaderDefineSecretSetOptions<Keys[number], Parsed>, environmentVariables?: EnvironmentVariables): Promise<SecretSet<Keys[number], Parsed>>;
|
package/dist/secrets/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type CommonSecretSetOptions, type CustomLoaderDefineSecretSetOptions, type SecretSet } from './core';
|
|
1
|
+
import { type LazySecretSet, type CommonSecretSetOptions, type CustomLoaderDefineSecretSetOptions, type SecretSet } from './core';
|
|
2
2
|
import { type SecretEnvironment } from './environment';
|
|
3
3
|
import { type InfisicalAuthMode } from './infisical';
|
|
4
4
|
import { type InfisicalProjectOptions } from './infisical-config';
|
|
@@ -15,4 +15,6 @@ export type InfisicalDefineSecretSetOptions<K extends string = string, Parsed =
|
|
|
15
15
|
};
|
|
16
16
|
export type DefineSecretSetOptions<K extends string, Parsed = Record<K, string>> = InfisicalDefineSecretSetOptions<K, Parsed> | CustomLoaderDefineSecretSetOptions<K, Parsed>;
|
|
17
17
|
export declare function getSecretEnvironment(): SecretEnvironment;
|
|
18
|
+
export declare function createSecretSet<const Keys extends readonly string[], Parsed = Record<Keys[number], string>>(keys: Keys, options: DefineSecretSetOptions<Keys[number], Parsed>): LazySecretSet<Keys[number], Parsed>;
|
|
19
|
+
/** Compatibility API: load immediately and fail before application startup. */
|
|
18
20
|
export declare function defineSecretSet<const Keys extends readonly string[], Parsed = Record<Keys[number], string>>(keys: Keys, options: DefineSecretSetOptions<Keys[number], Parsed>): Promise<SecretSet<Keys[number], Parsed>>;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { SecretSet } from './core';
|
|
2
|
+
/**
|
|
3
|
+
* Read only explicitly listed public values. No process globals, secret loader,
|
|
4
|
+
* environment writes, or async initialization; safe in frontend tooling.
|
|
5
|
+
* Validate application-specific formats with your schema after secrets().
|
|
6
|
+
*/
|
|
7
|
+
export declare function readPublicConfig<const Keys extends readonly string[]>(keys: Keys, values: Readonly<Record<string, string | undefined>>): Pick<SecretSet<Keys[number]>, 'secret' | 'secrets'>;
|
package/dist/secrets/vite.d.ts
CHANGED
|
@@ -11,3 +11,4 @@ export type ViteSecretSetOptions<K extends string> = {
|
|
|
11
11
|
};
|
|
12
12
|
export declare function getViteDefine<K extends string>(secretSet: Pick<SecretSet<K, unknown>, 'secrets'>, options?: ViteSecretSetOptions<K>): Record<string, string>;
|
|
13
13
|
export declare function viteSecretSetPlugin<K extends string>(secretSet: Pick<SecretSet<K, unknown>, 'secrets'>, options?: ViteSecretSetOptions<K>): ViteSecretSetPlugin;
|
|
14
|
+
export { readPublicConfig } from './public-config';
|
package/dist/secrets/worker.d.ts
CHANGED
|
@@ -5,17 +5,18 @@ import {
|
|
|
5
5
|
import {
|
|
6
6
|
getViteDefine2,
|
|
7
7
|
viteSecretSetPlugin2
|
|
8
|
-
} from "../../index-
|
|
8
|
+
} from "../../index-w882zry3.js";
|
|
9
9
|
import {
|
|
10
10
|
withSecretDeadline,
|
|
11
11
|
getProcessEnvironment,
|
|
12
12
|
resolveSecretEnvironment,
|
|
13
|
-
|
|
13
|
+
createSecretSet2,
|
|
14
14
|
cloudflareWorkerEnvLoader2
|
|
15
|
-
} from "../../index-
|
|
15
|
+
} from "../../index-hyt6c56h.js";
|
|
16
16
|
import {
|
|
17
|
-
SecretLoadError2
|
|
18
|
-
|
|
17
|
+
SecretLoadError2,
|
|
18
|
+
readPublicConfig2
|
|
19
|
+
} from "../../index-ktynw3vs.js";
|
|
19
20
|
|
|
20
21
|
// src/secrets/infisical.ts
|
|
21
22
|
import { z as z2 } from "zod";
|
|
@@ -328,7 +329,7 @@ Object.assign(processEnvLoader, { source: "process.env" });
|
|
|
328
329
|
function getSecretEnvironment() {
|
|
329
330
|
return resolveSecretEnvironment(undefined, getProcessEnvironment());
|
|
330
331
|
}
|
|
331
|
-
|
|
332
|
+
function createSecretSet(keys, options) {
|
|
332
333
|
let loader;
|
|
333
334
|
if (!options.loader) {
|
|
334
335
|
validateInfisicalOrganizationId(options.organizationId);
|
|
@@ -336,7 +337,7 @@ async function defineSecretSet(keys, options) {
|
|
|
336
337
|
} else {
|
|
337
338
|
loader = options.loader;
|
|
338
339
|
}
|
|
339
|
-
return
|
|
340
|
+
return createSecretSet2(keys, { ...options, loader }, getProcessEnvironment());
|
|
340
341
|
}
|
|
341
342
|
function createDefaultInfisicalLoader(options) {
|
|
342
343
|
return (context) => loadInfisicalSecrets({
|
|
@@ -350,10 +351,14 @@ function createDefaultInfisicalLoader(options) {
|
|
|
350
351
|
clientSecretEnvKey: options.clientSecretEnvKey ?? "INFISICAL_CLIENT_SECRET"
|
|
351
352
|
});
|
|
352
353
|
}
|
|
354
|
+
async function defineSecretSet(keys, options) {
|
|
355
|
+
return createSecretSet(keys, options).load();
|
|
356
|
+
}
|
|
353
357
|
export {
|
|
354
358
|
DEFAULT_INFISICAL_SITE_URL,
|
|
355
359
|
SecretLoadError2 as SecretLoadError,
|
|
356
360
|
cloudflareWorkerEnvLoader2 as cloudflareWorkerEnvLoader,
|
|
361
|
+
createSecretSet,
|
|
357
362
|
defineSecretSet,
|
|
358
363
|
fetchInfisicalSecrets,
|
|
359
364
|
getSecretEnvironment,
|
|
@@ -361,5 +366,6 @@ export {
|
|
|
361
366
|
infisicalLoader,
|
|
362
367
|
loadInfisicalSecrets,
|
|
363
368
|
processEnvLoader,
|
|
369
|
+
readPublicConfig2 as readPublicConfig,
|
|
364
370
|
viteSecretSetPlugin2 as viteSecretSetPlugin
|
|
365
371
|
};
|
package/dist/src/secrets/vite.js
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
import {
|
|
2
2
|
getViteDefine2,
|
|
3
3
|
viteSecretSetPlugin2
|
|
4
|
-
} from "../../index-
|
|
4
|
+
} from "../../index-w882zry3.js";
|
|
5
|
+
import {
|
|
6
|
+
readPublicConfig2
|
|
7
|
+
} from "../../index-ktynw3vs.js";
|
|
5
8
|
export {
|
|
6
9
|
getViteDefine2 as getViteDefine,
|
|
10
|
+
readPublicConfig2 as readPublicConfig,
|
|
7
11
|
viteSecretSetPlugin2 as viteSecretSetPlugin
|
|
8
12
|
};
|
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
import {
|
|
2
|
+
createSecretSet2,
|
|
2
3
|
defineSecretSet2,
|
|
3
4
|
cloudflareWorkerEnvLoader2
|
|
4
|
-
} from "../../index-
|
|
5
|
+
} from "../../index-hyt6c56h.js";
|
|
5
6
|
import {
|
|
6
|
-
SecretLoadError2
|
|
7
|
-
|
|
7
|
+
SecretLoadError2,
|
|
8
|
+
readPublicConfig2
|
|
9
|
+
} from "../../index-ktynw3vs.js";
|
|
8
10
|
export {
|
|
9
11
|
SecretLoadError2 as SecretLoadError,
|
|
10
12
|
cloudflareWorkerEnvLoader2 as cloudflareWorkerEnvLoader,
|
|
11
|
-
|
|
13
|
+
createSecretSet2 as createSecretSet,
|
|
14
|
+
defineSecretSet2 as defineSecretSet,
|
|
15
|
+
readPublicConfig2 as readPublicConfig
|
|
12
16
|
};
|
package/package.json
CHANGED
package/src/secrets/core.ts
CHANGED
|
@@ -68,6 +68,15 @@ export type SecretSet<K extends string, Parsed = Record<K, string>> = {
|
|
|
68
68
|
reload: (options?: SecretRequestOptions) => Promise<SecretSet<K, Parsed>>
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
+
/** A declaration that must be loaded explicitly before its values can be read. */
|
|
72
|
+
export type LazySecretSet<
|
|
73
|
+
K extends string,
|
|
74
|
+
Parsed = Record<K, string>
|
|
75
|
+
> = SecretSet<K, Parsed> & {
|
|
76
|
+
/** Initialize once. Concurrent calls share a load; use reload to refresh. */
|
|
77
|
+
load: (options?: SecretRequestOptions) => Promise<LazySecretSet<K, Parsed>>
|
|
78
|
+
}
|
|
79
|
+
|
|
71
80
|
// Shared across bundled entry points and package copies in the same JS realm.
|
|
72
81
|
// Only helper-written values are registered; original environment overrides aren't.
|
|
73
82
|
const registryKey = Symbol.for('hanzio.secrets.environmentWrites.v1')
|
|
@@ -118,21 +127,19 @@ function readOverrides<K extends string>(
|
|
|
118
127
|
) as Partial<Record<K, string>>
|
|
119
128
|
}
|
|
120
129
|
|
|
121
|
-
export
|
|
130
|
+
export function createSecretSet<
|
|
122
131
|
const Keys extends readonly string[],
|
|
123
132
|
Parsed = Record<Keys[number], string>
|
|
124
133
|
>(
|
|
125
134
|
keys: Keys,
|
|
126
135
|
options: CustomLoaderDefineSecretSetOptions<Keys[number], Parsed>,
|
|
127
136
|
environmentVariables?: EnvironmentVariables
|
|
128
|
-
):
|
|
137
|
+
): LazySecretSet<Keys[number], Parsed> {
|
|
129
138
|
type K = Keys[number]
|
|
130
139
|
|
|
131
140
|
const keySet = [...new Set(keys)] as K[]
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
environmentVariables
|
|
135
|
-
)
|
|
141
|
+
let environment: string | undefined
|
|
142
|
+
let initialized = false
|
|
136
143
|
const logger =
|
|
137
144
|
options.logger === false ? undefined : (options.logger ?? defaultLogger)
|
|
138
145
|
|
|
@@ -157,7 +164,8 @@ export async function defineSecretSet<
|
|
|
157
164
|
// Build, validate, and commit one snapshot under the caller’s deadline.
|
|
158
165
|
const loadSnapshot = async (
|
|
159
166
|
request: SecretRequestOptions,
|
|
160
|
-
signal: AbortSignal
|
|
167
|
+
signal: AbortSignal,
|
|
168
|
+
environment: string
|
|
161
169
|
) => {
|
|
162
170
|
// Fetch only keys that lack genuine local overrides.
|
|
163
171
|
const local = readOverrides(keySet, environmentVariables)
|
|
@@ -261,6 +269,7 @@ export async function defineSecretSet<
|
|
|
261
269
|
cached = next
|
|
262
270
|
parsed = nextParsed
|
|
263
271
|
sources = nextSources
|
|
272
|
+
initialized = true
|
|
264
273
|
}
|
|
265
274
|
|
|
266
275
|
const load = (request: SecretRequestOptions = {}): Promise<void> => {
|
|
@@ -273,12 +282,18 @@ export async function defineSecretSet<
|
|
|
273
282
|
const start = Date.now()
|
|
274
283
|
|
|
275
284
|
try {
|
|
285
|
+
environment ??= resolveSecretEnvironment(
|
|
286
|
+
options.environment,
|
|
287
|
+
environmentVariables
|
|
288
|
+
)
|
|
289
|
+
const resolvedEnvironment = environment
|
|
290
|
+
|
|
276
291
|
await withSecretDeadline(
|
|
277
292
|
{
|
|
278
293
|
timeoutMs: request.timeoutMs ?? options.timeoutMs,
|
|
279
294
|
signal: request.signal ?? options.signal
|
|
280
295
|
},
|
|
281
|
-
(signal) => loadSnapshot(request, signal)
|
|
296
|
+
(signal) => loadSnapshot(request, signal, resolvedEnvironment)
|
|
282
297
|
)
|
|
283
298
|
|
|
284
299
|
// Report the sources of committed values, not attempted fetches.
|
|
@@ -293,7 +308,7 @@ export async function defineSecretSet<
|
|
|
293
308
|
const counts = Object.fromEntries(sourceCounts)
|
|
294
309
|
|
|
295
310
|
log('info', 'Loaded secrets', {
|
|
296
|
-
environment,
|
|
311
|
+
environment: resolvedEnvironment,
|
|
297
312
|
count: keySet.length,
|
|
298
313
|
durationMs: Date.now() - start,
|
|
299
314
|
source: Object.keys(counts).join(' + ') || 'none',
|
|
@@ -306,7 +321,7 @@ export async function defineSecretSet<
|
|
|
306
321
|
: new SecretLoadError('LOADER', 'Secret loading failed.')
|
|
307
322
|
|
|
308
323
|
log('error', 'Secret loading failed', {
|
|
309
|
-
environment,
|
|
324
|
+
environment: environment ?? 'unresolved',
|
|
310
325
|
count: keySet.length,
|
|
311
326
|
durationMs: Date.now() - start,
|
|
312
327
|
code: safe.code
|
|
@@ -321,8 +336,19 @@ export async function defineSecretSet<
|
|
|
321
336
|
return inFlight
|
|
322
337
|
}
|
|
323
338
|
|
|
324
|
-
const
|
|
339
|
+
const assertLoaded = () => {
|
|
340
|
+
if (!initialized) {
|
|
341
|
+
throw new SecretLoadError(
|
|
342
|
+
'CONFIGURATION',
|
|
343
|
+
'Secret set has not been loaded. Await load() before reading values.'
|
|
344
|
+
)
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
const secretSet: LazySecretSet<K, Parsed> = {
|
|
325
349
|
secret(key) {
|
|
350
|
+
assertLoaded()
|
|
351
|
+
|
|
326
352
|
if (!Object.hasOwn(cached, key)) {
|
|
327
353
|
throw new SecretLoadError('MISSING_SECRETS', `Secret ${key} not found`)
|
|
328
354
|
}
|
|
@@ -331,9 +357,29 @@ export async function defineSecretSet<
|
|
|
331
357
|
},
|
|
332
358
|
|
|
333
359
|
// Return copies so consumers cannot mutate the internal snapshots.
|
|
334
|
-
secrets
|
|
335
|
-
|
|
336
|
-
|
|
360
|
+
secrets() {
|
|
361
|
+
assertLoaded()
|
|
362
|
+
|
|
363
|
+
return { ...cached }
|
|
364
|
+
},
|
|
365
|
+
parsed() {
|
|
366
|
+
assertLoaded()
|
|
367
|
+
|
|
368
|
+
return structuredClone(parsed)
|
|
369
|
+
},
|
|
370
|
+
sources() {
|
|
371
|
+
assertLoaded()
|
|
372
|
+
|
|
373
|
+
return { ...sources }
|
|
374
|
+
},
|
|
375
|
+
|
|
376
|
+
async load(request) {
|
|
377
|
+
if (!initialized) {
|
|
378
|
+
await load(request)
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
return secretSet
|
|
382
|
+
},
|
|
337
383
|
|
|
338
384
|
async reload(request) {
|
|
339
385
|
await load(request)
|
|
@@ -342,7 +388,17 @@ export async function defineSecretSet<
|
|
|
342
388
|
}
|
|
343
389
|
}
|
|
344
390
|
|
|
345
|
-
await load()
|
|
346
|
-
|
|
347
391
|
return secretSet
|
|
348
392
|
}
|
|
393
|
+
|
|
394
|
+
/** Compatibility API: declares and loads a complete snapshot before returning. */
|
|
395
|
+
export async function defineSecretSet<
|
|
396
|
+
const Keys extends readonly string[],
|
|
397
|
+
Parsed = Record<Keys[number], string>
|
|
398
|
+
>(
|
|
399
|
+
keys: Keys,
|
|
400
|
+
options: CustomLoaderDefineSecretSetOptions<Keys[number], Parsed>,
|
|
401
|
+
environmentVariables?: EnvironmentVariables
|
|
402
|
+
): Promise<SecretSet<Keys[number], Parsed>> {
|
|
403
|
+
return createSecretSet(keys, options, environmentVariables).load()
|
|
404
|
+
}
|
package/src/secrets/index.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
2
|
+
createSecretSet as createCoreSecretSet,
|
|
3
|
+
type LazySecretSet,
|
|
3
4
|
type CommonSecretSetOptions,
|
|
4
5
|
type CustomLoaderDefineSecretSetOptions,
|
|
5
6
|
type SecretSet,
|
|
@@ -51,13 +52,13 @@ export function getSecretEnvironment(): SecretEnvironment {
|
|
|
51
52
|
) as SecretEnvironment
|
|
52
53
|
}
|
|
53
54
|
|
|
54
|
-
export
|
|
55
|
+
export function createSecretSet<
|
|
55
56
|
const Keys extends readonly string[],
|
|
56
57
|
Parsed = Record<Keys[number], string>
|
|
57
58
|
>(
|
|
58
59
|
keys: Keys,
|
|
59
60
|
options: DefineSecretSetOptions<Keys[number], Parsed>
|
|
60
|
-
):
|
|
61
|
+
): LazySecretSet<Keys[number], Parsed> {
|
|
61
62
|
let loader: SecretSetLoader<Keys[number]>
|
|
62
63
|
|
|
63
64
|
if (!options.loader) {
|
|
@@ -69,7 +70,7 @@ export async function defineSecretSet<
|
|
|
69
70
|
}
|
|
70
71
|
|
|
71
72
|
// Supply process.env only from this Node/Bun entry point, keeping the core portable.
|
|
72
|
-
return
|
|
73
|
+
return createCoreSecretSet(
|
|
73
74
|
keys,
|
|
74
75
|
{ ...options, loader },
|
|
75
76
|
getProcessEnvironment()
|
|
@@ -92,3 +93,14 @@ function createDefaultInfisicalLoader<K extends string, Parsed>(
|
|
|
92
93
|
options.clientSecretEnvKey ?? 'INFISICAL_CLIENT_SECRET'
|
|
93
94
|
})
|
|
94
95
|
}
|
|
96
|
+
|
|
97
|
+
/** Compatibility API: load immediately and fail before application startup. */
|
|
98
|
+
export async function defineSecretSet<
|
|
99
|
+
const Keys extends readonly string[],
|
|
100
|
+
Parsed = Record<Keys[number], string>
|
|
101
|
+
>(
|
|
102
|
+
keys: Keys,
|
|
103
|
+
options: DefineSecretSetOptions<Keys[number], Parsed>
|
|
104
|
+
): Promise<SecretSet<Keys[number], Parsed>> {
|
|
105
|
+
return createSecretSet(keys, options).load()
|
|
106
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { SecretSet } from './core'
|
|
2
|
+
import { SecretLoadError } from './errors'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Read only explicitly listed public values. No process globals, secret loader,
|
|
6
|
+
* environment writes, or async initialization; safe in frontend tooling.
|
|
7
|
+
* Validate application-specific formats with your schema after secrets().
|
|
8
|
+
*/
|
|
9
|
+
export function readPublicConfig<const Keys extends readonly string[]>(
|
|
10
|
+
keys: Keys,
|
|
11
|
+
values: Readonly<Record<string, string | undefined>>
|
|
12
|
+
): Pick<SecretSet<Keys[number]>, 'secret' | 'secrets'> {
|
|
13
|
+
type K = Keys[number]
|
|
14
|
+
|
|
15
|
+
const snapshot = Object.fromEntries(
|
|
16
|
+
keys.map((key) => [
|
|
17
|
+
key,
|
|
18
|
+
Object.hasOwn(values, key) ? values[key] : undefined
|
|
19
|
+
])
|
|
20
|
+
)
|
|
21
|
+
const missing = keys.filter(
|
|
22
|
+
(key) => typeof snapshot[key] !== 'string' || !snapshot[key]
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
if (missing.length) {
|
|
26
|
+
throw new SecretLoadError(
|
|
27
|
+
'MISSING_SECRETS',
|
|
28
|
+
`Missing required public configuration: ${missing.join(', ')}`
|
|
29
|
+
)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const configured = snapshot as Record<K, string>
|
|
33
|
+
|
|
34
|
+
return {
|
|
35
|
+
secret(key) {
|
|
36
|
+
if (!Object.hasOwn(configured, key)) {
|
|
37
|
+
throw new SecretLoadError(
|
|
38
|
+
'CONFIGURATION',
|
|
39
|
+
`Public key ${key} is not configured.`
|
|
40
|
+
)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return configured[key]
|
|
44
|
+
},
|
|
45
|
+
secrets: () => ({ ...configured })
|
|
46
|
+
}
|
|
47
|
+
}
|
package/src/secrets/vite.ts
CHANGED
package/src/secrets/worker.ts
CHANGED
package/dist/index-srt06xd2.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
// src/secrets/errors.ts
|
|
2
|
-
class SecretLoadError2 extends Error {
|
|
3
|
-
code;
|
|
4
|
-
operation;
|
|
5
|
-
status;
|
|
6
|
-
constructor(code, message, details = {}) {
|
|
7
|
-
super(message);
|
|
8
|
-
this.name = "SecretLoadError";
|
|
9
|
-
this.code = code;
|
|
10
|
-
this.operation = details.operation;
|
|
11
|
-
this.status = details.status;
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export { SecretLoadError2 };
|