@toa.io/bindings.amqp 0.20.0-dev.3 → 0.20.0-dev.34

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 (75) hide show
  1. package/package.json +16 -10
  2. package/readme.md +10 -45
  3. package/schemas/annotation.cos.yaml +2 -0
  4. package/source/broadcast.js +0 -5
  5. package/source/communication.js +49 -0
  6. package/source/constants.js +0 -1
  7. package/source/consumer.js +0 -5
  8. package/source/deployment/annotation.test.ts +40 -0
  9. package/source/deployment/annotation.ts +26 -0
  10. package/source/deployment/context.ts +112 -0
  11. package/source/deployment/index.ts +10 -0
  12. package/source/deployment/instance.ts +3 -0
  13. package/source/deployment/sources.ts +44 -0
  14. package/source/deployment.ts +14 -0
  15. package/source/emitter.js +0 -5
  16. package/source/factory.js +22 -30
  17. package/source/index.js +0 -2
  18. package/source/producer.js +0 -6
  19. package/source/receiver.js +0 -6
  20. package/transpiled/broadcast.d.ts +10 -0
  21. package/transpiled/broadcast.js +31 -0
  22. package/transpiled/broadcast.js.map +1 -0
  23. package/transpiled/communication.d.ts +9 -0
  24. package/transpiled/communication.js +37 -0
  25. package/transpiled/communication.js.map +1 -0
  26. package/transpiled/constants.d.ts +1 -0
  27. package/transpiled/constants.js +3 -0
  28. package/transpiled/constants.js.map +1 -0
  29. package/transpiled/consumer.d.ts +9 -0
  30. package/transpiled/consumer.js +23 -0
  31. package/transpiled/consumer.js.map +1 -0
  32. package/transpiled/deployment/annotation.d.ts +10 -0
  33. package/transpiled/deployment/annotation.js +43 -0
  34. package/transpiled/deployment/annotation.js.map +1 -0
  35. package/transpiled/deployment/context.d.ts +7 -0
  36. package/transpiled/deployment/context.js +86 -0
  37. package/transpiled/deployment/context.js.map +1 -0
  38. package/transpiled/deployment/index.d.ts +4 -0
  39. package/transpiled/deployment/index.js +34 -0
  40. package/transpiled/deployment/index.js.map +1 -0
  41. package/transpiled/deployment/instance.d.ts +2 -0
  42. package/transpiled/deployment/instance.js +3 -0
  43. package/transpiled/deployment/instance.js.map +1 -0
  44. package/transpiled/deployment/sources.d.ts +8 -0
  45. package/transpiled/deployment/sources.js +35 -0
  46. package/transpiled/deployment/sources.js.map +1 -0
  47. package/transpiled/deployment.d.ts +4 -0
  48. package/transpiled/deployment.js +38 -0
  49. package/transpiled/deployment.js.map +1 -0
  50. package/transpiled/emitter.d.ts +9 -0
  51. package/transpiled/emitter.js +24 -0
  52. package/transpiled/emitter.js.map +1 -0
  53. package/transpiled/factory.d.ts +13 -0
  54. package/transpiled/factory.js +46 -0
  55. package/transpiled/factory.js.map +1 -0
  56. package/transpiled/index.d.ts +5 -0
  57. package/transpiled/index.js +9 -0
  58. package/transpiled/index.js.map +1 -0
  59. package/transpiled/producer.d.ts +5 -0
  60. package/transpiled/producer.js +31 -0
  61. package/transpiled/producer.js.map +1 -0
  62. package/transpiled/queues.d.ts +6 -0
  63. package/transpiled/queues.js +10 -0
  64. package/transpiled/queues.js.map +1 -0
  65. package/transpiled/receiver.d.ts +5 -0
  66. package/transpiled/receiver.js +34 -0
  67. package/transpiled/receiver.js.map +1 -0
  68. package/tsconfig.json +12 -0
  69. package/source/annotation.js +0 -5
  70. package/source/deployment.js +0 -32
  71. package/source/pointer.js +0 -18
  72. package/test/annotations.test.js +0 -16
  73. package/test/deployment.test.js +0 -36
  74. package/test/factory.test.js +0 -190
  75. package/test/pointer.test.js +0 -59
