@sera4/essentia 1.1.65 → 3.0.6

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.
Files changed (112) hide show
  1. package/README.md +352 -8
  2. package/dist/ts/cache/index.d.ts +33 -0
  3. package/dist/ts/cache/index.js +171 -0
  4. package/dist/ts/cache/index.js.map +1 -0
  5. package/dist/ts/configurations/server-config.d.ts +4 -0
  6. package/dist/ts/configurations/server-config.js +2 -0
  7. package/dist/ts/configurations/server-config.js.map +1 -0
  8. package/dist/ts/constants/index.d.ts +7 -0
  9. package/{constants → dist/ts/constants}/index.js +5 -6
  10. package/dist/ts/constants/index.js.map +1 -0
  11. package/dist/ts/cycle/index.d.ts +53 -0
  12. package/dist/ts/cycle/index.js +169 -0
  13. package/dist/ts/cycle/index.js.map +1 -0
  14. package/dist/ts/dns/DnsCache.d.ts +50 -0
  15. package/dist/ts/dns/DnsCache.js +137 -0
  16. package/dist/ts/dns/DnsCache.js.map +1 -0
  17. package/dist/ts/dns/index.d.ts +4 -0
  18. package/dist/ts/dns/index.js +4 -0
  19. package/dist/ts/dns/index.js.map +1 -0
  20. package/dist/ts/formatter/index.d.ts +44 -0
  21. package/dist/ts/formatter/index.js +76 -0
  22. package/dist/ts/formatter/index.js.map +1 -0
  23. package/dist/ts/hal/index.d.ts +26 -0
  24. package/dist/ts/hal/index.js +70 -0
  25. package/dist/ts/hal/index.js.map +1 -0
  26. package/dist/ts/health/index.d.ts +53 -0
  27. package/dist/ts/health/index.js +165 -0
  28. package/dist/ts/health/index.js.map +1 -0
  29. package/dist/ts/helpers/index.d.ts +1 -0
  30. package/dist/ts/helpers/index.js +2 -0
  31. package/dist/ts/helpers/index.js.map +1 -0
  32. package/dist/ts/helpers/test-server-wrapper.d.ts +56 -0
  33. package/dist/ts/helpers/test-server-wrapper.js +144 -0
  34. package/dist/ts/helpers/test-server-wrapper.js.map +1 -0
  35. package/dist/ts/index.d.ts +21 -0
  36. package/dist/ts/index.js +25 -0
  37. package/dist/ts/index.js.map +1 -0
  38. package/dist/ts/last-commit/index.d.ts +12 -0
  39. package/dist/ts/last-commit/index.js +82 -0
  40. package/dist/ts/last-commit/index.js.map +1 -0
  41. package/dist/ts/logger/index.d.ts +3 -0
  42. package/dist/ts/logger/index.js +4 -0
  43. package/dist/ts/logger/index.js.map +1 -0
  44. package/dist/ts/logger/s4-logger.d.ts +23 -0
  45. package/dist/ts/logger/s4-logger.js +103 -0
  46. package/dist/ts/logger/s4-logger.js.map +1 -0
  47. package/dist/ts/paginator/index.d.ts +2 -0
  48. package/dist/ts/paginator/index.js +3 -0
  49. package/dist/ts/paginator/index.js.map +1 -0
  50. package/dist/ts/paginator/s4-pagination.d.ts +26 -0
  51. package/dist/ts/paginator/s4-pagination.js +49 -0
  52. package/dist/ts/paginator/s4-pagination.js.map +1 -0
  53. package/dist/ts/paginator/sql-pagination.d.ts +52 -0
  54. package/dist/ts/paginator/sql-pagination.js +157 -0
  55. package/dist/ts/paginator/sql-pagination.js.map +1 -0
  56. package/dist/ts/paper-trail/helpers.d.ts +14 -0
  57. package/dist/ts/paper-trail/helpers.js +74 -0
  58. package/dist/ts/paper-trail/helpers.js.map +1 -0
  59. package/dist/ts/paper-trail/index.d.ts +54 -0
  60. package/dist/ts/paper-trail/index.js +236 -0
  61. package/dist/ts/paper-trail/index.js.map +1 -0
  62. package/dist/ts/prompts/index.d.ts +21 -0
  63. package/dist/ts/prompts/index.js +68 -0
  64. package/dist/ts/prompts/index.js.map +1 -0
  65. package/dist/ts/queue/index.d.ts +196 -0
  66. package/dist/ts/queue/index.js +371 -0
  67. package/dist/ts/queue/index.js.map +1 -0
  68. package/dist/ts/queue/publisher.d.ts +41 -0
  69. package/dist/ts/queue/publisher.js +84 -0
  70. package/dist/ts/queue/publisher.js.map +1 -0
  71. package/dist/ts/queue/queue-logger.d.ts +10 -0
  72. package/dist/ts/queue/queue-logger.js +35 -0
  73. package/dist/ts/queue/queue-logger.js.map +1 -0
  74. package/dist/ts/queue/subscriber.d.ts +55 -0
  75. package/dist/ts/queue/subscriber.js +98 -0
  76. package/dist/ts/queue/subscriber.js.map +1 -0
  77. package/dist/ts/safe-proxy/index.d.ts +35 -0
  78. package/dist/ts/safe-proxy/index.js +54 -0
  79. package/dist/ts/safe-proxy/index.js.map +1 -0
  80. package/dist/ts/serializer/index.d.ts +52 -0
  81. package/dist/ts/serializer/index.js +83 -0
  82. package/dist/ts/serializer/index.js.map +1 -0
  83. package/dist/ts/utils/index.d.ts +49 -0
  84. package/dist/ts/utils/index.js +157 -0
  85. package/dist/ts/utils/index.js.map +1 -0
  86. package/package.json +52 -11
  87. package/package.tar.gz +0 -0
  88. package/cache/index.js +0 -188
  89. package/formatter/index.js +0 -81
  90. package/hal/README.md +0 -54
  91. package/hal/index.js +0 -75
  92. package/health/health-logger.js +0 -40
  93. package/health/index.js +0 -188
  94. package/helpers/index.js +0 -1
  95. package/helpers/test-server-wrapper.js +0 -196
  96. package/last-commit.js +0 -12
  97. package/last_commit/index.js +0 -82
  98. package/logger/s4-logger.js +0 -128
  99. package/paginator/README.md +0 -183
  100. package/paginator/s4-pagination.js +0 -51
  101. package/paginator/sql-pagination.js +0 -178
  102. package/paper-trail/README.md +0 -68
  103. package/paper-trail/helpers.js +0 -99
  104. package/paper-trail/index.js +0 -842
  105. package/prompts/index.js +0 -78
  106. package/queue/index.js +0 -414
  107. package/queue/publisher.js +0 -96
  108. package/queue/queue-logger.js +0 -40
  109. package/queue/subscriber.js +0 -107
  110. package/safe_proxy/index.js +0 -61
  111. package/serializer/index.js +0 -90
  112. package/utils/index.js +0 -74
