@toa.io/extensions.configuration 1.0.0-alpha.28 → 1.0.0-alpha.281

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 (152) hide show
  1. package/CHANGELOG.md +340 -0
  2. package/components/configuration.values/manifest.toa.yaml +158 -0
  3. package/components/configuration.values/operations/create.d.ts +16 -0
  4. package/components/configuration.values/operations/create.js +31 -0
  5. package/components/configuration.values/operations/create.js.map +1 -0
  6. package/components/configuration.values/operations/fetch.d.ts +11 -0
  7. package/components/configuration.values/operations/fetch.js +13 -0
  8. package/components/configuration.values/operations/fetch.js.map +1 -0
  9. package/components/configuration.values/operations/get.d.ts +17 -0
  10. package/components/configuration.values/operations/get.js +19 -0
  11. package/components/configuration.values/operations/get.js.map +1 -0
  12. package/components/configuration.values/operations/lib/map.d.ts +11 -0
  13. package/components/configuration.values/operations/lib/map.js +21 -0
  14. package/components/configuration.values/operations/lib/map.js.map +1 -0
  15. package/components/configuration.values/operations/lib/map.test.d.ts +1 -0
  16. package/components/configuration.values/operations/lib/map.test.js +26 -0
  17. package/components/configuration.values/operations/lib/map.test.js.map +1 -0
  18. package/components/configuration.values/operations/lib/resolve.d.ts +27 -0
  19. package/components/configuration.values/operations/lib/resolve.js +24 -0
  20. package/components/configuration.values/operations/lib/resolve.js.map +1 -0
  21. package/components/configuration.values/operations/list.d.ts +10 -0
  22. package/components/configuration.values/operations/list.js +11 -0
  23. package/components/configuration.values/operations/list.js.map +1 -0
  24. package/components/configuration.values/source/create.ts +56 -0
  25. package/components/configuration.values/source/fetch.ts +24 -0
  26. package/components/configuration.values/source/get.ts +33 -0
  27. package/components/configuration.values/source/lib/map.test.ts +37 -0
  28. package/components/configuration.values/source/lib/map.ts +34 -0
  29. package/components/configuration.values/source/lib/resolve.ts +54 -0
  30. package/components/configuration.values/source/list.ts +19 -0
  31. package/components/configuration.values/tsconfig.json +10 -0
  32. package/components/configuration.values/tsconfig.tsbuildinfo +1 -0
  33. package/cucumber.mjs +5 -0
  34. package/features/site/_app/immutable/asset.js +1 -0
  35. package/features/site/favicon.ico +0 -0
  36. package/features/site/index.html +6 -0
  37. package/features/steps/UI.ts +91 -0
  38. package/features/steps/tsconfig.json +9 -0
  39. package/features/ui.feature +93 -0
  40. package/package.json +11 -10
  41. package/readme.md +160 -47
  42. package/schemas/annotation.cos.yaml +29 -1
  43. package/schemas/manifest.cos.yaml +9 -2
  44. package/source/Aspect.test.ts +89 -9
  45. package/source/Aspect.ts +64 -5
  46. package/source/Client.test.ts +108 -0
  47. package/source/Client.ts +258 -0
  48. package/source/Composition.ts +57 -0
  49. package/source/Factory.ts +29 -6
  50. package/source/Secret.test.ts +32 -0
  51. package/source/Secret.ts +29 -0
  52. package/source/UI.ts +212 -0
  53. package/source/configuration.test.ts +96 -40
  54. package/source/configuration.ts +30 -11
  55. package/source/const.ts +17 -0
  56. package/source/deployment.test.ts +78 -12
  57. package/source/deployment.ts +75 -27
  58. package/source/epoch.test.ts +29 -0
  59. package/source/epoch.ts +21 -0
  60. package/source/index.ts +4 -3
  61. package/source/manifest.test.ts +8 -5
  62. package/source/manifest.ts +2 -4
  63. package/source/schemas.ts +1 -1
  64. package/transpiled/Aspect.d.ts +18 -4
  65. package/transpiled/Aspect.js +50 -9
  66. package/transpiled/Aspect.js.map +1 -1
  67. package/transpiled/Aspect.test.js +63 -12
  68. package/transpiled/Aspect.test.js.map +1 -1
  69. package/transpiled/Client.d.ts +61 -0
  70. package/transpiled/Client.js +169 -0
  71. package/transpiled/Client.js.map +1 -0
  72. package/transpiled/Client.test.d.ts +1 -0
  73. package/transpiled/Client.test.js +78 -0
  74. package/transpiled/Client.test.js.map +1 -0
  75. package/transpiled/Composition.d.ts +14 -0
  76. package/transpiled/Composition.js +38 -0
  77. package/transpiled/Composition.js.map +1 -0
  78. package/transpiled/Factory.d.ts +8 -2
  79. package/transpiled/Factory.js +20 -9
  80. package/transpiled/Factory.js.map +1 -1
  81. package/transpiled/Secret.d.ts +12 -0
  82. package/transpiled/Secret.js +22 -0
  83. package/transpiled/Secret.js.map +1 -0
  84. package/transpiled/Secret.test.d.ts +1 -0
  85. package/transpiled/Secret.test.js +26 -0
  86. package/transpiled/Secret.test.js.map +1 -0
  87. package/transpiled/UI.d.ts +25 -0
  88. package/transpiled/UI.js +172 -0
  89. package/transpiled/UI.js.map +1 -0
  90. package/transpiled/configuration.d.ts +7 -2
  91. package/transpiled/configuration.js +30 -41
  92. package/transpiled/configuration.js.map +1 -1
  93. package/transpiled/configuration.test.js +90 -43
  94. package/transpiled/configuration.test.js.map +1 -1
  95. package/transpiled/const.d.ts +11 -0
  96. package/transpiled/const.js +11 -0
  97. package/transpiled/const.js.map +1 -0
  98. package/transpiled/deployment.d.ts +9 -3
  99. package/transpiled/deployment.js +62 -57
  100. package/transpiled/deployment.js.map +1 -1
  101. package/transpiled/deployment.test.js +61 -13
  102. package/transpiled/deployment.test.js.map +1 -1
  103. package/transpiled/epoch.d.ts +2 -0
  104. package/transpiled/epoch.js +17 -0
  105. package/transpiled/epoch.js.map +1 -0
  106. package/transpiled/epoch.test.d.ts +1 -0
  107. package/transpiled/epoch.test.js +22 -0
  108. package/transpiled/epoch.test.js.map +1 -0
  109. package/transpiled/index.d.ts +4 -3
  110. package/transpiled/index.js +4 -9
  111. package/transpiled/index.js.map +1 -1
  112. package/transpiled/manifest.d.ts +1 -1
  113. package/transpiled/manifest.js +2 -31
  114. package/transpiled/manifest.js.map +1 -1
  115. package/transpiled/manifest.test.js +9 -9
  116. package/transpiled/manifest.test.js.map +1 -1
  117. package/transpiled/schemas.js +5 -31
  118. package/transpiled/schemas.js.map +1 -1
  119. package/tsconfig.json +2 -1
  120. package/tsconfig.tsbuildinfo +1 -0
  121. package/ui/dist/_app/env.js +1 -0
  122. package/ui/dist/_app/immutable/assets/0.CNS8Cqhc.css +2 -0
  123. package/ui/dist/_app/immutable/assets/inter-cyrillic-ext-wght-normal.BOeWTOD4.woff2 +0 -0
  124. package/ui/dist/_app/immutable/assets/inter-cyrillic-wght-normal.DqGufNeO.woff2 +0 -0
  125. package/ui/dist/_app/immutable/assets/inter-greek-ext-wght-normal.DlzME5K_.woff2 +0 -0
  126. package/ui/dist/_app/immutable/assets/inter-greek-wght-normal.CkhJZR-_.woff2 +0 -0
  127. package/ui/dist/_app/immutable/assets/inter-latin-ext-wght-normal.DO1Apj_S.woff2 +0 -0
  128. package/ui/dist/_app/immutable/assets/inter-latin-wght-normal.Dx4kXJAl.woff2 +0 -0
  129. package/ui/dist/_app/immutable/assets/inter-vietnamese-wght-normal.CBcvBZtf.woff2 +0 -0
  130. package/ui/dist/_app/immutable/assets/ui.BghF56l_.css +1 -0
  131. package/ui/dist/_app/immutable/chunks/BBRsRBKl.js +6 -0
  132. package/ui/dist/_app/immutable/chunks/BBjoSbGn.js +8 -0
  133. package/ui/dist/_app/immutable/chunks/Bjy-W4x2.js +81 -0
  134. package/ui/dist/_app/immutable/chunks/Bzfl3Xp_.js +1 -0
  135. package/ui/dist/_app/immutable/chunks/C6nSKt5F.js +50 -0
  136. package/ui/dist/_app/immutable/chunks/CcD_ISxM.js +3 -0
  137. package/ui/dist/_app/immutable/chunks/DWNBcI-9.js +1 -0
  138. package/ui/dist/_app/immutable/chunks/xihTtKlq.js +1 -0
  139. package/ui/dist/_app/immutable/entry/app.Dr8IgX3r.js +2 -0
  140. package/ui/dist/_app/immutable/entry/start.Dy9qIspC.js +1 -0
  141. package/ui/dist/_app/immutable/nodes/0.zxJ_VvIF.js +1 -0
  142. package/ui/dist/_app/immutable/nodes/1.DasPjSN2.js +1 -0
  143. package/ui/dist/_app/immutable/nodes/2.9VqQT6jB.js +1 -0
  144. package/ui/dist/_app/immutable/nodes/3.D8a8RCsW.js +1 -0
  145. package/ui/dist/_app/immutable/nodes/4.y19mXraP.js +1 -0
  146. package/ui/dist/_app/version.json +1 -0
  147. package/ui/dist/apple-touch-icon.png +0 -0
  148. package/ui/dist/favicon-96x96.png +0 -0
  149. package/ui/dist/favicon.ico +0 -0
  150. package/ui/dist/index.html +52 -0
  151. package/ui/dist/robots.txt +3 -0
  152. package/transpiled/tsconfig.tsbuildinfo +0 -1
