@sera4/essentia 1.1.37 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sera4/essentia",
3
- "version": "1.1.37",
3
+ "version": "1.1.38",
4
4
  "description": "A library of utilities for Teleporte Web Services",
5
5
  "main": "index.js",
6
6
  "type": "module",
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, options) {
154
- const name = exchange;
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, options) {
171
- const name = exchange;
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, options) {
188
- const name = exchange;
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, 'fanout', name, options);
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 {String} exchange
205
+ * @param {Object} options
211
206
  */
212
- async unregisterPublisher(exchange) {
213
- const name = exchange;
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, options) {
233
- const name = `${exchange}-${queue}-${key}`;
234
- if (Object.keys(this.subs).includes(name)) {
235
- throw new Error(`Subscriber ${name} already registered`);
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, options) {
253
- const name = `${exchange}-${queue}-${key}`;
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, 'topic', key, name, options);
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 {String} exchange
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(exchange, queue, key) {
283
- const name = `${exchange}-${queue}-${key}`;
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
  }
@@ -6,16 +6,11 @@ import uuidv4 from "uuid/v4.js";
6
6
  */
7
7
  export class Publisher {
8
8
  /**
9
- * @param {String} exchange AMQP exchange name.
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, type, name, options = {}) {
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}. Type: ${this.type}`);
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
- await this.channel.assertExchange(this.exchange, this.type, this.options);
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);
@@ -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, type, key, name, options = {}) {
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
- await this.channel.assertExchange(this.exchange, this.type, this.options);
59
- await this.channel.assertQueue(this.queue, this.options);
56
+ const { name: exchangeName, options: exchangeOptions, type } = this.exchange;
57
+ const { name: queueName, options: queueOptions } = this.queue;
60
58
 
61
- logger.debug(`Binding queue ${this.queue} on ${this.exchange} exchange for ${this.key} key`);
62
- await this.channel.bindQueue(this.queue, this.exchange, this.key);
59
+ await this.channel.assertExchange(exchangeName, type, exchangeOptions);
60
+ await this.channel.assertQueue(queueName, queueOptions);
63
61
 
64
- this.channel.consume(this.queue, (msg) => {
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;