@toa.io/userland 1.0.0-alpha.33 → 1.0.0-alpha.331
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 +358 -0
- package/example/components/echo/manifest.toa.yaml +15 -5
- package/example/components/echo/operations/get.js +1 -5
- package/example/components/echo/operations/resolve.js +1 -5
- package/example/components/echo/operations/signal.js +3 -7
- package/example/components/math.calculations/manifest.toa.yaml +19 -6
- package/example/components/math.calculations/operations/add.js +1 -5
- package/example/components/math.calculations/operations/increment.js +1 -5
- package/example/components/math.proxy/manifest.toa.yaml +8 -3
- package/example/components/math.proxy/operations/add.js +1 -5
- package/example/components/tea.pots/manifest.toa.yaml +29 -9
- package/example/components/tea.pots/operations/same.js +1 -5
- package/example/components/tea.pots/receivers/store.orders.created.js +1 -5
- package/example/components/web/manifest.toa.yaml +2 -4
- package/example/components/web/operations/get.js +3 -8
- package/example/context.toa.yaml +2 -7
- package/example/stage/call.test.js +17 -10
- package/example/stage/events.test.js +63 -16
- package/example/stage/invoke.test.js +12 -9
- package/package.json +19 -13
- package/readme.md +58 -1
- package/stage/package.json +1 -0
- package/stage/src/component.js +3 -7
- package/stage/src/composition.js +3 -7
- package/stage/src/index.js +9 -16
- package/stage/src/manifest.js +2 -6
- package/stage/src/map.js +15 -0
- package/stage/src/quiescence.js +16 -0
- package/stage/src/remote.js +11 -8
- package/stage/src/service.js +11 -10
- package/stage/src/shutdown.js +16 -6
- package/stage/src/state.js +2 -4
- package/stage/src/workload.js +53 -0
- package/stage/test/boot.mock.js +10 -11
- package/stage/test/component.test.js +20 -10
- package/stage/test/composition.test.js +11 -9
- package/stage/test/manifest.test.js +15 -8
- package/stage/test/remote.test.js +14 -12
- package/stage/test/shutdown.test.js +28 -15
- package/stage/test/state.mock.js +5 -6
- package/stage/test/state.test.js +13 -12
- package/stage/types/index.d.ts +21 -8
- package/stage/types/state.d.ts +3 -4
- package/template/_gitignore +3 -0
- package/template/components/hello/manifest.toa.yaml +22 -0
- package/template/components/hello/operations/greet.ts +5 -0
- package/template/components/notes/manifest.toa.yaml +25 -0
- package/template/context.toa.yaml +29 -0
- package/template/cucumber.js +4 -0
- package/template/docker-compose.yaml +62 -0
- package/template/ecosystem.config.js +29 -0
- package/template/features/hello.feature +14 -0
- package/template/features/notes.feature +29 -0
- package/template/features/steps/application.ts +22 -0
- package/template/features/steps/http.ts +19 -0
- package/template/package.json +28 -0
- package/template/tsconfig.json +15 -0
|
@@ -1,51 +1,98 @@
|
|
|
1
|
-
|
|
1
|
+
import { it, before, after } from 'node:test'
|
|
2
|
+
import assert from 'node:assert/strict'
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
|
|
4
|
+
import { resolve } from 'node:path'
|
|
5
|
+
import * as boot from '@toa.io/boot'
|
|
6
|
+
import { Connector, Locator } from '@toa.io/core'
|
|
7
|
+
import { timeout } from '@toa.io/generic'
|
|
8
|
+
import * as stage from '@toa.io/userland/stage'
|
|
5
9
|
|
|
6
|
-
const
|
|
7
|
-
const root = resolve(__dirname, '../components')
|
|
10
|
+
const root = resolve(import.meta.dirname, '../components')
|
|
8
11
|
|
|
9
12
|
let remote
|
|
13
|
+
let emitter
|
|
14
|
+
let consumer
|
|
10
15
|
|
|
11
|
-
|
|
16
|
+
before(async () => {
|
|
12
17
|
process.env.TOA_DEV = '1'
|
|
18
|
+
process.env.TOA_CONFIGURATION_TEA_POTS = '{}'
|
|
19
|
+
|
|
20
|
+
// the outbox pumps on a tick; at the default five seconds a test would end before it ran
|
|
21
|
+
process.env.TOA_OUTBOX_INTERVAL = '100'
|
|
13
22
|
|
|
14
23
|
const path = resolve(root, 'tea.pots')
|
|
15
24
|
|
|
16
25
|
await stage.composition([path])
|
|
17
26
|
|
|
18
27
|
remote = await stage.remote('tea.pots')
|
|
28
|
+
|
|
29
|
+
// what `store.orders` would emit, sent the way it would send it
|
|
30
|
+
emitter = await boot.bindings.emit(AMQP, new Locator('orders', 'store'), 'created')
|
|
31
|
+
|
|
32
|
+
await emitter.connect()
|
|
19
33
|
})
|
|
20
34
|
|
|
21
|
-
|
|
35
|
+
after(async () => {
|
|
36
|
+
await consumer?.disconnect()
|
|
37
|
+
await emitter.disconnect()
|
|
22
38
|
await stage.shutdown()
|
|
23
39
|
|
|
24
40
|
delete process.env.TOA_DEV
|
|
41
|
+
delete process.env.TOA_CONFIGURATION_TEA_POTS
|
|
42
|
+
delete process.env.TOA_OUTBOX_INTERVAL
|
|
25
43
|
})
|
|
26
44
|
|
|
27
45
|
it('should receive event', async () => {
|
|
28
46
|
const created = await remote.invoke('transit', { input: { material: 'glass' } })
|
|
29
47
|
const id = created.id
|
|
30
|
-
const payload = { pot: id }
|
|
31
|
-
const message = { payload }
|
|
32
48
|
const request = { query: { id } }
|
|
33
49
|
|
|
34
|
-
await
|
|
50
|
+
await emitter.emit({ payload: { pot: id } })
|
|
51
|
+
|
|
52
|
+
// the receiver books the pot on delivery, which is asynchronous
|
|
53
|
+
const deadline = Date.now() + 5000
|
|
35
54
|
|
|
36
|
-
|
|
55
|
+
let reply
|
|
37
56
|
|
|
38
|
-
|
|
57
|
+
do {
|
|
58
|
+
await timeout(50)
|
|
59
|
+
|
|
60
|
+
reply = await remote.invoke('observe', request)
|
|
61
|
+
} while (reply.booked !== true && Date.now() < deadline)
|
|
62
|
+
|
|
63
|
+
assert.deepStrictEqual(reply.booked, true)
|
|
39
64
|
})
|
|
40
65
|
|
|
41
66
|
it('should emit event', async () => {
|
|
42
|
-
expect.assertions(1)
|
|
43
|
-
|
|
44
67
|
const material = 'steel'
|
|
45
68
|
|
|
46
|
-
|
|
47
|
-
|
|
69
|
+
const received = new Promise((resolve) => {
|
|
70
|
+
consumer = boot.receive('tea.pots.created', new Subscriber(resolve))
|
|
48
71
|
})
|
|
49
72
|
|
|
73
|
+
consumer = await consumer
|
|
74
|
+
|
|
75
|
+
await consumer.connect()
|
|
50
76
|
await remote.invoke('transit', { input: { material } })
|
|
77
|
+
|
|
78
|
+
const payload = await received
|
|
79
|
+
|
|
80
|
+
assert.deepStrictEqual(payload.material, material)
|
|
51
81
|
})
|
|
82
|
+
|
|
83
|
+
/** What the event consumer hands deliveries to. */
|
|
84
|
+
class Subscriber extends Connector {
|
|
85
|
+
#handler
|
|
86
|
+
|
|
87
|
+
constructor(handler) {
|
|
88
|
+
super()
|
|
89
|
+
|
|
90
|
+
this.#handler = handler
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async receive(message) {
|
|
94
|
+
this.#handler(message.payload)
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const AMQP = '@toa.io/bindings.amqp'
|
|
@@ -1,22 +1,25 @@
|
|
|
1
|
-
|
|
1
|
+
import { it, before, after } from 'node:test'
|
|
2
|
+
import assert from 'node:assert/strict'
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
|
|
4
|
+
import { resolve } from 'node:path'
|
|
5
|
+
import * as stage from '@toa.io/userland/stage'
|
|
5
6
|
|
|
6
|
-
const root = resolve(
|
|
7
|
+
const root = resolve(import.meta.dirname, '../components')
|
|
7
8
|
|
|
8
|
-
/** @type {toa.core.Component} */
|
|
9
|
+
/** @type {import('@toa.io/core').Component} */
|
|
9
10
|
let component
|
|
10
11
|
|
|
11
|
-
|
|
12
|
+
before(async () => {
|
|
12
13
|
process.env.TOA_DEV = '1'
|
|
14
|
+
process.env.TOA_CONFIGURATION_DEFAULT_ECHO = '{}'
|
|
15
|
+
process.env.TOA_CONFIGURATION_TEA_POTS = '{}'
|
|
13
16
|
|
|
14
17
|
const path = resolve(root, 'math.calculations')
|
|
15
18
|
|
|
16
19
|
component = await stage.component(path)
|
|
17
20
|
})
|
|
18
21
|
|
|
19
|
-
|
|
22
|
+
after(async () => {
|
|
20
23
|
await stage.shutdown()
|
|
21
24
|
|
|
22
25
|
delete process.env.TOA_DEV
|
|
@@ -28,6 +31,6 @@ it('should invoke', async () => {
|
|
|
28
31
|
|
|
29
32
|
const reply = await component.invoke('add', { input: { a, b } })
|
|
30
33
|
|
|
31
|
-
|
|
32
|
-
|
|
34
|
+
assert.strictEqual(reply.exception, undefined)
|
|
35
|
+
assert.deepStrictEqual(reply.output, a + b)
|
|
33
36
|
})
|
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@toa.io/userland",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.331",
|
|
4
|
+
"type": "module",
|
|
4
5
|
"description": "Toa development kit",
|
|
5
6
|
"homepage": "https://toa.io",
|
|
6
7
|
"author": {
|
|
@@ -16,23 +17,28 @@
|
|
|
16
17
|
"url": "https://github.com/toa-io/toa/issues"
|
|
17
18
|
},
|
|
18
19
|
"engines": {
|
|
19
|
-
"node": ">=
|
|
20
|
+
"node": ">= 24.0.0"
|
|
20
21
|
},
|
|
21
22
|
"publishConfig": {
|
|
22
23
|
"access": "public"
|
|
23
24
|
},
|
|
24
25
|
"main": "src/index.js",
|
|
25
|
-
"
|
|
26
|
+
"exports": {
|
|
27
|
+
".": "./src/index.js",
|
|
28
|
+
"./stage": {
|
|
29
|
+
"types": "./stage/types/index.d.ts",
|
|
30
|
+
"default": "./stage/src/index.js"
|
|
31
|
+
},
|
|
32
|
+
"./package.json": "./package.json"
|
|
33
|
+
},
|
|
26
34
|
"dependencies": {
|
|
27
|
-
"@toa.io/
|
|
28
|
-
"@toa.io/
|
|
29
|
-
"@toa.io/
|
|
30
|
-
"@toa.io/generic": "1.0.0-alpha.
|
|
31
|
-
"@toa.io/norm": "1.0.0-alpha.
|
|
32
|
-
"@toa.io/schemas": "1.0.0-alpha.
|
|
33
|
-
"@toa.io/storages.null": "0.
|
|
34
|
-
"@toa.io/yaml": "1.0.0-alpha.33",
|
|
35
|
-
"tap": "16.3.4"
|
|
35
|
+
"@toa.io/agent": "1.0.0-alpha.330",
|
|
36
|
+
"@toa.io/boot": "1.0.0-alpha.331",
|
|
37
|
+
"@toa.io/core": "1.0.0-alpha.330",
|
|
38
|
+
"@toa.io/generic": "1.0.0-alpha.330",
|
|
39
|
+
"@toa.io/norm": "1.0.0-alpha.331",
|
|
40
|
+
"@toa.io/schemas": "1.0.0-alpha.330",
|
|
41
|
+
"@toa.io/storages.null": "1.0.0-alpha.330"
|
|
36
42
|
},
|
|
37
|
-
"gitHead": "
|
|
43
|
+
"gitHead": "e7020d160910ca4f0c2ff9040b3cbb4fca8ea41f"
|
|
38
44
|
}
|
package/readme.md
CHANGED
|
@@ -1,7 +1,64 @@
|
|
|
1
1
|
# Toa development kit
|
|
2
2
|
|
|
3
3
|
- [Stage](./stage): integration tests framework (Node.js)
|
|
4
|
+
- [Template](#template): the application `toa create` writes
|
|
4
5
|
|
|
5
6
|
See [example](./example).
|
|
6
7
|
|
|
7
|
-
|
|
8
|
+
## Template
|
|
9
|
+
|
|
10
|
+
[`toa create <name>`](/runtime/cli/readme.md#create) writes an application into an empty
|
|
11
|
+
directory. Running it takes [Docker](https://docs.docker.com/get-docker/), for the services it
|
|
12
|
+
connects to, and [PM2](https://pm2.keymetrics.io), which keeps its processes:
|
|
13
|
+
|
|
14
|
+
```shell
|
|
15
|
+
$ npm install -g pm2
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
```text
|
|
19
|
+
package.json
|
|
20
|
+
tsconfig.json
|
|
21
|
+
docker-compose.yaml
|
|
22
|
+
ecosystem.config.js
|
|
23
|
+
context.toa.yaml
|
|
24
|
+
cucumber.js
|
|
25
|
+
components/
|
|
26
|
+
hello/
|
|
27
|
+
manifest.toa.yaml
|
|
28
|
+
operations/greet.ts
|
|
29
|
+
notes/
|
|
30
|
+
manifest.toa.yaml
|
|
31
|
+
features/
|
|
32
|
+
hello.feature
|
|
33
|
+
notes.feature
|
|
34
|
+
steps/
|
|
35
|
+
application.ts
|
|
36
|
+
http.ts
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
| File | Is |
|
|
40
|
+
| --------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
41
|
+
| `context.toa.yaml` | The Context: the application's name and where its infrastructure is. It is the least a Context holds. |
|
|
42
|
+
| `docker-compose.yaml` | RabbitMQ, MongoDB as a replica set of one, and Redis, on their conventional ports, with the credentials `toa env --dev` writes. |
|
|
43
|
+
| `ecosystem.config.js` | The two processes PM2 keeps: `sys`, what Toa serves beside an application — its gateway, configuration and introspection — and `app`, the components. |
|
|
44
|
+
| `components/hello` | A component with code and no state: `greet`, a computation, served at `GET /hello/`. |
|
|
45
|
+
| `components/notes` | A component with state and no code: an entity, `create` forwarded to the operation every component inherits, served at `/notes/`. |
|
|
46
|
+
| `features` | A scenario for each component, sent over HTTP. The steps start the components with the [stage](./stage), in the process of the test; `sys` is running already. |
|
|
47
|
+
| `package.json` | The packages of Toa at the version that created the application, and the scripts below. |
|
|
48
|
+
| `tsconfig.json` | What `tsc` checks the components and the steps with. |
|
|
49
|
+
|
|
50
|
+
| Script | Runs |
|
|
51
|
+
| ------------------- | --------------------------------------------------------------------------------------------------------- |
|
|
52
|
+
| `npm install` | Installs the packages, then `toa npm`, which adds what the declarations require beyond them. |
|
|
53
|
+
| `npm run dock` | The services, and waits until they are ready. |
|
|
54
|
+
| `npm run env` | `toa types`, `toa env --dev` and `toa map`. Run it again when a manifest or a component's source changes. |
|
|
55
|
+
| `npm run sys` | `sys` alone: what the scenarios need, since they start the components themselves. |
|
|
56
|
+
| `npm run features` | The scenarios. |
|
|
57
|
+
| `npm start` | `sys` and `app`: the application, with its gateway on `http://localhost:8000`. |
|
|
58
|
+
| `npm run restart` | `npm run env`, and both processes again. |
|
|
59
|
+
| `npm stop` | Stops and forgets both. |
|
|
60
|
+
| `npm run typecheck` | `tsc`. |
|
|
61
|
+
|
|
62
|
+
`pm2 logs` shows what the processes write, and `pm2 ls` whether they run.
|
|
63
|
+
|
|
64
|
+
The routes are open to anyone, which is right for a first run and for nothing else.
|
package/stage/package.json
CHANGED
package/stage/src/component.js
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const boot = require('@toa.io/boot')
|
|
4
|
-
const { state } = require('./state')
|
|
1
|
+
import * as boot from '@toa.io/boot'
|
|
2
|
+
import { state } from './state.js'
|
|
5
3
|
|
|
6
4
|
/** @type {toa.stage.Component} */
|
|
7
|
-
const component = async (path, options) => {
|
|
5
|
+
export const component = async (path, options) => {
|
|
8
6
|
const manifest = await boot.manifest(path, options)
|
|
9
7
|
const component = await boot.component(manifest)
|
|
10
8
|
|
|
@@ -14,5 +12,3 @@ const component = async (path, options) => {
|
|
|
14
12
|
|
|
15
13
|
return component
|
|
16
14
|
}
|
|
17
|
-
|
|
18
|
-
exports.component = component
|
package/stage/src/composition.js
CHANGED
|
@@ -1,15 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const boot = require('@toa.io/boot')
|
|
4
|
-
const { state } = require('./state')
|
|
1
|
+
import * as boot from '@toa.io/boot'
|
|
2
|
+
import { state } from './state.js'
|
|
5
3
|
|
|
6
4
|
/** @type {toa.stage.Composition} */
|
|
7
|
-
const composition = async (paths, options) => {
|
|
5
|
+
export const composition = async (paths, options) => {
|
|
8
6
|
const composition = await boot.composition(paths, options)
|
|
9
7
|
|
|
10
8
|
await composition.connect()
|
|
11
9
|
|
|
12
10
|
state.compositions.push(composition)
|
|
13
11
|
}
|
|
14
|
-
|
|
15
|
-
exports.composition = composition
|
package/stage/src/index.js
CHANGED
|
@@ -1,16 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
exports.manifest = manifest
|
|
11
|
-
exports.component = component
|
|
12
|
-
exports.composition = composition
|
|
13
|
-
exports.compose = composition
|
|
14
|
-
exports.serve = service
|
|
15
|
-
exports.remote = remote
|
|
16
|
-
exports.shutdown = shutdown
|
|
1
|
+
export { manifest } from './manifest.js'
|
|
2
|
+
export { map } from './map.js'
|
|
3
|
+
export { component } from './component.js'
|
|
4
|
+
export { composition, composition as compose } from './composition.js'
|
|
5
|
+
export { workload } from './workload.js'
|
|
6
|
+
export { service as serve } from './service.js'
|
|
7
|
+
export { remote } from './remote.js'
|
|
8
|
+
export { halt, restore } from './quiescence.js'
|
|
9
|
+
export { shutdown } from './shutdown.js'
|
package/stage/src/manifest.js
CHANGED
|
@@ -1,12 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const boot = require('@toa.io/boot')
|
|
1
|
+
import * as boot from '@toa.io/boot'
|
|
4
2
|
|
|
5
3
|
/**
|
|
6
4
|
* @type {toa.stage.Manifest}
|
|
7
5
|
*/
|
|
8
|
-
const manifest = async (path) => {
|
|
6
|
+
export const manifest = async (path) => {
|
|
9
7
|
return boot.manifest(path)
|
|
10
8
|
}
|
|
11
|
-
|
|
12
|
-
exports.manifest = manifest
|
package/stage/src/map.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import * as boot from '@toa.io/boot'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* What this process is given about the components it calls: the contract of the version each of
|
|
5
|
+
* them runs.
|
|
6
|
+
*
|
|
7
|
+
* A composition staged here is given none unless a test states one, and a component the map
|
|
8
|
+
* states nothing of is asked what it provides — which is what a run outside a deployment gets.
|
|
9
|
+
*
|
|
10
|
+
* @param {Record<string, import('@toa.io/core').Contract> | undefined} contracts
|
|
11
|
+
* @returns {void}
|
|
12
|
+
*/
|
|
13
|
+
export const map = (contracts) => {
|
|
14
|
+
boot.map.use(contracts)
|
|
15
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { state } from './state.js'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Stops what everything staged does of its own accord, holding every connection open — what a
|
|
5
|
+
* process does when it is told to go quiet. A component's `stop` run command runs here.
|
|
6
|
+
*/
|
|
7
|
+
export const halt = async () => {
|
|
8
|
+
for (const composition of state.compositions) await composition.halt()
|
|
9
|
+
for (const workload of state.workloads) await workload.halt()
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** Starts it again, running the `resume` command. */
|
|
13
|
+
export const restore = async () => {
|
|
14
|
+
for (const composition of state.compositions) await composition.restore()
|
|
15
|
+
for (const workload of state.workloads) await workload.restore()
|
|
16
|
+
}
|
package/stage/src/remote.js
CHANGED
|
@@ -1,19 +1,17 @@
|
|
|
1
|
-
|
|
1
|
+
import * as boot from '@toa.io/boot'
|
|
2
|
+
import { Locator } from '@toa.io/core'
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
const { Locator } = require('@toa.io/core')
|
|
5
|
-
|
|
6
|
-
const { state } = require('./state')
|
|
4
|
+
import { state } from './state.js'
|
|
7
5
|
|
|
8
6
|
/** @type {toa.stage.Remote} */
|
|
9
|
-
const remote = async (id) => {
|
|
7
|
+
export const remote = async (id) => {
|
|
10
8
|
const segments = id.split('.')
|
|
11
9
|
|
|
12
10
|
if (segments.length === 1) segments.unshift('default')
|
|
13
11
|
|
|
14
12
|
const [namespace, name] = segments
|
|
15
13
|
const locator = new Locator(name, namespace)
|
|
16
|
-
const remote = await boot.remote(locator)
|
|
14
|
+
const remote = await boot.remote(locator, SOURCE)
|
|
17
15
|
|
|
18
16
|
await remote.connect()
|
|
19
17
|
|
|
@@ -22,4 +20,9 @@ const remote = async (id) => {
|
|
|
22
20
|
return remote
|
|
23
21
|
}
|
|
24
22
|
|
|
25
|
-
|
|
23
|
+
/**
|
|
24
|
+
* A feature suite calls components the way a service does, so it says so: without this
|
|
25
|
+
* every call it makes is attributed to an unidentified origin, and a test run leaves
|
|
26
|
+
* `unknown` on the application's map.
|
|
27
|
+
*/
|
|
28
|
+
const SOURCE = { service: 'features' }
|
package/stage/src/service.js
CHANGED
|
@@ -1,14 +1,17 @@
|
|
|
1
|
-
|
|
1
|
+
import { createRequire } from 'node:module'
|
|
2
|
+
import { pathToFileURL } from 'node:url'
|
|
3
|
+
import * as boot from '@toa.io/boot'
|
|
4
|
+
import { state } from './state.js'
|
|
5
|
+
import { shortcuts } from '@toa.io/norm'
|
|
2
6
|
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
const { shortcuts } = require('@toa.io/norm')
|
|
7
|
+
// a shortcut resolves to a package name, which is not a path
|
|
8
|
+
const require = createRequire(import.meta.url)
|
|
6
9
|
|
|
7
|
-
const service = async (ref) => {
|
|
10
|
+
export const service = async (ref) => {
|
|
8
11
|
const path = shortcuts.resolve(ref)
|
|
9
|
-
const { Factory } = require(path)
|
|
10
|
-
const factory = new Factory(boot)
|
|
11
|
-
const service = factory.service()
|
|
12
|
+
const { Factory } = await import(pathToFileURL(require.resolve(path)).href)
|
|
13
|
+
const factory = new Factory(boot.host())
|
|
14
|
+
const service = await factory.service()
|
|
12
15
|
|
|
13
16
|
await service.connect()
|
|
14
17
|
|
|
@@ -16,5 +19,3 @@ const service = async (ref) => {
|
|
|
16
19
|
|
|
17
20
|
return service
|
|
18
21
|
}
|
|
19
|
-
|
|
20
|
-
exports.service = service
|
package/stage/src/shutdown.js
CHANGED
|
@@ -1,18 +1,28 @@
|
|
|
1
|
-
|
|
1
|
+
import * as boot from '@toa.io/boot'
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
import { state } from './state.js'
|
|
4
|
+
import { map } from './map.js'
|
|
4
5
|
|
|
5
6
|
/** @type {toa.stage.Shutdown} */
|
|
6
|
-
const shutdown = async () => {
|
|
7
|
+
export const shutdown = async () => {
|
|
7
8
|
const components = state.components.map((component) => component.disconnect())
|
|
8
9
|
const compositions = state.compositions.map((composition) => composition.disconnect())
|
|
10
|
+
const workloads = state.workloads.map((workload) => workload.disconnect())
|
|
9
11
|
const services = state.services.map((service) => service.disconnect())
|
|
10
12
|
const remotes = state.remotes.map((remote) => remote.disconnect())
|
|
11
|
-
const disconnections = [
|
|
13
|
+
const disconnections = [
|
|
14
|
+
...components,
|
|
15
|
+
...compositions,
|
|
16
|
+
...workloads,
|
|
17
|
+
...services,
|
|
18
|
+
...remotes
|
|
19
|
+
]
|
|
12
20
|
|
|
13
21
|
await Promise.all(disconnections)
|
|
14
22
|
|
|
23
|
+
// whatever a test stated is not carried into the next one, and what it composed is gone
|
|
24
|
+
map(undefined)
|
|
25
|
+
boot.map.forget()
|
|
26
|
+
|
|
15
27
|
state.reset()
|
|
16
28
|
}
|
|
17
|
-
|
|
18
|
-
exports.shutdown = shutdown
|
package/stage/src/state.js
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const state = {}
|
|
1
|
+
export const state = {}
|
|
4
2
|
|
|
5
3
|
const reset = () => {
|
|
6
4
|
Object.assign(state, {
|
|
7
5
|
reset,
|
|
8
6
|
components: [],
|
|
9
7
|
compositions: [],
|
|
8
|
+
workloads: [],
|
|
10
9
|
remotes: [],
|
|
11
10
|
services: []
|
|
12
11
|
})
|
|
@@ -15,4 +14,3 @@ const reset = () => {
|
|
|
15
14
|
reset()
|
|
16
15
|
|
|
17
16
|
/** @type {toa.stage.State} */
|
|
18
|
-
exports.state = state
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { createRequire } from 'node:module'
|
|
2
|
+
import { pathToFileURL } from 'node:url'
|
|
3
|
+
import * as boot from '@toa.io/boot'
|
|
4
|
+
import { Connector } from '@toa.io/core'
|
|
5
|
+
import { shortcuts } from '@toa.io/norm'
|
|
6
|
+
import { state } from './state.js'
|
|
7
|
+
|
|
8
|
+
// a shortcut resolves to a package name, which is not a path
|
|
9
|
+
const require = createRequire(import.meta.url)
|
|
10
|
+
|
|
11
|
+
/** The services, made against this process rather than against no process. */
|
|
12
|
+
const create = async (references, workload) => {
|
|
13
|
+
const services = []
|
|
14
|
+
|
|
15
|
+
for (const reference of references) {
|
|
16
|
+
const path = shortcuts.resolve(reference)
|
|
17
|
+
const { Factory } = await import(pathToFileURL(require.resolve(path)).href)
|
|
18
|
+
const service = await new Factory(boot.host(workload)).service()
|
|
19
|
+
|
|
20
|
+
if (service !== null) services.push(service)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return services
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* A process, as `toa compose` runs one: the composition behind a gate, whatever services it
|
|
28
|
+
* was told to run beside it, and whatever the extensions keep in a process — the readiness
|
|
29
|
+
* probe, the ear a halt arrives at.
|
|
30
|
+
*
|
|
31
|
+
* `composition` is the other way to boot the same components, and the difference is the point
|
|
32
|
+
* of this one: what belongs to a process is here and not there. So is what a halt takes,
|
|
33
|
+
* which is why a service that has to be halted is run from here rather than staged apart.
|
|
34
|
+
*/
|
|
35
|
+
export const workload = async (paths, options, services = []) => {
|
|
36
|
+
const workload = new boot.Workload(async (workload) => {
|
|
37
|
+
const composition = workload.gate(async () => await boot.composition(paths, options))
|
|
38
|
+
|
|
39
|
+
if (services.length === 0) return composition
|
|
40
|
+
|
|
41
|
+
const root = new Connector()
|
|
42
|
+
|
|
43
|
+
root.depends([composition, ...(await create(services, workload))])
|
|
44
|
+
|
|
45
|
+
return root
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
await workload.connect()
|
|
49
|
+
|
|
50
|
+
state.workloads.push(workload)
|
|
51
|
+
|
|
52
|
+
return workload
|
|
53
|
+
}
|
package/stage/test/boot.mock.js
CHANGED
|
@@ -1,16 +1,15 @@
|
|
|
1
|
-
|
|
1
|
+
import { mock } from 'node:test'
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
import { generate } from 'randomstring'
|
|
4
4
|
|
|
5
5
|
const connector = () => ({
|
|
6
|
-
connect:
|
|
7
|
-
disconnect:
|
|
8
|
-
link:
|
|
6
|
+
connect: mock.fn(),
|
|
7
|
+
disconnect: mock.fn(),
|
|
8
|
+
link: mock.fn()
|
|
9
9
|
})
|
|
10
10
|
|
|
11
|
-
const
|
|
12
|
-
const
|
|
13
|
-
const
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
module.exports = { manifest, component, composition, remote }
|
|
11
|
+
export const map = { use: mock.fn(), forget: mock.fn() }
|
|
12
|
+
export const manifest = mock.fn(async () => generate())
|
|
13
|
+
export const component = mock.fn(async () => connector())
|
|
14
|
+
export const composition = mock.fn(async () => connector())
|
|
15
|
+
export const remote = mock.fn(async () => connector())
|
|
@@ -1,25 +1,35 @@
|
|
|
1
|
-
|
|
1
|
+
import { it, mock as mocking } from 'node:test'
|
|
2
|
+
import assert from 'node:assert/strict'
|
|
3
|
+
import { isDeepStrictEqual } from 'node:util'
|
|
2
4
|
|
|
3
|
-
|
|
5
|
+
import { generate } from 'randomstring'
|
|
6
|
+
import * as _boot from './boot.mock.js'
|
|
4
7
|
|
|
5
8
|
const mock = {
|
|
6
|
-
boot:
|
|
9
|
+
boot: _boot
|
|
7
10
|
}
|
|
8
11
|
|
|
9
|
-
|
|
12
|
+
mocking.module('@toa.io/boot', { namedExports: mock.boot })
|
|
10
13
|
|
|
11
|
-
const stage =
|
|
14
|
+
const stage = await import('../src/index.js')
|
|
12
15
|
|
|
13
16
|
it('should be', () => {
|
|
14
|
-
|
|
17
|
+
assert.notStrictEqual(stage.component, undefined)
|
|
15
18
|
})
|
|
16
19
|
|
|
17
20
|
it('should boot component', async () => {
|
|
18
21
|
const path = generate()
|
|
19
22
|
const component = await stage.component(path)
|
|
20
23
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
24
|
+
assert.deepStrictEqual(mock.boot.manifest.mock.calls[0].arguments[0], path)
|
|
25
|
+
const manifest = await mock.boot.manifest.mock.calls[0].result
|
|
26
|
+
|
|
27
|
+
assert.ok(
|
|
28
|
+
mock.boot.component.mock.calls.some(
|
|
29
|
+
(call) =>
|
|
30
|
+
call.arguments.length === 1 && isDeepStrictEqual(call.arguments[0], manifest)
|
|
31
|
+
)
|
|
32
|
+
)
|
|
33
|
+
assert.deepStrictEqual(component, await mock.boot.component.mock.calls[0].result)
|
|
34
|
+
assert.ok(component.connect.mock.callCount() > 0)
|
|
25
35
|
})
|