@toa.io/bindings.amqp 0.4.0-dev.0 → 0.4.0-dev.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +7 -8
- package/source/broadcast.js +1 -2
- package/source/factory.js +20 -11
- package/source/receiver.js +3 -5
- package/test/broadcast.test.js +2 -2
- package/test/factory.test.js +14 -25
- package/test/receiver.test.js +2 -7
- package/source/communication.js +0 -54
- package/test/communication.test.js +0 -97
- package/test/comq.mock.js +0 -21
- package/types/communication.d.ts +0 -16
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@toa.io/bindings.amqp",
|
|
3
|
-
"version": "0.4.0-dev.
|
|
3
|
+
"version": "0.4.0-dev.6",
|
|
4
4
|
"description": "Toa AMQP Binding",
|
|
5
5
|
"author": "temich <tema.gurtovoy@gmail.com>",
|
|
6
6
|
"homepage": "https://github.com/toa-io/toa#readme",
|
|
@@ -19,15 +19,14 @@
|
|
|
19
19
|
"test": "echo \"Error: run tests from root\" && exit 1"
|
|
20
20
|
},
|
|
21
21
|
"devDependencies": {
|
|
22
|
-
"@toa.io/mock": "0.4.0-dev.
|
|
23
|
-
"@types/amqplib": "0.8.2"
|
|
22
|
+
"@toa.io/mock": "0.4.0-dev.6"
|
|
24
23
|
},
|
|
25
24
|
"dependencies": {
|
|
26
|
-
"@toa.io/console": "0.4.0-dev.
|
|
27
|
-
"@toa.io/core": "0.4.0-dev.
|
|
28
|
-
"@toa.io/generic": "0.4.0-dev.
|
|
29
|
-
"@toa.io/
|
|
30
|
-
"
|
|
25
|
+
"@toa.io/console": "0.4.0-dev.6",
|
|
26
|
+
"@toa.io/core": "0.4.0-dev.6",
|
|
27
|
+
"@toa.io/generic": "0.4.0-dev.6",
|
|
28
|
+
"@toa.io/generics.amqp": "0.4.0-dev.6",
|
|
29
|
+
"@toa.io/pointer": "0.4.0-dev.6"
|
|
31
30
|
},
|
|
32
31
|
"gitHead": "2be07592325b2e4dc823e81d882a4e50bf50de24"
|
|
33
32
|
}
|
package/source/broadcast.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
const { Connector } = require('@toa.io/core')
|
|
4
|
-
const { newid } = require('@toa.io/generic')
|
|
5
4
|
|
|
6
5
|
const { name } = require('./queues')
|
|
7
6
|
|
|
@@ -28,7 +27,7 @@ class Broadcast extends Connector {
|
|
|
28
27
|
|
|
29
28
|
this.#comm = comm
|
|
30
29
|
this.#locator = locator
|
|
31
|
-
this.#group = group
|
|
30
|
+
this.#group = group
|
|
32
31
|
|
|
33
32
|
this.depends(comm)
|
|
34
33
|
}
|
package/source/factory.js
CHANGED
|
@@ -1,16 +1,15 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
const { Locator } = require('@toa.io/core')
|
|
4
|
+
const { connector } = require('@toa.io/generics.amqp')
|
|
4
5
|
|
|
5
|
-
const { Pointer } = require('./pointer')
|
|
6
|
-
const { Communication } = require('./communication')
|
|
7
6
|
const { Producer } = require('./producer')
|
|
8
7
|
const { Consumer } = require('./consumer')
|
|
9
8
|
const { Emitter } = require('./emitter')
|
|
10
9
|
const { Receiver } = require('./receiver')
|
|
11
10
|
const { Broadcast } = require('./broadcast')
|
|
12
11
|
|
|
13
|
-
const { SYSTEM } = require('./constants')
|
|
12
|
+
const { SYSTEM, PREFIX } = require('./constants')
|
|
14
13
|
|
|
15
14
|
/**
|
|
16
15
|
* @implements {toa.core.bindings.Factory}
|
|
@@ -28,16 +27,28 @@ class Factory {
|
|
|
28
27
|
return new Consumer(comm, locator, endpoint)
|
|
29
28
|
}
|
|
30
29
|
|
|
30
|
+
/**
|
|
31
|
+
* @param {toa.core.Locator} locator
|
|
32
|
+
* @param {string} label
|
|
33
|
+
* @return {Emitter}
|
|
34
|
+
*/
|
|
31
35
|
emitter (locator, label) {
|
|
32
36
|
const comm = this.#getCommunication(locator)
|
|
33
37
|
|
|
34
38
|
return new Emitter(comm, locator, label)
|
|
35
39
|
}
|
|
36
40
|
|
|
37
|
-
|
|
38
|
-
|
|
41
|
+
/**
|
|
42
|
+
* @param {toa.core.Locator} source
|
|
43
|
+
* @param {string} label
|
|
44
|
+
* @param {string} group
|
|
45
|
+
* @param {toa.core.Receiver} receiver
|
|
46
|
+
* @return {Receiver}
|
|
47
|
+
*/
|
|
48
|
+
receiver (source, label, group, receiver) {
|
|
49
|
+
const comm = this.#getCommunication(source)
|
|
39
50
|
|
|
40
|
-
return new Receiver(comm,
|
|
51
|
+
return new Receiver(comm, label, group, receiver)
|
|
41
52
|
}
|
|
42
53
|
|
|
43
54
|
broadcast (name, group) {
|
|
@@ -49,13 +60,11 @@ class Factory {
|
|
|
49
60
|
|
|
50
61
|
/**
|
|
51
62
|
*
|
|
52
|
-
* @param {toa.core.Locator}
|
|
63
|
+
* @param {toa.core.Locator} source
|
|
53
64
|
* @return {toa.amqp.Communication}
|
|
54
65
|
*/
|
|
55
|
-
#getCommunication (
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
return new Communication(pointer)
|
|
66
|
+
#getCommunication (source) {
|
|
67
|
+
return connector(PREFIX, source)
|
|
59
68
|
}
|
|
60
69
|
}
|
|
61
70
|
|
package/source/receiver.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
const { Connector } = require('@toa.io/core')
|
|
4
|
-
const { name } = require('./queues')
|
|
5
4
|
|
|
6
5
|
class Receiver extends Connector {
|
|
7
6
|
/** @type {string} */
|
|
@@ -18,15 +17,14 @@ class Receiver extends Connector {
|
|
|
18
17
|
|
|
19
18
|
/**
|
|
20
19
|
* @param {toa.amqp.Communication} comm
|
|
21
|
-
* @param {
|
|
22
|
-
* @param {string} label
|
|
20
|
+
* @param {string} exchange
|
|
23
21
|
* @param {string} group
|
|
24
22
|
* @param {toa.core.Receiver} receiver
|
|
25
23
|
*/
|
|
26
|
-
constructor (comm,
|
|
24
|
+
constructor (comm, exchange, group, receiver) {
|
|
27
25
|
super()
|
|
28
26
|
|
|
29
|
-
this.#exchange =
|
|
27
|
+
this.#exchange = exchange
|
|
30
28
|
this.#group = group
|
|
31
29
|
this.#comm = comm
|
|
32
30
|
this.#receiver = receiver
|
package/test/broadcast.test.js
CHANGED
|
@@ -67,7 +67,7 @@ it('should receive', async () => {
|
|
|
67
67
|
expect(comm.consume).toHaveBeenCalledWith(exchange, group, expect.any(Function))
|
|
68
68
|
})
|
|
69
69
|
|
|
70
|
-
it('should
|
|
70
|
+
it('should consume exclusively if group is not provided', async () => {
|
|
71
71
|
jest.clearAllMocks()
|
|
72
72
|
|
|
73
73
|
broadcast = new Broadcast(comm, locator)
|
|
@@ -79,5 +79,5 @@ it('should create unique group if not provided', async () => {
|
|
|
79
79
|
|
|
80
80
|
const group = comm.consume.mock.calls[0][1]
|
|
81
81
|
|
|
82
|
-
expect(group).
|
|
82
|
+
expect(group).toBeUndefined()
|
|
83
83
|
})
|
package/test/factory.test.js
CHANGED
|
@@ -5,8 +5,7 @@
|
|
|
5
5
|
const { generate } = require('randomstring')
|
|
6
6
|
const { Locator } = require('@toa.io/core')
|
|
7
7
|
|
|
8
|
-
jest.mock('
|
|
9
|
-
jest.mock('../source/communication')
|
|
8
|
+
jest.mock('@toa.io/generics.amqp')
|
|
10
9
|
jest.mock('../source/producer')
|
|
11
10
|
jest.mock('../source/consumer')
|
|
12
11
|
jest.mock('../source/emitter')
|
|
@@ -14,14 +13,9 @@ jest.mock('../source/receiver')
|
|
|
14
13
|
jest.mock('../source/broadcast')
|
|
15
14
|
|
|
16
15
|
const {
|
|
17
|
-
/** @type {jest.
|
|
18
|
-
|
|
19
|
-
} = require('
|
|
20
|
-
|
|
21
|
-
const {
|
|
22
|
-
/** @type {jest.MockedClass<Pointer>} */
|
|
23
|
-
Pointer
|
|
24
|
-
} = require('../source/pointer')
|
|
16
|
+
/** @type {jest.MockedFunction<connector>} */
|
|
17
|
+
connector
|
|
18
|
+
} = require('@toa.io/generics.amqp')
|
|
25
19
|
|
|
26
20
|
const {
|
|
27
21
|
/** @type {jest.MockedClass<Producer>} */
|
|
@@ -66,6 +60,7 @@ beforeEach(() => {
|
|
|
66
60
|
const locator = /** @type {toa.core.Locator} */ { name: generate(), namespace: generate() }
|
|
67
61
|
const endpoints = [generate(), generate()]
|
|
68
62
|
const endpoint = generate()
|
|
63
|
+
const label = locator.id + '.' + endpoint
|
|
69
64
|
const name = generate()
|
|
70
65
|
const group = generate()
|
|
71
66
|
const component = /** @type {toa.core.Component} */ {}
|
|
@@ -121,22 +116,16 @@ describe.each(['Producer', 'Consumer', 'Emitter', 'Receiver', 'Broadcast'])('%s
|
|
|
121
116
|
})
|
|
122
117
|
|
|
123
118
|
if (method !== 'broadcast') {
|
|
124
|
-
it('should create
|
|
125
|
-
expect(
|
|
119
|
+
it('should create Communication', async () => {
|
|
120
|
+
expect(connector).toHaveBeenCalledWith('bindings-amqp', locator)
|
|
126
121
|
})
|
|
127
122
|
}
|
|
128
|
-
|
|
129
|
-
it('should create Communication', async () => {
|
|
130
|
-
const pointer = Pointer.mock.instances[0]
|
|
131
|
-
|
|
132
|
-
expect(Communication).toHaveBeenCalledWith(pointer)
|
|
133
|
-
})
|
|
134
123
|
})
|
|
135
124
|
|
|
136
125
|
describe('Producer', () => {
|
|
137
126
|
beforeEach(() => {
|
|
138
127
|
producer = factory.producer(locator, endpoints, component)
|
|
139
|
-
comm =
|
|
128
|
+
comm = connector.mock.results[0].value
|
|
140
129
|
})
|
|
141
130
|
|
|
142
131
|
it('should create instance', async () => {
|
|
@@ -148,7 +137,7 @@ describe('Producer', () => {
|
|
|
148
137
|
describe('Consumer', () => {
|
|
149
138
|
beforeEach(() => {
|
|
150
139
|
consumer = factory.consumer(locator, endpoint)
|
|
151
|
-
comm =
|
|
140
|
+
comm = connector.mock.results[0].value
|
|
152
141
|
})
|
|
153
142
|
|
|
154
143
|
it('should create instance', async () => {
|
|
@@ -160,7 +149,7 @@ describe('Consumer', () => {
|
|
|
160
149
|
describe('Emitter', () => {
|
|
161
150
|
beforeEach(() => {
|
|
162
151
|
emitter = factory.emitter(locator, endpoint)
|
|
163
|
-
comm =
|
|
152
|
+
comm = connector.mock.results[0].value
|
|
164
153
|
})
|
|
165
154
|
|
|
166
155
|
it('should create instance', async () => {
|
|
@@ -171,12 +160,12 @@ describe('Emitter', () => {
|
|
|
171
160
|
|
|
172
161
|
describe('Receiver', () => {
|
|
173
162
|
beforeEach(() => {
|
|
174
|
-
receiver = factory.receiver(locator,
|
|
175
|
-
comm =
|
|
163
|
+
receiver = factory.receiver(locator, label, group, processor)
|
|
164
|
+
comm = connector.mock.results[0].value
|
|
176
165
|
})
|
|
177
166
|
|
|
178
167
|
it('should create instance', async () => {
|
|
179
|
-
expect(Receiver).toHaveBeenCalledWith(comm,
|
|
168
|
+
expect(Receiver).toHaveBeenCalledWith(comm, label, group, processor)
|
|
180
169
|
expect(receiver).toStrictEqual(Receiver.mock.instances[0])
|
|
181
170
|
})
|
|
182
171
|
})
|
|
@@ -184,7 +173,7 @@ describe('Receiver', () => {
|
|
|
184
173
|
describe('Broadcast', () => {
|
|
185
174
|
beforeEach(() => {
|
|
186
175
|
broadcast = factory.broadcast(name, group)
|
|
187
|
-
comm =
|
|
176
|
+
comm = connector.mock.results[0].value
|
|
188
177
|
})
|
|
189
178
|
|
|
190
179
|
it('should create Locator', async () => {
|
package/test/receiver.test.js
CHANGED
|
@@ -19,9 +19,8 @@ it('should be', async () => {
|
|
|
19
19
|
/** @type {jest.MockedObject<toa.amqp.Communication>} */
|
|
20
20
|
const comm = mock.communication()
|
|
21
21
|
|
|
22
|
-
const
|
|
22
|
+
const exchange = generate()
|
|
23
23
|
const group = generate()
|
|
24
|
-
const label = generate()
|
|
25
24
|
|
|
26
25
|
const processor = /** @type {jest.MockedObject<toa.core.Receiver>} */ {
|
|
27
26
|
connect: jest.fn(async () => undefined),
|
|
@@ -35,7 +34,7 @@ let receiver
|
|
|
35
34
|
beforeEach(() => {
|
|
36
35
|
jest.clearAllMocks()
|
|
37
36
|
|
|
38
|
-
receiver = new Receiver(comm,
|
|
37
|
+
receiver = new Receiver(comm, exchange, group, processor)
|
|
39
38
|
})
|
|
40
39
|
|
|
41
40
|
it('should be instance of Connector', async () => {
|
|
@@ -49,10 +48,6 @@ it('should depend on communication', async () => {
|
|
|
49
48
|
it('should consume events', async () => {
|
|
50
49
|
await receiver.open()
|
|
51
50
|
|
|
52
|
-
expect(mock.queues.name).toHaveBeenCalledWith(locator, label)
|
|
53
|
-
|
|
54
|
-
const exchange = mock.queues.name.mock.results[0].value
|
|
55
|
-
|
|
56
51
|
expect(comm.consume).toHaveBeenCalledWith(exchange, group, expect.any(Function))
|
|
57
52
|
|
|
58
53
|
const callback = comm.consume.mock.calls[0][2]
|
package/source/communication.js
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const { connect } = require('comq')
|
|
4
|
-
const { Connector } = require('@toa.io/core')
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* @implements {toa.amqp.Communication}
|
|
8
|
-
*/
|
|
9
|
-
class Communication extends Connector {
|
|
10
|
-
/** @type {toa.pointer.Pointer} */
|
|
11
|
-
#pointer
|
|
12
|
-
|
|
13
|
-
/** @type {comq.IO} */
|
|
14
|
-
#io
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* @param {toa.pointer.Pointer} pointer
|
|
18
|
-
*/
|
|
19
|
-
constructor (pointer) {
|
|
20
|
-
super()
|
|
21
|
-
|
|
22
|
-
this.#pointer = pointer
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
async open () {
|
|
26
|
-
this.#io = await connect(this.#pointer.reference)
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
async close () {
|
|
30
|
-
await this.#io.seal()
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
async dispose () {
|
|
34
|
-
await this.#io.close()
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
async reply (queue, process) {
|
|
38
|
-
await this.#io.reply(queue, process)
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
async request (queue, request) {
|
|
42
|
-
return this.#io.request(queue, request)
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
async emit (exchange, message) {
|
|
46
|
-
await this.#io.emit(exchange, message)
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
async consume (exchange, group, consumer) {
|
|
50
|
-
await this.#io.consume(exchange, group, consumer)
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
exports.Communication = Communication
|
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const { generate } = require('randomstring')
|
|
4
|
-
const { Connector } = require('@toa.io/core')
|
|
5
|
-
const mock = require('./comq.mock')
|
|
6
|
-
|
|
7
|
-
jest.mock('comq', () => mock.comq)
|
|
8
|
-
|
|
9
|
-
const { Communication } = require('../source/communication')
|
|
10
|
-
|
|
11
|
-
it('should be', async () => {
|
|
12
|
-
expect(Communication).toBeDefined()
|
|
13
|
-
})
|
|
14
|
-
|
|
15
|
-
/** @type {toa.pointer.Pointer} */
|
|
16
|
-
let pointer
|
|
17
|
-
|
|
18
|
-
/** @type {toa.amqp.Communication} */
|
|
19
|
-
let comm
|
|
20
|
-
|
|
21
|
-
beforeEach(() => {
|
|
22
|
-
jest.clearAllMocks()
|
|
23
|
-
|
|
24
|
-
pointer = /** @type {toa.pointer.Pointer} */ { reference: generate() }
|
|
25
|
-
comm = new Communication(pointer)
|
|
26
|
-
})
|
|
27
|
-
|
|
28
|
-
it('should be instance of Connector', async () => {
|
|
29
|
-
expect(comm).toBeInstanceOf(Connector)
|
|
30
|
-
})
|
|
31
|
-
|
|
32
|
-
it('should connect to a given pointer reference', async () => {
|
|
33
|
-
await comm.open()
|
|
34
|
-
|
|
35
|
-
expect(mock.comq.connect).toHaveBeenCalledWith(pointer.reference)
|
|
36
|
-
})
|
|
37
|
-
|
|
38
|
-
describe('connected', () => {
|
|
39
|
-
/** @type {jest.MockedObject<comq.IO>} */
|
|
40
|
-
let io
|
|
41
|
-
|
|
42
|
-
beforeEach(async () => {
|
|
43
|
-
await comm.open()
|
|
44
|
-
|
|
45
|
-
io = await mock.comq.connect.mock.results[0].value
|
|
46
|
-
})
|
|
47
|
-
|
|
48
|
-
it('should close', async () => {
|
|
49
|
-
await comm.close()
|
|
50
|
-
|
|
51
|
-
expect(io.seal).toHaveBeenCalled()
|
|
52
|
-
})
|
|
53
|
-
|
|
54
|
-
it('should dispose', async () => {
|
|
55
|
-
await comm.dispose()
|
|
56
|
-
|
|
57
|
-
expect(io.close).toHaveBeenCalled()
|
|
58
|
-
})
|
|
59
|
-
|
|
60
|
-
it('should bind reply', async () => {
|
|
61
|
-
const queue = generate()
|
|
62
|
-
const producer = jest.fn(async () => undefined)
|
|
63
|
-
|
|
64
|
-
await comm.reply(queue, producer)
|
|
65
|
-
|
|
66
|
-
expect(io.reply).toHaveBeenCalledWith(queue, producer)
|
|
67
|
-
})
|
|
68
|
-
|
|
69
|
-
it('should send request', async () => {
|
|
70
|
-
const queue = generate()
|
|
71
|
-
const request = generate()
|
|
72
|
-
|
|
73
|
-
const reply = await comm.request(queue, request)
|
|
74
|
-
|
|
75
|
-
expect(io.request).toHaveBeenCalledWith(queue, request)
|
|
76
|
-
expect(reply).toStrictEqual(await io.request.mock.results[0].value)
|
|
77
|
-
})
|
|
78
|
-
|
|
79
|
-
it('should emit', async () => {
|
|
80
|
-
const exchange = generate()
|
|
81
|
-
const message = generate()
|
|
82
|
-
|
|
83
|
-
await comm.emit(exchange, message)
|
|
84
|
-
|
|
85
|
-
expect(io.emit).toHaveBeenCalledWith(exchange, message)
|
|
86
|
-
})
|
|
87
|
-
|
|
88
|
-
it('should consume', async () => {
|
|
89
|
-
const exchange = generate()
|
|
90
|
-
const group = generate()
|
|
91
|
-
const consumer = jest.fn(async () => undefined)
|
|
92
|
-
|
|
93
|
-
await comm.consume(exchange, group, consumer)
|
|
94
|
-
|
|
95
|
-
expect(io.consume).toHaveBeenCalledWith(exchange, group, consumer)
|
|
96
|
-
})
|
|
97
|
-
})
|
package/test/comq.mock.js
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const { generate } = require('randomstring')
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* @return {jest.MockedObject<comq.IO>}
|
|
7
|
-
*/
|
|
8
|
-
const io = () => (/** @type {jest.MockedObject<comq.IO>} */ {
|
|
9
|
-
request: jest.fn(async () => generate()),
|
|
10
|
-
reply: jest.fn(async () => undefined),
|
|
11
|
-
emit: jest.fn(async () => undefined),
|
|
12
|
-
consume: jest.fn(async () => undefined),
|
|
13
|
-
seal: jest.fn(async () => undefined),
|
|
14
|
-
close: jest.fn(async () => undefined)
|
|
15
|
-
})
|
|
16
|
-
|
|
17
|
-
const comq = {
|
|
18
|
-
connect: jest.fn(async () => io())
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
exports.comq = comq
|
package/types/communication.d.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { Connector } from '@toa.io/core/types'
|
|
2
|
-
import * as comq from 'comq'
|
|
3
|
-
|
|
4
|
-
declare namespace toa.amqp {
|
|
5
|
-
|
|
6
|
-
interface Communication extends Connector {
|
|
7
|
-
request(queue: string, request: any): Promise<any>
|
|
8
|
-
|
|
9
|
-
reply(queue: string, process: comq.producer): Promise<void>
|
|
10
|
-
|
|
11
|
-
emit(exchange: string, message: any): Promise<void>
|
|
12
|
-
|
|
13
|
-
consume(exchange: string, group: string, callback: comq.consumer): Promise<void>
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
}
|