-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathclient.js
137 lines (106 loc) · 3.79 KB
/
client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { v4 as uuid } from 'uuid'
import { Subject } from 'rxjs/Subject'
import curry from 'lodash.curry'
import 'rxjs/add/operator/first'
import 'rxjs/add/operator/partition'
import { connect } from './connection'
import { openChannel } from './channel'
import { Router } from './router'
import * as logging from './logging'
const PUB_OPTIONS = { contentEncoding: 'utf-8', contentType: 'application/json' }
const toBuffer = obj => Buffer.from(JSON.stringify(obj, null, '\t'))
export class ReactiveMQ {
static create(options) {
return new ReactiveMQ(options)
}
get channelAsPromised() {
return this.rxChannel
.filter(channel => channel)
.first()
.toPromise()
}
get commonOptions() {
return { logger: this.logger, connectionId: this.connectionId }
}
constructor({ appId = 'rx-amqp-client', logger = console, ...options }) {
this.appId = appId
this.logger = logger
this.connectionId = options.connectionId || (options.url && options.url.vhost)
this.loggingPrefix = this.connectionId ? `Client:${this.connectionId}` : 'Client'
this.rxConnection = connect(options.url, this.commonOptions)
this.rxChannel = openChannel(this.rxConnection, this.commonOptions)
this.replyQueues = new Set()
this.requests = new Map()
this.pubOptions = { appId: this.appId, ...PUB_OPTIONS }
if (options.routerConfig) {
this.connectRouter(options.routerConfig)
}
this.curry()
this.watchChannel()
}
curry() {
this.request = curry(this._request.bind(this)) // eslint-disable-line no-underscore-dangle
this.publish = curry(this._publish.bind(this)) // eslint-disable-line no-underscore-dangle
}
watchChannel() {
this.rxChannel
.filter(channel => !channel)
.subscribe(() => this.replyQueues.clear())
}
async connectRouter(routerConfig) {
if (this.router) { return }
if (!routerConfig) {
throw new Error(`[${this.loggingPrefix}]: "config.routerConfig" is required to start routing`)
}
if (routerConfig) {
this.router = Router.create({
...routerConfig,
...this.commonOptions,
appId: this.appId,
channel: this.rxChannel
})
}
}
async _request(exchange, replyTo, routingKey, message) {
const correlationId = uuid()
this.requests.set(correlationId, new Subject())
const channel = await this.channelAsPromised
await channel.assertQueue(replyTo, { exclusive: true })
this.assertConsume(channel, replyTo, this.resolveReply)
this.log(logging.formatOutgoingRequest(correlationId, routingKey, this.appId), message)
channel.publish(exchange, routingKey, toBuffer(message), {
replyTo,
correlationId,
...this.pubOptions
})
return this.requests.get(correlationId).first().toPromise().then(({ data }) => data)
}
assertConsume(channel, queue, handler) {
if (!this.replyQueues.has(queue)) {
this.replyQueues.add(queue)
return channel.consume(queue, handler.bind(this), { noAck: true })
}
return Promise.resolve(channel)
}
resolveReply(message) {
if (this.requests.has(message.properties.correlationId)) {
const reply = JSON.parse(message.content.toString())
this.requests
.get(message.properties.correlationId)
.next(reply)
this.log(logging.formatIncomingResponse(message, reply.error), reply)
}
}
async _publish(exchange, routingKey, message) {
const channel = await this.channelAsPromised
this.log(logging.formatEvent(routingKey, this.appId), message)
return channel.publish(exchange, routingKey, toBuffer(message), this.pubOptions)
}
log(message, data) {
if (!this.logger) { return }
this.logger.log(logging.formatMeta(this.loggingPrefix, message))
if (data) {
this.logger.dir(data, { colors: true, depth: 10 })
}
}
}