@@ -0,0 +1,34 @@
1
+ 'use strict';
2
+ const { Connector } = require('@toa.io/core');
3
+ class Receiver extends Connector {
4
+ /** @type {string} */
5
+ #exchange;
6
+ /** @type {string} */
7
+ #group;
8
+ /** @type {toa.amqp.Communication} */
9
+ #comm;
10
+ /** @type {toa.core.Receiver} */
11
+ #receiver;
12
+ constructor(comm, exchange, group, receiver) {
13
+ super();
14
+ this.#exchange = exchange;
15
+ this.#group = group;
16
+ this.#comm = comm;
17
+ this.#receiver = receiver;
18
+ this.depends(comm);
19
+ }
20
+ async open() {
21
+ await this.#comm.consume(this.#exchange, this.#group, this.#receive);
22
+ }
23
+ /**
24
+ * @param {any} message
25
+ * @param {object} properties
26
+ */
27
+ #receive = async (message, properties) => {
28
+ if (!('toa.io/amqp' in properties.headers))
29
+ message = { payload: message };
30
+ await this.#receiver.receive(message);
31
+ };
32
+ }
33
+ exports.Receiver = Receiver;
34
+ //# sourceMappingURL=receiver.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"receiver.js","sourceRoot":"","sources":["../source/receiver.js"],"names":[],"mappings":"AAAA,YAAY,CAAA;AAEZ,MAAM,EAAE,SAAS,EAAE,GAAG,OAAO,CAAC,cAAc,CAAC,CAAA;AAE7C,MAAM,QAAS,SAAQ,SAAS;IAC9B,qBAAqB;IACrB,SAAS,CAAA;IAET,qBAAqB;IACrB,MAAM,CAAA;IAEN,qCAAqC;IACrC,KAAK,CAAA;IAEL,gCAAgC;IAChC,SAAS,CAAA;IAET,YAAa,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,QAAQ;QAC1C,KAAK,EAAE,CAAA;QAEP,IAAI,CAAC,SAAS,GAAG,QAAQ,CAAA;QACzB,IAAI,CAAC,MAAM,GAAG,KAAK,CAAA;QACnB,IAAI,CAAC,KAAK,GAAG,IAAI,CAAA;QACjB,IAAI,CAAC,SAAS,GAAG,QAAQ,CAAA;QAEzB,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAA;IACpB,CAAC;IAED,KAAK,CAAC,IAAI;QACR,MAAM,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,SAAS,EAAE,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAA;IACtE,CAAC;IAED;;;OAGG;IACH,QAAQ,GAAG,KAAK,EAAE,OAAO,EAAE,UAAU,EAAE,EAAE;QACvC,IAAI,CAAC,CAAC,aAAa,IAAI,UAAU,CAAC,OAAO,CAAC;YAAE,OAAO,GAAG,EAAE,OAAO,EAAE,OAAO,EAAE,CAAA;QAE1E,MAAM,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,OAAO,CAAC,CAAA;IACvC,CAAC,CAAA;CACF;AAED,OAAO,CAAC,QAAQ,GAAG,QAAQ,CAAA"}
package/tsconfig.json ADDED
@@ -0,0 +1,12 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "./transpiled"
5
+ },
6
+ "include": [
7
+ "source"
8
+ ],
9
+ "exclude": [
10
+ "**/*.test.ts"
11
+ ]
12
+ }
@@ -1,5 +0,0 @@
1
- 'use strict'
2
-
3
- const { uris } = require('@toa.io/pointer')
4
-
5
- exports.annotation = uris.construct
@@ -1,32 +0,0 @@
1
- 'use strict'
2
-
3
- const pointer = require('@toa.io/pointer')
4
-
5
- const { PREFIX } = require('./constants')
6
-
7
- /**
8
- * @type {toa.deployment.dependency.Constructor}
9
- */
10
- const deployment = (instances, annotation) => {
11
- validate(annotation)
12
-
13
- /** @type {toa.pointer.deployment.Options} */
14
- const options = { prefix: PREFIX }
15
-
16
- return pointer.deployment(instances, annotation, options)
17
- }
18
-
19
- /**
20
- * @param {toa.pointer.URIs} annotation
21
- */
22
- const validate = (annotation) => {
23
- const defined = annotation !== undefined
24
- const defaults = defined && (typeof annotation === 'string' || annotation.default !== undefined)
25
- const correct = defined && (defaults || 'system' in annotation)
26
-
27
- if (!correct) {
28
- throw new Error('AMQP deployment requires either \'system\' or \'default\' pointer annotation')
29
- }
30
- }
31
-
32
- exports.deployment = deployment
package/source/pointer.js DELETED
@@ -1,18 +0,0 @@
1
- 'use strict'
2
-
3
- const { Pointer: Base } = require('@toa.io/pointer')
4
- const { PREFIX } = require('./constants')
5
-
6
- class Pointer extends Base {
7
- /**
8
- * @param {toa.core.Locator} locator
9
- */
10
- constructor (locator) {
11
- super(PREFIX, locator, OPTIONS)
12
- }
13
- }
14
-
15
- /** @type {toa.pointer.Options} */
16
- const OPTIONS = { protocol: 'amqp:' }
17
-
18
- exports.Pointer = Pointer
@@ -1,16 +0,0 @@
1
- 'use strict'
2
-
3
- const { generate } = require('randomstring')
4
- const mock = { uris: { construct: () => generate() }, Pointer: class {} }
5
-
6
- jest.mock('@toa.io/pointer', () => mock)
7
-
8
- const { annotation } = require('../')
9
-
10
- it('should export annotations', () => {
11
- expect(annotation).toBeDefined()
12
- })
13
-
14
- it('should export connectors.uris construct', () => {
15
- expect(annotation).toStrictEqual(mock.uris.construct)
16
- })
@@ -1,36 +0,0 @@
1
- 'use strict'
2
-
3
- const { generate } = require('randomstring')
4
- const { random } = require('@toa.io/generic')
5
- const mock = require('@toa.io/mock')
6
-
7
- const { deployment } = require('../')
8
-
9
- /** @type {toa.norm.context.dependencies.Instance[]} */
10
- let instances
11
-
12
- /** @returns {URL} */
13
- const gen = () => new URL('amqp://host-' + generate() + ':' + (random(1000) + 1000))
14
-
15
- beforeEach(() => {
16
- instances = mock.dependencies.instances()
17
- })
18
-
19
- it('should exist', () => {
20
- expect(deployment).toBeDefined()
21
- })
22
-
23
- it('should throw if annotation is not defined', () => {
24
- expect(() => deployment(instances, undefined))
25
- .toThrow('AMQP deployment requires either \'system\' or \'default\' pointer annotation')
26
- })
27
-
28
- it('should throw if \'system\' is not defined', () => {
29
- const url = gen()
30
- const annotation = {}
31
-
32
- for (const instance of instances) annotation[instance.locator.id] = url.href
33
-
34
- expect(() => deployment(instances, annotation))
35
- .toThrow('AMQP deployment requires either \'system\' or \'default\' pointer annotation')
36
- })
@@ -1,190 +0,0 @@
1
- 'use strict'
2
-
3
- // region setup
4
-
5
- const { generate } = require('randomstring')
6
- const { Locator } = require('@toa.io/core')
7
-
8
- jest.mock('@toa.io/generics.amqp')
9
- jest.mock('../source/producer')
10
- jest.mock('../source/consumer')
11
- jest.mock('../source/emitter')
12
- jest.mock('../source/receiver')
13
- jest.mock('../source/broadcast')
14
-
15
- const {
16
- /** @type {jest.MockedFunction<connector>} */
17
- connector
18
- } = require('@toa.io/generics.amqp')
19
-
20
- const {
21
- /** @type {jest.MockedClass<Producer>} */
22
- Producer
23
- } = require('../source/producer')
24
-
25
- const {
26
- /** @type {jest.MockedClass<Consumer>} */
27
- Consumer
28
- } = require('../source/consumer')
29
-
30
- const {
31
- /** @type {jest.MockedClass<Emitter>} */
32
- Emitter
33
- } = require('../source/emitter')
34
-
35
- const {
36
- /** @type {jest.MockedClass<Receiver>} */
37
- Receiver
38
- } = require('../source/receiver')
39
-
40
- const {
41
- /** @type {jest.MockedClass<Broadcast>} */
42
- Broadcast
43
- } = require('../source/broadcast')
44
-
45
- const { Factory } = require('../')
46
-
47
- it('should be', async () => {
48
- expect(Factory).toBeDefined()
49
- })
50
-
51
- /** @type {toa.core.bindings.Factory} */
52
- let factory
53
-
54
- beforeEach(() => {
55
- jest.clearAllMocks()
56
-
57
- factory = new Factory()
58
- })
59
-
60
- const locator = /** @type {toa.core.Locator} */ { name: generate(), namespace: generate() }
61
- const endpoints = [generate(), generate()]
62
- const endpoint = generate()
63
- const label = locator.id + '.' + endpoint
64
- const name = generate()
65
- const group = generate()
66
- const component = /** @type {toa.core.Component} */ {}
67
- const processor = /** @type {toa.core.Receiver} */ {}
68
-
69
- /** @type {jest.MockedObject<toa.amqp.Communication>} */
70
- let comm
71
-
72
- /** @type {toa.core.Connector} */
73
- let producer
74
-
75
- /** @type {toa.core.bindings.Consumer} */
76
- let consumer
77
-
78
- /** @type {toa.core.Connector} */
79
- let receiver
80
-
81
- /** @type {toa.core.bindings.Emitter} */
82
- let emitter
83
-
84
- /** @type {toa.core.bindings.Broadcast} */
85
- let broadcast
86
-
87
- // endregion
88
-
89
- describe.each(['Producer', 'Consumer', 'Emitter', 'Receiver', 'Broadcast'])('%s assets', (classname) => {
90
- const method = classname.toLowerCase()
91
-
92
- it('should be', async () => {
93
- expect(factory[method]).toBeDefined()
94
- })
95
-
96
- beforeEach(() => {
97
- jest.clearAllMocks()
98
-
99
- switch (method) {
100
- case 'producer':
101
- factory.producer(locator, endpoints, component)
102
- break
103
- case 'consumer':
104
- factory.consumer(locator, endpoint)
105
- break
106
- case 'emitter':
107
- factory.emitter(locator, endpoint)
108
- break
109
- case 'receiver':
110
- factory.receiver(locator, endpoint, group, processor)
111
- break
112
- case 'broadcast':
113
- factory.broadcast(name, group)
114
- break
115
- }
116
- })
117
-
118
- if (method !== 'broadcast') {
119
- it('should create Communication', async () => {
120
- expect(connector).toHaveBeenCalledWith('bindings-amqp', locator)
121
- })
122
- }
123
- })
124
-
125
- describe('Producer', () => {
126
- beforeEach(() => {
127
- producer = factory.producer(locator, endpoints, component)
128
- comm = connector.mock.results[0].value
129
- })
130
-
131
- it('should create instance', async () => {
132
- expect(Producer).toHaveBeenCalledWith(comm, locator, endpoints, component)
133
- expect(producer).toStrictEqual(Producer.mock.instances[0])
134
- })
135
- })
136
-
137
- describe('Consumer', () => {
138
- beforeEach(() => {
139
- consumer = factory.consumer(locator, endpoint)
140
- comm = connector.mock.results[0].value
141
- })
142
-
143
- it('should create instance', async () => {
144
- expect(Consumer).toHaveBeenCalledWith(comm, locator, endpoint)
145
- expect(consumer).toStrictEqual(Consumer.mock.instances[0])
146
- })
147
- })
148
-
149
- describe('Emitter', () => {
150
- beforeEach(() => {
151
- emitter = factory.emitter(locator, endpoint)
152
- comm = connector.mock.results[0].value
153
- })
154
-
155
- it('should create instance', async () => {
156
- expect(Emitter).toHaveBeenCalledWith(comm, locator, endpoint)
157
- expect(emitter).toStrictEqual(Emitter.mock.instances[0])
158
- })
159
- })
160
-
161
- describe('Receiver', () => {
162
- beforeEach(() => {
163
- receiver = factory.receiver(locator, label, group, processor)
164
- comm = connector.mock.results[0].value
165
- })
166
-
167
- it('should create instance', async () => {
168
- expect(Receiver).toHaveBeenCalledWith(comm, label, group, processor)
169
- expect(receiver).toStrictEqual(Receiver.mock.instances[0])
170
- })
171
- })
172
-
173
- describe('Broadcast', () => {
174
- beforeEach(() => {
175
- broadcast = factory.broadcast(name, group)
176
- comm = connector.mock.results[0].value
177
- })
178
-
179
- it('should create Locator', async () => {
180
- const locator = Broadcast.mock.calls[0][1]
181
-
182
- expect(locator.namespace).toStrictEqual('system')
183
- expect(locator.name).toStrictEqual(name)
184
- })
185
-
186
- it('should create instance', async () => {
187
- expect(Broadcast).toHaveBeenCalledWith(comm, expect.any(Locator), group)
188
- expect(broadcast).toStrictEqual(Broadcast.mock.instances[0])
189
- })
190
- })
@@ -1,59 +0,0 @@
1
- 'use strict'
2
-
3
- const { generate } = require('randomstring')
4
- const { Locator } = require('@toa.io/core')
5
- const { encode } = require('@toa.io/generic')
6
-
7
- const { Pointer } = require('../source/pointer')
8
-
9
- /** @type {toa.core.Locator} */
10
- let locator
11
-
12
- /** @type {Pointer} */
13
- let pointer
14
-
15
- const protocol = 'amqp:'
16
-
17
- let url
18
-
19
- beforeAll(() => {
20
- const username = generate()
21
- const password = generate()
22
-
23
- url = new URL('amqps://whatever:5672')
24
-
25
- url.username = username
26
- url.password = password
27
-
28
- process.env.TOA_BINDINGS_AMQP_DEFAULT_USERNAME = username
29
- process.env.TOA_BINDINGS_AMQP_DEFAULT_PASSWORD = password
30
- })
31
-
32
- beforeEach(() => {
33
- const name = generate()
34
- const namespace = generate()
35
- const uris = { default: url.href }
36
- const value = encode(uris)
37
- const key = 'TOA_BINDINGS_AMQP_POINTER'
38
-
39
- process.env[key] = value
40
-
41
- locator = new Locator(name, namespace)
42
- pointer = new Pointer(locator)
43
- })
44
-
45
- it('should be', () => undefined)
46
-
47
- it('should expose reference', () => {
48
- expect(pointer.reference).toStrictEqual(url.href)
49
- })
50
-
51
- it('should set amqp: protocol on localhost', () => {
52
- process.env.TOA_DEV = '1'
53
-
54
- pointer = new Pointer(locator)
55
-
56
- expect(pointer.protocol).toStrictEqual(protocol)
57
-
58
- delete process.env.TOA_DEV
59
- })