package/prompts/index.js DELETED
@@ -1,78 +0,0 @@
1
- /* Essentia Common Prompt Functions */
2
- // This file contains common prompt functions that are used in CLI scripts
3
- // that are run in the each project. These functions are used to
4
- // prompt the user for input and to wait for the user to press enter
5
- // before continuing. This is useful for scripts that are run in the
6
- // command line and need to wait for the user to do something before
7
- // continuing.
8
-
9
- import logger from "../logger/s4-logger.js";
10
- import { ArgumentParser } from 'argparse'
11
- import prompt from "prompt";
12
-
13
- // New common argument parser which makes parsing and setting up commandline options easier
14
- const setupCLIParser = (description) => {
15
- logger.info(description)
16
- return new ArgumentParser({ description });
17
- }
18
-
19
- // a common init function which waits for the command prompt to be delivered
20
- // a common use is for the awaiting of the queues to initialize before running
21
- const commonCLIInit = async(options) => {
22
- //add in defaults to options if they have not been specified or object is {}
23
- options = { asyncFcns: [], extraFcns: [], sleepTime: 1000, ...options };
24
-
25
- // Sleep for 1 second to allow the prompt to be displayed
26
- // at the end of bootstrap logs
27
- await cliSleep(options.sleepTime);
28
-
29
- options.extraFcns.forEach(fcn => {
30
- fcn();
31
- });
32
-
33
- for (const fcn of options.asyncFcns) {
34
- await fcn();
35
- }
36
- }
37
-
38
- const promptCLIAsync = async (options) => {
39
- return new Promise((resolve, reject) => {
40
- prompt.start();
41
- prompt.get(options, (err, res) => {
42
- if (err) {
43
- reject(err);
44
- } else {
45
- resolve(res);
46
- }
47
- })
48
- });
49
- }
50
-
51
- // A predefined waitPrompt for use at the end of the scripts that use
52
- // message queues. Message Queues often take a bit to fire
53
- const waitCLIPrompt = {
54
- "Push <enter> when done" : {
55
- type: "boolean"
56
- }
57
- };
58
-
59
- const continueCLIPrompt = {
60
- "Push <enter> to continue" : {
61
- type: "boolean"
62
- }
63
- };
64
-
65
- const cliSleep = async(ms) => {
66
- return new Promise((resolve) => {
67
- setTimeout(resolve, ms);
68
- });
69
- }
70
-
71
- export {
72
- commonCLIInit,
73
- continueCLIPrompt,
74
- promptCLIAsync,
75
- setupCLIParser,
76
- cliSleep,
77
- waitCLIPrompt
78
- };
package/queue/index.js DELETED
@@ -1,414 +0,0 @@
1
- "use strict";
2
- import amqp from "amqplib/callback_api.js";
3
- import logger from "./queue-logger.js";
4
- import urlParse from "url-parse";
5
- import { Publisher } from "./publisher.js";
6
- import { Subscriber } from "./subscriber.js";
7
-
8
- /**
9
- * An interface to AMQP service
10
- */
11
- class S4Queue {
12
- constructor() {
13
- this.connection = null;
14
- this.retry = 10;
15
- this.pubs = {};
16
- this.subs = {};
17
- }
18
-
19
- /**
20
- * Tests the connection to the service.
21
- * Internally, the same connection is used for test
22
- * and production code, but a separate channel is opened.
23
- * Once the channel is obtained, there is an assert on the
24
- * existence of a topic exchange called '--health-check--'.
25
- * The following must be satisfied for the test to pass
26
- * 1. must be connected
27
- * 2. must have a test channel
28
- * 3. must assert the exchange
29
- */
30
- async testConnection() {
31
- if (!this.isConnected()) {
32
- this.connectionTestChannel = null;
33
- throw("queue service not connected")
34
- }
35
-
36
- this.connectionTestChannel = this.connectionTestChannel || await this.connection.createChannel();
37
- if (!this.connectionTestChannel) {
38
- throw("queue service failed to obtain a channel")
39
- }
40
-
41
- await this.connectionTestChannel.assertExchange("--health-check--", "topic", {durable: false});
42
- }
43
-
44
- /**
45
- * Opens a connection to AMQP server.
46
- * @param {Object} options Connection parameters. At the very least options.connectionUrl must be present.
47
- */
48
- async openConnection(options) {
49
- logger.setEnabled(options.logEnabled);
50
-
51
- if (this.connection) {
52
- logger.warn("Already initialized");
53
- return;
54
- }
55
-
56
- const retry = parseInt(options.retryInterval);
57
- this.retry = retry || 10;
58
-
59
- logger.debug("Attempting connection...");
60
-
61
- // The docs say there is amqp.connect returning
62
- // a promise, but this version doesn't seem to like
63
- // when a callback isn't provided, therefore run
64
- // in a new Promise
65
- return new Promise((resolve, reject) => {
66
- // We may run into issues with SNI TLS if
67
- // we host with 3rd parties using our own certs.
68
- // To avoid any issues, let's parse the url
69
- // and provide the server name in the socket options
70
- const urlParts = urlParse(options.connectionUrl);
71
- const socketOptions = {servername: urlParts.hostname};
72
-
73
- amqp.connect(options.connectionUrl, socketOptions, (err, conn) => {
74
-
75
- if (err) {
76
- logger.error("Error while connecting", err)
77
- logger.error("Attempting again in", this.retry, "seconds");
78
- return setTimeout(() => this.openConnection(options), this.retry * 1000);
79
- }
80
-
81
- this.connection = conn;
82
- if (!this.connection) {
83
- return reject(new Error("Failed to obtain connection"));
84
- } else {
85
- this.updatePubsAndSubsConnectionRef(conn);
86
- }
87
-
88
- this.connection.on("error", (err) => {
89
- logger.error("connection error", err);
90
- // Do not attempt to reconnect as the
91
- // 'close' event will also be emitted
92
- // if this error closes the connection
93
- });
94
-
95
- this.connection.on("close", (err) => {
96
- logger.error("connection closed");
97
- this.connection = null;
98
- if (err) {
99
- logger.error("connection closed due to error:", err);
100
- logger.error("Reconnecting in", this.retry, "seconds");
101
- setTimeout(() => this.openConnection(options), this.retry * 1000);
102
- } else {
103
- // If there was no error, it is us who closed
104
- // this connection, thus a reconnection will not
105
- // be attempted.
106
- logger.debug("Will not re-attempt");
107
- }
108
- });
109
-
110
- logger.debug("Connected");
111
- resolve();
112
- });
113
- });
114
- }
115
-
116
- /**
117
- * Closes the connection to AMQP server
118
- * and clears the registered publishers
119
- * and subscribers
120
- */
121
- async closeConnection() {
122
- return new Promise((resolve, reject) => {
123
- if (!this.connection) {
124
- return resolve();
125
- }
126
- this.connection.close((err) => {
127
- if (err) {
128
- reject(err);
129
- } else {
130
- this.subs = {};
131
- this.pubs = {};
132
- this.connection = null;
133
- resolve();
134
- }
135
- });
136
- });
137
- }
138
-
139
- /**
140
- * Returns whether we have an active connection
141
- * or not.
142
- */
143
- isConnected() {
144
- return this.connection != null && typeof(this.connection) != 'undefined';
145
- }
146
-
147
- /**
148
- * @deprecated Use registerPublisher
149
- * Registers a new direct publisher on the given exchange.
150
- * If exchange doesn't exist, one will be created.
151
- * @param {String} exchange the name of the exchange on AMQP service.
152
- */
153
- async registerDirectPublisher(exchange) {
154
- return this.registerPublisher({exchange: {name: exchange, type: "direct"}});
155
- }
156
-
157
- /**
158
- * @deprecated Use registerPublisher
159
- * Registers a new topic publisher on the given exchange.
160
- * If exchange doesn't exist, one will be created.
161
- * @param {String} exchange the name of the exchange on AMQP service.
162
- */
163
- async registerTopicPublisher(exchange) {
164
- return this.registerPublisher({exchange: {name: exchange, type: "topic"}});
165
- }
166
-
167
- /**
168
- * @deprecated Use registerPublisher
169
- * Registers a new fanout publisher on the given exchange.
170
- * If exchange doesn't exist, one will be created.
171
- * @param {String} exchange the name of the exchange on AMQP service.
172
- */
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;
184
- if (Object.keys(this.pubs).includes(name)) {
185
- throw new Error(`Publisher ${name} already registered`);
186
- }
187
-
188
- const p = new Publisher(exchange, name);
189
- await p.init(this.connection);
190
- this.pubs[name] = p;
191
- }
192
-
193
- /**
194
- * Checks whether or not a publisher with given parameters
195
- * has been registered already
196
- * @param {String} exchange the name of the exchange.
197
- * @returns true if registered already, false otherwise
198
- */
199
- hasPublisher(exchange) {
200
- return Object.keys(this.pubs).includes(exchange);
201
- }
202
-
203
- /**
204
- * Removes a previously registered publisher
205
- * @param {Object} options
206
- */
207
- async unregisterPublisher(options) {
208
- const { exchange } = options;
209
- const { name } = exchange;
210
- if (!Object.keys(this.pubs).includes(name)) {
211
- throw new Error(`Publisher ${name} not registered`);
212
- }
213
- const p = this.pubs[name];
214
- await p.closeChannel();
215
-
216
- delete this.pubs[name];
217
- logger.debug("Unregistered publisher", name);
218
- }
219
-
220
- /**
221
- * @deprecated Use registerSubscriber
222
- * Registers a new direct subscriber on a given exchange and queue.
223
- * If exchange and queue don't exists, they'll be created
224
- * and bound through the provided routing key.
225
- * @param {String} exchange the name of the exchange.
226
- * @param {String} queue the name of the queue.
227
- * @param {String} key the name of the routing key.
228
- */
229
- async registerDirectSubscriber(exchange, queue, key) {
230
- const options = {
231
- key,
232
- exchange: { name: exchange, type: "direct" },
233
- queue: { name: queue }
234
- }
235
- return this.registerSubscriber(options);
236
- }
237
-
238
- /**
239
- * @deprecated Use registerSubscriber
240
- * Registers a new topic subscriber on a given exchange and queue.
241
- * If exchange and queue don't exists, they'll be created
242
- * and bound through the provided routing key.
243
- * @param {String} exchange the name of the exchange.
244
- * @param {String} queue the name of the queue.
245
- * @param {String} key the name of the routing key.
246
- */
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
-
265
- if (Object.keys(this.subs).includes(name)) {
266
- throw new Error(`Subscriber ${name} already registered`);
267
- }
268
-
269
- const s = new Subscriber(exchange, queue, key, name);
270
- await s.init(this.connection);
271
- this.subs[name] = s;
272
- }
273
-
274
- /**
275
- * Checks whether or not a subscriber with given parameters
276
- * has been registered already
277
- * @param {String} exchange the name of the exchange.
278
- * @param {String} queue the name of the queue.
279
- * @param {String} key the name of the routing key.
280
- * @returns true if registered already, false otherwise
281
- */
282
- hasSubscriber(exchange, queue, key) {
283
- const name = `${exchange}-${queue}-${key}`;
284
- return Object.keys(this.subs).includes(name);
285
- }
286
-
287
- /**
288
- * Removes a previously registered subscriber
289
- * @param {OBject} options
290
- */
291
- async unregisterSubscriber(options) {
292
- const {exchange, queue, key} = options;
293
- const name = `${exchange.name}-${queue.name}-${key}`;
294
-
295
- if (!Object.keys(this.subs).includes(name)) {
296
- throw new Error(`Subscriber ${name} not registered`);
297
- }
298
- const s = this.subs[name];
299
- await s.closeChannel();
300
-
301
- delete this.subs[name];
302
- logger.debug("Unregistered subscriber", name);
303
- }
304
-
305
- /**
306
- * Publishes a message on a given exchange with routing key.
307
- * @param {String} exchange the name of the exchange.
308
- * @param {String} key the name of the routing key.
309
- * @param {Object} msg a JSON object as the message.
310
- */
311
- async publishDirectMessage(exchange, key, msg) {
312
- return this.publishMessage(exchange, key, msg)
313
- }
314
-
315
- /**
316
- * Publishes a message on a given exchange with a topic.
317
- * @param {String} exchange the name of the exchange.
318
- * @param {String} topic the name of the topic.
319
- * @param {Object} msg a JSON object as the message.
320
- */
321
- async publishTopicMessage(exchange, topic, msg) {
322
- return this.publishMessage(exchange, topic, msg)
323
- }
324
-
325
- /**
326
- * Publish a message to the exchange
327
- * @param {String} exchange name of exchange
328
- * @param {String} key name of the key or topic
329
- * @param {Object} msg payload to be published
330
- */
331
- async publishMessage(exchange, key, msg) {
332
- if (!key) {
333
- throw(new Error(`Key argument not defined`));
334
- }
335
-
336
- const p = this.pubs[exchange];
337
- if (!p) {
338
- throw(new Error(`No PUB for ${exchange} found`));
339
- }
340
-
341
- let content = msg;
342
- if (content.constructor !== ({}).constructor) {
343
- // if not in JSON format, let's make it so
344
- content = {
345
- msg: msg
346
- }
347
- }
348
-
349
- return await p.publish({key, content});
350
- }
351
-
352
- /**
353
- * Binds an exchange to a queue
354
- * @param {String} exchange the name of the exchange.
355
- * @param {String} queue the name of the queue.
356
- * @param {pattern} (optional) pattern for routing key
357
- */
358
- async bindQueueToExchange(exchange, queue, pattern) {
359
- const p = this.pubs[exchange];
360
-
361
- if (!p) {
362
- throw(new Error(`No PUB for ${exchange} found`));
363
- }
364
-
365
- if (!queue) {
366
- throw(new Error(`Binding a queue requires a queue name`));
367
- }
368
-
369
- return await p.bindQueue(queue, pattern)
370
- }
371
-
372
- /**
373
- * Adds a listener for a specific key.
374
- * If a subscriber that was regitered was found
375
- * with that key, the listener is added, otherwise
376
- * it's ignored
377
- * @param {*} key AMQP routing key
378
- * @param {*} listener callback
379
- */
380
- addMessageListener(key, listener) {
381
- Object.keys(this.subs).forEach(k => {
382
- const s = this.subs[k];
383
- if (s.key === key) {
384
- s.registerCallback(listener);
385
- }
386
- });
387
- }
388
-
389
- /**
390
- * Updates the connection reference to all the previously
391
- * registered publishers and subscribers.
392
- * After we successfully establish a connection and register pubs/subs,
393
- * it's possible for the connection to break. This object will try
394
- * to re-establish a new connection every 3 seconds (or whatever configuration is set)
395
- * and if a new connection is established, we need to pass that connection to
396
- * all our pubs/subs, otherwise they'll be operating with old/stale references
397
- * @param {Object} connRef a new connection reference to the AMQP service
398
- */
399
- updatePubsAndSubsConnectionRef(connRef) {
400
- Object.keys(this.pubs).forEach(k => {
401
- logger.debug(`Updating publisher ${k} with a new connection object`);
402
- const p = this.pubs[k];
403
- p.init(connRef);
404
- });
405
- Object.keys(this.subs).forEach(k => {
406
- logger.debug(`Updating subscriber ${k} with a new connection object`);
407
- const s = this.subs[k];
408
- s.init(connRef);
409
- });
410
- }
411
- }
412
-
413
- const queue = new S4Queue();
414
- export default queue;
@@ -1,96 +0,0 @@
1
- import logger from "./queue-logger.js";
2
- import { v4 as uuidv4 } from 'uuid';
3
-
4
- /**
5
- * A publisher for AMQP service
6
- */
7
- export class Publisher {
8
- /**
9
- * @param {Object} exchange AMQP exchange name.
10
- * @param {String} name a name to identify this publisher in the logs.
11
- */
12
- constructor(exchange, name) {
13
- this.exchange = exchange;
14
- this.name = name;
15
- this.channel = null;
16
- }
17
-
18
- /**
19
- * Initializes this publisher with a connection
20
- * and asserts the exchange exists
21
- * @param {Object} conn amqp connection object
22
- */
23
- async init (conn) {
24
- logger.debug(`Initializing PUB channel for ${this.name}`);
25
-
26
- if(this.channel) {
27
- logger.warn(`PUB channel for ${this.name} already initialized`);
28
- return;
29
- }
30
-
31
- this.channel = await conn.createConfirmChannel();
32
- if (!this.channel) {
33
- throw(new Error(`Cannot create PUB channel for ${this.name}`));
34
- }
35
-
36
- logger.debug(`Initialized PUB channel for ${this.name}`);
37
- const {name, type, options} = this.exchange;
38
- await this.channel.assertExchange(name, type, options);
39
-
40
- this.channel.on("error", (err) => {
41
- logger.debug(`Connection error for ${this.name}`, err);
42
- this.channel = null;
43
- });
44
-
45
- this.channel.on("close", () => {
46
- logger.debug(`Connection closed for ${this.name}`);
47
- this.channel = null;
48
- });
49
- }
50
-
51
- /**
52
- * Publishes a message to AMQP
53
- * @param {Object} msg A json object to be published to AMQP.
54
- * A tracking uuid for the message will be returned.
55
- */
56
- async publish(msg) {
57
- if (!this.channel) {
58
- throw(new Error(`No channel for ${this.name} to publish`));
59
- }
60
-
61
- const parcel = {
62
- tracking_id: uuidv4(),
63
- timestamp: new Date(),
64
- data: msg.content
65
- }
66
- logger.debug("Publishing message");
67
- logger.debug("Key", msg.key);
68
- logger.debug("Data", parcel);
69
-
70
- const content = Buffer.from(JSON.stringify(parcel));
71
- const res = await this.channel.publish(this.exchange.name, msg.key, content, {persistent: true});
72
-
73
- if (!res) {
74
- throw(new Error("Channel write buffer is full"));
75
- }
76
-
77
- return parcel.tracking_id;
78
- }
79
-
80
- /**
81
- * Closes the channel
82
- * @returns promise
83
- */
84
- async closeChannel() {
85
- if (this.channel) {
86
- logger.debug("Closing publishing channel", this.name);
87
- return this.channel.close();
88
- } else {
89
- logger.error("Can't close a null publishing channel");
90
- }
91
- }
92
-
93
- async bindQueue(queue, pattern, args) {
94
- await this.channel.bindQueue(queue, this.exchange, pattern, args)
95
- }
96
- }
@@ -1,40 +0,0 @@
1
- import logger from "../logger/s4-logger.js";
2
- let enabled = false;
3
-
4
- const helpers = {
5
- debug(...args) {
6
- if (enabled) {
7
- logger.debug(":: AMQP ::", args);
8
- }
9
- },
10
-
11
- log(...args) {
12
- if (enabled) {
13
- logger.debug(":: AMQP ::", args);
14
- }
15
- },
16
-
17
- warn(...args) {
18
- if (enabled) {
19
- logger.warn(":: AMQP ::", args);
20
- }
21
- },
22
-
23
- info(...args) {
24
- if (enabled) {
25
- logger.info(":: AMQP ::", args);
26
- }
27
- },
28
-
29
- error(...args) {
30
- if (enabled) {
31
- logger.error(":: AMQP ::", args);
32
- }
33
- },
34
-
35
- setEnabled(e) {
36
- enabled = e;
37
- }
38
- }
39
-
40
- export default helpers;