@sera4/essentia 1.1.36 → 1.1.38
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 +1 -1
- package/package.tar.gz +0 -0
- package/queue/index.js +73 -33
- package/queue/publisher.js +5 -8
- package/queue/subscriber.js +10 -9
package/package.json
CHANGED
package/package.tar.gz
CHANGED
|
Binary file
|
package/queue/index.js
CHANGED
|
@@ -145,69 +145,80 @@ class S4Queue {
|
|
|
145
145
|
}
|
|
146
146
|
|
|
147
147
|
/**
|
|
148
|
+
* @deprecated Use registerPublisher
|
|
148
149
|
* Registers a new direct publisher on the given exchange.
|
|
149
150
|
* If exchange doesn't exist, one will be created.
|
|
150
151
|
* @param {String} exchange the name of the exchange on AMQP service.
|
|
151
152
|
*/
|
|
152
153
|
async registerDirectPublisher(exchange) {
|
|
153
|
-
|
|
154
|
-
if (Object.keys(this.pubs).includes(name)) {
|
|
155
|
-
throw new Error(`Publisher ${name} already registered`);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
const p = new Publisher(exchange, 'direct', true, name);
|
|
159
|
-
await p.init(this.connection);
|
|
160
|
-
this.pubs[name] = p;
|
|
154
|
+
return this.registerPublisher({exchange: {name: exchange, type: "direct"}});
|
|
161
155
|
}
|
|
162
156
|
|
|
163
157
|
/**
|
|
158
|
+
* @deprecated Use registerPublisher
|
|
164
159
|
* Registers a new topic publisher on the given exchange.
|
|
165
160
|
* If exchange doesn't exist, one will be created.
|
|
166
161
|
* @param {String} exchange the name of the exchange on AMQP service.
|
|
167
162
|
*/
|
|
168
163
|
async registerTopicPublisher(exchange) {
|
|
169
|
-
|
|
170
|
-
if (Object.keys(this.pubs).includes(name)) {
|
|
171
|
-
throw new Error(`Publisher ${name} already registered`);
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
const p = new Publisher(exchange, 'topic', true, name);
|
|
175
|
-
await p.init(this.connection);
|
|
176
|
-
this.pubs[name] = p;
|
|
164
|
+
return this.registerPublisher({exchange: {name: exchange, type: "topic"}});
|
|
177
165
|
}
|
|
178
166
|
|
|
179
167
|
/**
|
|
168
|
+
* @deprecated Use registerPublisher
|
|
180
169
|
* Registers a new fanout publisher on the given exchange.
|
|
181
170
|
* If exchange doesn't exist, one will be created.
|
|
182
171
|
* @param {String} exchange the name of the exchange on AMQP service.
|
|
183
172
|
*/
|
|
184
173
|
async registerFanoutPublisher(exchange) {
|
|
185
|
-
|
|
174
|
+
return this.registerPublisher({exchange: {name: exchange, type: "fanout"}});
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Registers a new publisher
|
|
179
|
+
* @param {Object} options connection options.
|
|
180
|
+
*/
|
|
181
|
+
async registerPublisher(options) {
|
|
182
|
+
const { exchange } = options;
|
|
183
|
+
const { name } = exchange;
|
|
186
184
|
if (Object.keys(this.pubs).includes(name)) {
|
|
187
185
|
throw new Error(`Publisher ${name} already registered`);
|
|
188
186
|
}
|
|
189
187
|
|
|
190
|
-
const p = new Publisher(exchange,
|
|
188
|
+
const p = new Publisher(exchange, name);
|
|
191
189
|
await p.init(this.connection);
|
|
192
190
|
this.pubs[name] = p;
|
|
193
191
|
}
|
|
194
192
|
|
|
193
|
+
/**
|
|
194
|
+
* Checks whether or not a publisher with given parameters
|
|
195
|
+
* has been registered already
|
|
196
|
+
* @param {String} exchange the name of the exchange.
|
|
197
|
+
* @returns true if registered already, false otherwise
|
|
198
|
+
*/
|
|
199
|
+
hasPublisher(exchange) {
|
|
200
|
+
return Object.keys(this.pubs).includes(exchange);
|
|
201
|
+
}
|
|
202
|
+
|
|
195
203
|
/**
|
|
196
204
|
* Removes a previously registered publisher
|
|
197
|
-
* @param {
|
|
205
|
+
* @param {Object} options
|
|
198
206
|
*/
|
|
199
|
-
|
|
200
|
-
const
|
|
207
|
+
async unregisterPublisher(options) {
|
|
208
|
+
const { exchange } = options;
|
|
209
|
+
const { name } = exchange;
|
|
201
210
|
if (!Object.keys(this.pubs).includes(name)) {
|
|
202
211
|
throw new Error(`Publisher ${name} not registered`);
|
|
203
212
|
}
|
|
204
213
|
const p = this.pubs[name];
|
|
205
214
|
await p.closeChannel();
|
|
215
|
+
|
|
206
216
|
delete this.pubs[name];
|
|
207
217
|
logger.debug("Unregistered publisher", name);
|
|
208
218
|
}
|
|
209
219
|
|
|
210
220
|
/**
|
|
221
|
+
* @deprecated Use registerSubscriber
|
|
211
222
|
* Registers a new direct subscriber on a given exchange and queue.
|
|
212
223
|
* If exchange and queue don't exists, they'll be created
|
|
213
224
|
* and bound through the provided routing key.
|
|
@@ -216,17 +227,16 @@ class S4Queue {
|
|
|
216
227
|
* @param {String} key the name of the routing key.
|
|
217
228
|
*/
|
|
218
229
|
async registerDirectSubscriber(exchange, queue, key) {
|
|
219
|
-
const
|
|
220
|
-
|
|
221
|
-
|
|
230
|
+
const options = {
|
|
231
|
+
key,
|
|
232
|
+
exchange: { name: exchange, type: "direct" },
|
|
233
|
+
queue: { name: queue }
|
|
222
234
|
}
|
|
223
|
-
|
|
224
|
-
const s = new Subscriber(exchange, queue, 'direct', true, key, name);
|
|
225
|
-
await s.init(this.connection);
|
|
226
|
-
this.subs[name] = s;
|
|
235
|
+
return this.registerSubscriber(options);
|
|
227
236
|
}
|
|
228
237
|
|
|
229
238
|
/**
|
|
239
|
+
* @deprecated Use registerSubscriber
|
|
230
240
|
* Registers a new topic subscriber on a given exchange and queue.
|
|
231
241
|
* If exchange and queue don't exists, they'll be created
|
|
232
242
|
* and bound through the provided routing key.
|
|
@@ -235,29 +245,59 @@ class S4Queue {
|
|
|
235
245
|
* @param {String} key the name of the routing key.
|
|
236
246
|
*/
|
|
237
247
|
async registerTopicSubscriber(exchange, queue, key) {
|
|
238
|
-
const
|
|
248
|
+
const options = {
|
|
249
|
+
key,
|
|
250
|
+
exchange: { name: exchange, type: "topic" },
|
|
251
|
+
queue: { name: queue }
|
|
252
|
+
}
|
|
253
|
+
return this.registerSubscriber(options);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* An overloaded version of registering a subscriber
|
|
258
|
+
* allowing for more fine tuned parameters
|
|
259
|
+
* @param {Object} options subscribing options
|
|
260
|
+
*/
|
|
261
|
+
async registerSubscriber(options) {
|
|
262
|
+
const {exchange, queue, key} = options;
|
|
263
|
+
const name = `${exchange.name}-${queue.name}-${key}`;
|
|
264
|
+
|
|
239
265
|
if (Object.keys(this.subs).includes(name)) {
|
|
240
266
|
throw new Error(`Subscriber ${name} already registered`);
|
|
241
267
|
}
|
|
242
268
|
|
|
243
|
-
const s = new Subscriber(exchange, queue,
|
|
269
|
+
const s = new Subscriber(exchange, queue, key, name);
|
|
244
270
|
await s.init(this.connection);
|
|
245
271
|
this.subs[name] = s;
|
|
246
272
|
}
|
|
247
273
|
|
|
248
274
|
/**
|
|
249
|
-
*
|
|
250
|
-
*
|
|
275
|
+
* Checks whether or not a subscriber with given parameters
|
|
276
|
+
* has been registered already
|
|
277
|
+
* @param {String} exchange the name of the exchange.
|
|
251
278
|
* @param {String} queue the name of the queue.
|
|
252
279
|
* @param {String} key the name of the routing key.
|
|
280
|
+
* @returns true if registered already, false otherwise
|
|
253
281
|
*/
|
|
254
|
-
|
|
282
|
+
hasSubscriber(exchange, queue, key) {
|
|
255
283
|
const name = `${exchange}-${queue}-${key}`;
|
|
284
|
+
return Object.keys(this.subs).includes(name);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Removes a previously registered subscriber
|
|
289
|
+
* @param {OBject} options
|
|
290
|
+
*/
|
|
291
|
+
async unregisterSubscriber(options) {
|
|
292
|
+
const {exchange, queue, key} = options;
|
|
293
|
+
const name = `${exchange.name}-${queue.name}-${key}`;
|
|
294
|
+
|
|
256
295
|
if (!Object.keys(this.subs).includes(name)) {
|
|
257
296
|
throw new Error(`Subscriber ${name} not registered`);
|
|
258
297
|
}
|
|
259
298
|
const s = this.subs[name];
|
|
260
299
|
await s.closeChannel();
|
|
300
|
+
|
|
261
301
|
delete this.subs[name];
|
|
262
302
|
logger.debug("Unregistered subscriber", name);
|
|
263
303
|
}
|
package/queue/publisher.js
CHANGED
|
@@ -6,15 +6,11 @@ import uuidv4 from "uuid/v4.js";
|
|
|
6
6
|
*/
|
|
7
7
|
export class Publisher {
|
|
8
8
|
/**
|
|
9
|
-
* @param {
|
|
10
|
-
* @param {String} type direct|fanout| etc (supported types by RabbitMQ).
|
|
11
|
-
* @param {boolean} durable true if the messages should persist service restarts.
|
|
9
|
+
* @param {Object} exchange AMQP exchange name.
|
|
12
10
|
* @param {String} name a name to identify this publisher in the logs.
|
|
13
11
|
*/
|
|
14
|
-
constructor(exchange,
|
|
12
|
+
constructor(exchange, name) {
|
|
15
13
|
this.exchange = exchange;
|
|
16
|
-
this.type = type;
|
|
17
|
-
this.durable = durable;
|
|
18
14
|
this.name = name;
|
|
19
15
|
this.channel = null;
|
|
20
16
|
}
|
|
@@ -25,7 +21,7 @@ export class Publisher {
|
|
|
25
21
|
* @param {Object} conn amqp connection object
|
|
26
22
|
*/
|
|
27
23
|
async init (conn) {
|
|
28
|
-
logger.debug(`Initializing PUB channel for ${this.name}
|
|
24
|
+
logger.debug(`Initializing PUB channel for ${this.name}`);
|
|
29
25
|
|
|
30
26
|
if(this.channel) {
|
|
31
27
|
logger.warn(`PUB channel for ${this.name} already initialized`);
|
|
@@ -38,7 +34,8 @@ export class Publisher {
|
|
|
38
34
|
}
|
|
39
35
|
|
|
40
36
|
logger.debug(`Initialized PUB channel for ${this.name}`);
|
|
41
|
-
|
|
37
|
+
const {name, type, options} = this.exchange;
|
|
38
|
+
await this.channel.assertExchange(name, type, options);
|
|
42
39
|
|
|
43
40
|
this.channel.on("error", (err) => {
|
|
44
41
|
logger.debug(`Connection error for ${this.name}`, err);
|
package/queue/subscriber.js
CHANGED
|
@@ -10,15 +10,13 @@ export class Subscriber {
|
|
|
10
10
|
* @param {String} exchange name of the AMQP exchange.
|
|
11
11
|
* @param {String} queue name of the AMQP service.
|
|
12
12
|
* @param {String} type direct|fanout or other supported connection types.
|
|
13
|
-
* @param {boolean} durable durable connection or not.
|
|
14
13
|
* @param {String} key routing key. This will be used to bind the queue to the exchange.
|
|
15
14
|
* @param {String} name a usefull name to be printed in the logs.
|
|
15
|
+
* @param {Object} options connection options
|
|
16
16
|
*/
|
|
17
|
-
constructor(exchange, queue,
|
|
17
|
+
constructor(exchange, queue, key, name) {
|
|
18
18
|
this.exchange = exchange;
|
|
19
19
|
this.queue = queue;
|
|
20
|
-
this.type = type;
|
|
21
|
-
this.durable = durable;
|
|
22
20
|
this.key = key;
|
|
23
21
|
this.name = name;
|
|
24
22
|
this.channel = null;
|
|
@@ -55,13 +53,16 @@ export class Subscriber {
|
|
|
55
53
|
});
|
|
56
54
|
|
|
57
55
|
logger.debug(`Initialized SUB channel for ${this.name}`);
|
|
58
|
-
|
|
59
|
-
|
|
56
|
+
const { name: exchangeName, options: exchangeOptions, type } = this.exchange;
|
|
57
|
+
const { name: queueName, options: queueOptions } = this.queue;
|
|
60
58
|
|
|
61
|
-
|
|
62
|
-
await this.channel.
|
|
59
|
+
await this.channel.assertExchange(exchangeName, type, exchangeOptions);
|
|
60
|
+
await this.channel.assertQueue(queueName, queueOptions);
|
|
63
61
|
|
|
64
|
-
|
|
62
|
+
logger.debug(`Binding queue ${queueName} on ${exchangeName} exchange for ${this.key} key`);
|
|
63
|
+
await this.channel.bindQueue(queueName, exchangeName, this.key);
|
|
64
|
+
|
|
65
|
+
this.channel.consume(queueName, (msg) => {
|
|
65
66
|
const { fields, content } = msg;
|
|
66
67
|
let payload = null;
|
|
67
68
|
let error = null;
|