@sebspark/environment 0.1.1 → 0.1.3
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/CHANGELOG.md +46 -0
- package/README.md +69 -1
- package/dist/index.d.mts +3 -3
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +11 -2
- package/dist/index.mjs.map +1 -1
- package/package.json +11 -3
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# @sebspark/environment
|
|
2
|
+
|
|
3
|
+
## 0.1.3
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 66ac832: * Upgraded to TypeScript 7
|
|
8
|
+
- Replaced tsup with tsdown for package builds
|
|
9
|
+
- Fixed cli-tester stdout parsing, resolving flaky e2e tests across the repo
|
|
10
|
+
- Patched vulnerable transitive dependencies (uuid, socket.io-parser, and others)
|
|
11
|
+
|
|
12
|
+
## 0.1.2
|
|
13
|
+
|
|
14
|
+
### Patch Changes
|
|
15
|
+
|
|
16
|
+
- b5f1b3f: add mergeEnvironment helper function
|
|
17
|
+
|
|
18
|
+
## 0.1.1
|
|
19
|
+
|
|
20
|
+
### Patch Changes
|
|
21
|
+
|
|
22
|
+
- 061b497: adjust filepath and add support for local env
|
|
23
|
+
|
|
24
|
+
## 0.1.0
|
|
25
|
+
|
|
26
|
+
### Minor Changes
|
|
27
|
+
|
|
28
|
+
- 626c170: add initSecretStore function and tests
|
|
29
|
+
|
|
30
|
+
## 0.0.4
|
|
31
|
+
|
|
32
|
+
### Patch Changes
|
|
33
|
+
|
|
34
|
+
- 7d8cc98: Dependabot dependency updates
|
|
35
|
+
|
|
36
|
+
## 0.0.3
|
|
37
|
+
|
|
38
|
+
### Patch Changes
|
|
39
|
+
|
|
40
|
+
- 736a0b3: No bundling of packages in packages
|
|
41
|
+
|
|
42
|
+
## 0.0.1
|
|
43
|
+
|
|
44
|
+
### Patch Changes
|
|
45
|
+
|
|
46
|
+
- 49ca25b: feat: publish @sebspark/environment
|
package/README.md
CHANGED
|
@@ -1,3 +1,71 @@
|
|
|
1
1
|
# `@sebspark/environment`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Utilities for reading environment variables and secrets in a type-safe, lazy way.
|
|
4
|
+
|
|
5
|
+
## `initEnvironment<T>()`
|
|
6
|
+
|
|
7
|
+
Returns a typed proxy that reads from `process.env` on each property access. Required variables are asserted at access time — an error is thrown if the value is missing.
|
|
8
|
+
|
|
9
|
+
```typescript
|
|
10
|
+
import { initEnvironment } from '@sebspark/environment'
|
|
11
|
+
|
|
12
|
+
type Env = {
|
|
13
|
+
API_URL: string
|
|
14
|
+
PORT: string
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const env = initEnvironment<Env>()
|
|
18
|
+
|
|
19
|
+
env.API_URL // reads process.env.API_URL, throws if missing
|
|
20
|
+
env.optional.PORT // reads process.env.PORT, returns undefined if missing
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Values are cached after the first access.
|
|
24
|
+
|
|
25
|
+
## `initSecretStore<T>(opts?)`
|
|
26
|
+
|
|
27
|
+
Returns a typed proxy that reads secrets from the filesystem. Useful for Kubernetes-mounted secrets where each secret is a file named after the key.
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
import { initSecretStore } from '@sebspark/environment'
|
|
31
|
+
|
|
32
|
+
type Secrets = {
|
|
33
|
+
DB_PASSWORD: string
|
|
34
|
+
API_KEY: string
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const secrets = initSecretStore<Secrets>({
|
|
38
|
+
dir: '/var/secrets', // directory containing secret files (optional)
|
|
39
|
+
fallback: true, // fall back to process.env if file is missing (optional)
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
secrets.DB_PASSWORD // reads /var/secrets/DB_PASSWORD
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Options:
|
|
46
|
+
|
|
47
|
+
| Option | Type | Description |
|
|
48
|
+
|---|---|---|
|
|
49
|
+
| `dir` | `string` | Directory to read secret files from. Defaults to CWD. |
|
|
50
|
+
| `fallback` | `boolean \| Partial<T>` | If `true`, falls back to `process.env`. If an object, uses its values as fallback. |
|
|
51
|
+
|
|
52
|
+
Values are cached after the first access. An error is thrown if the secret cannot be resolved.
|
|
53
|
+
|
|
54
|
+
## `mergeEnvironments(env, secrets)`
|
|
55
|
+
|
|
56
|
+
Merges the results of `initEnvironment` and `initSecretStore` into a single typed proxy. Secrets take precedence over environment variables for overlapping keys. Falls back to the environment proxy if a secret is not found.
|
|
57
|
+
|
|
58
|
+
```typescript
|
|
59
|
+
import { initEnvironment, initSecretStore, mergeEnvironments } from '@sebspark/environment'
|
|
60
|
+
|
|
61
|
+
type Env = { API_URL: string; DB_PASSWORD: string }
|
|
62
|
+
type Secrets = { DB_PASSWORD: string }
|
|
63
|
+
|
|
64
|
+
const env = initEnvironment<Env>()
|
|
65
|
+
const secrets = initSecretStore<Secrets>({ dir: '/var/secrets', fallback: true })
|
|
66
|
+
|
|
67
|
+
export const Environment = mergeEnvironments(env, secrets)
|
|
68
|
+
|
|
69
|
+
Environment.API_URL // from process.env
|
|
70
|
+
Environment.DB_PASSWORD // from /var/secrets/DB_PASSWORD, falls back to process.env
|
|
71
|
+
```
|
package/dist/index.d.mts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
//#region src/index.d.ts
|
|
2
|
-
declare const initEnvironment: <T extends Record<string, string>>() => T & {
|
|
2
|
+
export declare const initEnvironment: <T extends Record<string, string>>() => T & {
|
|
3
3
|
optional: Partial<T>;
|
|
4
4
|
} & {
|
|
5
5
|
[key: string]: string;
|
|
6
6
|
};
|
|
7
|
+
export declare const mergeEnvironments: <T extends Record<string, string>, U extends Record<string, string>>(env: T, secrets: U) => T & U;
|
|
7
8
|
type SecretStoreOptions<T> = {
|
|
8
9
|
dir?: string;
|
|
9
10
|
fallback?: boolean | Partial<T>;
|
|
10
11
|
};
|
|
11
|
-
declare const initSecretStore: <T extends Record<string, string>>(opts?: SecretStoreOptions<T>) => T & {
|
|
12
|
+
export declare const initSecretStore: <T extends Record<string, string>>(opts?: SecretStoreOptions<T>) => T & {
|
|
12
13
|
[key: string]: string;
|
|
13
14
|
};
|
|
14
15
|
//#endregion
|
|
15
|
-
export { initEnvironment, initSecretStore };
|
|
16
16
|
//# sourceMappingURL=index.d.mts.map
|
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":";qBAIa,kBAAmB,UAAU,6BAiCnC;EAAM,UAAU,QAAQ;;GAAU;;qBAG5B,oBACX,UAAU,wBACV,UAAU,wBAAsB,KAE3B,GAAC,SACG,MACR,IAAI;KAYF,mBAAmB;EACtB;EACA,qBAAqB,QAAQ;;qBAGlB,kBAAmB,UAAU,wBAAsB,OACvD,mBAAmB,OAwCrB;GAAO"}
|
package/dist/index.mjs
CHANGED
|
@@ -20,6 +20,14 @@ const initEnvironment = () => {
|
|
|
20
20
|
return environmentCache[key];
|
|
21
21
|
} });
|
|
22
22
|
};
|
|
23
|
+
const mergeEnvironments = (env, secrets) => new Proxy({}, { get(_, key) {
|
|
24
|
+
if (typeof key !== "string") return void 0;
|
|
25
|
+
try {
|
|
26
|
+
return secrets[key];
|
|
27
|
+
} catch {
|
|
28
|
+
return env[key];
|
|
29
|
+
}
|
|
30
|
+
} });
|
|
23
31
|
const initSecretStore = (opts) => {
|
|
24
32
|
const secretCache = {};
|
|
25
33
|
const { dir, fallback } = opts ?? {};
|
|
@@ -28,7 +36,8 @@ const initSecretStore = (opts) => {
|
|
|
28
36
|
if (key in secretCache) return secretCache[key];
|
|
29
37
|
const filePath = dir ? join(dir, key) : key;
|
|
30
38
|
try {
|
|
31
|
-
|
|
39
|
+
const value = readFileSync(filePath, "utf-8").trim();
|
|
40
|
+
secretCache[key] = value;
|
|
32
41
|
return secretCache[key];
|
|
33
42
|
} catch {}
|
|
34
43
|
if (fallback === true) {
|
|
@@ -45,6 +54,6 @@ const initSecretStore = (opts) => {
|
|
|
45
54
|
} });
|
|
46
55
|
};
|
|
47
56
|
//#endregion
|
|
48
|
-
export { initEnvironment, initSecretStore };
|
|
57
|
+
export { initEnvironment, initSecretStore, mergeEnvironments };
|
|
49
58
|
|
|
50
59
|
//# sourceMappingURL=index.mjs.map
|
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'\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,
|
|
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\nexport const mergeEnvironments = <\n T extends Record<string, string>,\n U extends Record<string, string>,\n>(\n env: T,\n secrets: U\n): T & U =>\n new Proxy({} as T & U, {\n get(_, key) {\n if (typeof key !== 'string') return undefined\n try {\n return (secrets as Record<string, string>)[key]\n } catch {\n return (env as Record<string, string>)[key]\n }\n },\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,CAAC;CAE9D,OAAO,IAAI,MACT,CAAC,GACD,EACE,IAAI,GAAG,GAA2C;EAChD,MAAM,MAAM;EACZ,IAAI,QAAQ,YACV,OAAO,IAAI,MACT,CAAC,GACD,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;EAC1B,EACF,CACF;EAEF,IAAI,OAAO,kBACT,OAAO,iBAAiB;EAE1B,MAAM,GAAG,MAAM,UAAU,QAAQ;EACjC,OAAO,OAAO,GAAG,IAAI,aAAa;EAClC,iBAAiB,OAAO;EACxB,OAAO,iBAAiB;CAC1B,EACF,CACF;AACF;AAEA,MAAa,qBAIX,KACA,YAEA,IAAI,MAAM,CAAC,GAAY,EACrB,IAAI,GAAG,KAAK;CACV,IAAI,OAAO,QAAQ,UAAU,OAAO,KAAA;CACpC,IAAI;EACF,OAAQ,QAAmC;CAC7C,QAAQ;EACN,OAAQ,IAA+B;CACzC;AACF,EACF,CAAC;AAOH,MAAa,mBACX,SACG;CACH,MAAM,cAAkD,CAAC;CACzD,MAAM,EAAE,KAAK,aAAa,QAAQ,CAAC;CAEnC,OAAO,IAAI,MACT,CAAC,GACD,EACE,IAAI,GAAG,GAA8B;EACnC,MAAM,MAAM;EACZ,IAAI,OAAO,aACT,OAAO,YAAY;EAIrB,MAAM,WAAW,MAAM,KAAK,KAAK,GAAG,IAAI;EACxC,IAAI;GACF,MAAM,QAAQ,aAAa,UAAU,OAAO,CAAC,CAAC,KAAK;GACnD,YAAY,OAAO;GACnB,OAAO,YAAY;EACrB,QAAQ,CAER;EAGA,IAAI,aAAa,MAAM;GAErB,MAAM,GAAG,MAAM,UAAU,QAAQ;GACjC,IAAI,UAAU,KAAA,GAAW;IACvB,YAAY,OAAO;IACnB,OAAO,YAAY;GACrB;EACF,OAAO,IAAI,YAAY,OAAO,UAAU;GACtC,YAAY,OAAO,SAAS;GAC5B,OAAO,YAAY;EACrB;EAEA,MAAM,IAAI,MAAM,UAAU,IAAI,aAAa;CAC7C,EACF,CACF;AACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sebspark/environment",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.mjs",
|
|
@@ -20,10 +20,18 @@
|
|
|
20
20
|
"dev": "tsc --watch --noEmit",
|
|
21
21
|
"lint": "biome check .",
|
|
22
22
|
"test": "vitest run --passWithNoTests --coverage",
|
|
23
|
-
"typecheck": "vitest --typecheck.only --passWithNoTests"
|
|
23
|
+
"typecheck": "vitest --typecheck.only --passWithNoTests --run"
|
|
24
24
|
},
|
|
25
25
|
"repository": {
|
|
26
26
|
"type": "git",
|
|
27
27
|
"url": "https://github.com/seb-oss/Spark-packages.git"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@biomejs/biome": "2.5.12",
|
|
31
|
+
"@sebspark/tsconfig": "*",
|
|
32
|
+
"depcheck": "1.4.7",
|
|
33
|
+
"typescript": "7.0.2",
|
|
34
|
+
"vite": "8.2.2",
|
|
35
|
+
"vitest": "5.0.0"
|
|
28
36
|
}
|
|
29
|
-
}
|
|
37
|
+
}
|