@@ -0,0 +1,91 @@
1
+ import assert from 'node:assert'
2
+ import * as http from 'node:http'
3
+ import { resolve } from 'node:path'
4
+ import tsflow from 'cucumber-tsflow'
5
+
6
+ import { UI } from '../../source/UI.js'
7
+ import { UI_PORT } from '../../source/const.js'
8
+
9
+ const { after, binding, given, then, when } = tsflow
10
+
11
+ /**
12
+ * The scenarios are about the server, not about the page: it is pointed at a fixture
13
+ * directory, so that running them never needs a UI build.
14
+ */
15
+ @binding()
16
+ export class Site {
17
+ private server: UI | null = null
18
+ private response: Response | null = null
19
+
20
+ @given('the UI is published')
21
+ public async publish (): Promise<void> {
22
+ this.server = new UI(UI_PORT, resolve(import.meta.dirname, '..', 'site'))
23
+
24
+ await this.server.connect()
25
+ }
26
+
27
+ @when('{string} is requested')
28
+ public async request (path: string): Promise<void> {
29
+ this.response = await get(path)
30
+ }
31
+
32
+ @then('the status is {int}')
33
+ public statusIs (status: number): void {
34
+ assert.equal(this.response?.status, status)
35
+ }
36
+
37
+ @then('the body contains {string}')
38
+ public bodyContains (text: string): void {
39
+ assert.ok(this.response?.body.includes(text),
40
+ `Expected the body to contain '${text}', got '${this.response?.body}'`)
41
+ }
42
+
43
+ @then('the body is empty')
44
+ public bodyIsEmpty (): void {
45
+ assert.equal(this.response?.body, '')
46
+ }
47
+
48
+ @then('the {string} header is {string}')
49
+ public headerIs (name: string, value: string): void {
50
+ assert.equal(this.response?.headers[name], value)
51
+ }
52
+
53
+ @after()
54
+ public async shutdown (): Promise<void> {
55
+ await this.server?.disconnect()
56
+
57
+ this.server = null
58
+ this.response = null
59
+ }
60
+ }
61
+
62
+ /**
63
+ * Raw `http`, not `fetch`: the URL parser normalizes `%2e%2e` away, and one of the
64
+ * scenarios is about exactly that segment reaching the server.
65
+ */
66
+ async function get (path: string): Promise<Response> {
67
+ return await new Promise((resolve, reject) => {
68
+ // `agent: false` — a keep-alive socket outlives the scenario that opened it
69
+ const request = http.get({ port: UI_PORT, path, agent: false }, (response) => {
70
+ let body = ''
71
+
72
+ response.setEncoding('utf8')
73
+ response.on('data', (chunk: string) => (body += chunk))
74
+ response.on('end', () => {
75
+ resolve({
76
+ status: response.statusCode!,
77
+ headers: response.headers as Record<string, string>,
78
+ body
79
+ })
80
+ })
81
+ })
82
+
83
+ request.on('error', reject)
84
+ })
85
+ }
86
+
87
+ interface Response {
88
+ status: number
89
+ headers: Record<string, string>
90
+ body: string
91
+ }
@@ -0,0 +1,9 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ESNext",
4
+ "noEmit": true,
5
+ "moduleResolution": "node",
6
+ "experimentalDecorators": true,
7
+ "emitDecoratorMetadata": true
8
+ }
9
+ }
@@ -0,0 +1,93 @@
1
+ @ui
2
+ Feature: Publishing the UI
3
+
4
+ The values service serves the UI on its own port, out of the directory `ui` builds. The
5
+ mount path is baked into the bundle and forwarded by the ingress, so the server routes
6
+ relative to it.
7
+
8
+ Scenario: The root is the way in
9
+ Given the UI is published
10
+ When "/" is requested
11
+ Then the status is 302
12
+ And the "location" header is "/.configuration/"
13
+
14
+ Scenario: The UI answers at its mount path
15
+ Given the UI is published
16
+ When "/.configuration" is requested
17
+ Then the status is 200
18
+ And the body contains "the page"
19
+
20
+ Scenario: A trailing slash is the same resource
21
+ Given the UI is published
22
+ When "/.configuration/" is requested
23
+ Then the status is 200
24
+ And the body contains "the page"
25
+
26
+ Scenario: A query string does not change the resource
27
+ Given the UI is published
28
+ When "/.configuration?epoch=e1" is requested
29
+ Then the status is 200
30
+ And the body contains "the page"
31
+
32
+ Scenario: An unknown route is the page
33
+ The client router knows what routes there are; this server does not.
34
+
35
+ Given the UI is published
36
+ When "/.configuration/whatever" is requested
37
+ Then the status is 200
38
+ And the body contains "the page"
39
+
40
+ Scenario: A route that looks like a file is still a route
41
+ A component is named `namespace.component`, and the address of its configuration
42
+ carries that name — so an extension is no way to tell an asset from a route.
43
+
44
+ Given the UI is published
45
+ When "/.configuration/dummies.dummy/" is requested
46
+ Then the status is 200
47
+ And the body contains "the page"
48
+
49
+ Scenario: And with no trailing slash to give it away
50
+ Given the UI is published
51
+ When "/.configuration/dummies.dummy" is requested
52
+ Then the status is 200
53
+ And the body contains "the page"
54
+
55
+ Scenario: A hashed asset is served forever
56
+ Given the UI is published
57
+ When "/.configuration/_app/immutable/asset.js" is requested
58
+ Then the status is 200
59
+ And the "content-type" header is "text/javascript; charset=utf-8"
60
+ And the "cache-control" header is "public, max-age=31536000, immutable"
61
+
62
+ Scenario: An icon is held, but not forever
63
+ It is named for what it is rather than for its content, so it is asked about again.
64
+
65
+ Given the UI is published
66
+ When "/.configuration/favicon.ico" is requested
67
+ Then the status is 200
68
+ And the "cache-control" header is "public, max-age=86400"
69
+
70
+ Scenario: The page itself is never cached
71
+ Given the UI is published
72
+ When "/.configuration/" is requested
73
+ Then the "cache-control" header is "no-cache"
74
+
75
+ Scenario: A missing asset is missing
76
+ Given the UI is published
77
+ When "/.configuration/_app/immutable/absent.js" is requested
78
+ Then the status is 404
79
+
80
+ Scenario: The path of the values API is not served by the UI
81
+ Given the UI is published
82
+ When "/configuration/values/" is requested
83
+ Then the status is 404
84
+
85
+ Scenario: A path that only looks like the mount path is absent
86
+ Given the UI is published
87
+ When "/.configurationable" is requested
88
+ Then the status is 404
89
+
90
+ Scenario: Nothing outside the site is reachable
91
+ Given the UI is published
92
+ When "/.configuration/%2e%2e/%2e%2e/package.json" is requested
93
+ Then the status is 404
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "name": "@toa.io/extensions.configuration",
3
- "version": "1.0.0-alpha.28",
3
+ "version": "1.0.0-alpha.281",
4
+ "type": "module",
4
5
  "description": "Toa Configuration",
