@toa.io/userland 1.0.0-alpha.34 → 1.0.0-alpha.340

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 (57) hide show
  1. package/CHANGELOG.md +430 -0
  2. package/example/components/echo/manifest.toa.yaml +15 -5
  3. package/example/components/echo/operations/get.js +1 -5
  4. package/example/components/echo/operations/resolve.js +1 -5
  5. package/example/components/echo/operations/signal.js +3 -7
  6. package/example/components/math.calculations/manifest.toa.yaml +19 -6
  7. package/example/components/math.calculations/operations/add.js +1 -5
  8. package/example/components/math.calculations/operations/increment.js +1 -5
  9. package/example/components/math.proxy/manifest.toa.yaml +8 -3
  10. package/example/components/math.proxy/operations/add.js +1 -5
  11. package/example/components/tea.pots/manifest.toa.yaml +29 -9
  12. package/example/components/tea.pots/operations/same.js +1 -5
  13. package/example/components/tea.pots/receivers/store.orders.created.js +1 -5
  14. package/example/components/web/manifest.toa.yaml +2 -4
  15. package/example/components/web/operations/get.js +3 -8
  16. package/example/context.toa.yaml +2 -7
  17. package/example/stage/call.test.js +17 -10
  18. package/example/stage/events.test.js +63 -16
  19. package/example/stage/invoke.test.js +12 -9
  20. package/package.json +19 -13
  21. package/readme.md +58 -1
  22. package/stage/package.json +1 -0
  23. package/stage/src/component.js +3 -7
  24. package/stage/src/composition.js +3 -7
  25. package/stage/src/index.js +9 -16
  26. package/stage/src/manifest.js +2 -6
  27. package/stage/src/map.js +15 -0
  28. package/stage/src/quiescence.js +16 -0
  29. package/stage/src/remote.js +11 -8
  30. package/stage/src/service.js +11 -10
  31. package/stage/src/shutdown.js +16 -6
  32. package/stage/src/state.js +2 -4
  33. package/stage/src/workload.js +53 -0
  34. package/stage/test/boot.mock.js +10 -11
  35. package/stage/test/component.test.js +20 -10
  36. package/stage/test/composition.test.js +11 -9
  37. package/stage/test/manifest.test.js +15 -8
  38. package/stage/test/remote.test.js +14 -12
  39. package/stage/test/shutdown.test.js +28 -15
  40. package/stage/test/state.mock.js +5 -6
  41. package/stage/test/state.test.js +13 -12
  42. package/stage/types/index.d.ts +21 -8
  43. package/stage/types/state.d.ts +3 -4
  44. package/template/_gitignore +3 -0
  45. package/template/components/hello/manifest.toa.yaml +22 -0
  46. package/template/components/hello/operations/greet.ts +5 -0
  47. package/template/components/notes/manifest.toa.yaml +25 -0
  48. package/template/context.toa.yaml +29 -0
  49. package/template/cucumber.js +4 -0
  50. package/template/docker-compose.yaml +62 -0
  51. package/template/ecosystem.config.js +29 -0
  52. package/template/features/hello.feature +14 -0
  53. package/template/features/notes.feature +29 -0
  54. package/template/features/steps/application.ts +22 -0
  55. package/template/features/steps/http.ts +19 -0
  56. package/template/package.json +28 -0
  57. package/template/tsconfig.json +15 -0
@@ -1,10 +1,5 @@
1
1
  name: examples
