@toa.io/userland 1.0.0-alpha.3 → 1.0.0-alpha.300

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 (154) hide show
  1. package/CHANGELOG.md +107 -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 +37 -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 +18 -13
  21. package/readme.md +0 -2
  22. package/stage/package.json +1 -0
  23. package/stage/src/component.js +3 -12
  24. package/stage/src/composition.js +3 -11
  25. package/stage/src/index.js +17 -17
  26. package/stage/src/manifest.js +2 -6
  27. package/stage/src/remote.js +11 -8
  28. package/stage/src/service.js +11 -10
  29. package/stage/src/shutdown.js +10 -9
  30. package/stage/src/state.js +2 -4
  31. package/stage/src/workload.js +19 -0
  32. package/stage/test/boot.mock.js +9 -11
  33. package/stage/test/component.test.js +20 -10
  34. package/stage/test/composition.test.js +11 -20
  35. package/stage/test/manifest.test.js +15 -8
  36. package/stage/test/remote.test.js +14 -12
  37. package/stage/test/shutdown.test.js +26 -22
  38. package/stage/test/state.mock.js +5 -6
  39. package/stage/test/state.test.js +13 -12
  40. package/stage/types/index.d.ts +17 -9
  41. package/stage/types/state.d.ts +3 -4
  42. package/example/components/echo/samples/get.yaml +0 -12
  43. package/example/components/echo/samples/signal.yaml +0 -4
  44. package/example/components/math.calculations/samples/add.yaml +0 -10
  45. package/example/components/math.calculations/samples/assets/ab.yaml +0 -2
  46. package/example/components/math.calculations/samples/increment.yaml +0 -27
  47. package/example/components/math.proxy/samples/add.yaml +0 -11
  48. package/example/components/tea.pots/samples/same.yaml +0 -8
  49. package/example/components/tea.pots/samples/transit.yaml +0 -43
  50. package/example/components/web/samples/get.yaml +0 -8
  51. package/example/samples/math.proxy.add.yaml +0 -5
  52. package/example/samples/messages/store.orders.created.yaml +0 -8
  53. package/samples/docs/sampling-dark.jpg +0 -0
  54. package/samples/docs/sampling-light.jpg +0 -0
  55. package/samples/notes.md +0 -12
  56. package/samples/package.json +0 -5
  57. package/samples/readme.md +0 -131
  58. package/samples/src/.replay/.suite/component.js +0 -20
  59. package/samples/src/.replay/.suite/index.js +0 -7
  60. package/samples/src/.replay/.suite/messages.js +0 -39
  61. package/samples/src/.replay/.suite/operation.js +0 -28
  62. package/samples/src/.replay/.suite/operations.js +0 -20
  63. package/samples/src/.replay/.suite/translate/.message/index.js +0 -5
  64. package/samples/src/.replay/.suite/translate/.message/request.js +0 -31
  65. package/samples/src/.replay/.suite/translate/.operation/.prepare/cast.js +0 -20
  66. package/samples/src/.replay/.suite/translate/.operation/.prepare/cast.test.js +0 -94
  67. package/samples/src/.replay/.suite/translate/.operation/.prepare/expand.js +0 -14
  68. package/samples/src/.replay/.suite/translate/.operation/.prepare/index.js +0 -7
  69. package/samples/src/.replay/.suite/translate/.operation/.prepare/shortcuts/.aspect.js +0 -26
  70. package/samples/src/.replay/.suite/translate/.operation/.prepare/shortcuts/configuration.js +0 -5
  71. package/samples/src/.replay/.suite/translate/.operation/.prepare/shortcuts/http.js +0 -5
  72. package/samples/src/.replay/.suite/translate/.operation/.prepare/shortcuts/index.js +0 -9
  73. package/samples/src/.replay/.suite/translate/.operation/.prepare/shortcuts/state.js +0 -5
  74. package/samples/src/.replay/.suite/translate/.operation/.prepare/types/async.js +0 -3
  75. package/samples/src/.replay/.suite/translate/.operation/.prepare/types/cast.js +0 -26
  76. package/samples/src/.replay/.suite/translate/.operation/.prepare/types/index.js +0 -5
  77. package/samples/src/.replay/.suite/translate/.operation/.prepare/types/map.js +0 -3
  78. package/samples/src/.replay/.suite/translate/.operation/.prepare/types/set.js +0 -3
  79. package/samples/src/.replay/.suite/translate/.operation/.prepare/types/sync.js +0 -3
  80. package/samples/src/.replay/.suite/translate/.operation/calls.js +0 -49
  81. package/samples/src/.replay/.suite/translate/.operation/cleanup.js +0 -16
  82. package/samples/src/.replay/.suite/translate/.operation/events.js +0 -16
  83. package/samples/src/.replay/.suite/translate/.operation/index.js +0 -11
  84. package/samples/src/.replay/.suite/translate/.operation/prepare.js +0 -14
  85. package/samples/src/.replay/.suite/translate/index.js +0 -7
  86. package/samples/src/.replay/.suite/translate/message.js +0 -26
  87. package/samples/src/.replay/.suite/translate/operation.js +0 -47
  88. package/samples/src/.replay/.suite/translate/schemas/index.js +0 -7
  89. package/samples/src/.replay/.suite/translate/schemas/message.cos.yaml +0 -10
  90. package/samples/src/.replay/.suite/translate/schemas/operation.cos.yaml +0 -23
  91. package/samples/src/.replay/index.js +0 -7
  92. package/samples/src/.replay/stage.js +0 -60
  93. package/samples/src/.replay/suite.js +0 -23
  94. package/samples/src/.replay/test.js +0 -19
  95. package/samples/src/components.js +0 -13
  96. package/samples/src/context.js +0 -23
  97. package/samples/src/index.js +0 -9
  98. package/samples/src/replay.js +0 -16
  99. package/samples/src/suite/.read/filter.js +0 -15
  100. package/samples/src/suite/.read/index.js +0 -7
  101. package/samples/src/suite/.read/messages.js +0 -40
  102. package/samples/src/suite/.read/operations.js +0 -51
  103. package/samples/src/suite/.read/parse.js +0 -20
  104. package/samples/src/suite/components.js +0 -31
  105. package/samples/src/suite/context.js +0 -20
  106. package/samples/src/suite/index.js +0 -7
  107. package/samples/test/components.test.js +0 -42
  108. package/samples/test/context/components/dummy/manifest.toa.yaml +0 -2
  109. package/samples/test/context/components/dummy/samples/do.yaml +0 -11
  110. package/samples/test/context/components/dummy/samples/dummies.dummy.do.yaml +0 -11
  111. package/samples/test/context/components/dummy/samples/dummies.dummy.undo.yaml +0 -7
  112. package/samples/test/context/components/dummy/samples/messages/somewhere.something.happened.yaml +0 -6
  113. package/samples/test/context/components/pot/manifest.toa.yaml +0 -2
  114. package/samples/test/context/components/pot/samples/do.yaml +0 -11
  115. package/samples/test/context/components/pot/samples/messages/somewhere.something.happened.yaml +0 -6
  116. package/samples/test/context/context.toa.yaml +0 -3
  117. package/samples/test/context/samples/dummies.dummy.observe.yaml +0 -6
  118. package/samples/test/context/samples/dummies.dummy.transit.yaml +0 -10
  119. package/samples/test/context/samples/messages/somewhere.something.happened.yaml +0 -6
  120. package/samples/test/context.fixtures.js +0 -8
  121. package/samples/test/context.test.js +0 -82
  122. package/samples/test/replay.mock.js +0 -7
  123. package/samples/test/replay.test.js +0 -152
  124. package/samples/test/replay.translate.message.fixtures.js +0 -24
  125. package/samples/test/replay.translate.message.test.js +0 -119
  126. package/samples/test/replay.translate.mock.js +0 -17
  127. package/samples/test/replay.translate.operation.fixtures.js +0 -107
  128. package/samples/test/replay.translate.operation.test.js +0 -68
  129. package/samples/test/stage.mock.js +0 -30
  130. package/samples/test/suite.components.test.js +0 -122
  131. package/samples/test/suite.context.test.js +0 -79
  132. package/samples/test/suite.mock.js +0 -10
  133. package/samples/types/index.d.ts +0 -8
  134. package/samples/types/message.d.ts +0 -24
  135. package/samples/types/operation.d.ts +0 -36
  136. package/samples/types/replay.d.ts +0 -13
  137. package/samples/types/suite.d.ts +0 -31
  138. package/stage/src/binding/binding.js +0 -50
  139. package/stage/src/binding/consumer.js +0 -30
  140. package/stage/src/binding/emitter.js +0 -26
  141. package/stage/src/binding/factory.js +0 -49
  142. package/stage/src/binding/index.js +0 -10
  143. package/stage/src/binding/label.js +0 -12
  144. package/stage/src/binding/producer.js +0 -43
  145. package/stage/src/binding/receiver.js +0 -34
  146. package/stage/test/binding/binding.test.js +0 -15
  147. package/stage/test/binding/consumer.test.js +0 -52
  148. package/stage/test/binding/emitter.test.js +0 -54
  149. package/stage/test/binding/producer.fixtures.js +0 -10
  150. package/stage/test/binding/producer.test.js +0 -49
  151. package/stage/test/binding/receiver.fixtures.js +0 -7
  152. package/stage/test/binding/receiver.test.js +0 -54
  153. package/stage/test/binding.mock.js +0 -8
  154. package/stage/types/binding.d.ts +0 -20
