@toa.io/boot 1.0.0-alpha.32 → 1.0.0-alpha.321

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 (62) hide show
  1. package/CHANGELOG.md +313 -0
  2. package/package.json +11 -8
  3. package/src/atom.js +41 -0
  4. package/src/atomicity.js +11 -0
  5. package/src/bindings/broadcast.js +3 -6
  6. package/src/bindings/constants.js +1 -3
  7. package/src/bindings/consume.js +18 -6
  8. package/src/bindings/emit.js +3 -6
  9. package/src/bindings/factory.js +5 -9
  10. package/src/bindings/inbound.js +11 -0
  11. package/src/bindings/index.js +7 -13
  12. package/src/bindings/outbound.js +10 -0
  13. package/src/bindings/produce.js +56 -17
  14. package/src/bindings/receive.js +3 -6
  15. package/src/bridge.js +22 -16
  16. package/src/call.js +28 -9
  17. package/src/cascade.js +16 -10
  18. package/src/component.js +101 -32
  19. package/src/composition.js +106 -15
  20. package/src/context.js +7 -8
  21. package/src/contract.js +11 -12
  22. package/src/emission.js +21 -14
  23. package/src/env.js +19 -12
  24. package/src/events.js +27 -0
  25. package/src/extensions/aspects.js +4 -8
  26. package/src/extensions/component.js +4 -8
  27. package/src/extensions/context.js +4 -8
  28. package/src/extensions/destinations.js +27 -0
  29. package/src/extensions/emitter.js +7 -9
  30. package/src/extensions/index.js +10 -19
  31. package/src/extensions/instances.js +2 -4
  32. package/src/extensions/load.js +7 -7
  33. package/src/extensions/manage.js +15 -0
  34. package/src/extensions/receiver.js +5 -9
  35. package/src/extensions/resolve.js +62 -13
  36. package/src/extensions/storage.js +6 -9
  37. package/src/extensions/tenants.js +5 -9
  38. package/src/guards.js +20 -0
  39. package/src/host.js +29 -0
  40. package/src/inbox.js +12 -0
  41. package/src/index.js +29 -34
  42. package/src/manifest.js +78 -36
  43. package/src/manifest.test.js +85 -8
  44. package/src/map.js +108 -0
  45. package/src/operation.js +26 -14
  46. package/src/outbox.js +22 -0
  47. package/src/rc.js +5 -0
  48. package/src/receivers.js +53 -23
  49. package/src/regional.js +18 -0
  50. package/src/regional.test.js +28 -0
  51. package/src/remote.js +41 -22
  52. package/src/rendering.js +50 -0
  53. package/src/span.js +19 -0
  54. package/src/storage.js +50 -16
  55. package/src/storage.test.js +57 -0
  56. package/src/workload.js +281 -0
  57. package/src/workload.test.js +321 -0
  58. package/types/bindings.d.ts +6 -2
  59. package/types/index.d.ts +62 -10
  60. package/types/manifest.d.ts +5 -4
  61. package/src/discovery.js +0 -43
  62. package/src/exposition.js +0 -17
@@ -0,0 +1,27 @@
1
+ import { instance } from './resolve.js'
2
+
3
+ /**
4
+ * Where a committed state change of this component goes, beside its own events. An extension
5
+ * that contributes one gives the component an outbox whether or not it declares an event, so
6
+ * this is read before the storage is made.
7
+ *
8
+ * @param {toa.norm.Component} manifest
9
+ * @returns {Promise<import('@toa.io/core/types').outbox.Destination[]>}
10
+ */
11
+ export const destinations = async (manifest) => {
12
+ const destinations = []
13
+
14
+ if (manifest.extensions === undefined) return destinations
15
+
16
+ for (const [name, declaration] of Object.entries(manifest.extensions)) {
17
+ const factory = instance(name, manifest.path)
18
+
19
+ if (factory.destination === undefined) continue
20
+
21
+ const destination = await factory.destination(manifest.locator, declaration, manifest)
22
+
23
+ if (destination !== undefined) destinations.push(destination)
24
+ }
25
+
26
+ return destinations
27
+ }
@@ -1,20 +1,18 @@
1
- 'use strict'
2
-
3
- const { instances } = require('./instances')
1
+ import { instances } from './instances.js'
4
2
 
