@sera4/essentia 3.0.13-rc.1 → 3.0.13

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 (154) hide show
  1. package/package.json +1 -1
  2. package/package.tar.gz +0 -0
  3. package/dist/cache/index.d.ts +0 -22
  4. package/dist/cache/index.js +0 -166
  5. package/dist/cache/index.js.map +0 -1
  6. package/dist/constants/index.d.ts +0 -6
  7. package/dist/constants/index.js +0 -9
  8. package/dist/constants/index.js.map +0 -1
  9. package/dist/formatter/index.d.ts +0 -37
  10. package/dist/formatter/index.js +0 -75
  11. package/dist/formatter/index.js.map +0 -1
  12. package/dist/hal/index.d.ts +0 -5
  13. package/dist/hal/index.js +0 -69
  14. package/dist/hal/index.js.map +0 -1
  15. package/dist/health/health-logger.d.ts +0 -9
  16. package/dist/health/health-logger.js +0 -34
  17. package/dist/health/health-logger.js.map +0 -1
  18. package/dist/health/index.d.ts +0 -19
  19. package/dist/health/index.js +0 -175
  20. package/dist/health/index.js.map +0 -1
  21. package/dist/helpers/index.d.ts +0 -1
  22. package/dist/helpers/index.js +0 -2
  23. package/dist/helpers/index.js.map +0 -1
  24. package/dist/helpers/test-server-wrapper.d.ts +0 -120
  25. package/dist/helpers/test-server-wrapper.js +0 -165
  26. package/dist/helpers/test-server-wrapper.js.map +0 -1
  27. package/dist/index.d.ts +0 -1
  28. package/dist/index.js +0 -17
  29. package/dist/index.js.map +0 -1
  30. package/dist/last-commit.d.ts +0 -1
  31. package/dist/last-commit.js.map +0 -1
  32. package/dist/last_commit/index.d.ts +0 -7
  33. package/dist/last_commit/index.js +0 -83
  34. package/dist/last_commit/index.js.map +0 -1
  35. package/dist/paginator/s4-pagination.d.ts +0 -9
  36. package/dist/paginator/s4-pagination.js +0 -49
  37. package/dist/paginator/s4-pagination.js.map +0 -1
  38. package/dist/paginator/sql-pagination.d.ts +0 -27
  39. package/dist/paginator/sql-pagination.js +0 -166
  40. package/dist/paginator/sql-pagination.js.map +0 -1
  41. package/dist/paper-trail/helpers.d.ts +0 -11
  42. package/dist/paper-trail/helpers.js +0 -74
  43. package/dist/paper-trail/helpers.js.map +0 -1
  44. package/dist/paper-trail/index.d.ts +0 -13
  45. package/dist/paper-trail/index.js +0 -662
  46. package/dist/paper-trail/index.js.map +0 -1
  47. package/dist/prompts/index.d.ts +0 -14
  48. package/dist/prompts/index.js +0 -63
  49. package/dist/prompts/index.js.map +0 -1
  50. package/dist/queue/index.d.ts +0 -166
  51. package/dist/queue/index.js +0 -371
  52. package/dist/queue/index.js.map +0 -1
  53. package/dist/queue/publisher.d.ts +0 -31
  54. package/dist/queue/publisher.js +0 -84
  55. package/dist/queue/publisher.js.map +0 -1
  56. package/dist/queue/queue-logger.d.ts +0 -9
  57. package/dist/queue/queue-logger.js +0 -35
  58. package/dist/queue/queue-logger.js.map +0 -1
  59. package/dist/queue/subscriber.d.ts +0 -39
  60. package/dist/queue/subscriber.js +0 -97
  61. package/dist/queue/subscriber.js.map +0 -1
  62. package/dist/safe_proxy/index.d.ts +0 -13
  63. package/dist/safe_proxy/index.js +0 -54
  64. package/dist/safe_proxy/index.js.map +0 -1
  65. package/dist/serializer/index.d.ts +0 -5
  66. package/dist/serializer/index.js +0 -83
  67. package/dist/serializer/index.js.map +0 -1
  68. package/dist/ts/cache/index.d.ts +0 -33
  69. package/dist/ts/cache/index.js +0 -171
  70. package/dist/ts/cache/index.js.map +0 -1
  71. package/dist/ts/configurations/server-config.d.ts +0 -4
  72. package/dist/ts/configurations/server-config.js +0 -2
  73. package/dist/ts/configurations/server-config.js.map +0 -1
  74. package/dist/ts/constants/index.d.ts +0 -7
  75. package/dist/ts/constants/index.js +0 -8
  76. package/dist/ts/constants/index.js.map +0 -1
  77. package/dist/ts/cycle/index.d.ts +0 -53
  78. package/dist/ts/cycle/index.js +0 -169
  79. package/dist/ts/cycle/index.js.map +0 -1
  80. package/dist/ts/dns/DnsCache.d.ts +0 -50
  81. package/dist/ts/dns/DnsCache.js +0 -137
  82. package/dist/ts/dns/DnsCache.js.map +0 -1
  83. package/dist/ts/dns/index.d.ts +0 -4
  84. package/dist/ts/dns/index.js +0 -4
  85. package/dist/ts/dns/index.js.map +0 -1
  86. package/dist/ts/formatter/index.d.ts +0 -44
  87. package/dist/ts/formatter/index.js +0 -76
  88. package/dist/ts/formatter/index.js.map +0 -1
  89. package/dist/ts/hal/index.d.ts +0 -26
  90. package/dist/ts/hal/index.js +0 -70
  91. package/dist/ts/hal/index.js.map +0 -1
  92. package/dist/ts/health/index.d.ts +0 -53
  93. package/dist/ts/health/index.js +0 -165
  94. package/dist/ts/health/index.js.map +0 -1
  95. package/dist/ts/helpers/index.d.ts +0 -1
  96. package/dist/ts/helpers/index.js +0 -2
  97. package/dist/ts/helpers/index.js.map +0 -1
  98. package/dist/ts/helpers/test-server-wrapper.d.ts +0 -56
  99. package/dist/ts/helpers/test-server-wrapper.js +0 -144
  100. package/dist/ts/helpers/test-server-wrapper.js.map +0 -1
  101. package/dist/ts/index.d.ts +0 -22
  102. package/dist/ts/index.js +0 -27
  103. package/dist/ts/index.js.map +0 -1
  104. package/dist/ts/last-commit/index.d.ts +0 -12
  105. package/dist/ts/last-commit/index.js +0 -82
  106. package/dist/ts/last-commit/index.js.map +0 -1
  107. package/dist/ts/logger/index.d.ts +0 -3
  108. package/dist/ts/logger/index.js +0 -4
  109. package/dist/ts/logger/index.js.map +0 -1
  110. package/dist/ts/logger/s4-logger.d.ts +0 -23
  111. package/dist/ts/logger/s4-logger.js +0 -105
  112. package/dist/ts/logger/s4-logger.js.map +0 -1
  113. package/dist/ts/paginator/index.d.ts +0 -2
  114. package/dist/ts/paginator/index.js +0 -3
  115. package/dist/ts/paginator/index.js.map +0 -1
  116. package/dist/ts/paginator/s4-pagination.d.ts +0 -26
  117. package/dist/ts/paginator/s4-pagination.js +0 -49
  118. package/dist/ts/paginator/s4-pagination.js.map +0 -1
  119. package/dist/ts/paginator/sql-pagination.d.ts +0 -52
  120. package/dist/ts/paginator/sql-pagination.js +0 -157
  121. package/dist/ts/paginator/sql-pagination.js.map +0 -1
  122. package/dist/ts/paper-trail/helpers.d.ts +0 -14
  123. package/dist/ts/paper-trail/helpers.js +0 -74
  124. package/dist/ts/paper-trail/helpers.js.map +0 -1
  125. package/dist/ts/paper-trail/index.d.ts +0 -54
  126. package/dist/ts/paper-trail/index.js +0 -236
  127. package/dist/ts/paper-trail/index.js.map +0 -1
  128. package/dist/ts/prompts/index.d.ts +0 -21
  129. package/dist/ts/prompts/index.js +0 -68
  130. package/dist/ts/prompts/index.js.map +0 -1
  131. package/dist/ts/queue/index.d.ts +0 -232
  132. package/dist/ts/queue/index.js +0 -440
  133. package/dist/ts/queue/index.js.map +0 -1
  134. package/dist/ts/queue/publisher.d.ts +0 -41
  135. package/dist/ts/queue/publisher.js +0 -84
  136. package/dist/ts/queue/publisher.js.map +0 -1
  137. package/dist/ts/queue/queue-logger.d.ts +0 -10
  138. package/dist/ts/queue/queue-logger.js +0 -35
  139. package/dist/ts/queue/queue-logger.js.map +0 -1
  140. package/dist/ts/queue/subscriber.d.ts +0 -55
  141. package/dist/ts/queue/subscriber.js +0 -98
  142. package/dist/ts/queue/subscriber.js.map +0 -1
  143. package/dist/ts/safe-proxy/index.d.ts +0 -35
  144. package/dist/ts/safe-proxy/index.js +0 -54
  145. package/dist/ts/safe-proxy/index.js.map +0 -1
  146. package/dist/ts/serializer/index.d.ts +0 -52
  147. package/dist/ts/serializer/index.js +0 -83
  148. package/dist/ts/serializer/index.js.map +0 -1
  149. package/dist/ts/task-scheduler/index.d.ts +0 -130
  150. package/dist/ts/task-scheduler/index.js +0 -259
  151. package/dist/ts/task-scheduler/index.js.map +0 -1
  152. package/dist/ts/utils/index.d.ts +0 -49
  153. package/dist/ts/utils/index.js +0 -157
  154. package/dist/ts/utils/index.js.map +0 -1
