@azimuth-sh/context 0.0.0-alpha.0 → 0.2.0-alpha.8

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.
Files changed (3) hide show
  1. package/README.md +45 -3
  2. package/index.js +76 -0
  3. package/package.json +9 -5
package/README.md CHANGED
@@ -1,6 +1,48 @@
1
1
  # @azimuth-sh/context
2
2
 
3
- This version reserves the package name and holds no code. Do not install it.
3
+ The Azimuth context library for Node. It applies the values of an Azimuth launch to a component
4
+ and gives the component's report. It opens no port and knows nothing about tests.
4
5
 
5
- The Azimuth context library for Node is published under this name by the release workflow of
6
- [azimuth-sh/azimuth](https://github.com/azimuth-sh/azimuth). See <https://azimuth.sh>.
6
+ ## Install
7
+
8
+ ```sh
9
+ npm install --save-exact @azimuth-sh/context@<version>
10
+ ```
11
+
12
+ The library is released together with the `azimuth` command line tool under one version, so
13
+ `<version>` is the version of your `azimuth`.
14
+
15
+ ## Registration
16
+
17
+ The component registers its name, a condition under which applying is possible, and the mapping
18
+ of its keys. The condition is evaluated once, at start:
19
+
20
+ ```js
21
+ import { registerAzimuthContext } from "@azimuth-sh/context";
22
+
23
+ const context = registerAzimuthContext("payouts-web", {
24
+ applyingWhen: () => process.env.NODE_ENV !== "production",
25
+ map: { "payouts.instant": "PAYOUTS_INSTANT" },
26
+ });
27
+ ```
28
+
29
+ When the condition holds, the library reads `AZIMUTH_CONTEXT` and applies the value of each
30
+ mapped key it names. Without the variable it applies nothing.
31
+
32
+ ## Mapping
33
+
34
+ Every key the component uses is mapped by hand. A text names an environment variable, which the
35
+ library reads and writes in `process.env`. A team that keeps a flag in another store maps the key
36
+ to a source of its own, an object with `read()` and `apply(text)`.
37
+
38
+ ## Reporting
39
+
40
+ `context.report()` returns the report as JSON text. For each mapped key it holds the value, the
41
+ source it came from and the time it was read. Expose it at the report path the component declares.
42
+
43
+ ## Status
44
+
45
+ Azimuth is an early alpha. Formats and commands still change between versions.
46
+
47
+ - Site: https://azimuth.sh
48
+ - Source: https://github.com/azimuth-sh/azimuth
package/index.js ADDED
@@ -0,0 +1,76 @@
1
+ const VARIABLE = "AZIMUTH_CONTEXT";
2
+
3
+ // The report's own words for a source's position relative to Azimuth's applied value.
4
+ const BEFORE = "before azimuth";
5
+ const AZIMUTH = "azimuth";
6
+ const AFTER = "after azimuth";
7
+
8
+ const ENVIRONMENT = "environment";
9
+
10
+ // The source of a key mapped to a name. `process.env` has one value per name and no record of who
11
+ // wrote it, so the origin of the applied text is kept here and compared with the current text.
12
+ function environmentSource(name) {
13
+ let applied = null;
14
+ return {
15
+ read() {
16
+ const current = process.env[name];
17
+ if (current === undefined) return { value: null, source: null };
18
+ if (applied === null) return { value: current, source: { name: ENVIRONMENT, precedence: BEFORE } };
19
+ if (current === applied) return { value: current, source: { name: AZIMUTH, precedence: AZIMUTH } };
20
+ return { value: current, source: { name: ENVIRONMENT, precedence: AFTER } };
21
+ },
22
+ apply(text) {
23
+ process.env[name] = text;
24
+ applied = text;
25
+ },
26
+ };
27
+ }
28
+
29
+ function readLaunch() {
30
+ const text = process.env[VARIABLE];
31
+ if (text === undefined) return {};
32
+ let parsed;
33
+ try {
34
+ parsed = JSON.parse(text);
35
+ } catch (error) {
36
+ throw new Error(`${VARIABLE} must be one JSON object of text values: ${error.message}`, { cause: error });
37
+ }
38
+ if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) {
39
+ throw new Error(`${VARIABLE} must be one JSON object of text values.`);
40
+ }
41
+ for (const [key, value] of Object.entries(parsed)) {
42
+ if (typeof value !== "string") {
43
+ throw new Error(`${VARIABLE} must be one JSON object of text values; "${key}" is not text.`);
44
+ }
45
+ }
46
+ return parsed;
47
+ }
48
+
49
+ export function registerAzimuthContext(name, { applyingWhen, map }) {
50
+ const applying = Boolean(applyingWhen());
51
+ const launch = applying ? readLaunch() : {};
52
+
53
+ const entries = Object.entries(map).map(([key, setting]) => [
54
+ key,
55
+ typeof setting === "string" ? environmentSource(setting) : setting,
56
+ ]);
57
+
58
+ for (const [key, source] of entries) {
59
+ if (Object.hasOwn(launch, key)) source.apply(launch[key]);
60
+ }
61
+
62
+ return {
63
+ report() {
64
+ const context = {};
65
+ for (const [key, source] of entries) {
66
+ const { value, source: origin } = source.read();
67
+ context[key] = {
68
+ value: value ?? null,
69
+ source: origin ? { name: origin.name, precedence: origin.precedence } : null,
70
+ read: new Date().toISOString(),
71
+ };
72
+ }
73
+ return JSON.stringify({ format: 1, component: name, applying: applying ? "possible" : "not possible", context });
74
+ },
75
+ };
76
+ }
package/package.json CHANGED
@@ -1,19 +1,23 @@
1
1
  {
2
2
  "name": "@azimuth-sh/context",
3
- "version": "0.0.0-alpha.0",
4
- "description": "Reserves the name of the Azimuth context library for Node. This version holds no code.",
3
+ "version": "0.2.0-alpha.8",
4
+ "description": "Applies the values of an Azimuth launch to a Node component and gives the component's report.",
5
5
  "license": "Apache-2.0",
6
6
  "homepage": "https://azimuth.sh",
7
7
  "repository": {
8
8
  "type": "git",
9
- "url": "git+https://github.com/azimuth-sh/azimuth.git",
9
+ "url": "https://github.com/azimuth-sh/azimuth.git",
10
10
  "directory": "libraries/node"
11
11
  },
12
12
  "author": "Azimuth",
13
+ "type": "module",
14
+ "main": "./index.js",
15
+ "exports": "./index.js",
13
16
  "files": [
17
+ "index.js",
14
18
  "README.md"
15
19
  ],
16
- "publishConfig": {
17
- "access": "public"
20
+ "engines": {
21
+ "node": ">=18"
18
22
  }
19
23
  }