5
3
  /**
6
- * @param {toa.core.bindings.Emitter} emitter
4
+ * @param {import('@toa.io/core/types').bindings.Emitter} emitter
7
5
  * @param {string} label
8
- * @returns {toa.core.bindings.Emitter}
6
+ * @param {import('@toa.io/core').Locator} locator
7
+ * @returns {import('@toa.io/core/types').bindings.Emitter}
9
8
  */
10
- const emitter = (emitter, label) => {
9
+ export const emitter = (emitter, label, locator) => {
11
10
  let decorated = emitter
12
11
 
13
12
  for (const factory of Object.values(instances)) {
14
- if (factory.emitter !== undefined) decorated = factory.emitter(decorated, label)
13
+ if (factory.emitter !== undefined)
14
+ decorated = factory.emitter(decorated, label, locator)
15
15
  }
16
16
 
17
17
  return decorated
18
18
  }
19
-
20
- exports.emitter = emitter
@@ -1,19 +1,10 @@
1
- 'use strict'
2
-
3
- const { aspects } = require('./aspects')
4
- const { component } = require('./component')
5
- const { context } = require('./context')
6
- const { emitter } = require('./emitter')
7
- const { load } = require('./load')
8
- const { receiver } = require('./receiver')
9
- const { storage } = require('./storage')
10
- const { tenants } = require('./tenants')
11
-
12
- exports.aspects = aspects
13
- exports.component = component
14
- exports.context = context
15
- exports.emitter = emitter
16
- exports.load = load
17
- exports.receiver = receiver
18
- exports.storage = storage
19
- exports.tenants = tenants
1
+ export { aspects } from './aspects.js'
2
+ export { component } from './component.js'
3
+ export { context } from './context.js'
4
+ export { destinations } from './destinations.js'
5
+ export { emitter } from './emitter.js'
6
+ export { load } from './load.js'
7
+ export { manage } from './manage.js'
8
+ export { receiver } from './receiver.js'
9
+ export { storage } from './storage.js'
10
+ export { tenants } from './tenants.js'
@@ -1,6 +1,4 @@
1
- 'use strict'
2
-
3
1
  /**
4
- * @type {{ [key: string]: toa.core.extensions.Factory}}
2
+ * @type {{ [key: string]: import('@toa.io/core/types').extensions.Factory}}
5
3
  */
6
- exports.instances = {}
4
+ export const instances = {}
@@ -1,14 +1,14 @@
1
- 'use strict'
2
-
3
- const { resolve } = require('./resolve')
1
+ import { resolve } from './resolve.js'
4
2
 
5
3
  /**
6
4
  * @param {toa.norm.Component} manifest
7
5
  */
8
- const load = (manifest) => {
6
+ export const load = async (manifest) => {
9
7
  if (manifest.extensions === undefined) return
10
8
 
11
- for (const name of Object.keys(manifest.extensions)) resolve(name, manifest.path)
9
+ await Promise.all(
10
+ Object.keys(manifest.extensions).map(
11
+ async (name) => await resolve(name, manifest.path)
12
+ )
13
+ )
12
14
  }
13
-
14
- exports.load = load
@@ -0,0 +1,15 @@
1
+ import { instances } from './instances.js'
2
+
3
+ /**
4
+ * @param {import('@toa.io/core').Connector} composition
5
+ * @returns {import('@toa.io/core').Connector}
6
+ */
7
+ export const manage = (composition) => {
8
+ let managed = composition
9
+
10
+ for (const factory of Object.values(instances)) {
11
+ if (factory.manage !== undefined) managed = factory.manage(managed)
12
+ }
13
+
14
+ return managed
15
+ }
@@ -1,13 +1,11 @@
1
- 'use strict'
2
-
3
- const { instances } = require('./instances')
1
+ import { instances } from './instances.js'
4
2
 
5
3
  /**
6
- * @param {toa.core.Receiver} receiver
7
- * @param {toa.core.Locator} locator
8
- * @returns {toa.core.Receiver}
4
+ * @param {import('@toa.io/core/types').Receiver} receiver
5
+ * @param {import('@toa.io/core').Locator} locator
6
+ * @returns {import('@toa.io/core/types').Receiver}
9
7
  */
10
- const receiver = (receiver, locator) => {
8
+ export const receiver = (receiver, locator) => {
11
9
  let decorated = receiver
12
10
 
13
11
  for (const factory of Object.values(instances)) {
@@ -16,5 +14,3 @@ const receiver = (receiver, locator) => {
16
14
 
17
15
  return decorated
18
16
  }
19
-
20
- exports.receiver = receiver
@@ -1,27 +1,76 @@
1
- 'use strict'
1
+ import { createRequire } from 'node:module'
2
+ import { pathToFileURL } from 'node:url'
3
+ import { find } from '@toa.io/generic'
4
+ import * as boot from '../../src/index.js'
2
5
 
3
- const { directory: { find } } = require('@toa.io/filesystem')
4
- const boot = require('../..')
6
+ import { instances } from './instances.js'
5
7
 
6
- const { instances } = require('./instances')
8
+ // what `find` returns is a package directory, and a module is loaded by file
9
+ const require = createRequire(import.meta.url)
10
+
11
+ // an extension is loaded once, and what is remembered while it loads is the
12
+ // promise, so two components cannot each make a factory
13
+ const loading = {}
14
+
15
+ /**
16
+ * Loads an extension and remembers its factory.
17
+ *
18
+ * @param {string} reference
19
+ * @param {string} base
20
+ * @returns {Promise<import('@toa.io/core/types').extensions.Factory>}
21
+ */
22
+ export const resolve = async (reference, base = process.cwd()) => {
23
+ const path = find(pack(reference), base)
24
+
25
+ loading[path] ??= create(path)
26
+ instances[path] = await loading[path]
27
+
28
+ return keyed(instances[path], reference)
29
+ }
7
30
 
8
31
  /**
32
+ * The factory of an extension already loaded. Everything that decorates what a
33
+ * component is made of runs after its extensions are loaded, and stays
34
+ * synchronous.
35
+ *
9
36
  * @param {string} reference
10
37
  * @param {string} base
11
- * @returns {toa.core.extensions.Factory}
38
+ * @returns {import('@toa.io/core/types').extensions.Factory}
12
39
  */
13
- const resolve = (reference, base = process.cwd()) => {
14
- const path = find(reference, base)
40
+ export const instance = (reference, base = process.cwd()) => {
41
+ const factory = instances[find(pack(reference), base)]
15
42
 
16
- if (instances[path] === undefined) instances[path] = create(path)
43
+ if (factory === undefined) throw new Error(`Extension '${reference}' is not loaded`)
17
44
 
18
- return instances[path]
45
+ return keyed(factory, reference)
19
46
  }
20
47
 
21
- const create = (path) => {
22
- const { Factory } = require(path)
48
+ /**
49
+ * `package#key` is a declaration a package claims beside its main one, and what the package's
50
+ * factory offers for that key answers for it — so a key is never taken for the package itself.
51
+ *
52
+ * @param {import('@toa.io/core/types').extensions.Factory} factory
53
+ * @param {string} reference
54
+ * @returns {import('@toa.io/core/types').extensions.Factory}
55
+ */
56
+ const keyed = (factory, reference) => {
57
+ const key = reference.split('#')[1]
58
+
59
+ if (key === undefined) return factory
60
+
61
+ const claimed = factory.keys?.[key]
23
62
 
24
- return new Factory(boot)
63
+ if (claimed === undefined)
64
+ throw new Error(`'${reference}' names a key its package does not claim`)
65
+
66
+ return claimed
25
67
  }
26
68
 
27
- exports.resolve = resolve
69
+ /** The package a reference is of. */
70
+ const pack = (reference) => reference.split('#')[0]
71
+
72
+ const create = async (path) => {
73
+ const { Factory } = await import(pathToFileURL(require.resolve(path)).href)
74
+
75
+ return new Factory(boot.host())
76
+ }
@@ -1,19 +1,16 @@
1
- 'use strict'
2
-
3
- const { instances } = require('./instances')
1
+ import { instances } from './instances.js'
4
2
 
5
3
  /**
6
- * @param {toa.core.Storage} storage
7
- * @returns {toa.core.Storage}
4
+ * @param {import('@toa.io/core/types').storages.Storage} storage
5
+ * @param {import('@toa.io/core').Locator} locator whose storage it is
6
+ * @returns {import('@toa.io/core/types').storages.Storage}
8
7
  */
9
- const storage = (storage) => {
8
+ export const storage = (storage, locator) => {
10
9
  let decorated = storage
11
10
 
12
11
  for (const factory of Object.values(instances)) {
13
- if (factory.storage !== undefined) decorated = factory.storage(decorated)
12
+ if (factory.storage !== undefined) decorated = factory.storage(decorated, locator)
14
13
  }
15
14
 
16
15
  return decorated
17
16
  }
18
-
19
- exports.storage = storage
@@ -1,27 +1,23 @@
1
- 'use strict'
2
-
3
- const { resolve } = require('./resolve')
1
+ import { resolve } from './resolve.js'
4
2
 
5
3
  /**
6
4
  * @param {toa.norm.Component} manifest
7
- * @returns {toa.core.Connector[]}
5
+ * @returns {import('@toa.io/core').Connector[]}
8
6
  */
9
- const tenants = (manifest) => {
7
+ export const tenants = async (manifest) => {
10
8
  const tenants = []
11
9
 
12
10
  if (manifest.extensions === undefined) return tenants
13
11
 
14
12
  for (const [name, declaration] of Object.entries(manifest.extensions)) {
15
- const factory = resolve(name, manifest.path)
13
+ const factory = await resolve(name, manifest.path)
16
14
 
17
15
  if (factory.tenant === undefined) continue
18
16
 
19
- const tenant = factory.tenant(manifest.locator, declaration)
17
+ const tenant = await factory.tenant(manifest.locator, declaration, manifest)
20
18
 
21
19
  tenants.push(tenant)
22
20
  }
23
21
 
24
22
  return tenants
25
23
  }
26
-
27
- exports.tenants = tenants
package/src/guards.js ADDED
@@ -0,0 +1,20 @@
1
+ import { Guard } from '@toa.io/core'
2
+ import * as boot from './index.js'
3
+
4
+ export async function guards(manifest, context) {
5
+ if (manifest.guards === undefined) return
6
+
7
+ const entries = Object.entries(manifest.guards)
8
+
9
+ return await Promise.all(
10
+ entries.map(async ([name, guard]) => {
11
+ const bridge = await load(guard, name, context)
12
+
13
+ return new Guard(name, bridge)
14
+ })
15
+ )
16
+ }
17
+
18
+ async function load(guard, name, context) {
19
+ return await boot.bridge.guard(guard.bridge, guard.path, name, context)
20
+ }
package/src/host.js ADDED
@@ -0,0 +1,29 @@
1
+ import { Gate } from '@toa.io/core'
2
+
3
+ import * as boot from './index.js'
4
+
5
+ /**
6
+ * @param {import('./workload.js').Workload} [workload] the process, where there is one
7
+ *
8
+ * What the process hosting an extension provides to it. The counterpart of a component's
9
+ * context: an extension reaches the core through this and through nothing else.
10
+ *
11
+ * @returns {import('@toa.io/core/types').extensions.Host}
12
+ */
13
+ export const host = (workload) => ({
14
+ // an extension knows a version only where something told it one; see `Remotes` in exposition
15
+ remote: (locator, source, contract) => boot.remote(locator, source, { contract }),
16
+ broadcast: boot.bindings.broadcast,
17
+ composition: boot.composition,
18
+ receive: boot.receive,
19
+ atom: boot.atomicity,
20
+ outbound: boot.bindings.outbound,
21
+ inbound: boot.bindings.inbound,
22
+
23
+ // a composition booted outside a process — by a scenario, by `toa call` — has no workload, so
24
+ // its gates are ones nothing ever takes down and a halt asked of it is nobody's to perform
25
+ gate: (build) => workload?.gate(build) ?? new Gate(build),
26
+ quiesce: async () => await workload?.quiesce(),
27
+ cancel: async () => await workload?.cancel(),
28
+ stop: (seconds) => workload?.stop(seconds)
29
+ })
package/src/inbox.js ADDED
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Whether any operation of this component declares `once`, which is what decides that a place
3
+ * to record a call is made at all. Local to the manifest, unlike the events something consumes,
4
+ * so there is no variable a deployment computes.
5
+ *
6
+ * @param {toa.norm.Component} manifest
7
+ * @returns {boolean}
8
+ */
9
+ export const inbox = (manifest) =>
10
+ Object.values(manifest.operations ?? {}).some(
11
+ (definition) => definition.once === true || typeof definition.once === 'number'
12
+ )
package/src/index.js CHANGED
@@ -1,36 +1,31 @@
1
- 'use strict'
1
+ // the environment is read at module scope elsewhere, so it is loaded first
2
+ export { load as env } from './env.js'
2
3
 
3
- require('./env')
4
+ export * as bindings from './bindings/index.js'
5
+ export * as bridge from './bridge.js'
6
+ export * as contract from './contract.js'
7
+ export * as extensions from './extensions/index.js'
4
8
 
5
- const { call } = require('./call')
6
- const { cascade } = require('./cascade')
7
- const { component } = require('./component')
8
- const { composition } = require('./composition')
9
- const { context } = require('./context')
10
- const { emission } = require('./emission')
11
- const { exposition } = require('./exposition')
12
- const { manifest } = require('./manifest')
13
- const { operation } = require('./operation')
14
- const { receivers, receive } = require('./receivers')
15
- const { remote } = require('./remote')
16
- const { storage } = require('./storage')
17
-
18
- exports.bindings = require('./bindings')
19
- exports.bridge = require('./bridge')
20
- exports.contract = require('./contract')
21
- exports.discovery = require('./discovery')
22
- exports.extensions = require('./extensions')
23
-
24
- exports.call = call
25
- exports.cascade = cascade
26
- exports.component = component
27
- exports.composition = composition
28
- exports.context = context
29
- exports.emission = emission
30
- exports.exposition = exposition
31
- exports.manifest = manifest
32
- exports.operation = operation
33
- exports.receivers = receivers
34
- exports.receive = receive
35
- exports.remote = remote
36
- exports.storage = storage
9
+ export { call } from './call.js'
10
+ export { cascade } from './cascade.js'
11
+ export { component } from './component.js'
12
+ export { composition } from './composition.js'
13
+ export { context } from './context.js'
14
+ export { emission } from './emission.js'
15
+ export { events } from './events.js'
16
+ export { inbox } from './inbox.js'
17
+ export { outbox } from './outbox.js'
18
+ export { atomicity } from './atomicity.js'
19
+ export { atom } from './atom.js'
20
+ export * as map from './map.js'
21
+ export { manifest } from './manifest.js'
22
+ export { operation } from './operation.js'
23
+ export { receivers, receive } from './receivers.js'
24
+ export { remote } from './remote.js'
25
+ export { rendering } from './rendering.js'
26
+ export { regional } from './regional.js'
27
+ export { storage } from './storage.js'
28
+ export { guards } from './guards.js'
29
+ export { host } from './host.js'
30
+ export { rc } from './rc.js'
31
+ export { Workload } from './workload.js'
package/src/manifest.js CHANGED
@@ -1,40 +1,20 @@
1
- 'use strict'
1
+ import clone from 'clone-deep'
2
+ import { existsSync, readFileSync } from 'node:fs'
3
+ import { readFile } from 'node:fs/promises'
4
+ import { basename, dirname, join } from 'node:path'
5
+ import { merge } from '@toa.io/generic'
6
+ import { component as load, definition, revive, NORMALIZED } from '@toa.io/norm'
7
+ import { Locator } from '@toa.io/core'
2
8
 
3
- const clone = require('clone-deep')
4
- const { merge } = require('@toa.io/generic')
5
- const { component: load } = require('@toa.io/norm')
6
- const { Locator } = require('@toa.io/core')
9
+ import { span } from './span.js'
7
10
 
8
- /**
9
- * @type {toa.boot.Manifest}
10
- */
11
- const manifest = async (path, options = {}) => {
11
+ export const manifest = async (path, options = {}) => {
12
12
  options = merge(clone(options), DEFAULTS)
13
13
 
14
- const manifest = await load(path)
15
-
16
- if (options?.bindings !== undefined) {
17
- if ('operations' in manifest) {
18
- for (const operation of Object.values(manifest.operations)) {
19
- operation.bindings = options.bindings
20
- }
21
- }
22
-
23
- const check = (binding) => require(binding).properties?.async === true
24
- const asyncBinding = options.bindings.find(check)
25
-
26
- if (asyncBinding === undefined) throw new Error('Bindings override must contain at least one async binding')
27
-
28
- if ('events' in manifest) {
29
- for (const event of Object.values(manifest.events)) event.binding = asyncBinding
30
- }
31
-
32
- if ('receivers' in manifest) {
33
- for (const receiver of Object.values(manifest.receivers)) {
34
- if (receiver.source === undefined) receiver.binding = asyncBinding
35
- }
36
- }
37
- }
14
+ const manifest = await span(
15
+ { name: `manifest ${basename(path)}`, attributes: { path } },
16
+ async () => await read(path)
17
+ )
38
18
 
39
19
  if (manifest.extensions === undefined) manifest.extensions = {}
40
20
 
@@ -44,13 +24,75 @@ const manifest = async (path, options = {}) => {
44
24
  }
45
25
  }
46
26
 
47
- if ('storage' in options && 'entity' in manifest) manifest.entity.storage = options.storage
27
+ if ('storage' in options && 'entity' in manifest)
28
+ manifest.entity.storage = options.storage
48
29
 
49
30
  manifest.locator = new Locator(manifest.name, manifest.namespace)
50
31
 
51
32
  return manifest
52
33
  }
53
34
 
54
- const DEFAULTS = {}
35
+ /**
36
+ * A manifest already normalised, where there is one, and the sources read where there is not.
37
+ *
38
+ * A component of an application's own comes with the manifest its build wrote beside it. A
39
+ * component an extension ships comes with none — its package is published once, long before
40
+ * any image is built — but `@toa.io/definitions` carries the digest of those manifests, which
41
+ * a deploy has read for as long as it has been unable to install the extension. A service runs
42
+ * them from the package, so it reads the same digest rather than reading their sources again.
43
+ *
44
+ * Only a workspace normalises, then, which is the one place a source can have changed since.
45
+ *
46
+ * @param {string} path
47
+ */
48
+ async function read(path) {
49
+ const file = join(path, NORMALIZED)
50
+
51
+ if (existsSync(file)) return revive(JSON.parse(await readFile(file, 'utf8')), path)
52
+
53
+ return (await digested(path)) ?? (await load(path))
54
+ }
55
55
 
56
- exports.manifest = manifest
56
+ /**
57
+ * What the digest holds for a component of an extension's own, by the directory it is in.
58
+ *
59
+ * An extension ships its components in `components/` beside its manifest and nowhere else, so
60
+ * that is the whole of what is looked for: anything else — an application's own component, a
61
+ * fixture in a workspace — is read from its sources.
62
+ *
63
+ * @param {string} path
64
+ * @returns {Promise<object | undefined>}
65
+ */
66
+ async function digested(path) {
67
+ const directory = dirname(path)
68
+
69
+ if (basename(directory) !== COMPONENTS) return undefined
70
+
71
+ const root = dirname(directory)
72
+ const manifest = join(root, MANIFEST)
73
+
74
+ if (!existsSync(manifest)) return undefined
75
+
76
+ const { name } = JSON.parse(readFileSync(manifest, 'utf8'))
77
+
78
+ // the digest is of what Toa ships, and a package it does not define has none
79
+ if (name === undefined || !name.startsWith(SCOPE)) return undefined
80
+
81
+ const { module } = await definition(name)
82
+ const { labels, manifests } = module.components?.() ?? {}
83
+
84
+ if (manifests === undefined) return undefined
85
+
86
+ // a chart names a component's workload the way the digest labels it
87
+ const index = labels.indexOf(basename(path).replace('.', '-'))
88
+
89
+ return index === -1 ? undefined : revive(manifests[index], path)
90
+ }
91
+
92
+ const SCOPE = '@toa.io/'
93
+
94
+ const COMPONENTS = 'components'
95
+
96
+ const MANIFEST = 'package.json'
97
+
98
+ const DEFAULTS = {}