5
6
  "author": "temich <tema.gurtovoy@gmail.com>",
6
7
  "homepage": "https://github.com/toa-io/toa#readme",
@@ -17,16 +18,16 @@
17
18
  "access": "public"
18
19
  },
19
20
  "scripts": {
20
- "transpile": "tsc"
21
+ "transpile": "tsc && tsc -p ./components/configuration.values",
22
+ "build:ui": "npm --prefix ui ci && npm --prefix ui run build",
23
+ "prepack": "npm run build:ui",
24
+ "features": "TSX_TSCONFIG_PATH=features/steps/tsconfig.json NODE_OPTIONS=--import=tsx cucumber-js"
21
25
  },
22
- "jest": {
23
- "preset": "ts-jest",
24
- "testEnvironment": "node"
25
- },
26
- "gitHead": "89c3b7d1314e357c9e33df8d95f8d46954cd3bcf",
26
+ "gitHead": "2cf23f576c46efbe56c21427d877cef3e7ed98d4",
27
27
  "dependencies": {
28
- "@toa.io/core": "1.0.0-alpha.28",
29
- "@toa.io/generic": "1.0.0-alpha.28",
30
- "@toa.io/schemas": "1.0.0-alpha.28"
28
+ "@toa.io/core": "1.0.0-alpha.278",
29
+ "@toa.io/generic": "1.0.0-alpha.278",
30
+ "@toa.io/schemas": "1.0.0-alpha.278",
31
+ "openspan": "1.0.0-alpha.277"
31
32
  }