2
- packages: components/*
3
2
  registry: example
4
3
 
5
- amqp: amqp://localhost
6
- mongodb: mongodb://localhost
7
-
8
- origins:
9
- default.web:
10
- dev: http://localhost
4
+ amqp: amqp://localhost:31010
5
+ mongodb: mongodb://localhost:31020
@@ -1,16 +1,21 @@
1
- 'use strict'
1
+ import { it, before, after } from 'node:test'
2
+ import assert from 'node:assert/strict'
2
3
 
3
- const { resolve } = require('node:path')
4
- const { exceptions: { RequestContractException } } = require('@toa.io/core')
5
- const stage = require('@toa.io/userland/stage')
4
+ import { resolve } from 'node:path'
5
+ import { exceptions } from '@toa.io/core'
6
+ import * as stage from '@toa.io/userland/stage'
6
7
 
7
- const root = resolve(__dirname, '../components')
8
+ const { RequestContractException } = exceptions
9
+
10
+ const root = resolve(import.meta.dirname, '../components')
8
11
 
9
12
  let echo
10
13
  let math
11
14
 
12
- beforeAll(async () => {
15
+ before(async () => {
13
16
  process.env.TOA_DEV = '1'
17
+ process.env.TOA_CONFIGURATION_DEFAULT_ECHO = '{}'
18
+ process.env.TOA_CONFIGURATION_TEA_POTS = '{}'
14
19
 
15
20
  const paths = ['echo', 'math.calculations'].map((rel) => resolve(root, rel))
16
21
 
@@ -20,7 +25,7 @@ beforeAll(async () => {
20
25
  math = await stage.remote('math.calculations')
21
26
  })
22
27
 
23
- afterAll(async () => {
28
+ after(async () => {
24
29
  await stage.shutdown()
25
30
 
26
31
  delete process.env.TOA_DEV
@@ -29,13 +34,15 @@ afterAll(async () => {
29
34
  it('should call endpoint', async () => {
30
35
  const reply = await echo.invoke('signal', {})
31
36
 
32
- expect(reply).toStrictEqual('quack')
37
+ assert.deepStrictEqual(reply, 'quack')
33
38
  })
34
39
 
35
40
  it('should throw on invalid input', async () => {
36
41
  const a = 'not a number'
37
42
  const b = 'neither'
38
43
 
39
- await expect(math.invoke('add', { input: { a, b } }))
40
- .rejects.toBeInstanceOf(RequestContractException)
44
+ await assert.rejects(math.invoke('add', { input: { a, b } }), (error) => {
45
+ assert.ok(error instanceof RequestContractException)
46
+ return true
47
+ })
41
48
  })
@@ -1,51 +1,98 @@
1
- 'use strict'
1
+ import { it, before, after } from 'node:test'
2
+ import assert from 'node:assert/strict'
2
3
 
3
- const { resolve } = require('node:path')
4
- const stage = require('@toa.io/userland/stage')
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 binding = stage.binding.binding
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
- beforeAll(async () => {
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
- afterAll(async () => {
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 binding.emit('store.orders.created', message)
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
- const reply = await remote.invoke('observe', request)
55
+ let reply
37
56
 
38
- expect(reply.booked).toStrictEqual(true)
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
- await binding.subscribe('tea.pots.created', (pot) => {
47
- expect(pot.payload.material).toStrictEqual(material)
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
- 'use strict'
1
+ import { it, before, after } from 'node:test'
2
+ import assert from 'node:assert/strict'
2
3
 
3
- const { resolve } = require('node:path')
4
- const stage = require('@toa.io/userland/stage')
4
+ import { resolve } from 'node:path'
5
+ import * as stage from '@toa.io/userland/stage'
5
6
 
6
- const root = resolve(__dirname, '../components')
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
- beforeAll(async () => {
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
- afterAll(async () => {
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
- expect(reply.exception).toBeUndefined()
32
- expect(reply.output).toStrictEqual(a + b)
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.34",
3
+ "version": "1.0.0-alpha.340",
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": ">= 18.0.0"
20
+ "node": ">= 24.0.0"
20
21
  },
21
22
  "publishConfig": {
22
23
  "access": "public"
23
24
  },
24
25
  "main": "src/index.js",
25
- "types": "types/index.d.ts",
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/boot": "1.0.0-alpha.34",
28
- "@toa.io/core": "1.0.0-alpha.34",
29
- "@toa.io/filesystem": "1.0.0-alpha.34",
30
- "@toa.io/generic": "1.0.0-alpha.34",
31
- "@toa.io/norm": "1.0.0-alpha.34",
32
- "@toa.io/schemas": "1.0.0-alpha.34",
33
- "@toa.io/storages.null": "0.22.0",
34
- "@toa.io/yaml": "1.0.0-alpha.34",
35
- "tap": "16.3.4"
35
+ "@toa.io/agent": "1.0.0-alpha.333",
36
+ "@toa.io/boot": "1.0.0-alpha.340",
37
+ "@toa.io/core": "1.0.0-alpha.333",
38
+ "@toa.io/generic": "1.0.0-alpha.332",
39
+ "@toa.io/norm": "1.0.0-alpha.340",
40
+ "@toa.io/schemas": "1.0.0-alpha.332",
41
+ "@toa.io/storages.null": "1.0.0-alpha.333"
36
42
  },
37
- "gitHead": "caf71c04ebd589ff54a25f533b975484aaf9a254"
43
+ "gitHead": "1871346ef431050d23bd8e91f2b920cea1aeb749"
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
- [^1]: not yet implemented
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.
@@ -1,4 +1,5 @@
1
1
  {
2
+ "type": "module",
2
3
  "private": true,
3
4
  "main": "src/index.js",
4
5
  "types": "types/index.d.ts"
@@ -1,10 +1,8 @@
1
- 'use strict'
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
@@ -1,15 +1,11 @@
1
- 'use strict'
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
@@ -1,16 +1,9 @@
1
- 'use strict'
2
-
3
- const { manifest } = require('./manifest')
4
- const { component } = require('./component')
5
- const { composition } = require('./composition')
6
- const { service } = require('./service')
7
- const { remote } = require('./remote')
8
- const { shutdown } = require('./shutdown')
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'
@@ -1,12 +1,8 @@
1
- 'use strict'
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
@@ -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
+ }
@@ -1,19 +1,17 @@
1
- 'use strict'
1
+ import * as boot from '@toa.io/boot'
2
+ import { Locator } from '@toa.io/core'
2
3
 
3
- const boot = require('@toa.io/boot')
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
- exports.remote = remote
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' }
@@ -1,14 +1,17 @@
1
- 'use strict'
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
- const boot = require('@toa.io/boot')
4
- const { state } = require('./state')
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
@@ -1,18 +1,28 @@
1
- 'use strict'
1
+ import * as boot from '@toa.io/boot'
2
2
 
3
- const { state } = require('./state')
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 = [...components, ...compositions, ...services, ...remotes]
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
@@ -1,12 +1,11 @@
1
- 'use strict'
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
+ }