@sera4/essentia 1.0.21 → 1.0.22
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 +0 -1
- package/package.json +2 -1
- package/package.tar.gz +0 -0
- package/queue/index.js +20 -19
- package/queue/publisher.js +26 -14
- package/queue/subscriber.js +4 -4
package/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sera4/essentia",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.22",
|
|
4
4
|
"description": "A library of utilities for Teleporte Web Services",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -29,6 +29,7 @@
|
|
|
29
29
|
"async": "^2.6.1",
|
|
30
30
|
"fast-safe-stringify": "^2.0.6",
|
|
31
31
|
"git-rev": "^0.2.1",
|
|
32
|
+
"uuid": "^3.3.3",
|
|
32
33
|
"winston": "^3.2.1",
|
|
33
34
|
"winston-logstash": "^0.4.0"
|
|
34
35
|
}
|
package/package.tar.gz
CHANGED
|
Binary file
|
package/queue/index.js
CHANGED
|
@@ -148,31 +148,32 @@ class S4Queue {
|
|
|
148
148
|
* @param {String} key the name of the routing key.
|
|
149
149
|
* @param {Object} msg a JSON object as the message.
|
|
150
150
|
*/
|
|
151
|
-
publishDirectMessage(exchange, key, msg) {
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
if (!key) {
|
|
155
|
-
throw(new Error(`Direct message require a key`));
|
|
156
|
-
}
|
|
151
|
+
async publishDirectMessage(exchange, key, msg) {
|
|
152
|
+
return new Promise((resolve, reject) => {
|
|
153
|
+
const p = this.pubs[exchange];
|
|
157
154
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
155
|
+
if (!key) {
|
|
156
|
+
reject(new Error(`Direct message require a key`));
|
|
157
|
+
}
|
|
161
158
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
159
|
+
if (!p) {
|
|
160
|
+
reject(new Error(`No PUB for ${exchange} found`));
|
|
161
|
+
}
|
|
165
162
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
// if not in JSON format, let's make it so
|
|
169
|
-
content = {
|
|
170
|
-
msg: msg
|
|
163
|
+
if (p.type !== 'direct') {
|
|
164
|
+
reject(new Error(`PUB ${exchange} is not direct`));
|
|
171
165
|
}
|
|
172
|
-
}
|
|
173
166
|
|
|
174
|
-
|
|
167
|
+
let content = msg;
|
|
168
|
+
if (content.constructor !== ({}).constructor) {
|
|
169
|
+
// if not in JSON format, let's make it so
|
|
170
|
+
content = {
|
|
171
|
+
msg: msg
|
|
172
|
+
}
|
|
173
|
+
}
|
|
175
174
|
|
|
175
|
+
return p.publish({key, content});
|
|
176
|
+
});
|
|
176
177
|
}
|
|
177
178
|
|
|
178
179
|
/**
|
package/queue/publisher.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const logger = require("./queue-logger");
|
|
2
|
+
const uuidv4 = require("uuid/v4");
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* A publisher for AMQP service
|
|
@@ -6,7 +7,7 @@ const logger = require("./queue-logger");
|
|
|
6
7
|
class Publisher {
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
|
-
*
|
|
10
|
+
*
|
|
10
11
|
* @param {String} exchange AMQP exchange name.
|
|
11
12
|
* @param {String} type direct|fanout| etc (supported types by RabbitMQ).
|
|
12
13
|
* @param {boolean} durable true if the messages should persist service restarts.
|
|
@@ -27,7 +28,7 @@ class Publisher {
|
|
|
27
28
|
*/
|
|
28
29
|
async init (conn) {
|
|
29
30
|
logger.debug(`Initializing PUB channel for ${this.name}`);
|
|
30
|
-
|
|
31
|
+
|
|
31
32
|
if(this.channel) {
|
|
32
33
|
logger.warn(`PUB channel for ${this.name} already initialized`);
|
|
33
34
|
return;
|
|
@@ -54,20 +55,31 @@ class Publisher {
|
|
|
54
55
|
|
|
55
56
|
/**
|
|
56
57
|
* Publishes a message to AMQP
|
|
57
|
-
* @param {
|
|
58
|
+
* @param {Object} msg A json object to be published to AMQP.
|
|
59
|
+
* A tracking uuid for the message will be returned.
|
|
58
60
|
*/
|
|
59
|
-
publish(msg) {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
const content = Buffer.from(JSON.stringify(msg.content));
|
|
65
|
-
this.channel.publish(this.exchange, msg.key, content, {persistent: true}, (err, ok) => {
|
|
66
|
-
if (err) {
|
|
67
|
-
logger.error(`An error occured publishing message to ${this.name}`);
|
|
68
|
-
} else {
|
|
69
|
-
logger.debug("Message delivered:", msg);
|
|
61
|
+
async publish(msg) {
|
|
62
|
+
return new Promise((resolve, reject) => {
|
|
63
|
+
|
|
64
|
+
if (!this.channel) {
|
|
65
|
+
reject(new Error(`No channel for ${this.name} to publish`));
|
|
70
66
|
}
|
|
67
|
+
|
|
68
|
+
logger.debug("Publishing message", msg);
|
|
69
|
+
const content = {
|
|
70
|
+
tracking_id: uuidv4(),
|
|
71
|
+
data: Buffer.from(JSON.stringify(msg.content))
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
this.channel.publish(this.exchange, msg.key, content, {persistent: true}, (err, ok) => {
|
|
75
|
+
if (err) {
|
|
76
|
+
logger.error(`An error occured publishing message to ${this.name}`);
|
|
77
|
+
reject(err);
|
|
78
|
+
} else {
|
|
79
|
+
logger.debug("Message delivered:", msg);
|
|
80
|
+
resolve(content.tracking_id);
|
|
81
|
+
}
|
|
82
|
+
});
|
|
71
83
|
});
|
|
72
84
|
}
|
|
73
85
|
}
|
package/queue/subscriber.js
CHANGED
|
@@ -29,11 +29,11 @@ class Subscriber {
|
|
|
29
29
|
* Initializes the subscriber on the given connection.
|
|
30
30
|
* During this call, the exchange and the queues are asserted
|
|
31
31
|
* and bound via the key provided in the constructor.
|
|
32
|
-
* @param {
|
|
32
|
+
* @param {Object} conn aqmp connection object
|
|
33
33
|
*/
|
|
34
34
|
async init (conn) {
|
|
35
35
|
logger.debug(`Initializing SUB channel for ${this.name}`);
|
|
36
|
-
|
|
36
|
+
|
|
37
37
|
if(this.channel) {
|
|
38
38
|
logger.warn(`SUB channel for ${this.name} already initialized`);
|
|
39
39
|
return;
|
|
@@ -57,10 +57,10 @@ class Subscriber {
|
|
|
57
57
|
logger.debug(`Initialized SUB channel for ${this.name}`);
|
|
58
58
|
await this.channel.assertExchange(this.exchange, this.type, {durable: this.durable});
|
|
59
59
|
await this.channel.assertQueue(this.queue, { durable: this.durable });
|
|
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);
|
|
63
|
-
|
|
63
|
+
|
|
64
64
|
this.channel.consume(this.queue, (msg) => {
|
|
65
65
|
const { fields, content } = msg;
|
|
66
66
|
const payload = JSON.parse(content.toString());
|