32
33
  }
package/readme.md CHANGED
@@ -11,8 +11,12 @@ namespace: dummies
11
11
 
12
12
  configuration:
13
13
  schema:
14
- foo: string
15
- bar: number
14
+ type: object
15
+ properties:
16
+ foo:
17
+ type: string
18
+ bar:
19
+ type: number
16
20
  defaults:
17
21
  foo: bar
18
22
  bar: 1
@@ -45,22 +49,28 @@ configuration:
45
49
  $ toa conceal configuration BAZ_VALUE=$ecr3t
46
50
  ```
47
51
 
48
- ---
52
+ ### Change at runtime
49
53
 
50
- ## Problem Definition
54
+ ```http
55
+ POST /configuration/values/dummies.dummy/ HTTP/1.1
56
+ authorization: Token ...
57
+ content-type: application/yaml
51
58
 
52
- - Components should be runnable in different deployment environments.
53
- - Some algorithm's parameters should be deployed secretly.
54
- - Components should be reusable in different contexts.
59
+ configuration:
60
+ foo: quux
61
+ bar: 2
62
+ ```
63
+
64
+ ---
55
65
 
56
66
  ## Manifest
57
67
 
58
- Component's configuration is declared using `configuration` manifest,
59
- containing `schema` and optionnaly `defaults` properties.
68
+ Component's configuration is declared using the `configuration` manifest, containing `schema`
69
+ and optionally `defaults` properties.
60
70
 
61
71
  ### Schema
62
72
 
63
- Configuration schema is declared with [COS](/libraries/concise).
73
+ Configuration schema is declared with [JSON Schema](https://json-schema.org).
64
74
 
65
75
  ```yaml