@@ -1,232 +0,0 @@
1
- interface ConnectionOptions {
2
- connectionUrl: string;
3
- logEnabled?: boolean;
4
- retryInterval?: number;
5
- }
6
- interface Exchange {
7
- name: string;
8
- type: string;
9
- options?: any;
10
- }
11
- interface PublisherOptions {
12
- exchange: Exchange;
13
- }
14
- interface SubscriberOptions {
15
- exchange: Exchange;
16
- queue: {
17
- name: string;
18
- options?: any;
19
- };
20
- key: string;
21
- }
22
- interface UnregisterSubscriberOptions {
23
- exchange: Exchange;
24
- queue: {
25
- name: string;
26
- };
27
- key: string;
28
- }
29
- interface FanoutQueueConfig {
30
- name: string;
31
- routingKey?: string;
32
- options?: any;
33
- }
34
- interface FanoutConfiguration {
35
- exchange: {
36
- name: string;
37
- type: "fanout";
38
- options?: any;
39
- };
40
- queues: FanoutQueueConfig[];
41
- }
42
- type MessageListener = (payload: any, error?: any) => void;
43
- /**
44
- * An interface to AMQP service
45
- */
46
- declare class S4Queue {
47
- private connection;
48
- private connectionTestChannel;
49
- private retry;
50
- private pubs;
51
- private subs;
52
- private fanouts;
53
- constructor();
54
- /**
55
- * Tests the connection to the service.
56
- * Internally, the same connection is used for test
57
- * and production code, but a separate channel is opened.
58
- * Once the channel is obtained, there is an assert on the
59
- * existence of a topic exchange called '--health-check--'.
60
- * The following must be satisfied for the test to pass
61
- * 1. must be connected
62
- * 2. must have a test channel
63
- * 3. must assert the exchange
64
- */
65
- testConnection(): Promise<void>;
66
- /**
67
- * Opens a connection to AMQP server.
68
- * @param options Connection parameters. At the very least options.connectionUrl must be present.
69
- */
70
- openConnection(options: ConnectionOptions): Promise<void>;
71
- /**
72
- * Closes the connection to AMQP server
73
- * and clears the registered publishers
74
- * and subscribers
75
- */
76
- closeConnection(): Promise<void>;
77
- /**
78
- * Returns whether we have an active connection
79
- * or not.
80
- */
81
- isConnected(): boolean;
82
- /**
83
- * @deprecated Use registerPublisher
84
- * Registers a new direct publisher on the given exchange.
85
- * If exchange doesn't exist, one will be created.
86
- * @param exchange the name of the exchange on AMQP service.
87
- */
88
- registerDirectPublisher(exchange: string): Promise<void>;
89
- /**
90
- * @deprecated Use registerPublisher
91
- * Registers a new topic publisher on the given exchange.
92
- * If exchange doesn't exist, one will be created.
93
- * @param exchange the name of the exchange on AMQP service.
94
- */
95
- registerTopicPublisher(exchange: string): Promise<void>;
96
- /**
97
- * @deprecated Use registerPublisher
98
- * Registers a new fanout publisher on the given exchange.
99
- * If exchange doesn't exist, one will be created.
100
- * @param exchange the name of the exchange on AMQP service.
101
- */
102
- registerFanoutPublisher(exchange: string): Promise<void>;
103
- /**
104
- * Registers a new publisher
105
- * @param options connection options.
106
- */
107
- registerPublisher(options: PublisherOptions): Promise<void>;
108
- /**
109
- * Checks whether or not a publisher with given parameters
110
- * has been registered already
111
- * @param exchange the name of the exchange.
112
- * @returns true if registered already, false otherwise
113
- */
114
- hasPublisher(exchange: string): boolean;
115
- /**
116
- * Checks whether or not a fanout with given exchange name
117
- * has been registered already
118
- * @param exchange the name of the exchange.
119
- * @returns true if registered already, false otherwise
120
- */
121
- hasFanout(exchange: string): boolean;
122
- /**
123
- * Removes a previously registered publisher
124
- * @param options
125
- */
126
- unregisterPublisher(options: PublisherOptions): Promise<void>;
127
- /**
128
- * @deprecated Use registerSubscriber
129
- * Registers a new direct subscriber on a given exchange and queue.
130
- * If exchange and queue don't exists, they'll be created
131
- * and bound through the provided routing key.
132
- * @param exchange the name of the exchange.
133
- * @param queue the name of the queue.
134
- * @param key the name of the routing key.
135
- */
136
- registerDirectSubscriber(exchange: string, queue: string, key: string): Promise<void>;
137
- /**
138
- * @deprecated Use registerSubscriber
139
- * Registers a new topic subscriber on a given exchange and queue.
140
- * If exchange and queue don't exists, they'll be created
141
- * and bound through the provided routing key.
142
- * @param exchange the name of the exchange.
143
- * @param queue the name of the queue.
144
- * @param key the name of the routing key.
145
- */
146
- registerTopicSubscriber(exchange: string, queue: string, key: string): Promise<void>;
147
- /**
148
- * An overloaded version of registering a subscriber
149
- * allowing for more fine tuned parameters
150
- * @param options subscribing options
151
- */
152
- registerSubscriber(options: SubscriberOptions): Promise<void>;
153
- /**
154
- * Checks whether or not a subscriber with given parameters
155
- * has been registered already
156
- * @param exchange the name of the exchange.
157
- * @param queue the name of the queue.
158
- * @param key the name of the routing key.
159
- * @returns true if registered already, false otherwise
160
- */
161
- hasSubscriber(exchange: string, queue: string, key: string): boolean;
162
- /**
163
- * Removes a previously registered subscriber
164
- * @param options
165
- */
166
- unregisterSubscriber(options: UnregisterSubscriberOptions): Promise<void>;
167
- /**
168
- * Publishes a message on a given exchange with routing key.
169
- * @param exchange the name of the exchange.
170
- * @param key the name of the routing key.
171
- * @param msg a JSON object as the message.
172
- */
173
- publishDirectMessage(exchange: string, key: string, msg: any): Promise<string>;
174
- /**
175
- * Publishes a message on a given exchange with a topic.
176
- * @param exchange the name of the exchange.
177
- * @param topic the name of the topic.
178
- * @param msg a JSON object as the message.
179
- */
180
- publishTopicMessage(exchange: string, topic: string, msg: any): Promise<string>;
181
- /**
182
- * Publish a message to the exchange
183
- * @param exchange name of exchange
184
- * @param key name of the key or topic
185
- * @param msg payload to be published
186
- */
187
- publishMessage(exchange: string, key: string, msg: any): Promise<string>;
188
- /**
189
- * Binds an exchange to a queue
190
- * @param exchange the name of the exchange.
191
- * @param queue the name of the queue.
192
- * @param pattern (optional) pattern for routing key
193
- */
194
- bindQueueToExchange(exchange: string, queue: string, pattern?: string): Promise<void>;
195
- /**
196
- * Adds a listener for a specific key.
197
- * If a subscriber that was registered was found
198
- * with that key, the listener is added, otherwise
199
- * it's ignored
200
- * @param key AMQP routing key
201
- * @param listener callback
202
- */
203
- addMessageListener(key: string, listener: MessageListener): void;
204
- /**
205
- * Registers a fanout exchange with multiple bound queues.
206
- * Creates the fanout exchange if it doesn't exist, then creates and binds
207
- * all specified queues to the exchange.
208
- * @param config Fanout configuration with exchange and queue definitions
209
- */
210
- registerFanout(config: FanoutConfiguration): Promise<void>;
211
- /**
212
- * Unregisters a fanout configuration by removing all associated subscribers
213
- * and optionally the publisher/exchange.
214
- * @param config Fanout configuration to unregister
215
- * @param removeExchange Whether to also remove the exchange (default: false)
216
- */
217
- unregisterFanout(config: FanoutConfiguration, removeExchange?: boolean): Promise<void>;
218
- /**
219
- * Updates the connection reference to all the previously
220
- * registered publishers, subscribers, and fanouts.
221
- * After we successfully establish a connection and register pubs/subs/fanouts,
222
- * it's possible for the connection to break. This object will try
223
- * to re-establish a new connection every 3 seconds (or whatever configuration is set)
224
- * and if a new connection is established, we need to pass that connection to
225
- * all our pubs/subs, otherwise they'll be operating with old/stale references
226
- * @param connRef a new connection reference to the AMQP service
227
- */
228
- updatePubsAndSubsConnectionRef(connRef: any): void;
229
- }
230
- declare const queue: S4Queue;
231
- export default queue;
232
- export type { FanoutConfiguration, FanoutQueueConfig, Exchange, PublisherOptions, SubscriberOptions };
@@ -1,440 +0,0 @@
1
- import amqp from "amqplib";
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
- this.fanouts = {};
17
- }
18
- /**
19
- * Tests the connection to the service.
20
- * Internally, the same connection is used for test
21
- * and production code, but a separate channel is opened.
22
- * Once the channel is obtained, there is an assert on the
23
- * existence of a topic exchange called '--health-check--'.
24
- * The following must be satisfied for the test to pass
25
- * 1. must be connected
26
- * 2. must have a test channel
27
- * 3. must assert the exchange
28
- */
29
- async testConnection() {
30
- if (!this.isConnected()) {
31
- this.connectionTestChannel = null;
32
- throw new Error("queue service not connected");
33
- }
34
- if (!this.connectionTestChannel) {
35
- const ch = await this.connection.createChannel();
36
- if (!ch) {
37
- throw new Error("queue service failed to obtain a channel");
38
- }
39
- ch.on("error", () => { this.connectionTestChannel = null; });
40
- ch.on("close", () => { this.connectionTestChannel = null; });
41
- this.connectionTestChannel = ch;
42
- }
43
- await this.connectionTestChannel.assertExchange("--health-check--", "topic", { durable: false });
44
- }
45
- /**
46
- * Opens a connection to AMQP server.
47
- * @param options Connection parameters. At the very least options.connectionUrl must be present.
48
- */
49
- async openConnection(options) {
50
- logger.setEnabled(options.logEnabled || false);
51
- if (this.connection) {
52
- logger.warn("Already initialized");
53
- return;
54
- }
55
- const retry = parseInt(options.retryInterval?.toString() || "10");
56
- this.retry = retry || 10;
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
- return new Promise((resolve) => {
64
- const attempt = async () => {
65
- logger.debug("Attempting connection...");
66
- try {
67
- const conn = await amqp.connect(options.connectionUrl, socketOptions);
68
- this.connection = conn;
69
- this.updatePubsAndSubsConnectionRef(conn);
70
- this.connection.on("error", (err) => {
71
- logger.error("connection error", err);
72
- // Do not attempt to reconnect as the
73
- // 'close' event will also be emitted
74
- // if this error closes the connection
75
- });
76
- this.connection.on("close", (err) => {
77
- logger.error("connection closed");
78
- this.connection = null;
79
- this.connectionTestChannel = null;
80
- if (err) {
81
- logger.error("connection closed due to error:", err);
82
- logger.error("Reconnecting in", this.retry, "seconds");
83
- setTimeout(attempt, this.retry * 1000);
84
- }
85
- else {
86
- logger.debug("Will not re-attempt");
87
- }
88
- });
89
- logger.debug("Connected");
90
- resolve();
91
- }
92
- catch (err) {
93
- logger.error("Error while connecting", err);
94
- logger.error("Attempting again in", this.retry, "seconds");
95
- setTimeout(attempt, this.retry * 1000);
96
- }
97
- };
98
- attempt();
99
- });
100
- }
101
- /**
102
- * Closes the connection to AMQP server
103
- * and clears the registered publishers
104
- * and subscribers
105
- */
106
- async closeConnection() {
107
- if (!this.connection)
108
- return;
109
- await this.connection.close();
110
- this.subs = {};
111
- this.pubs = {};
112
- this.connectionTestChannel = null;
113
- this.connection = null;
114
- }
115
- /**
116
- * Returns whether we have an active connection
117
- * or not.
118
- */
119
- isConnected() {
120
- return this.connection != null && typeof (this.connection) != 'undefined';
121
- }
122
- /**
123
- * @deprecated Use registerPublisher
124
- * Registers a new direct publisher on the given exchange.
125
- * If exchange doesn't exist, one will be created.
126
- * @param exchange the name of the exchange on AMQP service.
127
- */
128
- async registerDirectPublisher(exchange) {
129
- return this.registerPublisher({ exchange: { name: exchange, type: "direct" } });
130
- }
131
- /**
132
- * @deprecated Use registerPublisher
133
- * Registers a new topic publisher on the given exchange.
134
- * If exchange doesn't exist, one will be created.
135
- * @param exchange the name of the exchange on AMQP service.
136
- */
137
- async registerTopicPublisher(exchange) {
138
- return this.registerPublisher({ exchange: { name: exchange, type: "topic" } });
139
- }
140
- /**
141
- * @deprecated Use registerPublisher
142
- * Registers a new fanout publisher on the given exchange.
143
- * If exchange doesn't exist, one will be created.
144
- * @param exchange the name of the exchange on AMQP service.
145
- */
146
- async registerFanoutPublisher(exchange) {
147
- return this.registerPublisher({ exchange: { name: exchange, type: "fanout" } });
148
- }
149
- /**
150
- * Registers a new publisher
151
- * @param options connection options.
152
- */
153
- async registerPublisher(options) {
154
- const { exchange } = options;
155
- const { name } = exchange;
156
- if (Object.keys(this.pubs).includes(name)) {
157
- throw new Error(`Publisher ${name} already registered`);
158
- }
159
- const p = new Publisher(exchange, name);
160
- await p.init(this.connection);
161
- this.pubs[name] = p;
162
- }
163
- /**
164
- * Checks whether or not a publisher with given parameters
165
- * has been registered already
166
- * @param exchange the name of the exchange.
167
- * @returns true if registered already, false otherwise
168
- */
169
- hasPublisher(exchange) {
170
- return Object.keys(this.pubs).includes(exchange);
171
- }
172
- /**
173
- * Checks whether or not a fanout with given exchange name
174
- * has been registered already
175
- * @param exchange the name of the exchange.
176
- * @returns true if registered already, false otherwise
177
- */
178
- hasFanout(exchange) {
179
- return Object.keys(this.fanouts).includes(exchange);
180
- }
181
- /**
182
- * Removes a previously registered publisher
183
- * @param options
184
- */
185
- async unregisterPublisher(options) {
186
- const { exchange } = options;
187
- const { name } = exchange;
188
- if (!Object.keys(this.pubs).includes(name)) {
189
- throw new Error(`Publisher ${name} not registered`);
190
- }
191
- const p = this.pubs[name];
192
- await p.closeChannel();
193
- delete this.pubs[name];
194
- logger.debug("Unregistered publisher", name);
195
- }
196
- /**
197
- * @deprecated Use registerSubscriber
198
- * Registers a new direct subscriber on a given exchange and queue.
199
- * If exchange and queue don't exists, they'll be created
200
- * and bound through the provided routing key.
201
- * @param exchange the name of the exchange.
202
- * @param queue the name of the queue.
203
- * @param key the name of the routing key.
204
- */
205
- async registerDirectSubscriber(exchange, queue, key) {
206
- const options = {
207
- key,
208
- exchange: { name: exchange, type: "direct" },
209
- queue: { name: queue }
210
- };
211
- return this.registerSubscriber(options);
212
- }
213
- /**
214
- * @deprecated Use registerSubscriber
215
- * Registers a new topic subscriber on a given exchange and queue.
216
- * If exchange and queue don't exists, they'll be created
217
- * and bound through the provided routing key.
218
- * @param exchange the name of the exchange.
219
- * @param queue the name of the queue.
220
- * @param key the name of the routing key.
221
- */
222
- async registerTopicSubscriber(exchange, queue, key) {
223
- const options = {
224
- key,
225
- exchange: { name: exchange, type: "topic" },
226
- queue: { name: queue }
227
- };
228
- return this.registerSubscriber(options);
229
- }
230
- /**
231
- * An overloaded version of registering a subscriber
232
- * allowing for more fine tuned parameters
233
- * @param options subscribing options
234
- */
235
- async registerSubscriber(options) {
236
- const { exchange, queue, key } = options;
237
- const name = `${exchange.name}-${queue.name}-${key}`;
238
- if (Object.keys(this.subs).includes(name)) {
239
- throw new Error(`Subscriber ${name} already registered`);
240
- }
241
- const s = new Subscriber(exchange, queue, key, name);
242
- await s.init(this.connection);
243
- this.subs[name] = s;
244
- }
245
- /**
246
- * Checks whether or not a subscriber with given parameters
247
- * has been registered already
248
- * @param exchange the name of the exchange.
249
- * @param queue the name of the queue.
250
- * @param key the name of the routing key.
251
- * @returns true if registered already, false otherwise
252
- */
253
- hasSubscriber(exchange, queue, key) {
254
- const name = `${exchange}-${queue}-${key}`;
255
- return Object.keys(this.subs).includes(name);
256
- }
257
- /**
258
- * Removes a previously registered subscriber
259
- * @param options
260
- */
261
- async unregisterSubscriber(options) {
262
- const { exchange, queue, key } = options;
263
- const name = `${exchange.name}-${queue.name}-${key}`;
264
- if (!Object.keys(this.subs).includes(name)) {
265
- throw new Error(`Subscriber ${name} not registered`);
266
- }
267
- const s = this.subs[name];
268
- await s.closeChannel();
269
- delete this.subs[name];
270
- logger.debug("Unregistered subscriber", name);
271
- }
272
- /**
273
- * Publishes a message on a given exchange with routing key.
274
- * @param exchange the name of the exchange.
275
- * @param key the name of the routing key.
276
- * @param msg a JSON object as the message.
277
- */
278
- async publishDirectMessage(exchange, key, msg) {
279
- return this.publishMessage(exchange, key, msg);
280
- }
281
- /**
282
- * Publishes a message on a given exchange with a topic.
283
- * @param exchange the name of the exchange.
284
- * @param topic the name of the topic.
285
- * @param msg a JSON object as the message.
286
- */
287
- async publishTopicMessage(exchange, topic, msg) {
288
- return this.publishMessage(exchange, topic, msg);
289
- }
290
- /**
291
- * Publish a message to the exchange
292
- * @param exchange name of exchange
293
- * @param key name of the key or topic
294
- * @param msg payload to be published
295
- */
296
- async publishMessage(exchange, key, msg) {
297
- if (!key) {
298
- throw new Error(`Key argument not defined`);
299
- }
300
- const p = this.pubs[exchange];
301
- if (!p) {
302
- throw new Error(`No PUB for ${exchange} found`);
303
- }
304
- let content = msg;
305
- if (content.constructor !== ({}).constructor) {
306
- // if not in JSON format, let's make it so
307
- content = {
308
- msg: msg
309
- };
310
- }
311
- return await p.publish({ key, content });
312
- }
313
- /**
314
- * Binds an exchange to a queue
315
- * @param exchange the name of the exchange.
316
- * @param queue the name of the queue.
317
- * @param pattern (optional) pattern for routing key
318
- */
319
- async bindQueueToExchange(exchange, queue, pattern) {
320
- const p = this.pubs[exchange];
321
- if (!p) {
322
- throw new Error(`No PUB for ${exchange} found`);
323
- }
324
- if (!queue) {
325
- throw new Error(`Binding a queue requires a queue name`);
326
- }
327
- return await p.bindQueue(queue, pattern || "");
328
- }
329
- /**
330
- * Adds a listener for a specific key.
331
- * If a subscriber that was registered was found
332
- * with that key, the listener is added, otherwise
333
- * it's ignored
334
- * @param key AMQP routing key
335
- * @param listener callback
336
- */
337
- addMessageListener(key, listener) {
338
- Object.keys(this.subs).forEach(k => {
339
- const s = this.subs[k];
340
- if (s.key === key) {
341
- s.registerCallback(listener);
342
- }
343
- });
344
- }
345
- /**
346
- * Registers a fanout exchange with multiple bound queues.
347
- * Creates the fanout exchange if it doesn't exist, then creates and binds
348
- * all specified queues to the exchange.
349
- * @param config Fanout configuration with exchange and queue definitions
350
- */
351
- async registerFanout(config) {
352
- const { exchange, queues } = config;
353
- logger.debug(`Registering fanout configuration for exchange: ${exchange.name}`);
354
- // First, register the fanout publisher (this creates the exchange)
355
- await this.registerPublisher({ exchange });
356
- // Then register subscribers for each queue
357
- for (const queueConfig of queues) {
358
- const subscriberOptions = {
359
- exchange,
360
- queue: {
361
- name: queueConfig.name,
362
- options: queueConfig.options
363
- },
364
- key: queueConfig.routingKey || "" // Fanout exchanges ignore routing keys, but we need it for the interface
365
- };
366
- await this.registerSubscriber(subscriberOptions);
367
- logger.debug(`Registered fanout subscriber for queue: ${queueConfig.name}${queueConfig.routingKey ? ` with routing key: ${queueConfig.routingKey}` : ' (no routing key - fanout ignores it)'}`);
368
- }
369
- // Store the fanout configuration
370
- this.fanouts[exchange.name] = config;
371
- logger.info(`Successfully registered fanout configuration with ${queues.length} queues`);
372
- }
373
- /**
374
- * Unregisters a fanout configuration by removing all associated subscribers
375
- * and optionally the publisher/exchange.
376
- * @param config Fanout configuration to unregister
377
- * @param removeExchange Whether to also remove the exchange (default: false)
378
- */
379
- async unregisterFanout(config, removeExchange = false) {
380
- const { exchange, queues } = config;
381
- logger.debug(`Unregistering fanout configuration for exchange: ${exchange.name}`);
382
- // Unregister all subscribers
383
- for (const queueConfig of queues) {
384
- const unregisterOptions = {
385
- exchange,
386
- queue: {
387
- name: queueConfig.name
388
- },
389
- key: queueConfig.routingKey || ""
390
- };
391
- try {
392
- await this.unregisterSubscriber(unregisterOptions);
393
- logger.debug(`Unregistered fanout subscriber for queue: ${queueConfig.name}${queueConfig.routingKey ? ` with routing key: ${queueConfig.routingKey}` : ' (no routing key)'}`);
394
- }
395
- catch (error) {
396
- logger.warn(`Failed to unregister subscriber for queue ${queueConfig.name}:`, error);
397
- }
398
- }
399
- // Optionally remove the exchange/publisher
400
- if (removeExchange) {
401
- try {
402
- await this.unregisterPublisher({ exchange });
403
- logger.debug(`Unregistered fanout publisher for exchange: ${exchange.name}`);
404
- }
405
- catch (error) {
406
- logger.warn(`Failed to unregister publisher for exchange ${exchange.name}:`, error);
407
- }
408
- }
409
- // Remove the fanout configuration from storage
410
- delete this.fanouts[exchange.name];
411
- logger.info(`Successfully unregistered fanout configuration`);
412
- }
413
- /**
414
- * Updates the connection reference to all the previously
415
- * registered publishers, subscribers, and fanouts.
416
- * After we successfully establish a connection and register pubs/subs/fanouts,
417
- * it's possible for the connection to break. This object will try
418
- * to re-establish a new connection every 3 seconds (or whatever configuration is set)
419
- * and if a new connection is established, we need to pass that connection to
420
- * all our pubs/subs, otherwise they'll be operating with old/stale references
421
- * @param connRef a new connection reference to the AMQP service
422
- */
423
- updatePubsAndSubsConnectionRef(connRef) {
424
- Object.keys(this.pubs).forEach(k => {
425
- logger.debug(`Updating publisher ${k} with a new connection object`);
426
- const p = this.pubs[k];
427
- p.init(connRef);
428
- });
429
- Object.keys(this.subs).forEach(k => {
430
- logger.debug(`Updating subscriber ${k} with a new connection object`);
431
- const s = this.subs[k];
432
- s.init(connRef);
433
- });
434
- // Note: Fanouts don't need connection updates as they use the same pubs/subs
435
- logger.debug(`Updated connection for ${Object.keys(this.pubs).length} publishers and ${Object.keys(this.subs).length} subscribers`);
436
- }
437
- }
438
- const queue = new S4Queue();
439
- export default queue;
440
- //# sourceMappingURL=index.js.map