@toa.io/bindings.amqp 0.1.0-alpha.12

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/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2020-present Artem Gurtovoi
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "@toa.io/bindings.amqp",
3
+ "version": "0.1.0-alpha.12+7af8037",
4
+ "description": "Toa AMQP Binding",
5
+ "author": "temich <tema.gurtovoy@gmail.com>",
6
+ "homepage": "https://github.com/toa-io/toa#readme",
7
+ "main": "src/index.js",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/toa-io/toa.git"
11
+ },
12
+ "bugs": {
13
+ "url": "https://github.com/toa-io/toa/issues"
14
+ },
15
+ "publishConfig": {
16
+ "access": "public"
17
+ },
18
+ "scripts": {
19
+ "test": "echo \"Error: run tests from root\" && exit 1"
20
+ },
21
+ "dependencies": {
22
+ "amqplib": "0.8.0",
23
+ "execa": "5.1.1"
24
+ },
25
+ "gitHead": "7af80377d59d326298804b1c10c8dffde54abd16"
26
+ }
@@ -0,0 +1,31 @@
1
+ 'use strict'
2
+
3
+ const { Connector } = require('@toa.io/core')
4
+ const { newid } = require('@toa.io/gears')
5
+
6
+ class Broadcast extends Connector {
7
+ #group
8
+ #channel
9
+ #prefix
10
+
11
+ constructor (channel, prefix, group) {
12
+ super()
13
+
14
+ this.#group = group === undefined ? newid() : group
15
+
16
+ this.#channel = channel
17
+ this.#prefix = prefix + '.'
18
+
19
+ this.depends(channel)
20
+ }
21
+
22
+ async send (label, payload) {
23
+ await this.#channel.publish(this.#prefix + label, payload, { expiration: 10, persistent: false })
24
+ }
25
+
26
+ async receive (label, callback) {
27
+ await this.#channel.subscribe(this.#prefix + label, this.#group, callback)
28
+ }
29
+ }
30
+
31
+ exports.Broadcast = Broadcast
package/src/channel.js ADDED
@@ -0,0 +1,127 @@
1
+ 'use strict'
2
+
3
+ const { Connector } = require('@toa.io/core')
4
+ const { console, newid } = require('@toa.io/gears')
5
+
6
+ const { pack, unpack } = require('./message')
7
+
8
+ class Channel extends Connector {
9
+ #id
10
+ #connection
11
+ #channel
12
+
13
+ #bound = {}
14
+ #expected = {}
15
+
16
+ constructor (connection) {
17
+ super()
18
+
19
+ this.#id = newid()
20
+ this.#connection = connection
21
+
22
+ this.depends(connection)
23
+ }
24
+
25
+ async connection () {
26
+ this.#channel = await this.#connection.channel()
27
+ }
28
+
29
+ async request (label, request) {
30
+ const queue = 'request.' + label
31
+ const correlation = newid()
32
+
33
+ await this.#channel.assertQueue(queue, QUEUE)
34
+ await this.#bind(label)
35
+
36
+ const message = pack(request)
37
+ const properties = { replyTo: this.#id, correlationId: correlation }
38
+
39
+ await this.#channel.sendToQueue(queue, message, properties)
40
+
41
+ const reply = await this.#reply(label, correlation)
42
+
43
+ return unpack(reply.content)
44
+ }
45
+
46
+ async reply (label, invocation) {
47
+ const queue = 'request.' + label
48
+ const exchange = 'reply.' + label
49
+
50
+ await this.#channel.assertQueue(queue, QUEUE)
51
+ await this.#channel.assertExchange(exchange, 'direct', EXCHANGE)
52
+
53
+ await this.#channel.consume(queue, async (received) => {
54
+ const content = unpack(received.content)
55
+ const reply = await invocation(content)
56
+
57
+ const message = pack(reply)
58
+ const properties = { correlationId: received.properties.correlationId }
59
+
60
+ await this.#channel.publish(exchange, received.properties.replyTo, message, properties)
61
+ await this.#channel.ack(received)
62
+ })
63
+ }
64
+
65
+ async publish (label, payload, options) {
66
+ const exchange = 'event.' + label
67
+
68
+ await this.#channel.assertExchange(exchange, 'fanout', EXCHANGE)
69
+
70
+ const message = pack(payload)
71
+
72
+ await this.#channel.publish(exchange, '', message, options)
73
+ }
74
+
75
+ async subscribe (label, id, callback) {
76
+ const exchange = 'event.' + label
77
+ const queue = exchange + '..' + id
78
+
79
+ await this.#channel.assertExchange(exchange, 'fanout', EXCHANGE)
80
+ await this.#channel.assertQueue(queue, QUEUE)
81
+ await this.#channel.bindQueue(queue, exchange, '')
82
+
83
+ await this.#channel.consume(queue, async (received) => {
84
+ const content = unpack(received.content)
85
+ await callback(content)
86
+ await this.#channel.ack(received)
87
+ })
88
+ }
89
+
90
+ async #bind (label) {
91
+ const queue = `reply.${label}.${this.#id}`
92
+ const exchange = 'reply.' + label
93
+
94
+ if (this.#bound[queue]) return
95
+
96
+ await this.#channel.assertQueue(queue, { exclusive: true, ...QUEUE })
97
+ await this.#channel.assertExchange(exchange, 'direct', EXCHANGE)
98
+ await this.#channel.bindQueue(queue, exchange, this.#id)
99
+ await this.#channel.consume(queue, (message) => this.#replies(message))
100
+
101
+ this.#bound[queue] = true
102
+ }
103
+
104
+ #reply (label, correlation) {
105
+ return new Promise((resolve) => (this.#expected[correlation] = resolve))
106
+ }
107
+
108
+ async #replies (message) {
109
+ const correlation = message.properties.correlationId
110
+ const resolve = this.#expected[correlation]
111
+
112
+ if (!resolve) {
113
+ console.warn(`Unexpected reply '${correlation}'. Possible message redelivery.`)
114
+ return
115
+ }
116
+
117
+ resolve(message)
118
+
119
+ delete this.#expected[correlation]
120
+ await this.#channel.ack(message)
121
+ }
122
+ }
123
+
124
+ const QUEUE = { durable: process.env.TOA_ENV !== 'dev' }
125
+ const EXCHANGE = { durable: process.env.TOA_ENV !== 'dev' }
126
+
127
+ exports.Channel = Channel
@@ -0,0 +1,43 @@
1
+ 'use strict'
2
+
3
+ const amqp = require('amqplib')
4
+
5
+ const { Connector } = require('@toa.io/core')
6
+ const { console } = require('@toa.io/gears')
7
+
8
+ class Connection extends Connector {
9
+ #url
10
+ #connection
11
+
12
+ constructor (host) {
13
+ super()
14
+
15
+ this.#url = Connection.#locate(host)
16
+ }
17
+
18
+ async connection () {
19
+ this.#connection = await amqp.connect(this.#url)
20
+ console.info(`AMQP Binding connected to ${this.#url}`)
21
+ }
22
+
23
+ async disconnection () {
24
+ // TODO: handle current operations
25
+ // http://www.squaremobius.net/amqp.node/channel_api.html#model_close
26
+ await this.#connection.close()
27
+ console.info(`AMQP Binding disconnected from ${this.#url}`)
28
+ }
29
+
30
+ async channel () {
31
+ return this.#connection.createChannel()
32
+ }
33
+
34
+ static #locate (host) {
35
+ // TODO: read ./deployments.js
36
+ const user = 'user'
37
+ const password = 'password'
38
+
39
+ return `amqp://${user}:${password}@${host}`
40
+ }
41
+ }
42
+
43
+ exports.Connection = Connection
@@ -0,0 +1,25 @@
1
+ 'use strict'
2
+
3
+ const { Connector } = require('@toa.io/core')
4
+
5
+ const { name } = require('./queue')
6
+
7
+ class Consumer extends Connector {
8
+ #channel
9
+ #queue
10
+
11
+ constructor (channel, locator, endpoint) {
12
+ super()
13
+
14
+ this.#channel = channel
15
+ this.#queue = name(locator, endpoint)
16
+
17
+ this.depends(channel)
18
+ }
19
+
20
+ async request (request) {
21
+ return this.#channel.request(this.#queue, request)
22
+ }
23
+ }
24
+
25
+ exports.Consumer = Consumer
@@ -0,0 +1,28 @@
1
+ 'use strict'
2
+
3
+ const deployments = () => {
4
+ const fullname = 'rabbitmq'
5
+
6
+ // TODO: provide passwords as secrets for component containers
7
+ const user = 'user'
8
+ const password = 'password'
9
+ const erlangCookie = 'cookie'
10
+
11
+ return [{
12
+ chart: {
13
+ name: 'rabbitmq',
14
+ version: '8.24.3',
15
+ repository: 'https://charts.bitnami.com/bitnami'
16
+ },
17
+ values: {
18
+ fullnameOverride: fullname,
19
+ auth: {
20
+ user,
21
+ password,
22
+ erlangCookie
23
+ }
24
+ }
25
+ }]
26
+ }
27
+
28
+ exports.deployments = deployments
package/src/emitter.js ADDED
@@ -0,0 +1,28 @@
1
+ 'use strict'
2
+
3
+ const { Connector } = require('@toa.io/core')
4
+ const { name } = require('./queue')
5
+
6
+ class Emitter extends Connector {
7
+ #channel
8
+ #locator
9
+ #label
10
+
11
+ constructor (channel, locator, label) {
12
+ super()
13
+
14
+ this.#channel = channel
15
+ this.#locator = locator
16
+ this.#label = label
17
+
18
+ this.depends(channel)
19
+ }
20
+
21
+ async emit (payload) {
22
+ const label = name(this.#locator, this.#label)
23
+
24
+ await this.#channel.publish(label, payload)
25
+ }
26
+ }
27
+
28
+ exports.Emitter = Emitter
package/src/factory.js ADDED
@@ -0,0 +1,58 @@
1
+ 'use strict'
2
+
3
+ const { Locator } = require('@toa.io/core')
4
+
5
+ const { Channel } = require('./channel')
6
+ const { Consumer } = require('./consumer')
7
+ const { Producer } = require('./producer')
8
+ const { Emitter } = require('./emitter')
9
+ const { Receiver } = require('./receiver')
10
+ const { Broadcast } = require('./broadcast')
11
+ const { Connection } = require('./connection')
12
+
13
+ class Factory {
14
+ #connections = {}
15
+
16
+ producer (locator, endpoints, producer) {
17
+ const channel = this.#channel(locator)
18
+
19
+ return new Producer(channel, locator, endpoints, producer)
20
+ }
21
+
22
+ consumer (locator, endpoint) {
23
+ const channel = this.#channel(locator)
24
+
25
+ return new Consumer(channel, locator, endpoint)
26
+ }
27
+
28
+ emitter (locator, label) {
29
+ const channel = this.#channel(locator)
30
+
31
+ return new Emitter(channel, locator, label)
32
+ }
33
+
34
+ receiver (locator, label, id, receiver) {
35
+ const channel = this.#channel(locator)
36
+
37
+ return new Receiver(channel, locator, label, id, receiver)
38
+ }
39
+
40
+ broadcast (name, group) {
41
+ const locator = new Locator()
42
+ const channel = this.#channel(locator)
43
+
44
+ return new Broadcast(channel, name, group)
45
+ }
46
+
47
+ #channel () {
48
+ const host = 'rabbitmq' // locator.host('rabbitmq')
49
+
50
+ if (this.#connections[host] === undefined) {
51
+ this.#connections[host] = new Connection(host)
52
+ }
53
+
54
+ return new Channel(this.#connections[host])
55
+ }
56
+ }
57
+
58
+ exports.Factory = Factory
package/src/index.js ADDED
@@ -0,0 +1,8 @@
1
+ 'use strict'
2
+
3
+ const { Factory } = require('./factory')
4
+ const { deployments } = require('./deployments')
5
+
6
+ exports.Factory = Factory
7
+ exports.properties = { async: true }
8
+ exports.deployments = deployments
package/src/message.js ADDED
@@ -0,0 +1,7 @@
1
+ 'use strict'
2
+
3
+ const pack = (content) => Buffer.from(JSON.stringify(content))
4
+ const unpack = (content) => JSON.parse(content)
5
+
6
+ exports.pack = pack
7
+ exports.unpack = unpack
@@ -0,0 +1,36 @@
1
+ 'use strict'
2
+
3
+ const { Connector } = require('@toa.io/core')
4
+
5
+ const { name } = require('./queue')
6
+
7
+ class Producer extends Connector {
8
+ #channel
9
+ #locator
10
+ #producer
11
+ #endpoints
12
+
13
+ constructor (channel, locator, endpoints, producer) {
14
+ super()
15
+
16
+ this.#channel = channel
17
+ this.#locator = locator
18
+ this.#endpoints = endpoints
19
+ this.#producer = producer
20
+
21
+ this.depends(channel)
22
+ this.depends(producer)
23
+ }
24
+
25
+ async connection () {
26
+ return Promise.all(this.#endpoints.map((endpoint) => this.#endpoint(endpoint)))
27
+ }
28
+
29
+ async #endpoint (endpoint) {
30
+ const queue = name(this.#locator, endpoint)
31
+
32
+ await this.#channel.reply(queue, async (request) => this.#producer.invoke(endpoint, request))
33
+ }
34
+ }
35
+
36
+ exports.Producer = Producer
package/src/queue.js ADDED
@@ -0,0 +1,7 @@
1
+ 'use strict'
2
+
3
+ const { concat } = require('@toa.io/gears')
4
+
5
+ const name = (locator, endpoint) => locator.domain + '.' + concat(locator.name, '.') + endpoint
6
+
7
+ exports.name = name
@@ -0,0 +1,29 @@
1
+ 'use strict'
2
+
3
+ const { Connector } = require('@toa.io/core')
4
+
5
+ const { name } = require('./queue')
6
+
7
+ class Receiver extends Connector {
8
+ #channel
9
+ #receiver
10
+ #label
11
+ #id
12
+
13
+ constructor (channel, locator, label, id, receiver) {
14
+ super()
15
+
16
+ this.#channel = channel
17
+ this.#receiver = receiver
18
+ this.#label = name(locator, label)
19
+ this.#id = id
20
+
21
+ this.depends(channel)
22
+ }
23
+
24
+ async connection () {
25
+ await this.#channel.subscribe(this.#label, this.#id, (payload) => this.#receiver.receive(payload))
26
+ }
27
+ }
28
+
29
+ exports.Receiver = Receiver