66
76
  # manifest.toa.yaml
@@ -69,30 +79,18 @@ namespace: dummies
69
79
 
70
80
  configuration:
71
81
  schema:
72
- foo: string
73
- bar: number
74
- ```
75
-
76
- > Introducing non-backward compatible changes to a configuration schema will result in a loss of
77
- > compatibility with existing contexts and deployment environments.
78
- > Therefore, configuration schema changes are subject to component versioning.
79
-
80
- If `configuration` object doesn't contain property `schema`, then it is considered to be schema.
81
-
82
- ```yaml
83
- # manifest.toa.yaml
84
- name: dummy
85
- namespace: dummies
86
-
87
- configuration:
88
- foo: string
89
- bar: number
82
+ type: object
83
+ properties:
84
+ foo:
85
+ type: string
86
+ bar:
87
+ type: number
90
88
  ```
91
89
 
92
90
  ### Defaults
93
91
 
94
- The default configuration value can be provided using the `defaults` property, which should conform
95
- to the configuration schema.
92
+ The default configuration value can be provided using the `defaults` property, which should
93
+ conform to the configuration schema.
96
94
 
97
95
  ```yaml
98
96
  # manifest.toa.yaml
@@ -101,27 +99,22 @@ namespace: dummies
101
99
 
102
100
  configuration:
103
101
  schema:
104
- foo: string
105
- bar: number
102
+ type: object
103
+ properties:
104
+ foo:
105
+ type: string
106
+ bar:
107
+ type: number
106
108
  defaults:
107
109
  foo: hello
108
110
  bar: 0
