@sera4/essentia 1.1.35 → 1.1.37

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.35",
3
+ "version": "1.1.37",
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
@@ -148,14 +148,15 @@ class S4Queue {
148
148
  * Registers a new direct publisher on the given exchange.
149
149
  * If exchange doesn't exist, one will be created.
150
150
  * @param {String} exchange the name of the exchange on AMQP service.
151
+ * @param {Object} options connection options
151
152
  */
152
- async registerDirectPublisher(exchange) {
153
+ async registerDirectPublisher(exchange, options) {
153
154
  const name = exchange;
154
155
  if (Object.keys(this.pubs).includes(name)) {
155
156
  throw new Error(`Publisher ${name} already registered`);
156
157
  }
157
158
 
158
- const p = new Publisher(exchange, 'direct', true, name);
159
+ const p = new Publisher(exchange, 'direct', name, options);
159
160
  await p.init(this.connection);
160
161
  this.pubs[name] = p;
161
162
  }
@@ -164,14 +165,15 @@ class S4Queue {
164
165
  * Registers a new topic publisher on the given exchange.
165
166
  * If exchange doesn't exist, one will be created.
166
167
  * @param {String} exchange the name of the exchange on AMQP service.
168
+ * @param {Object} options connection options
167
169
  */
168
- async registerTopicPublisher(exchange) {
170
+ async registerTopicPublisher(exchange, options) {
169
171
  const name = exchange;
170
172
  if (Object.keys(this.pubs).includes(name)) {
171
173
  throw new Error(`Publisher ${name} already registered`);
172
174
  }
173
175
 
174
- const p = new Publisher(exchange, 'topic', true, name);
176
+ const p = new Publisher(exchange, 'topic', name, options);
175
177
  await p.init(this.connection);
176
178
  this.pubs[name] = p;
177
179
  }
@@ -180,18 +182,44 @@ class S4Queue {
180
182
  * Registers a new fanout publisher on the given exchange.
181
183
  * If exchange doesn't exist, one will be created.
182
184
  * @param {String} exchange the name of the exchange on AMQP service.
185
+ * @param {Object} options connection options
183
186
  */
184
- async registerFanoutPublisher(exchange) {
187
+ async registerFanoutPublisher(exchange, options) {
185
188
  const name = exchange;
186
189
  if (Object.keys(this.pubs).includes(name)) {
187
190
  throw new Error(`Publisher ${name} already registered`);
188
191
  }
189
192
 
190
- const p = new Publisher(exchange, 'fanout', true, name);
193
+ const p = new Publisher(exchange, 'fanout', name, options);
191
194
  await p.init(this.connection);
192
195
  this.pubs[name] = p;
193
196
  }
194
197
 
198
+ /**
199
+ * Checks whether or not a publisher with given parameters
200
+ * has been registered already
201
+ * @param {String} exchange the name of the exchange.
202
+ * @returns true if registered already, false otherwise
203
+ */
204
+ hasPublisher(exchange) {
205
+ return Object.keys(this.pubs).includes(exchange);
206
+ }
207
+
208
+ /**
209
+ * Removes a previously registered publisher
210
+ * @param {String} exchange
211
+ */
212
+ async unregisterPublisher(exchange) {
213
+ const name = exchange;
214
+ if (!Object.keys(this.pubs).includes(name)) {
215
+ throw new Error(`Publisher ${name} not registered`);
216
+ }
217
+ const p = this.pubs[name];
218
+ await p.closeChannel();
219
+ delete this.pubs[name];
220
+ logger.debug("Unregistered publisher", name);
221
+ }
222
+
195
223
  /**
196
224
  * Registers a new direct subscriber on a given exchange and queue.
197
225
  * If exchange and queue don't exists, they'll be created
@@ -199,14 +227,15 @@ class S4Queue {
199
227
  * @param {String} exchange the name of the exchange.
200
228
  * @param {String} queue the name of the queue.
201
229
  * @param {String} key the name of the routing key.
230
+ * @param {Object} options connection options
202
231
  */
203
- async registerDirectSubscriber(exchange, queue, key) {
232
+ async registerDirectSubscriber(exchange, queue, key, options) {
204
233
  const name = `${exchange}-${queue}-${key}`;
205
234
  if (Object.keys(this.subs).includes(name)) {
206
235
  throw new Error(`Subscriber ${name} already registered`);
207
236
  }
208
237
 
209
- const s = new Subscriber(exchange, queue, 'direct', true, key, name);
238
+ const s = new Subscriber(exchange, queue, 'direct', key, name, options);
210
239
  await s.init(this.connection);
211
240
  this.subs[name] = s;
212
241
  }
@@ -218,18 +247,49 @@ class S4Queue {
218
247
  * @param {String} exchange the name of the exchange.
219
248
  * @param {String} queue the name of the queue.
220
249
  * @param {String} key the name of the routing key.
250
+ * @param {Object} options connection options
221
251
  */
222
- async registerTopicSubscriber(exchange, queue, key) {
252
+ async registerTopicSubscriber(exchange, queue, key, options) {
223
253
  const name = `${exchange}-${queue}-${key}`;
224
254
  if (Object.keys(this.subs).includes(name)) {
225
255
  throw new Error(`Subscriber ${name} already registered`);
226
256
  }
227
257
 
228
- const s = new Subscriber(exchange, queue, 'topic', true, key, name);
258
+ const s = new Subscriber(exchange, queue, 'topic', key, name, options);
229
259
  await s.init(this.connection);
230
260
  this.subs[name] = s;
231
261
  }
232
262
 
263
+ /**
264
+ * Checks whether or not a subscriber with given parameters
265
+ * has been registered already
266
+ * @param {String} exchange the name of the exchange.
267
+ * @param {String} queue the name of the queue.
268
+ * @param {String} key the name of the routing key.
269
+ * @returns true if registered already, false otherwise
270
+ */
271
+ hasSubscriber(exchange, queue, key) {
272
+ const name = `${exchange}-${queue}-${key}`;
273
+ return Object.keys(this.subs).includes(name);
274
+ }
275
+
276
+ /**
277
+ * 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.
281
+ */
282
+ async unregisterSubscriber(exchange, queue, key) {
283
+ const name = `${exchange}-${queue}-${key}`;
284
+ if (!Object.keys(this.subs).includes(name)) {
285
+ throw new Error(`Subscriber ${name} not registered`);
286
+ }
287
+ const s = this.subs[name];
288
+ await s.closeChannel();
289
+ delete this.subs[name];
290
+ logger.debug("Unregistered subscriber", name);
291
+ }
292
+
233
293
  /**
234
294
  * Publishes a message on a given exchange with routing key.
235
295
  * @param {String} exchange the name of the exchange.
@@ -8,13 +8,14 @@ export class Publisher {
8
8
  /**
9
9
  * @param {String} exchange AMQP exchange name.
10
10
  * @param {String} type direct|fanout| etc (supported types by RabbitMQ).
11
- * @param {boolean} durable true if the messages should persist service restarts.
12
11
  * @param {String} name a name to identify this publisher in the logs.
12
+ * @param {Object} options connection options
13
+
13
14
  */
14
- constructor(exchange, type, durable, name) {
15
+ constructor(exchange, type, name, options = {}) {
15
16
  this.exchange = exchange;
16
17
  this.type = type;
17
- this.durable = durable;
18
+ this.options = {durable: true, ...options};
18
19
  this.name = name;
19
20
  this.channel = null;
20
21
  }
@@ -38,7 +39,7 @@ export class Publisher {
38
39
  }
39
40
 
40
41
  logger.debug(`Initialized PUB channel for ${this.name}`);
41
- await this.channel.assertExchange(this.exchange, this.type, {durable: this.durable});
42
+ await this.channel.assertExchange(this.exchange, this.type, this.options);
42
43
 
43
44
  this.channel.on("error", (err) => {
44
45
  logger.debug(`Connection error for ${this.name}`, err);
@@ -80,6 +81,19 @@ export class Publisher {
80
81
  return parcel.tracking_id;
81
82
  }
82
83
 
84
+ /**
85
+ * Closes the channel
86
+ * @returns promise
87
+ */
88
+ async closeChannel() {
89
+ if (this.channel) {
90
+ logger.debug("Closing publishing channel", this.name);
91
+ return this.channel.close();
92
+ } else {
93
+ logger.error("Can't close a null publishing channel");
94
+ }
95
+ }
96
+
83
97
  async bindQueue(queue, pattern, args) {
84
98
  await this.channel.bindQueue(queue, this.exchange, pattern, args)
85
99
  }
@@ -10,15 +10,15 @@ 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, type, durable, key, name) {
17
+ constructor(exchange, queue, type, key, name, options = {}) {
18
18
  this.exchange = exchange;
19
19
  this.queue = queue;
20
20
  this.type = type;
21
- this.durable = durable;
21
+ this.options = {durable: true, ...options};
22
22
  this.key = key;
23
23
  this.name = name;
24
24
  this.channel = null;
@@ -55,8 +55,8 @@ export class Subscriber {
55
55
  });
56
56
 
57
57
  logger.debug(`Initialized SUB channel for ${this.name}`);
58
- await this.channel.assertExchange(this.exchange, this.type, {durable: this.durable});
59
- await this.channel.assertQueue(this.queue, { durable: this.durable });
58
+ await this.channel.assertExchange(this.exchange, this.type, this.options);
59
+ await this.channel.assertQueue(this.queue, this.options);
60
60
 
61
61
  logger.debug(`Binding queue ${this.queue} on ${this.exchange} exchange for ${this.key} key`);
62
62
  await this.channel.bindQueue(this.queue, this.exchange, this.key);
@@ -82,6 +82,19 @@ export class Subscriber {
82
82
  }, { noAck: false });
83
83
  }
84
84
 
85
+ /**
86
+ * Closes the channel
87
+ * @returns promise
88
+ */
89
+ async closeChannel() {
90
+ if (this.channel) {
91
+ logger.debug("Closing subscriber channel", this.name);
92
+ return this.channel.close();
93
+ } else {
94
+ logger.error("Can't close a null subscriber channel");
95
+ }
96
+ }
97
+
85
98
  /**
86
99
  * Register a new callback to recive message through this
87
100
  * subscriber object