-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
209 lines (188 loc) · 7.63 KB
/
index.js
File metadata and controls
209 lines (188 loc) · 7.63 KB
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
const _ = require('lodash');
const config = require('@cheevr/config');
const globalConfig = require('@cheevr/config').addDefaultConfig(__dirname, 'config');
const EventEmitter = require('events').EventEmitter;
/**
* The standard callback definition that can be used anywhere where standards are followed.
* @typedef {function} Callback
* @param {Error|string|null} [err] Will contain error information if there's has been one
* @param {*} [...args]
*/
/**
* The generic instance configuration that will be implemented by the individual instance implementations
* @typedef {object} InstanceConfig
* @property {string} type The type of message service to use
* @abstract
*/
/**
* This event is fired whenever a connection to the RabbitMQ server has been established
* @event Instance#connected
* @param {Instance} instance The instance that emitted the event
*/
/**
* This event is fired whenever a connection to the RabbitMQ server is lost
* @event Instance#disconnected
* @param {Instance} instance The instance that emitted the event
*/
/**
* This event is fired whenever a connection to the RabbitMQ server is interrupted (as in unexpectedly disconnected)
* @event Instance#interrupted
* @param {Instance} instance The instance that emitted the event
*/
/**
* This event is fired whenever a connection to the RabbitMQ server has been established after it has been lost
* through an interrupt.
* @event Instance#reconnected
* @param {Instance} instance The instance that emitted the event
*/
/**
* Whenever this host or one of the channels emits an error it will emitted as an error
* @event Instance#error
* @param {Error|string} The error that occurred
* @param {Instance} instance The instance that emitted the event
* @param {Channel} [channel] If the error was emitted from the channel this is the instance that emitted it
*/
class Manager extends EventEmitter {
constructor() {
super();
this.reset();
}
/**
* Resets and removes all instances. Allows to optionally pass in a configuration.
* @param {Object<string, InstanceConfig>} [config] A map with names pointing to instance configuration
* @returns {Instance.Manager}
*/
reset(config = _.cloneDeep(globalConfig.queue)) {
this._instances = {};
config && this.configure(config);
return this;
}
/**
* Set the configuration for the message queues manually.
* @param {Object<string, InstanceConfig>} config A map with names pointing to instance configuration
* @param {boolean} [merge=false] Whether to merge the given config with the current one.
* @returns {Instance.Manager}
*/
configure(config, merge) {
merge ? Object.assign(this._config, config) : this._config = config;
return this;
}
/**
* Returns the instance object that holds information about all the channels it has.
* @param {string} [name=_default_] The name of instance to return.
* @returns {Instance}
*/
instance(name = '_default_') {
let opts = this._config[name];
if (!opts) {
opts = config.defaults.queue[config.defaults.queue.defaultType].instance;
}
let type = (opts && opts.type) || config.defaults.queue.defaultType;
this._instances[name] = this._instances[name] || new (require('./' + type))(name, opts);
this._instances[name].on('error', err => this.emit('error', err));
return this._instances[name];
}
/**
* Returns a channel object that you can send, receive or listen for messages on.
* @param {string} name The name of the channel you want to operate on
* @param {string} [instanceName=_default_] The name name of the message queue instance
* @returns {Channel}
*/
queue(name, instanceName) {
return this.instance(instanceName).channel(name);
}
/**
* Checks each instance and channel if they are connected.
* @returns {boolean}
*/
ready() {
for (let name in this._instances) {
if (!this._instances[name].ready) {
return false;
}
}
return true;
}
/**
* Middleware function that will add an .mq property on the request that gives access to the message queue system.
* The default message queue will be made available right on the .mq object, others will be reachable through their
* name on .mq.<queueName>. Should there be no default queue defined, the first one found will be used as default.
* @returns {function} The middleware function that can be used by any standard express format web server.
*/
middleware() {
let defaultInstance;
for (let instanceName in this._config) {
if (this._config[instanceName].default || !defaultInstance) {
defaultInstance = this.instance(instanceName);
}
}
if (defaultInstance) {
for (let instanceName in this._config) {
defaultInstance[instanceName] = this.instance(instanceName)
}
}
return (req, res, next) => {
req.mq = defaultInstance;
next();
}
}
/**
* Send a message to a queue on the default server instance.
* @param {string} queue The name of the queue to operate on
* @param {string} [instance=_default_] Server instance name to look for queue
* @param {Object|String|Buffer} msg The message to put on the queue
* @param {function} cb Callback that will receive err/response
*/
send(queue, instance, msg, cb) {
if (!(instance instanceof String)) {
cb = msg;
msg = instance;
instance = undefined;
}
return this.queue(queue, instance).send(msg, cb);
}
/**
* Receive a message from a queue on the default server instance.
* @param {string} queue The name of the queue to operate on
* @param {string} [instance=_default_] Server instance name to look for queue
* @param {function} cb Callback that will receive err/response
*/
receive(queue, instance, cb) {
if (!(instance instanceof String)) {
cb = instance;
instance = undefined;
}
return this.queue(queue, instance).receive(cb);
}
/**
* Listen for messages on a queue on the default server instance.
* @param {string} queue The name of the queue to operate on
* @param {string} [instance=_default_] Server instance name to look for queue
* @param {function} cb Callback that will receive err/response
* @returns {string} The consumer id that can be used to unlisten.
*/
listen(queue, instance, cb) {
if (!(instance instanceof String)) {
cb = instance;
instance = undefined;
}
return this.queue(queue, instance).listen(cb);
}
/**
* Remove a listener on a queue on the default service instance.
* @param {string} queue The name of the queue to operate on
* @param {string} [instance=_default_] Server instance name to look for queue
* @param {string} id The consumer id of the listener
* @param {function} cb Callback that will receive err/response
* @returns {*}
*/
unlisten(queue, instance, id, cb) {
if (!(instance instanceof String)) {
cb = id;
id = instance;
instance = undefined;
}
return this.queue(queue, instance).unlisten(id, cb);
}
}
module.exports = new Manager();