109
111
  ```
110
112
 
111
- #### Schema defaults hint
113
+ ### Epoch
112
114
 
113
- The configuration schema itself can contain default primitive values using the COS syntax.
114
-
115
- ```yaml
116
- # manifest.toa.yaml
117
- name: dummy
118
- namespace: dummies
119
-
120
- configuration:
121
- schema:
122
- foo: hello
123
- bar: 0
124
- ```
115
+ The configuration epoch of a component is the SHA-256 of its configuration schema, as
116
+ canonical JSON. A configuration object belongs to the epoch of the schema it was validated
117
+ against. A schema change is a new epoch.
125
118
 
126
119
  ## Annotation
127
120
 
@@ -136,9 +129,26 @@ configuration:
136
129
  bar@staging: 2
137
130
  ```
138
131
 
132
+ The annotated object is deployed as the defaults of the component for its epoch, in place of
133
+ the manifest `defaults`.
134
+
135
+ Every key names a component except `resources`, which is the values service's own: it deploys
136
+ like any other and states what it may take. A component actually named `resources` is written
137
+ with its namespace, `default.resources` — the bare form is only shorthand for that.
138
+
139
+ ```yaml
140
+ # context.toa.yaml
141
+ configuration:
142
+ resources:
143
+ cpu: [200m, 1000m]
144
+ memory: [200Mi, 500Mi]
145
+ dummies.dummy:
146
+ foo: bye
147
+ ```
148
+
139
149
  ## Secrets
140
150
 
141
- Configuration annotation top-level values which are uppercase strings prefixed with `$` considered as secrets.
151
+ Configuration values which are uppercase strings prefixed with `$` are considered as secrets.
142
152
 
143
153
  ```yaml
144
154
  # context.toa.yaml
@@ -158,14 +168,117 @@ Deployed kubernetes secret's name is predefined as `configuration`.
158
168
  $ toa conceal configuration STRIPE_API_KEY=xxxxxxxx
159
169
  ```
160
170
 
171
+ A secret is substituted in the component's process, from the variable
172
+ `TOA_CONFIGURATION__<NAME>` deployed to it. The values service holds and returns the
173
+ reference.
174
+
175
+ In the component, a secret is a `Secret` object: `unwrap()` returns the string, while
176
+ `toString()`, JSON and `util.inspect` give `<REDACTED>`.
177
+
178
+ ```javascript
179
+ function transition (input, entity, context) {
180
+ const key = context.configuration.apiKey.unwrap()
181
+
182
+ // ...
183
+ }
184
+ ```
185
+
186
+ A value a component reads as a secret is given as a reference: a plain string in its place
187
+ has no `unwrap`.
188
+
189
+ ## Values
190
+
191
+ Configuration values are held by the `configuration.values` component, deployed as the
192
+ `configuration-values` service. The service is deployed with the variable
193
+ `TOA_CONFIGURATION_VALUES`: the epoch, the schema and the defaults of every component
194
+ declaring configuration.
195
+
196
+ ```json
197
+ {
198
+ "dummies.dummy": {
199
+ "epoch": "3f2a…",
200
+ "schema": { "type": "object", "properties": { "foo": { "type": "string" } } },
201
+ "defaults": { "foo": "bye" }
202
+ }
203
+ }
204
+ ```
205
+
206
+ Configuration objects are immutable. Creating a configuration is creating a new object for
207
+ the component's current epoch; the latest object for a component and an epoch is the last
208
+ created one. Each object records its `originator`.
209
+
210
+ The configuration of a component for an epoch is:
211
+
212
+ 1. The latest object created for the component and the epoch;
213
+ 2. Otherwise, the deployed defaults, if the epoch is the deployed one;
214
+ 3. Otherwise, none.
215
+
216
+ ### Operations
217
+
218
+ - `get({ component, epoch? })`: the configuration with the schema it is checked against, as
219
+ `{ configuration, schema, epoch }`, or `null` when there is none. The epoch is the deployed
220
+ one when omitted; an epoch the deployment does not know has no `schema`.
221
+ - `fetch([{ component, epoch }])`: the same for several pairs at once, as
222
+ `[{ component, epoch, configuration }]`.
223
+ - `list()`: every component's configuration for its deployed epoch, by component name, as
224
+ `[{ component, epoch, schema, configuration }]`.
225
+ - `create({ component, configuration, originator })`: a new object for the component's
226
+ deployed epoch. The configuration must satisfy the schema. Errors: `UNKNOWN_COMPONENT`,
227
+ `INVALID_CONFIGURATION`.
228
+
229
+ Creating a configuration publishes the `configuration.values.created` event with the
230
+ object as stored.
231
+
232
+ ### Resources
233
+
234
+ | Method | Path | Role |
235
+ |--------|---------------------------------------|-------------------------------|
236
+ | `GET` | `/configuration/values/` | `system:configuration:get` |
237
+ | `GET` | `/configuration/values/:component/` | `system:configuration:get` |
238
+ | `POST` | `/configuration/values/:component/` | `system:configuration:create` |
239
+
240
+ `GET /configuration/values/` lists every component's configuration for its deployed epoch, by
241
+ component name, as `[{ component, epoch, schema, configuration }]`.
242
+
243
+ `GET /configuration/values/:component/` returns `{ configuration, schema, epoch }` for the
244
+ deployed epoch, `404` when there is none.
245
+
246
+ `POST` takes `{ configuration }`, records the Identity as the `originator`, and returns
247
+ `{ id, epoch }`. A configuration not satisfying the schema, or an unknown component, is
248
+ `422`.
249
+
250
+ ## UI
251
+
252
+ The values service serves a page listing the configured components and creating
253
+ configurations, mounted at `/.configuration` on port `8003`. Reading it needs the
254
+ `system:configuration:get` role, creating needs `system:configuration:create`.
255
+
256
+ The page is always published: unlike the introspection annotation, the configuration
257
+ annotation is the per-component values map and has nowhere to carry a switch.
258
+
161
259
  ## Aspect
