-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
mqemitter-redis.js
225 lines (172 loc) · 5.43 KB
/
mqemitter-redis.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
'use strict'
const Redis = require('ioredis')
const MQEmitter = require('mqemitter')
const hyperid = require('hyperid')()
const inherits = require('inherits')
const { LRUCache } = require('lru-cache')
const msgpack = require('msgpack-lite')
const EE = require('events').EventEmitter
function MQEmitterRedis (opts) {
if (!(this instanceof MQEmitterRedis)) {
return new MQEmitterRedis(opts)
}
opts = { ...opts }
opts.enableAutoPipelining = true
this._opts = opts
this.subConn = opts.subConn || new Redis(opts.connectionString || opts)
this.pubConn = opts.pubConn || new Redis(opts.connectionString || opts)
this._topics = {}
this._cache = new LRUCache({
max: 10000,
ttl: 60 * 1000 // one minute
})
this.state = new EE()
const that = this
function onError (err) {
if (err && !that.closing) {
that.state.emit('error', err)
}
}
this._onError = onError
function handler (sub, topic, payload) {
const packet = msgpack.decode(payload)
if (!that._cache.get(packet.id)) {
that._emit(packet.msg)
}
that._cache.set(packet.id, true)
}
this.subConn.on('messageBuffer', function (topic, message) {
handler(topic, topic, message)
})
this.subConn.on('pmessageBuffer', function (sub, topic, message) {
handler(sub, topic, message)
})
this.subConn.on('connect', function () {
that.state.emit('subConnect')
})
this.subConn.on('error', function (err) {
that._onError(err)
})
this.pubConn.on('connect', function () {
that.state.emit('pubConnect')
})
this.pubConn.on('error', function (err) {
that._onError(err)
})
MQEmitter.call(this, opts)
this._opts.regexWildcardOne = new RegExp(this._opts.wildcardOne.replace(/([/,!\\^${}[\]().*+?|<>\-&])/g, '\\$&'), 'g')
this._opts.regexWildcardSome = new RegExp((this._opts.matchEmptyLevels ? this._opts.separator.replace(/([/,!\\^${}[\]().*+?|<>\-&])/g, '\\$&') + '?' : '') + this._opts.wildcardSome.replace(/([/,!\\^${}[\]().*+?|<>\-&])/g, '\\$&'), 'g')
}
inherits(MQEmitterRedis, MQEmitter)
;['emit', 'on', 'removeListener', 'close'].forEach(function (name) {
MQEmitterRedis.prototype['_' + name] = MQEmitterRedis.prototype[name]
})
MQEmitterRedis.prototype.close = function (cb) {
cb = cb || noop
if (this.closed || this.closing) {
return cb()
}
this.closing = true
let count = 2
const that = this
function onEnd () {
if (--count === 0) {
that._close(cb)
}
}
this.subConn.on('end', onEnd)
this.subConn.quit()
this.pubConn.on('end', onEnd)
this.pubConn.quit()
return this
}
MQEmitterRedis.prototype._subTopic = function (topic) {
return topic
.replace(this._opts.regexWildcardOne, '*')
.replace(this._opts.regexWildcardSome, '*')
}
MQEmitterRedis.prototype.on = function on (topic, cb, done) {
const subTopic = this._subTopic(topic)
const onFinish = function () {
if (done) {
setImmediate(done)
}
}
this._on(topic, cb)
if (this._topics[subTopic]) {
this._topics[subTopic]++
onFinish.call(this)
return this
}
this._topics[subTopic] = 1
if (this._containsWildcard(topic)) {
this.subConn.psubscribe(subTopic, onFinish.bind(this))
} else {
this.subConn.subscribe(subTopic, onFinish.bind(this))
}
return this
}
MQEmitterRedis.prototype.emit = function (msg, done) {
done = done || this._onError
if (this.closed) {
const err = new Error('mqemitter-redis is closed')
return done(err)
}
const packet = {
id: hyperid(),
msg
}
this.pubConn.publish(msg.topic, msgpack.encode(packet)).then(() => done()).catch(done)
}
MQEmitterRedis.prototype.removeListener = function (topic, cb, done) {
const subTopic = this._subTopic(topic)
const onFinish = function () {
if (done) {
setImmediate(done)
}
}
this._removeListener(topic, cb)
if (--this._topics[subTopic] > 0) {
onFinish()
return this
}
delete this._topics[subTopic]
if (this._containsWildcard(topic)) {
this.subConn.punsubscribe(subTopic, onFinish)
} else if (this._matcher.match(topic)) {
this.subConn.unsubscribe(subTopic, onFinish)
}
return this
}
MQEmitterRedis.prototype._containsWildcard = function (topic) {
return (topic.indexOf(this._opts.wildcardOne) >= 0) ||
(topic.indexOf(this._opts.wildcardSome) >= 0)
}
function noop () {}
module.exports = MQEmitterRedis
function MQEmitterRedisPrefix (pubSubPrefix, options) {
MQEmitterRedis.call(this, options)
this._pubSubPrefix = pubSubPrefix
this._sym_proxiedCallback = Symbol('proxiedCallback')
}
inherits(MQEmitterRedisPrefix, MQEmitterRedis)
MQEmitterRedisPrefix.prototype.on = function (topic, cb, done) {
const t = this._pubSubPrefix + topic
cb[this._sym_proxiedCallback] = function (packet, cbcb) {
const t = packet.topic.slice(this._pubSubPrefix.length)
const p = { ...packet, topic: t }
return cb.call(this, p, cbcb)
}.bind(this)
return MQEmitterRedis.prototype.on.call(this, t, cb[this._sym_proxiedCallback], done)
}
MQEmitterRedisPrefix.prototype.removeListener = function (topic, func, done) {
const t = this._pubSubPrefix + topic
const f = func[this._sym_proxiedCallback]
return MQEmitterRedis.prototype.removeListener.call(this, t, f, done)
}
MQEmitterRedisPrefix.prototype.emit = function (packet, done) {
const t = this._pubSubPrefix + packet.topic
const p = { ...packet, topic: t }
return MQEmitterRedis.prototype.emit.call(this, p, done)
}
module.exports.MQEmitterRedisPrefix = MQEmitterRedisPrefix