@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
@@ -0,0 +1,371 @@
1
+ import amqp from "amqplib/callback_api.js";
2
+ import logger from "./queue-logger.js";
3
+ import urlParse from "url-parse";
4
+ import { Publisher } from "./publisher.js";
5
+ import { Subscriber } from "./subscriber.js";
6
+ /**
7
+ * An interface to AMQP service
8
+ */
9
+ class S4Queue {
10
+ constructor() {
11
+ this.connection = null;
12
+ this.connectionTestChannel = null;
13
+ this.retry = 10;
14
+ this.pubs = {};
15
+ this.subs = {};
16
+ }
17
+ /**
18
+ * Tests the connection to the service.
19
+ * Internally, the same connection is used for test
20
+ * and production code, but a separate channel is opened.
21
+ * Once the channel is obtained, there is an assert on the
22
+ * existence of a topic exchange called '--health-check--'.
23
+ * The following must be satisfied for the test to pass
24
+ * 1. must be connected
25
+ * 2. must have a test channel
26
+ * 3. must assert the exchange
27
+ */
28
+ async testConnection() {
29
+ if (!this.isConnected()) {
30
+ this.connectionTestChannel = null;
31
+ throw new Error("queue service not connected");
32
+ }
33
+ this.connectionTestChannel = this.connectionTestChannel || await this.connection.createChannel();
34
+ if (!this.connectionTestChannel) {
35
+ throw new Error("queue service failed to obtain a channel");
36
+ }
37
+ await this.connectionTestChannel.assertExchange("--health-check--", "topic", { durable: false });
38
+ }
39
+ /**
40
+ * Opens a connection to AMQP server.
41
+ * @param options Connection parameters. At the very least options.connectionUrl must be present.
42
+ */
43
+ async openConnection(options) {
44
+ logger.setEnabled(options.logEnabled || false);
45
+ if (this.connection) {
46
+ logger.warn("Already initialized");
47
+ return;
48
+ }
49
+ const retry = parseInt(options.retryInterval?.toString() || "10");
50
+ this.retry = retry || 10;
51
+ logger.debug("Attempting connection...");
52
+ // The docs say there is amqp.connect returning
53
+ // a promise, but this version doesn't seem to like
54
+ // when a callback isn't provided, therefore run
55
+ // in a new Promise
56
+ return new Promise((resolve, reject) => {
57
+ // We may run into issues with SNI TLS if
58
+ // we host with 3rd parties using our own certs.
59
+ // To avoid any issues, let's parse the url
60
+ // and provide the server name in the socket options
61
+ const urlParts = urlParse(options.connectionUrl);
62
+ const socketOptions = { servername: urlParts.hostname };
63
+ amqp.connect(options.connectionUrl, socketOptions, (err, conn) => {
64
+ if (err) {
65
+ logger.error("Error while connecting", err);
66
+ logger.error("Attempting again in", this.retry, "seconds");
67
+ return setTimeout(() => this.openConnection(options), this.retry * 1000);
68
+ }
69
+ this.connection = conn;
70
+ if (!this.connection) {
71
+ return reject(new Error("Failed to obtain connection"));
72
+ }
73
+ else {
74
+ this.updatePubsAndSubsConnectionRef(conn);
75
+ }
76
+ this.connection.on("error", (err) => {
77
+ logger.error("connection error", err);
78
+ // Do not attempt to reconnect as the
79
+ // 'close' event will also be emitted
80
+ // if this error closes the connection
81
+ });
82
+ this.connection.on("close", (err) => {
83
+ logger.error("connection closed");
84
+ this.connection = null;
85
+ if (err) {
86
+ logger.error("connection closed due to error:", err);
87
+ logger.error("Reconnecting in", this.retry, "seconds");
88
+ setTimeout(() => this.openConnection(options), this.retry * 1000);
89
+ }
90
+ else {
91
+ // If there was no error, it is us who closed
92
+ // this connection, thus a reconnection will not
93
+ // be attempted.
94
+ logger.debug("Will not re-attempt");
95
+ }
96
+ });
97
+ logger.debug("Connected");
98
+ resolve();
99
+ });
100
+ });
101
+ }
102
+ /**
103
+ * Closes the connection to AMQP server
104
+ * and clears the registered publishers
105
+ * and subscribers
106
+ */
107
+ async closeConnection() {
108
+ return new Promise((resolve, reject) => {
109
+ if (!this.connection) {
110
+ return resolve();
111
+ }
112
+ this.connection.close((err) => {
113
+ if (err) {
114
+ reject(err);
115
+ }
116
+ else {
117
+ this.subs = {};
118
+ this.pubs = {};
119
+ this.connection = null;
120
+ resolve();
121
+ }
122
+ });
123
+ });
124
+ }
125
+ /**
126
+ * Returns whether we have an active connection
127
+ * or not.
128
+ */
129
+ isConnected() {
130
+ return this.connection != null && typeof (this.connection) != 'undefined';
131
+ }
132
+ /**
133
+ * @deprecated Use registerPublisher
134
+ * Registers a new direct publisher on the given exchange.
135
+ * If exchange doesn't exist, one will be created.
136
+ * @param exchange the name of the exchange on AMQP service.
137
+ */
138
+ async registerDirectPublisher(exchange) {
139
+ return this.registerPublisher({ exchange: { name: exchange, type: "direct" } });
140
+ }
141
+ /**
142
+ * @deprecated Use registerPublisher
143
+ * Registers a new topic publisher on the given exchange.
144
+ * If exchange doesn't exist, one will be created.
145
+ * @param exchange the name of the exchange on AMQP service.
146
+ */
147
+ async registerTopicPublisher(exchange) {
148
+ return this.registerPublisher({ exchange: { name: exchange, type: "topic" } });
149
+ }
150
+ /**
151
+ * @deprecated Use registerPublisher
152
+ * Registers a new fanout publisher on the given exchange.
153
+ * If exchange doesn't exist, one will be created.
154
+ * @param exchange the name of the exchange on AMQP service.
155
+ */
156
+ async registerFanoutPublisher(exchange) {
157
+ return this.registerPublisher({ exchange: { name: exchange, type: "fanout" } });
158
+ }
159
+ /**
160
+ * Registers a new publisher
161
+ * @param options connection options.
162
+ */
163
+ async registerPublisher(options) {
164
+ const { exchange } = options;
165
+ const { name } = exchange;
166
+ if (Object.keys(this.pubs).includes(name)) {
167
+ throw new Error(`Publisher ${name} already registered`);
168
+ }
169
+ const p = new Publisher(exchange, name);
170
+ await p.init(this.connection);
171
+ this.pubs[name] = p;
172
+ }
173
+ /**
174
+ * Checks whether or not a publisher with given parameters
175
+ * has been registered already
176
+ * @param exchange the name of the exchange.
177
+ * @returns true if registered already, false otherwise
178
+ */
179
+ hasPublisher(exchange) {
180
+ return Object.keys(this.pubs).includes(exchange);
181
+ }
182
+ /**
183
+ * Removes a previously registered publisher
184
+ * @param options
185
+ */
186
+ async unregisterPublisher(options) {
187
+ const { exchange } = options;
188
+ const { name } = exchange;
189
+ if (!Object.keys(this.pubs).includes(name)) {
190
+ throw new Error(`Publisher ${name} not registered`);
191
+ }
192
+ const p = this.pubs[name];
193
+ await p.closeChannel();
194
+ delete this.pubs[name];
195
+ logger.debug("Unregistered publisher", name);
196
+ }
197
+ /**
198
+ * @deprecated Use registerSubscriber
199
+ * Registers a new direct subscriber on a given exchange and queue.
200
+ * If exchange and queue don't exists, they'll be created
201
+ * and bound through the provided routing key.
202
+ * @param exchange the name of the exchange.
203
+ * @param queue the name of the queue.
204
+ * @param key the name of the routing key.
205
+ */
206
+ async registerDirectSubscriber(exchange, queue, key) {
207
+ const options = {
208
+ key,
209
+ exchange: { name: exchange, type: "direct" },
210
+ queue: { name: queue }
211
+ };
212
+ return this.registerSubscriber(options);
213
+ }
214
+ /**
215
+ * @deprecated Use registerSubscriber
216
+ * Registers a new topic subscriber on a given exchange and queue.
217
+ * If exchange and queue don't exists, they'll be created
218
+ * and bound through the provided routing key.
219
+ * @param exchange the name of the exchange.
220
+ * @param queue the name of the queue.
221
+ * @param key the name of the routing key.
222
+ */
223
+ async registerTopicSubscriber(exchange, queue, key) {
224
+ const options = {
225
+ key,
226
+ exchange: { name: exchange, type: "topic" },
227
+ queue: { name: queue }
228
+ };
229
+ return this.registerSubscriber(options);
230
+ }
231
+ /**
232
+ * An overloaded version of registering a subscriber
233
+ * allowing for more fine tuned parameters
234
+ * @param options subscribing options
235
+ */
236
+ async registerSubscriber(options) {
237
+ const { exchange, queue, key } = options;
238
+ const name = `${exchange.name}-${queue.name}-${key}`;
239
+ if (Object.keys(this.subs).includes(name)) {
240
+ throw new Error(`Subscriber ${name} already registered`);
241
+ }
242
+ const s = new Subscriber(exchange, queue, key, name);
243
+ await s.init(this.connection);
244
+ this.subs[name] = s;
245
+ }
246
+ /**
247
+ * Checks whether or not a subscriber with given parameters
248
+ * has been registered already
249
+ * @param exchange the name of the exchange.
250
+ * @param queue the name of the queue.
251
+ * @param key the name of the routing key.
252
+ * @returns true if registered already, false otherwise
253
+ */
254
+ hasSubscriber(exchange, queue, key) {
255
+ const name = `${exchange}-${queue}-${key}`;
256
+ return Object.keys(this.subs).includes(name);
257
+ }
258
+ /**
259
+ * Removes a previously registered subscriber
260
+ * @param options
261
+ */
262
+ async unregisterSubscriber(options) {
263
+ const { exchange, queue, key } = options;
264
+ const name = `${exchange.name}-${queue.name}-${key}`;
265
+ if (!Object.keys(this.subs).includes(name)) {
266
+ throw new Error(`Subscriber ${name} not registered`);
267
+ }
268
+ const s = this.subs[name];
269
+ await s.closeChannel();
270
+ delete this.subs[name];
271
+ logger.debug("Unregistered subscriber", name);
272
+ }
273
+ /**
274
+ * Publishes a message on a given exchange with routing key.
275
+ * @param exchange the name of the exchange.
276
+ * @param key the name of the routing key.
277
+ * @param msg a JSON object as the message.
278
+ */
279
+ async publishDirectMessage(exchange, key, msg) {
280
+ return this.publishMessage(exchange, key, msg);
281
+ }
282
+ /**
283
+ * Publishes a message on a given exchange with a topic.
284
+ * @param exchange the name of the exchange.
285
+ * @param topic the name of the topic.
286
+ * @param msg a JSON object as the message.
287
+ */
288
+ async publishTopicMessage(exchange, topic, msg) {
289
+ return this.publishMessage(exchange, topic, msg);
290
+ }
291
+ /**
292
+ * Publish a message to the exchange
293
+ * @param exchange name of exchange
294
+ * @param key name of the key or topic
295
+ * @param msg payload to be published
296
+ */
297
+ async publishMessage(exchange, key, msg) {
298
+ if (!key) {
299
+ throw new Error(`Key argument not defined`);
300
+ }
301
+ const p = this.pubs[exchange];
302
+ if (!p) {
303
+ throw new Error(`No PUB for ${exchange} found`);
304
+ }
305
+ let content = msg;
306
+ if (content.constructor !== ({}).constructor) {
307
+ // if not in JSON format, let's make it so
308
+ content = {
309
+ msg: msg
310
+ };
311
+ }
312
+ return await p.publish({ key, content });
313
+ }
314
+ /**
315
+ * Binds an exchange to a queue
316
+ * @param exchange the name of the exchange.
317
+ * @param queue the name of the queue.
318
+ * @param pattern (optional) pattern for routing key
319
+ */
320
+ async bindQueueToExchange(exchange, queue, pattern) {
321
+ const p = this.pubs[exchange];
322
+ if (!p) {
323
+ throw new Error(`No PUB for ${exchange} found`);
324
+ }
325
+ if (!queue) {
326
+ throw new Error(`Binding a queue requires a queue name`);
327
+ }
328
+ return await p.bindQueue(queue, pattern || "");
329
+ }
330
+ /**
331
+ * Adds a listener for a specific key.
332
+ * If a subscriber that was registered was found
333
+ * with that key, the listener is added, otherwise
334
+ * it's ignored
335
+ * @param key AMQP routing key
336
+ * @param listener callback
337
+ */
338
+ addMessageListener(key, listener) {
339
+ Object.keys(this.subs).forEach(k => {
340
+ const s = this.subs[k];
341
+ if (s.key === key) {
342
+ s.registerCallback(listener);
343
+ }
344
+ });
345
+ }
346
+ /**
347
+ * Updates the connection reference to all the previously
348
+ * registered publishers and subscribers.
349
+ * After we successfully establish a connection and register pubs/subs,
350
+ * it's possible for the connection to break. This object will try
351
+ * to re-establish a new connection every 3 seconds (or whatever configuration is set)
352
+ * and if a new connection is established, we need to pass that connection to
353
+ * all our pubs/subs, otherwise they'll be operating with old/stale references
354
+ * @param connRef a new connection reference to the AMQP service
355
+ */
356
+ updatePubsAndSubsConnectionRef(connRef) {
357
+ Object.keys(this.pubs).forEach(k => {
358
+ logger.debug(`Updating publisher ${k} with a new connection object`);
359
+ const p = this.pubs[k];
360
+ p.init(connRef);
361
+ });
362
+ Object.keys(this.subs).forEach(k => {
363
+ logger.debug(`Updating subscriber ${k} with a new connection object`);
364
+ const s = this.subs[k];
365
+ s.init(connRef);
366
+ });
367
+ }
368
+ }
369
+ const queue = new S4Queue();
370
+ export default queue;
371
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../ts/queue/index.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,yBAAyB,CAAC;AAC3C,OAAO,MAAM,MAAM,mBAAmB,CAAC;AACvC,OAAO,QAAQ,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAqC7C;;GAEG;AACH,MAAM,OAAO;IAOX;QACE,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC;QACvB,IAAI,CAAC,qBAAqB,GAAG,IAAI,CAAC;QAClC,IAAI,CAAC,KAAK,GAAG,EAAE,CAAC;QAChB,IAAI,CAAC,IAAI,GAAG,EAAE,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,EAAE,CAAC;IACjB,CAAC;IAED;;;;;;;;;;OAUG;IACH,KAAK,CAAC,cAAc;QAClB,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE,EAAE,CAAC;YACxB,IAAI,CAAC,qBAAqB,GAAG,IAAI,CAAC;YAClC,M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C,MAAM,CAAC,KAAK,CAAC,uBAAuB,CAAC,+BAA+B,CAAC,CAAC;YACtE,MAAM,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;YACvB,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QAClB,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAED,MAAM,KAAK,GAAG,IAAI,OAAO,EAAE,CAAC;AAC5B,eAAe,KAAK,CAAC"}
@@ -0,0 +1,41 @@
1
+ interface Exchange {
2
+ name: string;
3
+ type: string;
4
+ options?: any;
5
+ }
6
+ interface PublishMessage {
7
+ key: string;
8
+ content: any;
9
+ }
10
+ /**
11
+ * A publisher for AMQP service
12
+ */
13
+ export declare class Publisher {
14
+ private exchange;
15
+ private name;
16
+ private channel;
17
+ /**
18
+ * @param exchange AMQP exchange configuration
19
+ * @param name a name to identify this publisher in the logs
20
+ */
21
+ constructor(exchange: Exchange, name: string);
22
+ /**
23
+ * Initializes this publisher with a connection
24
+ * and asserts the exchange exists
25
+ * @param conn amqp connection object
26
+ */
27
+ init(conn: any): Promise<void>;
28
+ /**
29
+ * Publishes a message to AMQP
30
+ * @param msg A json object to be published to AMQP.
31
+ * A tracking uuid for the message will be returned.
32
+ */
33
+ publish(msg: PublishMessage): Promise<string>;
34
+ /**
35
+ * Closes the channel
36
+ * @returns promise
37
+ */
38
+ closeChannel(): Promise<void>;
39
+ bindQueue(queue: string, pattern: string, args?: any): Promise<void>;
40
+ }
41
+ export {};
@@ -0,0 +1,84 @@
1
+ import logger from "./queue-logger.js";
2
+ import { v4 as uuidv4 } from 'uuid';
3
+ /**
4
+ * A publisher for AMQP service
5
+ */
6
+ export class Publisher {
7
+ /**
8
+ * @param exchange AMQP exchange configuration
9
+ * @param name a name to identify this publisher in the logs
10
+ */
11
+ constructor(exchange, name) {
12
+ this.exchange = exchange;
13
+ this.name = name;
14
+ this.channel = null;
15
+ }
16
+ /**
17
+ * Initializes this publisher with a connection
18
+ * and asserts the exchange exists
19
+ * @param conn amqp connection object
20
+ */
21
+ async init(conn) {
22
+ logger.debug(`Initializing PUB channel for ${this.name}`);
23
+ if (this.channel) {
24
+ logger.warn(`PUB channel for ${this.name} already initialized`);
25
+ return;
26
+ }
27
+ this.channel = await conn.createConfirmChannel();
28
+ if (!this.channel) {
29
+ throw new Error(`Cannot create PUB channel for ${this.name}`);
30
+ }
31
+ logger.debug(`Initialized PUB channel for ${this.name}`);
32
+ const { name, type, options } = this.exchange;
33
+ await this.channel.assertExchange(name, type, options);
34
+ this.channel.on("error", (err) => {
35
+ logger.debug(`Connection error for ${this.name}`, err);
36
+ this.channel = null;
37
+ });
38
+ this.channel.on("close", () => {
39
+ logger.debug(`Connection closed for ${this.name}`);
40
+ this.channel = null;
41
+ });
42
+ }
43
+ /**
44
+ * Publishes a message to AMQP
45
+ * @param msg A json object to be published to AMQP.
46
+ * A tracking uuid for the message will be returned.
47
+ */
48
+ async publish(msg) {
49
+ if (!this.channel) {
50
+ throw new Error(`No channel for ${this.name} to publish`);
51
+ }
52
+ const parcel = {
53
+ tracking_id: uuidv4(),
54
+ timestamp: new Date(),
55
+ data: msg.content
56
+ };
57
+ logger.debug("Publishing message");
58
+ logger.debug("Key", msg.key);
59
+ logger.debug("Data", parcel);
60
+ const content = Buffer.from(JSON.stringify(parcel));
61
+ const res = await this.channel.publish(this.exchange.name, msg.key, content, { persistent: true });
62
+ if (!res) {
63
+ throw new Error("Channel write buffer is full");
64
+ }
65
+ return parcel.tracking_id;
66
+ }
67
+ /**
68
+ * Closes the channel
69
+ * @returns promise
70
+ */
71
+ async closeChannel() {
72
+ if (this.channel) {
73
+ logger.debug("Closing publishing channel", this.name);
74
+ return this.channel.close();
75
+ }
76
+ else {
77
+ logger.error("Can't close a null publishing channel");
78
+ }
79
+ }
80
+ async bindQueue(queue, pattern, args) {
81
+ await this.channel.bindQueue(queue, this.exchange.name, pattern, args);
82
+ }
83
+ }
84
+ //# sourceMappingURL=publisher.js.map
@@ -0,0 +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QAED,OAAO,MAAM,CAAC,WAAW,CAAC;IAC5B,CAAC;IAED;;;OAGG;IACH,KAAK,CAAC,YAAY;QAChB,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;YACjB,MAAM,CAAC,KAAK,CAAC,4BAA4B,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC;YACtD,OAAO,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;QAC9B,CAAC;aAAM,CAAC;YACN,MAAM,CAAC,KAAK,CAAC,uCAAuC,CAAC,CAAC;QACxD,CAAC;IACH,CAAC;IAED,KAAK,CAAC,SAAS,CAAC,KAAa,EAAE,OAAe,EAAE,IAAU;QACxD,MAAM,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,KAAK,EAAE,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC;IACzE,CAAC;CACF"}
@@ -0,0 +1,10 @@
1
+ interface QueueLogger {
2
+ debug(...args: any[]): void;
3
+ log(...args: any[]): void;
4
+ warn(...args: any[]): void;
5
+ info(...args: any[]): void;
6
+ error(...args: any[]): void;
7
+ setEnabled(enabled: boolean): void;
8
+ }
9
+ declare const helpers: QueueLogger;
10
+ export default helpers;
@@ -0,0 +1,35 @@
1
+ import { S4Logger } from "../logger/s4-logger.js";
2
+ const logger = new S4Logger();
3
+ let enabled = false;
4
+ const helpers = {
5
+ debug(...args) {
6
+ if (enabled) {
7
+ logger.debug(":: AMQP ::", ...args);
8
+ }
9
+ },
10
+ log(...args) {
11
+ if (enabled) {
12
+ logger.debug(":: AMQP ::", ...args);
13
+ }
14
+ },
15
+ warn(...args) {
16
+ if (enabled) {
17
+ logger.warn(":: AMQP ::", ...args);
18
+ }
19
+ },
20
+ info(...args) {
21
+ if (enabled) {
22
+ logger.info(":: AMQP ::", ...args);
23
+ }
24
+ },
25
+ error(...args) {
26
+ if (enabled) {
27
+ logger.error(":: AMQP ::", ...args);
28
+ }
29
+ },
30
+ setEnabled(e) {
31
+ enabled = e;
32
+ }
33
+ };
34
+ export default helpers;
35
+ //# sourceMappingURL=queue-logger.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"queue-logger.js","sourceRoot":"","sources":["../../../ts/queue/queue-logger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,wBAAwB,CAAC;AAElD,MAAM,MAAM,GAAG,IAAI,QAAQ,EAAE,CAAC;AAC9B,IAAI,OAAO,GAAG,KAAK,CAAC;AAWpB,MAAM,OAAO,GAAgB;IAC3B,KAAK,CAAC,GAAG,IAAW;QAClB,IAAI,OAAO,EAAE,CAAC;YACZ,MAAM,CAAC,KAAK,CAAC,YAAY,EAAE,GAAG,IAAI,CAAC,CAAC;QACtC,CAAC;IACH,CAAC;IAED,GAAG,CAAC,GAAG,IAAW;QAChB,IAAI,OAAO,EAAE,CAAC;YACZ,MAAM,CAAC,KAAK,CAAC,YAAY,EAAE,GAAG,IAAI,CAAC,CAAC;QACtC,CAAC;IACH,CAAC;IAED,IAAI,CAAC,GAAG,IAAW;QACjB,IAAI,OAAO,EAAE,CAAC;YACZ,MAAM,CAAC,IAAI,CAAC,YAAY,EAAE,GAAG,IAAI,CAAC,CAAC;QACrC,CAAC;IACH,CAAC;IAED,IAAI,CAAC,GAAG,IAAW;QACjB,IAAI,OAAO,EAAE,CAAC;YACZ,MAAM,CAAC,IAAI,CAAC,YAAY,EAAE,GAAG,IAAI,CAAC,CAAC;QACrC,CAAC;IACH,CAAC;IAED,KAAK,CAAC,GAAG,IAAW;QAClB,IAAI,OAAO,EAAE,CAAC;YACZ,MAAM,CAAC,KAAK,CAAC,YAAY,EAAE,GAAG,IAAI,CAAC,CAAC;QACtC,CAAC;IACH,CAAC;IAED,UAAU,CAAC,CAAU;QACnB,OAAO,GAAG,CAAC,CAAC;IACd,CAAC;CACF,CAAC;AAEF,eAAe,OAAO,CAAC"}
@@ -0,0 +1,55 @@
1
+ interface Exchange {
2
+ name: string;
3
+ type: string;
4
+ options?: any;
5
+ }
6
+ interface Queue {
7
+ name: string;
8
+ options?: any;
9
+ }
10
+ interface MessagePayload {
11
+ metadata: any;
12
+ error?: any;
13
+ original_content?: string;
14
+ [key: string]: any;
15
+ }
16
+ type MessageCallback = (payload: MessagePayload, error?: any) => void;
17
+ /**
18
+ * A subscriber for AMQP service
19
+ */
20
+ export declare class Subscriber {
21
+ private exchange;
22
+ private queue;
23
+ private _key;
24
+ private name;
25
+ private channel;
26
+ private callbacks;
27
+ get key(): string;
28
+ /**
29
+ * Builds a subscriber
30
+ * @param exchange name of the AMQP exchange
31
+ * @param queue name of the AMQP service
32
+ * @param key routing key. This will be used to bind the queue to the exchange
33
+ * @param name a useful name to be printed in the logs
34
+ */
35
+ constructor(exchange: Exchange, queue: Queue, key: string, name: string);
36
+ /**
37
+ * Initializes the subscriber on the given connection.
38
+ * During this call, the exchange and the queues are asserted
39
+ * and bound via the key provided in the constructor.
40
+ * @param conn amqp connection object
41
+ */
42
+ init(conn: any): Promise<void>;
43
+ /**
44
+ * Closes the channel
45
+ * @returns promise
46
+ */
47
+ closeChannel(): Promise<void>;
48
+ /**
49
+ * Register a new callback to receive message through this
50
+ * subscriber object
51
+ * @param cb A callback to be called when a message arrives
52
+ */
53
+ registerCallback(cb: MessageCallback): void;
54
+ }
55
+ export {};