162
260
 
163
261
  Component's configuration values are available as a well-known Aspect `configuration`.
164
262
 
165
263
  ```javascript
166
264
  function transition (input, entity, context) {
167
- const foo = context.configiuration.foo
265
+ const foo = context.configuration.foo
168
266
 
169
267
  // ...
170
268
  }
171
269
  ```
270
+
271
+ On start, a component requests its configuration for its epoch from the values service and
272
+ waits until there is one, reporting every fifth attempt. The schema is applied, and secrets
273
+ are substituted. After a configuration is created, the running component receives the new
274
+ object and takes it when its `_created` is later than that of the value it holds.
275
+
276
+ ### Local override
277
+
278
+ When the variable `TOA_CONFIGURATION_<NAMESPACE>_<NAME>` is set, the component's
279
+ configuration is the variable's value with the manifest `defaults` and the schema applied,
280
+ and the values service is not used.
281
+
282
+ ```shell
283
+ $ TOA_CONFIGURATION_DUMMIES_DUMMY='{"foo":"local"}' toa run components/dummy
284
+ ```
@@ -1 +1,29 @@
1
- <object>
1
+ # context.toa.yaml configuration annotation
2
+ #
3
+ # Every key names a component and holds that component's values, except `resources`, which
4
+ # is the values service's own: it deploys like any other and has to state what it may take.
5
+ # A component actually named `resources` is written with its namespace, `default.resources`
6
+ # — the bare form is only shorthand for that.
7
+ properties:
8
+ resources:
9
+ # `null` deploys the service without any, which every deployment has to state one way
10
+ # or the other — see `migrations/263.md`.
11
+ type: object
12
+ nullable: true
13
+ properties:
14
+ cpu:
15
+ type: array
16
+ items:
17
+ type: string
18
+ minItems: 2
19
+ maxItems: 2
20
+ memory:
21
+ type: array
22
+ items:
23
+ type: string
24
+ minItems: 2
25
+ maxItems: 2
26
+ patternProperties:
27
+ ^(?!resources$).+$:
28
+ type: object
29
+ type: object
@@ -1,2 +1,9 @@
1
- schema: object
2
- defaults?: object
1
+ properties:
2
+ schema:
3
+ type: object
4
+ defaults:
5
+ type: object
6
+ additionalProperties: false
7
+ type: object
8
+ required:
9
+ - schema
@@ -1,15 +1,95 @@
1
- import { Aspect } from './Aspect'
1
+ import { it, beforeEach, afterEach, mock } from 'node:test'
2
+ import assert from 'node:assert/strict'
3
+ import { isDeepStrictEqual } from 'node:util'
2
4
 