@@ -1,68 +0,0 @@
1
- 'use strict'
2
-
3
- const clone = require('clone-deep')
4
- const { generate } = require('randomstring')
5
-
6
- const fixtures = require('./replay.translate.operation.fixtures')
7
- const { operation: translate } = require('../src/.replay/.suite/translate')
8
-
9
- it('should be', () => {
10
- expect(translate).toBeDefined()
11
- })
12
-
13
- /** @type {toa.samples.Operation} */
14
- let declaration
15
-
16
- /** @type {toa.sampling.Request} */
17
- let request
18
-
19
- /** @type {toa.sampling.request.Sample} */
20
- let expected
21
-
22
- const autonomous = true
23
-
24
- beforeEach(() => {
25
- declaration = clone(fixtures.declaration)
26
- expected = clone(fixtures.expected)
27
- request = translate(declaration, autonomous)
28
- })
29
-
30
- it('should translate input', () => {
31
- expect(request).toStrictEqual(expected)
32
- })
33
-
34
- describe('validation', () => {
35
- it('should not allow additional properties', () => {
36
- declaration.foo = generate()
37
-
38
- expect(() => translate(declaration, true)).toThrow('not expected')
39
- })
40
- })
41
-
42
- describe('specials', () => {
43
- it('should transform configuration object to permanent sample', async () => {
44
- const configuration = { foo: generate() }
45
- const declaration = /** @type {toa.samples.Operation} */ { configuration }
46
- const request = translate(declaration, true)
47
-
48
- expect(request.sample.extensions.configuration).toStrictEqual([{
49
- result: configuration,
50
- permanent: true
51
- }])
52
- })
53
-
54
- it('should throw if configuration is provided twice', async () => {
55
- const configuration = { foo: generate() }
56
-
57
- const declaration = {
58
- configuration,
59
- extensions: {
60
- configuration: [{
61
- result: configuration
62
- }]
63
- }
64
- }
65
-
66
- expect(() => translate(declaration, true)).toThrow('ambiguous')
67
- })
68
- })
@@ -1,30 +0,0 @@
1
- // noinspection JSCheckFunctionSignatures
2
-
3
- 'use strict'
4
-
5
- const { Locator } = require('@toa.io/core')
6
- const stage = jest.requireActual('@toa.io/userland/stage')
7
-
8
- const manifest = jest.fn(async (path) => stage.manifest(path))
9
- const composition = jest.fn()
10
- const shutdown = jest.fn()
11
-
12
- const remote = jest.fn(async (id) => {
13
- const [namespace, name] = id.split('.')
14
- const locator = new Locator(name, namespace)
15
- const invoke = jest.fn(async (endpoint, request) => request.reply)
16
- const disconnect = jest.fn(async () => undefined)
17
-
18
- return { locator, invoke, disconnect }
19
- })
20
-
21
- const component = jest.fn(async () => {
22
- const invoke = jest.fn(async (endpoint, request) => request.reply)
23
-
24
- return { invoke }
25
- })
26
-
27
- const emit = jest.fn()
28
- const binding = { binding: { emit } }
29
-
30
- exports.stage = { manifest, composition, component, remote, shutdown, binding }
@@ -1,122 +0,0 @@
1
- 'use strict'
2
-
3
- const { resolve } = require('node:path')
4
- const yaml = require('@toa.io/yaml')
5
-
6
- const { components } = require('../src/suite')
7
-
8
- it('should be', () => {
9
- expect(components).toBeDefined()
10
- })
11
-
12
- const dummy = resolve(__dirname, 'context/components/dummy')
13
- const pot = resolve(__dirname, 'context/components/pot')
14
- const component = 'dummies.dummy'
15
-
16
- /** @type {toa.samples.Suite} */
17
- let suite
18
-
19
- beforeAll(async () => {
20
- const paths = [dummy]
21
-
22
- suite = await components(paths)
23
- })
24
-
25
- it('should return object', () => {
26
- expect(typeof suite).toStrictEqual('object')
27
- })
28
-
29
- it('should set title', async () => {
30
- expect(suite.title).toStrictEqual('Component samples')
31
- })
32
-
33
- it('should define suite as autonomous', async () => {
34
- expect(suite.autonomous).toStrictEqual(true)
35
- })
36
-
37
- it('should load operation samples', async () => {
38
- const expected = await operations()
39
-
40
- expect(Object.keys(suite.operations)).toStrictEqual([component])
41
-
42
- const set = suite.operations[component]
43
-
44
- expect(Object.keys(set)).toStrictEqual(['do', 'undo'])
45
-
46
- expect(set.do).toStrictEqual(expected.do)
47
- expect(set.undo).toStrictEqual(expected.undo)
48
- })
49
-
50
- describe('options', () => {
51
- const paths = [dummy, pot]
52
-
53
- it('should filter samples by component id', async () => {
54
- /** @type {toa.samples.suite.Options} */
55
- const options = { component: 'dummies.dummy' }
56
-
57
- suite = await components(paths, options)
58
-
59
- expect(suite.operations['dummies.pot']).toBeUndefined()
60
- })
61
-
62
- it('should filter samples by operation name', async () => {
63
- /** @type {toa.samples.suite.Options} */
64
- const options = { operation: 'do' }
65
-
66
- suite = await components(paths, options)
67
-
68
- expect('undo' in suite.operations['dummies.dummy']).toStrictEqual(false)
69
- })
70
-
71
- it('should filter operation samples by title', async () => {
72
- /** @type {toa.samples.suite.Options} */
73
- const options = { title: 'Should not undo' }
74
-
75
- suite = await components(paths, options)
76
-
77
- expect('do' in suite.operations['dummies.dummy']).toStrictEqual(false)
78
- expect(suite.operations['dummies.dummy'].undo.length).toStrictEqual(1)
79
- expect(suite.operations['dummies.dummy'].undo[0].title).toStrictEqual(options.title)
80
- })
81
-
82
- it('should filter operation samples by title as regexp', async () => {
83
- /** @type {toa.samples.suite.Options} */
84
- const options = { title: 'Should [a-z]{2}t undo' }
85
-
86
- suite = await components(paths, options)
87
-
88
- expect(suite.operations['dummies.dummy']?.do).toBeUndefined()
89
- expect(suite.operations['dummies.dummy'].undo.length).toStrictEqual(1)
90
- expect(suite.operations['dummies.dummy'].undo[0].title).toStrictEqual('Should not undo')
91
- })
92
-
93
- it('should filter operation samples by title', async () => {
94
- /** @type {toa.samples.suite.Options} */
95
- const options = { title: 'Should not undo' }
96
-
97
- suite = await components(paths, options)
98
-
99
- expect(Object.keys(suite.operations['dummies.dummy']).length).toStrictEqual(1)
100
- expect(suite.operations['dummies.dummy'].undo[0].title).toStrictEqual(options.title)
101
- })
102
- })
103
-
104
- /**
105
- * @returns {Promise<toa.samples.operations.Set>}
106
- */
107
- const operations = async () => {
108
- const path = resolve(dummy, 'samples')
109
-
110
- /** @type {toa.samples.Operation[]} */
111
- const do1 = (await yaml.load.all(resolve(path, 'do.yaml')))
112
-
113
- /** @type {toa.samples.Operation[]} */
114
- const do2 = (await yaml.load.all(resolve(path, 'dummies.dummy.do.yaml')))
115
-
116
- /** @type {toa.samples.Operation[]} */
117
- const undo = (await yaml.load.all(resolve(path, 'dummies.dummy.undo.yaml')))
118
-
119
- return {
120
- do: [...do1, ...do2], undo
121
- }
122
- }
@@ -1,79 +0,0 @@
1
- 'use strict'
2
-
3
- const { join } = require('node:path')
4
- const yaml = require('@toa.io/yaml')
5
-
6
- const { context } = require('../src/suite')
7
-
8
- it('should be', async () => {
9
- expect(context).toBeDefined()
10
- })
11
-
12
- const root = join(__dirname, 'context')
13
-
14
- /** @type {toa.samples.Suite} */
15
- let suite
16
-
17
- beforeAll(async () => {
18
- suite = await context(root)
19
- })
20
-
21
- it('should return object', async () => {
22
- expect(typeof suite).toStrictEqual('object')
23
- })
24
-
25
- it('should set title', async () => {
26
- expect(suite.title).toStrictEqual('Integration samples')
27
- })
28
-
29
- it('should define suite as non-autonomous', async () => {
30
- expect(suite.autonomous).toStrictEqual(false)
31
- })
32
-
33
- it('should load operation samples', async () => {
34
- const expected = await operations()
35
-
36
- expect(suite.operations).toStrictEqual(expected)
37
- })
38
-
39
- it('should load message samples', async () => {
40
- const expected = await messages()
41
-
42
- expect(suite.messages).toStrictEqual(expected)
43
- })
44
-
45
- /**
46
- * @returns {Promise<toa.samples.suite.Operations>}
47
- */
48
- const operations = async () => {
49
- const path = join(root, 'samples')
50
- const component = 'dummies.dummy'
51
- const endpoints = ['transit', 'observe']
52
-
53
- /** @type {toa.samples.suite.Operations} */
54
- const operations = {}
55
-
56
- /** @type {toa.samples.operations.Set} */
57
- const set = operations[component] = {}
58
-
59
- for (const endpoint of endpoints) {
60
- const filename = component + '.' + endpoint + '.yaml'
61
- const filepath = join(path, filename)
62
-
63
- set[endpoint] = /** @type {toa.samples.Operation[]} */ await yaml.load.all(filepath)
64
- }
65
-
66
- return operations
67
- }
68
-
69
- /**
70
- * @returns {Promise<toa.samples.messages.Set>}
71
- */
72
- const messages = async () => {
73
- const label = 'somewhere.something.happened'
74
- const file = label + '.yaml'
75
- const path = join(root, 'samples/messages', file)
76
- const samples = /** @type {toa.samples.Message[]} */ await yaml.load.all(path)
77
-
78
- return { [label]: samples }
79
- }
@@ -1,10 +0,0 @@
1
- 'use strict'
2
-
3
- const { generate } = require('randomstring')
4
-
5
- const suite = {
6
- components: jest.fn(async () => generate()),
7
- context: jest.fn(async () => generate())
8
- }
9
-
10
- exports.suite = suite
@@ -1,8 +0,0 @@
1
- import * as _replay from './replay'
2
-
3
- export { Operation } from './operation'
4
- export { Message } from './message'
5
-
6
- export const context: _replay.context
7
- export const components: _replay.components
8
- export const replay: _replay.replay
@@ -1,24 +0,0 @@
1
- import type * as _core from '@toa.io/core'
2
- import type * as _operation from './operation'
3
-
4
- declare namespace toa.samples{
5
-
6
- namespace messages{
7
-
8
- type Set = Record<string, Message[]>
9
-
10
- }
11
-
12
- type Message = {
13
- title?: string
14
- component: string
15
- payload: Object
16
- input?: any
17
- query?: _core.Query
18
- request?: _operation.Operation | null
19
- }
20
-
21
- }
22
-
23
- export type Message = toa.samples.Message
24
- export type Set = toa.samples.messages.Set
@@ -1,36 +0,0 @@
1
- import * as _core from '@toa.io/core'
2
- import * as _sampling from '@toa.io/extensions.sampling/types/request'
3
-
4
- declare namespace toa.samples{
5
-
6
- namespace operations{
7
-
8
- type Call = {
9
- input?: any
10
- output?: any
11
- }
12
-
13
- type Calls = Record<string, Call[]>
14
-
15
- type Events = Record<string, Object>
16
-
17
- type Set = Record<string, Operation[]>
18
-
19
- }
20
-
21
- type Operation = {
22
- title?: string
23
- input?: any
24
- output?: any
25
- current?: _core.storages.Record
26
- next?: _core.storages.Record
27
- remote?: operations.Calls
28
- local?: operations.Calls
29
- events?: operations.Events
30
- extensions?: _sampling.Extensions
31
- }
32
-
33
- }
34
-
35
- export type Operation = toa.samples.Operation
36
- export type Set = toa.samples.operations.Set
@@ -1,13 +0,0 @@
1
- import * as _suite from './suite'
2
-
3
- declare namespace toa.samples.replay {
4
-
5
- type components = (paths: string[], options?: _suite.Options) => Promise<boolean>
6
- type context = (path: string, options?: _suite.Options) => Promise<boolean>
7
- type replay = (suite: _suite.Suite, paths: string[], options?: object) => Promise<boolean>
8
-
9
- }
10
-
11
- export type context = toa.samples.replay.context
12
- export type components = toa.samples.replay.components
13
- export type replay = toa.samples.replay.replay
@@ -1,31 +0,0 @@
1
- import * as _core from '@toa.io/core'
2
- import * as _operations from './operation'
3
- import * as _messages from './message'
4
-
5
- declare namespace toa.samples{
6
-
7
- namespace suite{
8
- type Operations = Record<string, _operations.Set>
9
-
10
- type Options = {
11
- id?: string
12
- component?: string
13
- autonomous?: boolean
14
- integration?: boolean
15
- operation?: string
16
- title?: string
17
- runner?: object
18
- }
19
- }
20
-
21
- type Suite = {
22
- title?: string
23
- autonomous: boolean
24
- operations?: suite.Operations
25
- messages?: _messages.Set
26
- }
27
-
28
- }
29
-
30
- export type Suite = toa.samples.Suite
31
- export type Options = toa.samples.suite.Options
@@ -1,50 +0,0 @@
1
- 'use strict'
2
-
3
- /**
4
- * @implements {toa.stage.binding.Binding}
5
- */
6
- class Binding {
7
- /** @type {Record<string, function[]>} */
8
- #callbacks = {}
9
-
10
- /** @type {Record<string, function>} */
11
- #producers = {}
12
-
13
- async subscribe (label, callback) {
14
- if (this.#callbacks[label] === undefined) this.#callbacks[label] = []
15
-
16
- this.#callbacks[label].push(callback)
17
- }
18
-
19
- async emit (label, message) {
20
- if (!(label in this.#callbacks)) return
21
-
22
- const callbacks = this.#callbacks[label]
23
- const promises = callbacks.map((callback) => callback(message))
24
-
25
- await Promise.all(promises)
26
- }
27
-
28
- async reply (label, produce) {
29
- if (label in this.#producers) throw new Error(`Label '${label}' is already bound`)
30
-
31
- this.#producers[label] = produce
32
- }
33
-
34
- async request (label, request) {
35
- if (!(label in this.#producers)) throw new Error(`Label '${label}' is not bound`)
36
-
37
- const produce = this.#producers[label]
38
-
39
- return produce(request)
40
- }
41
-
42
- reset () {
43
- this.#callbacks = {}
44
- this.#producers = {}
45
- }
46
- }
47
-
48
- const binding = new Binding()
49
-
50
- exports.binding = binding
@@ -1,30 +0,0 @@
1
- 'use strict'
2
-
3
- const { Connector } = require('@toa.io/core')
4
-
5
- const { binding } = require('./binding')
6
- const { label } = require('./label')
7
-
8
- /**
9
- * @implements {toa.core.bindings.Consumer}
10
- */
11
- class Consumer extends Connector {
12
- /** @type {string} */
13
- #label
14
-
15
- /**
16
- * @param {toa.core.Locator} locator
17
- * @param {string} endpoint
18
- */
19
- constructor (locator, endpoint) {
20
- super()
21
-
22
- this.#label = label(locator, endpoint)
23
- }
24
-
25
- async request (request) {
26
- return binding.request(this.#label, request)
27
- }
28
- }
29
-
30
- exports.Consumer = Consumer
@@ -1,26 +0,0 @@
1
- 'use strict'
2
-
3
- const { Connector } = require('@toa.io/core')
4
-
5
- const { binding } = require('./binding')
6
- const { label } = require('./label')
7
-
8
- /**
9
- * @implements {toa.core.bindings.Emitter}
10
- */
11
- class Emitter extends Connector {
12
- /** @type {string} */
13
- #label
14
-
15
- constructor (locator, endpoint) {
16
- super()
17
-
18
- this.#label = label(locator, endpoint)
19
- }
20
-
21
- async emit (message) {
22
- await binding.emit(this.#label, message)
23
- }
24
- }
25
-
26
- exports.Emitter = Emitter
@@ -1,49 +0,0 @@
1
- 'use strict'
2
-
3
- const { Emitter } = require('./emitter')
4
- const { Receiver } = require('./receiver')
5
- const { Consumer } = require('./consumer')
6
- const { Producer } = require('./producer')
7
-
8
- class Factory {
9
- /**
10
- * @param {toa.core.Locator} source
11
- * @param {string} label
12
- * @param {string} group
13
- * @param {toa.core.Receiver} receiver
14
- * @returns {toa.core.Connector}
15
- */
16
- receiver (source, label, group, receiver) {
17
- return new Receiver(label, receiver)
18
- }
19
-
20
- /**
21
- * @param {toa.core.Locator} locator
22
- * @param {string} label
23
- * @returns {toa.core.bindings.Emitter}
24
- */
25
- emitter (locator, label) {
26
- return new Emitter(locator, label)
27
- }
28
-
29
- /**
30
- * @param {toa.core.Locator} locator
31
- * @param {string} endpoint
32
- * @returns {toa.core.bindings.Consumer}
33
- */
34
- consumer (locator, endpoint) {
35
- return new Consumer(locator, endpoint)
36
- }
37
-
38
- /**
39
- * @param {toa.core.Locator} locator
40
- * @param {string[]} endpoints
41
- * @param {toa.core.Component} component
42
- * @returns {toa.core.Connector}
43
- */
44
- producer (locator, endpoints, component) {
45
- return new Producer(locator, endpoints, component)
46
- }
47
- }
48
-
49
- exports.Factory = Factory
@@ -1,10 +0,0 @@
1
- 'use strict'
2
-
3
- const { Factory } = require('./factory')
4
- const { binding } = require('./binding')
5
-
6
- const properties = { async: true }
7
-
8
- exports.properties = properties
9
- exports.Factory = Factory
10
- exports.binding = binding
@@ -1,12 +0,0 @@
1
- 'use strict'
2
-
3
- /**
4
- * @param {toa.core.Locator} locator
5
- * @param {string} endpoint
6
- * @returns {string}
7
- */
8
- const label = (locator, endpoint) => {
9
- return locator.id + '.' + endpoint
10
- }
11
-
12
- exports.label = label
@@ -1,43 +0,0 @@
1
- 'use strict'
2
-
3
- const { Connector } = require('@toa.io/core')
4
-
5
- const { binding } = require('./binding')
6
- const { label } = require('./label')
7
-
8
- /**
9
- * @implements {toa.core.Connector}
10
- */
11
- class Producer extends Connector {
12
- /** @type {toa.core.Locator} */
13
- #locator
14
-
15
- /** @type {string[]} */
16
- #endpoints
17
-
18
- /** @type {toa.core.Component} */
19
- #component
20
-
21
- /**
22
- * @param {toa.core.Locator} locator
23
- * @param {string[]} endpoints
24
- * @param {toa.core.Component} component
25
- */
26
- constructor (locator, endpoints, component) {
27
- super()
28
-
29
- this.#locator = locator
30
- this.#endpoints = endpoints
31
- this.#component = component
32
- }
33
-
34
- async open () {
35
- for (const endpoint of this.#endpoints) {
36
- const command = label(this.#locator, endpoint)
37
-
38
- await binding.reply(command, (request) => this.#component.invoke(endpoint, request))
39
- }
40
- }
41
- }
42
-
43
- exports.Producer = Producer
@@ -1,34 +0,0 @@
1
- 'use strict'
2
-
3
- const { Connector } = require('@toa.io/core')
4
-
5
- const { binding } = require('./binding')
6
-
7
- /**
8
- * @implements {toa.core.Connector}
9
- */
10
- class Receiver extends Connector {
11
- /** @type {string} */
12
- #label
13
-
14
- /** @type {toa.core.Receiver} */
15
- #receiver
16
-
17
- /**
18
- * @param {toa.core.Locator} locator
19
- * @param {string} label
20
- * @param {toa.core.Receiver} receiver
21
- */
22
- constructor (label, receiver) {
23
- super()
24
-
25
- this.#label = label
26
- this.#receiver = receiver
27
- }
28
-
29
- async open () {
30
- await binding.subscribe(this.#label, (message) => this.#receiver.receive(message))
31
- }
32
- }
33
-
34
- exports.Receiver = Receiver