@sera4/essentia 1.0.27 → 1.0.30

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/index.js CHANGED
@@ -8,5 +8,6 @@ module.exports = {
8
8
  halDecorator : require("./hal"),
9
9
  formatter : require("./formatter"),
10
10
  healthCheck : require("./health"),
11
- queue : require("./queue")
11
+ queue : require("./queue"),
12
+ utils : require("./utils")
12
13
  }
package/package.json CHANGED
@@ -1,11 +1,13 @@
1
1
  {
2
2
  "name": "@sera4/essentia",
3
- "version": "1.0.27",
3
+ "version": "1.0.30",
4
4
  "description": "A library of utilities for Teleporte Web Services",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
- "test": "./node_modules/.bin/mocha --reporter mocha-junit-reporter --reporter-options mochaFile=./test-results/result.xml --exit",
8
- "testdev": "./node_modules/.bin/mocha --watch",
7
+ "test": "NODE_ENV=test ./node_modules/.bin/mocha --forbid-only --reporter mocha-junit-reporter --reporter-options mochaFile=./test-results/result.xml --exit",
8
+ "test:dev": "NODE_ENV=test ./node_modules/.bin/mocha --watch --forbid-only",
9
+ "test:only": "NODE_ENV=test ./node_modules/.bin/mocha --watch",
10
+ "test:pretty": "NODE_ENV=test ./node_modules/.bin/mocha --reporter mocha-simple-html-reporter --reporter-options output=./test-results/result.html --exit",
9
11
  "last-commit": "node last-commit.js"
10
12
  },
11
13
  "author": "Dev DM <dev@sera4.com>",
package/package.tar.gz CHANGED
Binary file
package/queue/index.js CHANGED
@@ -198,6 +198,25 @@ class S4Queue {
198
198
  this.subs[name] = s;
199
199
  }
200
200
 
201
+ /**
202
+ * Registers a new topic subscriber on a given exchange and queue.
203
+ * If exchange and queue don't exists, they'll be created
204
+ * and bound through the provided routing key.
205
+ * @param {String} exchange the name of the exchange.
206
+ * @param {String} queue the name of the queue.
207
+ * @param {String} key the name of the routing key.
208
+ */
209
+ async registerTopicSubscriber(exchange, queue, key) {
210
+ const name = `${exchange}-${queue}-${key}`;
211
+ if (Object.keys(this.subs).includes(name)) {
212
+ throw new Error(`Subscriber ${name} already registered`);
213
+ }
214
+
215
+ const s = new Subscriber(exchange, queue, 'topic', true, key, name);
216
+ await s.init(this.connection);
217
+ this.subs[name] = s;
218
+ }
219
+
201
220
  /**
202
221
  * Publishes a message on a given exchange with routing key.
203
222
  * @param {String} exchange the name of the exchange.
package/utils/index.js ADDED
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+
3
+ module.exports = {
4
+ isValidUuidV4 : (input) => {
5
+ if (!input) {
6
+ return false
7
+ } else {
8
+ return /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/.test(input);
9
+ }
10
+ },
11
+ // convert a regular string or Array to array (ie, "[1,2,3]", "1,2,3")
12
+ // useful for controller inputs
13
+ convertToIntegerArray: (input) => {
14
+ return module.exports.convertToArray(input)
15
+ .filter((el) => /^[0-9]+/.test(el))
16
+ .map((el) => parseInt(el));
17
+ },
18
+ convertToArray: (input) => {
19
+ if (Array.isArray(input)){
20
+ return input
21
+ } else {
22
+ if (!!input) {
23
+ if (typeof input === "number") {
24
+ return [ input ]
25
+ }
26
+ let matches;
27
+ if (matches = input.match(/\[(.*?)\]/)) {
28
+ input = matches[1]
29
+ }
30
+ input = input.replace(/[\[\]]/, '')
31
+
32
+ return input.split(',').filter((el) => /^[0-9A-Za-z-.]+/.test(el))
33
+ } else {
34
+ return []
35
+ }
36
+ }
37
+ }
38
+ }