3
- it('should return value', async () => {
4
- const configuration = {
5
- foo: 'bar',
6
- bar: {
7
- baz: 'quux'
5
+ import { Connector, Locator } from '@toa.io/core'
6
+ import { generate } from 'randomstring'
7
+ import { Aspect } from './Aspect.js'
8
+ import type { Client, Listener } from './Client.js'
9
+ import type { Manifest } from './manifest.js'
10
+
11
+ class Fake extends Connector {
12
+ public readonly fetch = mock.fn(async () => ({ configuration: { foo: 'served' }, created: 5 }))
13
+ public readonly subscribe = mock.fn()
14
+ public readonly unsubscribe = mock.fn()
15
+ }
16
+
17
+ const manifest: Manifest = {
18
+ schema: {
19
+ type: 'object',
20
+ properties: {
21
+ foo: { type: 'string' },
22
+ bar: { type: 'object', properties: { baz: { type: 'string' } }, default: { baz: 'quux' } }
8
23
  }
9
24
  }
25
+ }
26
+
27
+ let locator: Locator
28
+
29
+ beforeEach(() => {
30
+ locator = new Locator(generate(), generate())
31
+ })
32
+
33
+ afterEach(() => {
34
+ delete process.env['TOA_CONFIGURATION_' + locator.uppercase]
35
+ })
36
+
37
+ it('should be named', async () => {
38
+ assert.deepStrictEqual(new Aspect(locator, manifest, null).name, 'configuration')
39
+ })
40
+
41
+ it('should resolve locally without a client', async () => {
42
+ process.env['TOA_CONFIGURATION_' + locator.uppercase] = JSON.stringify({ foo: 'local' })
43
+
44
+ const aspect = new Aspect(locator, manifest, null)
45
+
46
+ await aspect.connect()
47
+
48
+ assert.deepStrictEqual(aspect.invoke(), { foo: 'local', bar: { baz: 'quux' } })
49
+ assert.deepStrictEqual(aspect.invoke(['foo']), 'local')
50
+ assert.deepStrictEqual(aspect.invoke(['bar', 'baz']), 'quux')
51
+ })
52
+
53
+ it('should fetch from the client and follow it', async () => {
54
+ const client = new Fake()
55
+ const aspect = new Aspect(locator, manifest, client as unknown as Client)
56
+
57
+ await aspect.connect()
58
+
59
+ assert.deepStrictEqual(client.connected, true)
60
+ assert.strictEqual(client.fetch.mock.callCount(), 1)
61
+
62
+ const [component, epoch] = client.fetch.mock.calls[0].arguments as unknown as [string, string]
63
+
64
+ assert.deepStrictEqual(component, locator.id)
65
+ assert.match(epoch, /^[a-f0-9]{64}$/)
66
+ assert.deepStrictEqual(aspect.invoke(), { foo: 'served', bar: { baz: 'quux' } })
67
+
68
+ assert.ok(client.subscribe.mock.calls.some((call: any) => call.arguments.length === 3 && isDeepStrictEqual(call.arguments[0], component) && isDeepStrictEqual(call.arguments[1], epoch) && typeof call.arguments[2] === 'function'))
69
+
70
+ const listener = client.subscribe.mock.calls[0].arguments[2] as Listener
71
+
72
+ listener({ configuration: { foo: 'updated' }, created: 6 })
73
+
74
+ assert.deepStrictEqual(aspect.invoke(['foo']), 'updated')
75
+
76
+ // what is not newer than the held value is left alone
77
+ listener({ configuration: { foo: 'stale' }, created: 6 })
78
+ listener({ configuration: { foo: 'older' }, created: 4 })
79
+
80
+ assert.deepStrictEqual(aspect.invoke(['foo']), 'updated')
81
+
82
+ // a value that does not fit keeps the previous one
83
+ listener({ configuration: { foo: { nested: true } }, created: 7 })
84
+
85
+ assert.deepStrictEqual(aspect.invoke(['foo']), 'updated')
86
+
87
+ // and the one after it still applies
88
+ listener({ configuration: { foo: 'latest' }, created: 8 })
89
+
90
+ assert.deepStrictEqual(aspect.invoke(['foo']), 'latest')
10
91
 
11
- const aspect = new Aspect(configuration)
92
+ await aspect.disconnect()
12
93
 
13
- expect(aspect.invoke(['foo'])).toStrictEqual(configuration.foo)
14
- expect(aspect.invoke(['bar', 'baz'])).toStrictEqual(configuration.bar.baz)
94
+ assert.ok(client.unsubscribe.mock.calls.some((call: any) => call.arguments.length === 3 && isDeepStrictEqual(call.arguments[0], component) && isDeepStrictEqual(call.arguments[1], epoch) && isDeepStrictEqual(call.arguments[2], listener)))
15
95
  })