@sebspark/environment 0.1.0 → 0.1.1
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.d.mts +5 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +16 -4
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -4,7 +4,11 @@ declare const initEnvironment: <T extends Record<string, string>>() => T & {
|
|
|
4
4
|
} & {
|
|
5
5
|
[key: string]: string;
|
|
6
6
|
};
|
|
7
|
-
|
|
7
|
+
type SecretStoreOptions<T> = {
|
|
8
|
+
dir?: string;
|
|
9
|
+
fallback?: boolean | Partial<T>;
|
|
10
|
+
};
|
|
11
|
+
declare const initSecretStore: <T extends Record<string, string>>(opts?: SecretStoreOptions<T>) => T & {
|
|
8
12
|
[key: string]: string;
|
|
9
13
|
};
|
|
10
14
|
//#endregion
|
package/dist/index.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/index.ts"],"mappings":";
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/index.ts"],"mappings":";cAIa,eAAA,aAA6B,MAAA,uBAiCnC,CAAA;EAAM,QAAA,EAAU,OAAA,CAAQ,CAAA;AAAA;EAAA,CAAU,GAAA;AAAA;AAAA,KAGpC,kBAAA;EACH,GAAA;EACA,QAAA,aAAqB,OAAA,CAAQ,CAAA;AAAA;AAAA,cAGlB,eAAA,aAA6B,MAAA,kBACxC,IAAA,GAAO,kBAAA,CAAmB,CAAA,MAwCrB,CAAA;EAAA,CAAO,GAAA;AAAA"}
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import assert from "node:assert";
|
|
2
2
|
import { readFileSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
3
4
|
//#region src/index.ts
|
|
4
5
|
const initEnvironment = () => {
|
|
5
6
|
const environmentCache = {};
|
|
@@ -19,17 +20,28 @@ const initEnvironment = () => {
|
|
|
19
20
|
return environmentCache[key];
|
|
20
21
|
} });
|
|
21
22
|
};
|
|
22
|
-
const initSecretStore = () => {
|
|
23
|
+
const initSecretStore = (opts) => {
|
|
23
24
|
const secretCache = {};
|
|
25
|
+
const { dir, fallback } = opts ?? {};
|
|
24
26
|
return new Proxy({}, { get(_, k) {
|
|
25
27
|
const key = k;
|
|
26
28
|
if (key in secretCache) return secretCache[key];
|
|
29
|
+
const filePath = dir ? join(dir, key) : key;
|
|
27
30
|
try {
|
|
28
|
-
secretCache[key] = readFileSync(
|
|
31
|
+
secretCache[key] = readFileSync(filePath, "utf-8").trim();
|
|
32
|
+
return secretCache[key];
|
|
33
|
+
} catch {}
|
|
34
|
+
if (fallback === true) {
|
|
35
|
+
const { [key]: value } = process.env;
|
|
36
|
+
if (value !== void 0) {
|
|
37
|
+
secretCache[key] = value;
|
|
38
|
+
return secretCache[key];
|
|
39
|
+
}
|
|
40
|
+
} else if (fallback && key in fallback) {
|
|
41
|
+
secretCache[key] = fallback[key];
|
|
29
42
|
return secretCache[key];
|
|
30
|
-
} catch (err) {
|
|
31
|
-
throw new Error(`Secret ${key} is required`);
|
|
32
43
|
}
|
|
44
|
+
throw new Error(`Secret ${key} is required`);
|
|
33
45
|
} });
|
|
34
46
|
};
|
|
35
47
|
//#endregion
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["import assert from 'node:assert'\nimport { readFileSync } from 'node:fs'\n\nexport const initEnvironment = <T extends Record<string, string>>() => {\n const environmentCache: Record<string, string | undefined> = {}\n\n return new Proxy(\n {},\n {\n get(_, k: keyof T | 'optional' | string | symbol) {\n const key = k as string\n if (key === 'optional') {\n return new Proxy(\n {},\n {\n get(_, k: keyof T | string | symbol) {\n const key = k as string\n if (key in environmentCache) {\n return environmentCache[key]\n }\n const { [key]: value } = process.env\n environmentCache[key] = value\n return environmentCache[key]\n },\n }\n ) as T\n }\n if (key in environmentCache) {\n return environmentCache[key]\n }\n const { [key]: value } = process.env\n assert(value, `${key} is required`)\n environmentCache[key] = value\n return environmentCache[key]\n },\n }\n ) as T & { optional: Partial<T> } & { [key: string]: string }\n}\n\nexport const initSecretStore = <T extends Record<string, string>>() => {\n const secretCache: Record<string, string | undefined> = {}\n\n return new Proxy(\n {},\n {\n get(_, k: keyof T | string | symbol) {\n const key = k as string\n if (key in secretCache) {\n return secretCache[key]\n }\n try {\n const value = readFileSync(
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["import assert from 'node:assert'\nimport { readFileSync } from 'node:fs'\nimport { join } from 'node:path'\n\nexport const initEnvironment = <T extends Record<string, string>>() => {\n const environmentCache: Record<string, string | undefined> = {}\n\n return new Proxy(\n {},\n {\n get(_, k: keyof T | 'optional' | string | symbol) {\n const key = k as string\n if (key === 'optional') {\n return new Proxy(\n {},\n {\n get(_, k: keyof T | string | symbol) {\n const key = k as string\n if (key in environmentCache) {\n return environmentCache[key]\n }\n const { [key]: value } = process.env\n environmentCache[key] = value\n return environmentCache[key]\n },\n }\n ) as T\n }\n if (key in environmentCache) {\n return environmentCache[key]\n }\n const { [key]: value } = process.env\n assert(value, `${key} is required`)\n environmentCache[key] = value\n return environmentCache[key]\n },\n }\n ) as T & { optional: Partial<T> } & { [key: string]: string }\n}\n\ntype SecretStoreOptions<T> = {\n dir?: string\n fallback?: boolean | Partial<T>\n}\n\nexport const initSecretStore = <T extends Record<string, string>>(\n opts?: SecretStoreOptions<T>\n) => {\n const secretCache: Record<string, string | undefined> = {}\n const { dir, fallback } = opts ?? {}\n\n return new Proxy(\n {},\n {\n get(_, k: keyof T | string | symbol) {\n const key = k as string\n if (key in secretCache) {\n return secretCache[key]\n }\n\n // 1. Try reading from file (dir/KEY or just KEY if no dir)\n const filePath = dir ? join(dir, key) : key\n try {\n const value = readFileSync(filePath, 'utf-8').trim()\n secretCache[key] = value\n return secretCache[key]\n } catch {\n // file not found — continue to fallback\n }\n\n // 2. Try fallback\n if (fallback === true) {\n // auto-read from process.env using the same key name\n const { [key]: value } = process.env\n if (value !== undefined) {\n secretCache[key] = value\n return secretCache[key]\n }\n } else if (fallback && key in fallback) {\n secretCache[key] = fallback[key as keyof T] as string\n return secretCache[key]\n }\n\n throw new Error(`Secret ${key} is required`)\n },\n }\n ) as T & { [key: string]: string }\n}\n"],"mappings":";;;;AAIA,MAAa,wBAA0D;CACrE,MAAM,mBAAuD,EAAE;CAE/D,OAAO,IAAI,MACT,EAAE,EACF,EACE,IAAI,GAAG,GAA2C;EAChD,MAAM,MAAM;EACZ,IAAI,QAAQ,YACV,OAAO,IAAI,MACT,EAAE,EACF,EACE,IAAI,GAAG,GAA8B;GACnC,MAAM,MAAM;GACZ,IAAI,OAAO,kBACT,OAAO,iBAAiB;GAE1B,MAAM,GAAG,MAAM,UAAU,QAAQ;GACjC,iBAAiB,OAAO;GACxB,OAAO,iBAAiB;KAE3B,CACF;EAEH,IAAI,OAAO,kBACT,OAAO,iBAAiB;EAE1B,MAAM,GAAG,MAAM,UAAU,QAAQ;EACjC,OAAO,OAAO,GAAG,IAAI,cAAc;EACnC,iBAAiB,OAAO;EACxB,OAAO,iBAAiB;IAE3B,CACF;;AAQH,MAAa,mBACX,SACG;CACH,MAAM,cAAkD,EAAE;CAC1D,MAAM,EAAE,KAAK,aAAa,QAAQ,EAAE;CAEpC,OAAO,IAAI,MACT,EAAE,EACF,EACE,IAAI,GAAG,GAA8B;EACnC,MAAM,MAAM;EACZ,IAAI,OAAO,aACT,OAAO,YAAY;EAIrB,MAAM,WAAW,MAAM,KAAK,KAAK,IAAI,GAAG;EACxC,IAAI;GAEF,YAAY,OADE,aAAa,UAAU,QAAQ,CAAC,MACtB;GACxB,OAAO,YAAY;UACb;EAKR,IAAI,aAAa,MAAM;GAErB,MAAM,GAAG,MAAM,UAAU,QAAQ;GACjC,IAAI,UAAU,KAAA,GAAW;IACvB,YAAY,OAAO;IACnB,OAAO,YAAY;;SAEhB,IAAI,YAAY,OAAO,UAAU;GACtC,YAAY,OAAO,SAAS;GAC5B,OAAO,YAAY;;EAGrB,MAAM,IAAI,MAAM,UAAU,IAAI,cAAc;IAE/C,CACF"}
|