@sera4/essentia 1.1.37 → 1.1.39
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 +71 -76
- package/queue/publisher.js +6 -10
- package/queue/subscriber.js +9 -8
package/package.json
CHANGED
package/package.tar.gz
CHANGED
|
Binary file
|
package/queue/index.js
CHANGED
|
@@ -145,52 +145,47 @@ 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
|
-
* @param {Object} options connection options
|
|
152
152
|
*/
|
|
153
|
-
async registerDirectPublisher(exchange
|
|
154
|
-
|
|
155
|
-
if (Object.keys(this.pubs).includes(name)) {
|
|
156
|
-
throw new Error(`Publisher ${name} already registered`);
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
const p = new Publisher(exchange, 'direct', name, options);
|
|
160
|
-
await p.init(this.connection);
|
|
161
|
-
this.pubs[name] = p;
|
|
153
|
+
async registerDirectPublisher(exchange) {
|
|
154
|
+
return this.registerPublisher({exchange: {name: exchange, type: "direct"}});
|
|
162
155
|
}
|
|
163
156
|
|
|
164
157
|
/**
|
|
158
|
+
* @deprecated Use registerPublisher
|
|
165
159
|
* Registers a new topic publisher on the given exchange.
|
|
166
160
|
* If exchange doesn't exist, one will be created.
|
|
167
161
|
* @param {String} exchange the name of the exchange on AMQP service.
|
|
168
|
-
* @param {Object} options connection options
|
|
169
162
|
*/
|
|
170
|
-
async registerTopicPublisher(exchange
|
|
171
|
-
|
|
172
|
-
if (Object.keys(this.pubs).includes(name)) {
|
|
173
|
-
throw new Error(`Publisher ${name} already registered`);
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
const p = new Publisher(exchange, 'topic', name, options);
|
|
177
|
-
await p.init(this.connection);
|
|
178
|
-
this.pubs[name] = p;
|
|
163
|
+
async registerTopicPublisher(exchange) {
|
|
164
|
+
return this.registerPublisher({exchange: {name: exchange, type: "topic"}});
|
|
179
165
|
}
|
|
180
166
|
|
|
181
167
|
/**
|
|
168
|
+
* @deprecated Use registerPublisher
|
|
182
169
|
* Registers a new fanout publisher on the given exchange.
|
|
183
170
|
* If exchange doesn't exist, one will be created.
|
|
184
171
|
* @param {String} exchange the name of the exchange on AMQP service.
|
|
185
|
-
* @param {Object} options connection options
|
|
186
172
|
*/
|
|
187
|
-
async registerFanoutPublisher(exchange
|
|
188
|
-
|
|
173
|
+
async registerFanoutPublisher(exchange) {
|
|
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;
|
|
189
184
|
if (Object.keys(this.pubs).includes(name)) {
|
|
190
185
|
throw new Error(`Publisher ${name} already registered`);
|
|
191
186
|
}
|
|
192
187
|
|
|
193
|
-
const p = new Publisher(exchange,
|
|
188
|
+
const p = new Publisher(exchange, name);
|
|
194
189
|
await p.init(this.connection);
|
|
195
190
|
this.pubs[name] = p;
|
|
196
191
|
}
|
|
@@ -207,55 +202,71 @@ class S4Queue {
|
|
|
207
202
|
|
|
208
203
|
/**
|
|
209
204
|
* Removes a previously registered publisher
|
|
210
|
-
* @param {
|
|
205
|
+
* @param {Object} options
|
|
211
206
|
*/
|
|
212
|
-
|
|
213
|
-
const
|
|
207
|
+
async unregisterPublisher(options) {
|
|
208
|
+
const { exchange } = options;
|
|
209
|
+
const { name } = exchange;
|
|
214
210
|
if (!Object.keys(this.pubs).includes(name)) {
|
|
215
211
|
throw new Error(`Publisher ${name} not registered`);
|
|
216
212
|
}
|
|
217
213
|
const p = this.pubs[name];
|
|
218
214
|
await p.closeChannel();
|
|
215
|
+
|
|
219
216
|
delete this.pubs[name];
|
|
220
217
|
logger.debug("Unregistered publisher", name);
|
|
221
218
|
}
|
|
222
219
|
|
|
223
220
|
/**
|
|
221
|
+
* @deprecated Use registerSubscriber
|
|
224
222
|
* Registers a new direct subscriber on a given exchange and queue.
|
|
225
223
|
* If exchange and queue don't exists, they'll be created
|
|
226
224
|
* and bound through the provided routing key.
|
|
227
225
|
* @param {String} exchange the name of the exchange.
|
|
228
226
|
* @param {String} queue the name of the queue.
|
|
229
227
|
* @param {String} key the name of the routing key.
|
|
230
|
-
* @param {Object} options connection options
|
|
231
228
|
*/
|
|
232
|
-
async registerDirectSubscriber(exchange, queue, key
|
|
233
|
-
const
|
|
234
|
-
|
|
235
|
-
|
|
229
|
+
async registerDirectSubscriber(exchange, queue, key) {
|
|
230
|
+
const options = {
|
|
231
|
+
key,
|
|
232
|
+
exchange: { name: exchange, type: "direct" },
|
|
233
|
+
queue: { name: queue }
|
|
236
234
|
}
|
|
237
|
-
|
|
238
|
-
const s = new Subscriber(exchange, queue, 'direct', key, name, options);
|
|
239
|
-
await s.init(this.connection);
|
|
240
|
-
this.subs[name] = s;
|
|
235
|
+
return this.registerSubscriber(options);
|
|
241
236
|
}
|
|
242
237
|
|
|
243
238
|
/**
|
|
239
|
+
* @deprecated Use registerSubscriber
|
|
244
240
|
* Registers a new topic subscriber on a given exchange and queue.
|
|
245
241
|
* If exchange and queue don't exists, they'll be created
|
|
246
242
|
* and bound through the provided routing key.
|
|
247
243
|
* @param {String} exchange the name of the exchange.
|
|
248
244
|
* @param {String} queue the name of the queue.
|
|
249
245
|
* @param {String} key the name of the routing key.
|
|
250
|
-
* @param {Object} options connection options
|
|
251
246
|
*/
|
|
252
|
-
async registerTopicSubscriber(exchange, queue, key
|
|
253
|
-
const
|
|
247
|
+
async registerTopicSubscriber(exchange, queue, key) {
|
|
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
|
+
|
|
254
265
|
if (Object.keys(this.subs).includes(name)) {
|
|
255
266
|
throw new Error(`Subscriber ${name} already registered`);
|
|
256
267
|
}
|
|
257
268
|
|
|
258
|
-
const s = new Subscriber(exchange, queue,
|
|
269
|
+
const s = new Subscriber(exchange, queue, key, name);
|
|
259
270
|
await s.init(this.connection);
|
|
260
271
|
this.subs[name] = s;
|
|
261
272
|
}
|
|
@@ -275,17 +286,18 @@ class S4Queue {
|
|
|
275
286
|
|
|
276
287
|
/**
|
|
277
288
|
* Removes a previously registered subscriber
|
|
278
|
-
* @param {
|
|
279
|
-
* @param {String} queue the name of the queue.
|
|
280
|
-
* @param {String} key the name of the routing key.
|
|
289
|
+
* @param {OBject} options
|
|
281
290
|
*/
|
|
282
|
-
async unregisterSubscriber(
|
|
283
|
-
const
|
|
291
|
+
async unregisterSubscriber(options) {
|
|
292
|
+
const {exchange, queue, key} = options;
|
|
293
|
+
const name = `${exchange.name}-${queue.name}-${key}`;
|
|
294
|
+
|
|
284
295
|
if (!Object.keys(this.subs).includes(name)) {
|
|
285
296
|
throw new Error(`Subscriber ${name} not registered`);
|
|
286
297
|
}
|
|
287
298
|
const s = this.subs[name];
|
|
288
299
|
await s.closeChannel();
|
|
300
|
+
|
|
289
301
|
delete this.subs[name];
|
|
290
302
|
logger.debug("Unregistered subscriber", name);
|
|
291
303
|
}
|
|
@@ -297,29 +309,7 @@ class S4Queue {
|
|
|
297
309
|
* @param {Object} msg a JSON object as the message.
|
|
298
310
|
*/
|
|
299
311
|
async publishDirectMessage(exchange, key, msg) {
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
if (!key) {
|
|
303
|
-
throw(new Error(`Direct message require a key`));
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
if (!p) {
|
|
307
|
-
throw(new Error(`No PUB for ${exchange} found`));
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
if (p.type !== 'direct') {
|
|
311
|
-
throw(new Error(`PUB ${exchange} is not direct`));
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
let content = msg;
|
|
315
|
-
if (content.constructor !== ({}).constructor) {
|
|
316
|
-
// if not in JSON format, let's make it so
|
|
317
|
-
content = {
|
|
318
|
-
msg: msg
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
return await p.publish({key, content});
|
|
312
|
+
return this.publishMessage(exchange, key, msg)
|
|
323
313
|
}
|
|
324
314
|
|
|
325
315
|
/**
|
|
@@ -329,20 +319,25 @@ class S4Queue {
|
|
|
329
319
|
* @param {Object} msg a JSON object as the message.
|
|
330
320
|
*/
|
|
331
321
|
async publishTopicMessage(exchange, topic, msg) {
|
|
332
|
-
|
|
322
|
+
return this.publishMessage(exchange, topic, msg)
|
|
323
|
+
}
|
|
333
324
|
|
|
334
|
-
|
|
335
|
-
|
|
325
|
+
/**
|
|
326
|
+
* Publish a message to the exchange
|
|
327
|
+
* @param {String} exchange name of exchange
|
|
328
|
+
* @param {String} key name of the key or topic
|
|
329
|
+
* @param {Object} msg payload to be published
|
|
330
|
+
*/
|
|
331
|
+
async publishMessage(exchange, key, msg) {
|
|
332
|
+
if (!key) {
|
|
333
|
+
throw(new Error(`Key argument not defined`));
|
|
336
334
|
}
|
|
337
335
|
|
|
336
|
+
const p = this.pubs[exchange];
|
|
338
337
|
if (!p) {
|
|
339
338
|
throw(new Error(`No PUB for ${exchange} found`));
|
|
340
339
|
}
|
|
341
340
|
|
|
342
|
-
if (p.type !== 'topic') {
|
|
343
|
-
throw(new Error(`PUB ${exchange} is not topic`));
|
|
344
|
-
}
|
|
345
|
-
|
|
346
341
|
let content = msg;
|
|
347
342
|
if (content.constructor !== ({}).constructor) {
|
|
348
343
|
// if not in JSON format, let's make it so
|
|
@@ -351,7 +346,7 @@ class S4Queue {
|
|
|
351
346
|
}
|
|
352
347
|
}
|
|
353
348
|
|
|
354
|
-
return await p.publish({key
|
|
349
|
+
return await p.publish({key, content});
|
|
355
350
|
}
|
|
356
351
|
|
|
357
352
|
/**
|
package/queue/publisher.js
CHANGED
|
@@ -6,16 +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).
|
|
9
|
+
* @param {Object} exchange AMQP exchange name.
|
|
11
10
|
* @param {String} name a name to identify this publisher in the logs.
|
|
12
|
-
* @param {Object} options connection options
|
|
13
|
-
|
|
14
11
|
*/
|
|
15
|
-
constructor(exchange,
|
|
12
|
+
constructor(exchange, name) {
|
|
16
13
|
this.exchange = exchange;
|
|
17
|
-
this.type = type;
|
|
18
|
-
this.options = {durable: true, ...options};
|
|
19
14
|
this.name = name;
|
|
20
15
|
this.channel = null;
|
|
21
16
|
}
|
|
@@ -26,7 +21,7 @@ export class Publisher {
|
|
|
26
21
|
* @param {Object} conn amqp connection object
|
|
27
22
|
*/
|
|
28
23
|
async init (conn) {
|
|
29
|
-
logger.debug(`Initializing PUB channel for ${this.name}
|
|
24
|
+
logger.debug(`Initializing PUB channel for ${this.name}`);
|
|
30
25
|
|
|
31
26
|
if(this.channel) {
|
|
32
27
|
logger.warn(`PUB channel for ${this.name} already initialized`);
|
|
@@ -39,7 +34,8 @@ export class Publisher {
|
|
|
39
34
|
}
|
|
40
35
|
|
|
41
36
|
logger.debug(`Initialized PUB channel for ${this.name}`);
|
|
42
|
-
|
|
37
|
+
const {name, type, options} = this.exchange;
|
|
38
|
+
await this.channel.assertExchange(name, type, options);
|
|
43
39
|
|
|
44
40
|
this.channel.on("error", (err) => {
|
|
45
41
|
logger.debug(`Connection error for ${this.name}`, err);
|
|
@@ -72,7 +68,7 @@ export class Publisher {
|
|
|
72
68
|
logger.debug("Data", parcel);
|
|
73
69
|
|
|
74
70
|
const content = Buffer.from(JSON.stringify(parcel));
|
|
75
|
-
const res = await this.channel.publish(this.exchange, msg.key, content, {persistent: true});
|
|
71
|
+
const res = await this.channel.publish(this.exchange.name, msg.key, content, {persistent: true});
|
|
76
72
|
|
|
77
73
|
if (!res) {
|
|
78
74
|
throw(new Error("Channel write buffer is full"));
|
package/queue/subscriber.js
CHANGED
|
@@ -14,11 +14,9 @@ export class Subscriber {
|
|
|
14
14
|
* @param {String} name a usefull name to be printed in the logs.
|
|
15
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.options = {durable: true, ...options};
|
|
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;
|