@webframe/config 0.0.0-stage → 0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 tw050x
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,24 @@
1
- # Temporary Holding Version
1
+ # @webframe/config
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Reads configuration, secrets and constants from a directory with a file for each key. That's how Docker and Kubernetes mount configs and secrets.
4
+
5
+ ```sh
6
+ npm install @webframe/config
7
+ ```
8
+
9
+ ```ts
10
+ import { defineFileStore } from '@webframe/config'
11
+
12
+ export const configs = defineFileStore({ directory: '/run/configs' })
13
+ export const secrets = defineFileStore({ directory: '/run/secrets' })
14
+
15
+ configs.read('logs.level') // 'debug', from /run/configs/logs.level
16
+ configs.readOr('login.enabled', 'false') // the fallback if there's no file
17
+ secrets.exists('google.client-secret') // true or false
18
+ ```
19
+
20
+ Keys are file names, so they can hold dots and other characters (`cors.*.allowed-origins`). Values are the file's contents, trimmed.
21
+
22
+ The full documentation is at [webframe.tw050x.net/packages/config](https://webframe.tw050x.net/packages/config).
23
+
24
+ Part of [webframe](https://webframe.tw050x.net), MIT licensed.
@@ -0,0 +1,29 @@
1
+ /** How long a value read is kept before reading its file again: 5 minutes. */
2
+ export declare const DEFAULT_TTL: number;
3
+ export type FileStoreOptions = {
4
+ /** The directory holding a file per key, resolved from the working directory. */
5
+ directory: string;
6
+ /** How long a value is cached, in milliseconds; 0 reads the file every time. */
7
+ ttl?: number;
8
+ };
9
+ export type FileStore = {
10
+ /** Whether the key has a file. */
11
+ exists(key: string): boolean;
12
+ /** The key's value: its file's contents, trimmed. Throws if it has none. */
13
+ read(key: string): string;
14
+ /** The key's value, or the fallback if it has no file. */
15
+ readOr<T>(key: string, fallback: T): string | T;
16
+ /** Forgets cached values, so the next reads read the files. */
17
+ clear(): void;
18
+ };
19
+ /**
20
+ * Reads values from a directory with a file for each key, as Docker and Kubernetes mount configs
21
+ * and secrets. Keys are file names, such as `logs.level` or `cors.*.allowed-origins`.
22
+ *
23
+ * @example
24
+ * const configs = defineFileStore({ directory: '.configs' });
25
+ * const secrets = defineFileStore({ directory: '/run/secrets' });
26
+ * configs.read('logs.level'); // 'debug'
27
+ */
28
+ export declare function defineFileStore(options: FileStoreOptions): FileStore;
29
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,8EAA8E;AAC9E,eAAO,MAAM,WAAW,QAAgB,CAAC;AAEzC,MAAM,MAAM,gBAAgB,GAAG;IAC7B,iFAAiF;IACjF,SAAS,EAAE,MAAM,CAAC;IAClB,gFAAgF;IAChF,GAAG,CAAC,EAAE,MAAM,CAAC;CACd,CAAA;AAED,MAAM,MAAM,SAAS,GAAG;IACtB,kCAAkC;IAClC,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;IAC7B,4EAA4E;IAC5E,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAC;IAC1B,0DAA0D;IAC1D,MAAM,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,GAAG,MAAM,GAAG,CAAC,CAAC;IAChD,+DAA+D;IAC/D,KAAK,IAAI,IAAI,CAAC;CACf,CAAA;AAED;;;;;;;;GAQG;AACH,wBAAgB,eAAe,CAAC,OAAO,EAAE,gBAAgB,GAAG,SAAS,CAiCpE"}
package/dist/index.js ADDED
@@ -0,0 +1,43 @@
1
+ import { existsSync, readFileSync } from "node:fs";
2
+ import { isAbsolute, relative, resolve } from "node:path";
3
+ /** How long a value read is kept before reading its file again: 5 minutes. */
4
+ export const DEFAULT_TTL = 5 * 60 * 1000;
5
+ /**
6
+ * Reads values from a directory with a file for each key, as Docker and Kubernetes mount configs
7
+ * and secrets. Keys are file names, such as `logs.level` or `cors.*.allowed-origins`.
8
+ *
9
+ * @example
10
+ * const configs = defineFileStore({ directory: '.configs' });
11
+ * const secrets = defineFileStore({ directory: '/run/secrets' });
12
+ * configs.read('logs.level'); // 'debug'
13
+ */
14
+ export function defineFileStore(options) {
15
+ const directory = resolve(options.directory);
16
+ const ttl = options.ttl ?? DEFAULT_TTL;
17
+ const cache = new Map();
18
+ const pathOf = (key) => {
19
+ const path = resolve(directory, key);
20
+ const inside = relative(directory, path);
21
+ if (key === '' || inside === '' || inside.startsWith('..') || isAbsolute(inside)) {
22
+ throw new Error(`Invalid key: ${key}`);
23
+ }
24
+ return path;
25
+ };
26
+ const exists = (key) => existsSync(pathOf(key));
27
+ const read = (key) => {
28
+ const path = pathOf(key);
29
+ const cached = cache.get(path);
30
+ if (cached && Date.now() - cached.timestamp < ttl) {
31
+ return cached.value;
32
+ }
33
+ if (existsSync(path) === false) {
34
+ throw new Error(`File not found: ${key}`);
35
+ }
36
+ const value = readFileSync(path, 'utf-8').trim();
37
+ cache.set(path, { value, timestamp: Date.now() });
38
+ return value;
39
+ };
40
+ const readOr = (key, fallback) => exists(key) ? read(key) : fallback;
41
+ return Object.freeze({ exists, read, readOr, clear: () => cache.clear() });
42
+ }
43
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACnD,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAE1D,8EAA8E;AAC9E,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC;AAoBzC;;;;;;;;GAQG;AACH,MAAM,UAAU,eAAe,CAAC,OAAyB;IACvD,MAAM,SAAS,GAAG,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;IAC7C,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,IAAI,WAAW,CAAC;IACvC,MAAM,KAAK,GAAG,IAAI,GAAG,EAAgD,CAAC;IAEtE,MAAM,MAAM,GAAG,CAAC,GAAW,EAAE,EAAE;QAC7B,MAAM,IAAI,GAAG,OAAO,CAAC,SAAS,EAAE,GAAG,CAAC,CAAC;QACrC,MAAM,MAAM,GAAG,QAAQ,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;QACzC,IAAI,GAAG,KAAK,EAAE,IAAI,MAAM,KAAK,EAAE,IAAI,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,UAAU,CAAC,MAAM,CAAC,EAAE,CAAC;YACjF,MAAM,IAAI,KAAK,CAAC,gBAAgB,GAAG,EAAE,CAAC,CAAC;QACzC,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC,CAAC;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eholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Read configuration, secrets and constants from a directory of files, one file per key.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "default": "./dist/index.js"
11
+ }
12
+ },
13
+ "files": [
14
+ "dist",
15
+ "src"
16
+ ],
17
+ "dependencies": {},
18
+ "engines": {
19
+ "node": ">=22"
20
+ },
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "git+https://github.com/tw050x/webframe.git",
24
+ "directory": "packages/node/config"
25
+ },
26
+ "homepage": "https://webframe.tw050x.net/packages/config",
27
+ "publishConfig": {
28
+ "access": "public"
29
+ }
30
+ }
package/src/index.ts ADDED
@@ -0,0 +1,67 @@
1
+ import { existsSync, readFileSync } from "node:fs";
2
+ import { isAbsolute, relative, resolve } from "node:path";
3
+
4
+ /** How long a value read is kept before reading its file again: 5 minutes. */
5
+ export const DEFAULT_TTL = 5 * 60 * 1000;
6
+
7
+ export type FileStoreOptions = {
8
+ /** The directory holding a file per key, resolved from the working directory. */
9
+ directory: string;
10
+ /** How long a value is cached, in milliseconds; 0 reads the file every time. */
11
+ ttl?: number;
12
+ }
13
+
14
+ export type FileStore = {
15
+ /** Whether the key has a file. */
16
+ exists(key: string): boolean;
17
+ /** The key's value: its file's contents, trimmed. Throws if it has none. */
18
+ read(key: string): string;
19
+ /** The key's value, or the fallback if it has no file. */
20
+ readOr<T>(key: string, fallback: T): string | T;
21
+ /** Forgets cached values, so the next reads read the files. */
22
+ clear(): void;
23
+ }
24
+
25
+ /**
26
+ * Reads values from a directory with a file for each key, as Docker and Kubernetes mount configs
27
+ * and secrets. Keys are file names, such as `logs.level` or `cors.*.allowed-origins`.
28
+ *
29
+ * @example
30
+ * const configs = defineFileStore({ directory: '.configs' });
31
+ * const secrets = defineFileStore({ directory: '/run/secrets' });
32
+ * configs.read('logs.level'); // 'debug'
33
+ */
34
+ export function defineFileStore(options: FileStoreOptions): FileStore {
35
+ const directory = resolve(options.directory);
36
+ const ttl = options.ttl ?? DEFAULT_TTL;
37
+ const cache = new Map<string, { value: string; timestamp: number }>();
38
+
39
+ const pathOf = (key: string) => {
40
+ const path = resolve(directory, key);
41
+ const inside = relative(directory, path);
42
+ if (key === '' || inside === '' || inside.startsWith('..') || isAbsolute(inside)) {
43
+ throw new Error(`Invalid key: ${key}`);
44
+ }
45
+ return path;
46
+ };
47
+
48
+ const exists = (key: string) => existsSync(pathOf(key));
49
+
50
+ const read = (key: string) => {
51
+ const path = pathOf(key);
52
+ const cached = cache.get(path);
53
+ if (cached && Date.now() - cached.timestamp < ttl) {
54
+ return cached.value;
55
+ }
56
+ if (existsSync(path) === false) {
57
+ throw new Error(`File not found: ${key}`);
58
+ }
59
+ const value = readFileSync(path, 'utf-8').trim();
60
+ cache.set(path, { value, timestamp: Date.now() });
61
+ return value;
62
+ };
63
+
64
+ const readOr = <T>(key: string, fallback: T) => exists(key) ? read(key) : fallback;
65
+
66
+ return Object.freeze({ exists, read, readOr, clear: () => cache.clear() });
67
+ }