sdk-environment 0.0.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) 2020 Uladzimir Kasacheuski
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.
@@ -0,0 +1 @@
1
+ export declare const todo = true;
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.todo = void 0;
4
+ exports.todo = true;
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAa,QAAA,IAAI,GAAG,IAAI,CAAC"}
package/license.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 ehmpathy
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/package.json ADDED
@@ -0,0 +1,109 @@
1
+ {
2
+ "name": "sdk-environment",
3
+ "author": "ehmpathy",
4
+ "description": "parse and expose the environment your code runs in",
5
+ "version": "0.0.0",
6
+ "repository": "ehmpathy/sdk-environment",
7
+ "homepage": "https://github.com/ehmpathy/sdk-environment",
8
+ "keywords": [
9
+ "environment",
10
+ "config",
11
+ "access",
12
+ "server",
13
+ "commit",
14
+ "runtime"
15
+ ],
16
+ "bugs": "https://github.com/ehmpathy/sdk-environment/issues",
17
+ "license": "MIT",
18
+ "main": "dist/index.js",
19
+ "engines": {
20
+ "node": ">=8.0.0"
21
+ },
22
+ "files": [
23
+ "/dist"
24
+ ],
25
+ "dependencies": {
26
+ "@noble/hashes": "2.0.1",
27
+ "domain-glossaries": "1.0.0",
28
+ "type-fns": "1.21.0"
29
+ },
30
+ "devDependencies": {
31
+ "@biomejs/biome": "2.3.8",
32
+ "@commitlint/cli": "19.5.0",
33
+ "@commitlint/config-conventional": "19.5.0",
34
+ "@ehmpathy/uni-time": "1.4.2",
35
+ "@noble/hashes": "2.0.1",
36
+ "@swc/core": "1.15.3",
37
+ "@swc/jest": "0.2.39",
38
+ "@tsconfig/node-lts-strictest": "18.12.1",
39
+ "@tsconfig/node20": "20.1.5",
40
+ "@tsconfig/strictest": "2.0.5",
41
+ "@types/jest": "30.0.0",
42
+ "@types/node": "22.15.21",
43
+ "core-js": "3.26.1",
44
+ "cz-conventional-changelog": "3.3.0",
45
+ "declapract": "0.13.14",
46
+ "declapract-typescript-ehmpathy": "0.47.36",
47
+ "declastruct": "1.7.3",
48
+ "declastruct-github": "1.3.0",
49
+ "depcheck": "1.4.3",
50
+ "domain-objects": "0.31.9",
51
+ "esbuild-register": "3.6.0",
52
+ "helpful-errors": "1.5.3",
53
+ "husky": "8.0.3",
54
+ "jest": "30.2.0",
55
+ "rhachet": "1.41.4",
56
+ "rhachet-brains-anthropic": "0.4.1",
57
+ "rhachet-brains-xai": "0.3.3",
58
+ "rhachet-roles-bhrain": "0.27.6",
59
+ "rhachet-roles-bhuild": "0.21.4",
60
+ "rhachet-roles-ehmpathy": "1.35.0",
61
+ "test-fns": "1.7.2",
62
+ "ts-jest": "29.1.3",
63
+ "ts-node": "10.9.2",
64
+ "tsc-alias": "1.8.10",
65
+ "tsx": "4.20.6",
66
+ "typescript": "5.4.5",
67
+ "yalc": "1.0.0-pre.53"
68
+ },
69
+ "config": {
70
+ "commitizen": {
71
+ "path": "./node_modules/cz-conventional-changelog"
72
+ }
73
+ },
74
+ "scripts": {
75
+ "build:ts": "tsc -p ./tsconfig.build.json",
76
+ "commit:with-cli": "npx cz",
77
+ "fix:format:biome": "biome check --write",
78
+ "fix:format": "npm run fix:format:biome",
79
+ "fix:lint": "biome check --write",
80
+ "fix": "npm run fix:format && npm run fix:lint",
81
+ "build:clean:bun": "rm -f ./bin/*.bc",
82
+ "build:clean:tsc": "(chmod -R u+w dist 2>/dev/null || true) && rm -rf dist/",
83
+ "build:clean": "npm run build:clean:tsc && npm run build:clean:bun",
84
+ "build:compile:tsc": "tsc -p ./tsconfig.build.json && tsc-alias -p ./tsconfig.build.json",
85
+ "build:compile": "npm run build:compile:tsc && npm run build:compile:bun --if-present",
86
+ "build": "npm run build:clean && npm run build:compile && npm run build:complete --if-present",
87
+ "test:auth": "[ \"$ECHO\" = 'true' ] && echo '. .agent/repo=.this/role=any/skills/use.apikeys.sh' || . .agent/repo=.this/role=any/skills/use.apikeys.sh",
88
+ "test:commits": "LAST_TAG=$(git describe --tags --abbrev=0 @^ 2> /dev/null || git rev-list --max-parents=0 HEAD) && npx commitlint --from $LAST_TAG --to HEAD --verbose",
89
+ "test:types": "tsc -p ./tsconfig.json --noEmit",
90
+ "test:format:biome": "biome format",
91
+ "test:format": "npm run test:format:biome",
92
+ "test:lint:deps": "npx depcheck -c ./.depcheckrc.yml",
93
+ "test:lint:biome": "biome check --diagnostic-level=error",
94
+ "test:lint:biome:all": "biome check",
95
+ "test:lint": "npm run test:lint:biome && npm run test:lint:deps",
96
+ "test:unit": "jest -c ./jest.unit.config.ts --forceExit --verbose --passWithNoTests $([ -z $THOROUGH ] && echo '--changedSince=main') $([ -n $RESNAP ] && echo '--updateSnapshot')",
97
+ "test:integration": "jest -c ./jest.integration.config.ts --forceExit --verbose --passWithNoTests $([ -z $THOROUGH ] && echo '--changedSince=main') $([ -n $RESNAP ] && echo '--updateSnapshot')",
98
+ "test:acceptance:locally": "npm run build && LOCALLY=true jest -c ./jest.acceptance.config.ts --forceExit --verbose --runInBand --passWithNoTests $([ -n $RESNAP ] && echo '--updateSnapshot')",
99
+ "test": "eval $(ECHO=true npm run --silent test:auth) && npm run test:commits && npm run test:types && npm run test:format && npm run test:lint && npm run test:unit && npm run test:integration && npm run test:acceptance:locally",
100
+ "test:acceptance": "npm run build && jest -c ./jest.acceptance.config.ts --forceExit --verbose --runInBand --passWithNoTests $([ -n $RESNAP ] && echo '--updateSnapshot')",
101
+ "prepush": "npm run test && npm run build",
102
+ "prepublish": "npm run build",
103
+ "preversion": "npm run prepush",
104
+ "postversion": "git push origin HEAD --tags --no-verify",
105
+ "prepare:husky": "husky install && chmod ug+x .husky/*",
106
+ "prepare:rhachet": "rhachet init --hooks --roles behaver mechanic reviewer",
107
+ "upgrade:rhachet": "rhachet upgrade"
108
+ }
109
+ }
package/readme.md ADDED
@@ -0,0 +1,243 @@
1
+ # sdk-environment
2
+
3
+ ![test](https://github.com/ehmpathy/sdk-environment/workflows/test/badge.svg)
4
+ ![publish](https://github.com/ehmpathy/sdk-environment/workflows/publish/badge.svg)
5
+
6
+ parse and expose the environment your code runs in
7
+
8
+ # install
9
+
10
+ ```sh
11
+ npm install sdk-environment
12
+ ```
13
+
14
+ # what
15
+
16
+ an environment is defined by three attributes:
17
+
18
+ ```ts
19
+ interface Environment {
20
+ access: 'test' | 'prep' | 'prod';
21
+ server: string; // 'local' | 'local@cicd' | 'cloud@aws.lambda' | ...
22
+ commit: string; // '$gitref@$hash' or '$gitref@$hash+' if dirty
23
+ }
24
+ ```
25
+
26
+ | attribute | what | why |
27
+ |-----------|------|-----|
28
+ | `access` | what resources can we touch? | controls which databases, apis, secrets, etc. are reachable |
29
+ | `server` | where does this run? | enables server-specific behavior (log format, cache strategy, etc.) |
30
+ | `commit` | what code is this? | enables traceability and debug |
31
+
32
+ ## access
33
+
34
+ which tier of resources this process can access.
35
+
36
+ | value | what | examples |
37
+ |-------|------|----------|
38
+ | `test` | ephemeral, isolated, disposable | in-memory db, mocked apis, test fixtures |
39
+ | `prep` | persistent, shared, pre-production | prep db, sandbox apis, qa data |
40
+ | `prod` | persistent, shared, production | production db, live apis, real users |
41
+
42
+ ## server
43
+
44
+ where this process executes. format: `$tier` or `$tier@$platform`
45
+
46
+ | value | what | examples |
47
+ |-------|------|----------|
48
+ | `local` | developer machine | laptop, docker compose |
49
+ | `local@cicd` | ci runner | github actions, circleci |
50
+ | `cloud@aws.lambda` | aws lambda | serverless functions |
51
+ | `cloud@aws.ecs` | aws ecs | containerized services |
52
+ | `cloud@aws.ec2` | aws ec2 | virtual machines |
53
+ | `cloud@gcp.cloudrun` | gcp cloud run | containerized services |
54
+
55
+ the tier (`local` or `cloud`) is always parseable:
56
+
57
+ ```ts
58
+ const tier = environment.server.split('@')[0]; // 'local' | 'cloud'
59
+ ```
60
+
61
+ ## commit
62
+
63
+ what code this process runs. format: `$gitref@$hash` or `$gitref@$hash+` if dirty
64
+
65
+ | context | format | example |
66
+ |---------|--------|---------|
67
+ | tagged release | `v$tag@$hash` | `v1.2.3@a1b2c3d` |
68
+ | branch build | `$branch@$hash` | `feat/auth@e4f5g6h` |
69
+ | local dev | `$branch@$hash` | `main@i7j8k9l` |
70
+ | dirty (uncommitted changes) | `$gitref@$hash+` | `main@i7j8k9l+` |
71
+
72
+ the `+` suffix indicates uncommitted changes exist. enables:
73
+ - trace logs back to exact code version
74
+ - detect if deployed with uncommitted changes
75
+ - debug production issues
76
+ - track releases
77
+
78
+ # use
79
+
80
+ ```ts
81
+ import { getEnvironment } from 'sdk-environment';
82
+
83
+ const environment = await getEnvironment();
84
+
85
+ console.log(environment);
86
+ // {
87
+ // access: 'prod',
88
+ // server: 'cloud@aws.lambda',
89
+ // commit: 'v1.2.3@a1b2c3d'
90
+ // }
91
+ ```
92
+
93
+ ## pluggable parsers
94
+
95
+ each attribute is derived via an ordered list of parsers. first parser to return a value wins.
96
+
97
+ ```ts
98
+ import {
99
+ getEnvironment,
100
+ fromEnvar,
101
+ fromAwsAccountAlias,
102
+ fromNodeEnv,
103
+ fromLambdaTaskRoot,
104
+ fromCiEnvar,
105
+ fromGit,
106
+ } from 'sdk-environment';
107
+
108
+ // default parsers (what getEnvironment() uses out of the box)
109
+ const environment = await getEnvironment({
110
+ parsers: {
111
+ access: [
112
+ fromEnvar('ACCESS'), // explicit override via ACCESS=prod
113
+ fromAwsAccountAlias(), // infer from aws account alias (e.g., 'myorg-prod' → prod)
114
+ fromNodeEnv(), // fallback: NODE_ENV=test → test
115
+ ],
116
+ server: [
117
+ fromEnvar('SERVER'), // explicit override via SERVER=cloud@aws.lambda
118
+ fromLambdaTaskRoot(), // LAMBDA_TASK_ROOT present → cloud@aws.lambda
119
+ fromCiEnvar(), // CI=true → local@cicd
120
+ () => 'local', // default to local
121
+ ],
122
+ commit: [
123
+ fromEnvar('COMMIT'), // explicit override via COMMIT=v1.0.0@abc123
124
+ fromGit(), // git describe + rev-parse + dirty check → v1.0.0@abc123+
125
+ ],
126
+ },
127
+ });
128
+ ```
129
+
130
+ ### custom parsers
131
+
132
+ a parser is a function that returns a value or null:
133
+
134
+ ```ts
135
+ type Parser<T> = () => T | null | Promise<T | null>;
136
+ ```
137
+
138
+ add custom parsers for your infrastructure:
139
+
140
+ ```ts
141
+ import { getEnvironment, fromEnvar } from 'sdk-environment';
142
+
143
+ // custom parser: infer access from k8s namespace
144
+ const fromK8sNamespace = (): 'test' | 'prep' | 'prod' | null => {
145
+ const ns = process.env.K8S_NAMESPACE;
146
+ if (ns?.endsWith('-prod')) return 'prod';
147
+ if (ns?.endsWith('-prep')) return 'prep';
148
+ if (ns?.endsWith('-test')) return 'test';
149
+ return null;
150
+ };
151
+
152
+ const environment = await getEnvironment({
153
+ parsers: {
154
+ access: [
155
+ fromEnvar('ACCESS'),
156
+ fromK8sNamespace(), // your custom parser
157
+ fromAwsAccountAlias(),
158
+ fromNodeEnv(),
159
+ ],
160
+ },
161
+ });
162
+ ```
163
+
164
+ ## with config
165
+
166
+ pair with config to load secrets per-environment:
167
+
168
+ ```ts
169
+ import { getEnvironment } from 'sdk-environment';
170
+ import { getConfig } from './config';
171
+
172
+ const environment = await getEnvironment();
173
+ const config = await getConfig({ environment });
174
+
175
+ // config.database.password loaded from:
176
+ // - test: local .env or mock
177
+ // - prep: aws ssm /myapp/prep/database.password
178
+ // - prod: aws ssm /myapp/prod/database.password
179
+ ```
180
+
181
+ ## override via env vars
182
+
183
+ set explicit values when parsers can't infer correctly:
184
+
185
+ ```sh
186
+ ACCESS=prod SERVER=cloud@aws.lambda COMMIT=v1.2.3@abc123 node app.js
187
+ ```
188
+
189
+ # design
190
+
191
+ ## why these three?
192
+
193
+ every runtime question reduces to one of:
194
+
195
+ 1. **what can i touch?** → `access`
196
+ 2. **where am i?** → `server`
197
+ 3. **what code is this?** → `commit`
198
+
199
+ other concerns (region, account, cluster) are derivable or orthogonal.
200
+
201
+ ## access vs server
202
+
203
+ these are independent axes:
204
+
205
+ | access | server | example |
206
+ |--------|--------|---------|
207
+ | test | local | `npm test` on laptop |
208
+ | test | local@cicd | ci runner runs tests |
209
+ | prep | local | dev laptop against prep db |
210
+ | prep | cloud@aws.lambda | prep lambda |
211
+ | prod | local | production debug session |
212
+ | prod | cloud@aws.lambda | production lambda |
213
+
214
+ this separation enables precise behavior:
215
+
216
+ ```ts
217
+ // fail loudly in test, warn in prod
218
+ if (environment.access === 'test') {
219
+ throw new SchemaError('config drift detected');
220
+ } else {
221
+ log.warn('config drift detected', { diff });
222
+ }
223
+
224
+ // verbose logs locally, structured logs in cloud
225
+ const tier = environment.server.split('@')[0];
226
+ if (tier === 'local') {
227
+ log.pretty();
228
+ } else {
229
+ log.json();
230
+ }
231
+ ```
232
+
233
+ ## prep over dev
234
+
235
+ we use `prep` (preparation) instead of `dev` (development):
236
+
237
+ | term | describes | semantic fit |
238
+ |------|-----------|--------------|
239
+ | prod | where we **produce** value | what happens there |
240
+ | prep | where we **prepare** for production | what happens there |
241
+ | dev | who uses it (developers) | who, not what |
242
+
243
+ `test → prep → prod` reads as a progression of readiness.