@sera4/essentia 1.0.26 → 1.0.28

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.26",
3
+ "version": "1.0.28",
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
@@ -16,6 +16,38 @@ class S4Queue {
16
16
  this.subs = {};
17
17
  }
18
18
 
19
+ /**
20
+ * Opens a connection and immediately closes it.
21
+ * To be used for health checks.
22
+ * @param {Object} options Connection parameters. At the very least options.connectionUrl must be present.
23
+ */
24
+ async testConnection(options) {
25
+ return new Promise((resolve, reject) => {
26
+ // We may run into issues with SNI TLS if
27
+ // we host with 3rd parties using our own certs.
28
+ // To avoid any issues, let's parse the url
29
+ // and provide the server name in the socket options
30
+ const urlParts = urlParse(options.connectionUrl);
31
+ const socketOptions = {servername: urlParts.hostname};
32
+
33
+ amqp.connect(options.connectionUrl, socketOptions, (err, conn) => {
34
+ if (err) {
35
+ return reject(err);
36
+ }
37
+ if (!conn) {
38
+ return reject("Failed to obtain connection");
39
+ }
40
+ conn.close(e => {
41
+ if (e) {
42
+ reject(e)
43
+ } else {
44
+ resolve(true)
45
+ }
46
+ })
47
+ });
48
+ });
49
+ }
50
+
19
51
  /**
20
52
  * Opens a connection to AMQP server.
21
53
  * @param {Object} options Connection parameters. At the very least options.connectionUrl must be present.
@@ -166,6 +198,25 @@ class S4Queue {
166
198
  this.subs[name] = s;
167
199
  }
168
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
+
169
220
  /**
170
221
  * Publishes a message on a given exchange with routing key.
171
222
  * @param {String} exchange the name of the exchange.
package/utils/index.js ADDED
@@ -0,0 +1,11 @@
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
+ }