@droidsolutions-oss/amqp-ts 2.5.0 → 2.6.1

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 (60) hide show
  1. package/README.md +23 -16
  2. package/lib/AmqpProperties.d.ts +81 -0
  3. package/lib/AmqpProperties.js +3 -0
  4. package/lib/AmqpProperties.js.map +1 -0
  5. package/lib/Binding.d.ts +18 -0
  6. package/lib/Binding.js +112 -0
  7. package/lib/Binding.js.map +1 -0
  8. package/lib/Connection/Connection.d.ts +109 -0
  9. package/lib/Connection/Connection.js +346 -0
  10. package/lib/Connection/Connection.js.map +1 -0
  11. package/lib/Connection/ConnectionMetrics.d.ts +7 -0
  12. package/lib/Connection/ConnectionMetrics.js +3 -0
  13. package/lib/Connection/ConnectionMetrics.js.map +1 -0
  14. package/lib/Connection/ReconnectStrategy.d.ts +4 -0
  15. package/lib/Connection/ReconnectStrategy.js +3 -0
  16. package/lib/Connection/ReconnectStrategy.js.map +1 -0
  17. package/lib/Connection/Topology.d.ts +19 -0
  18. package/lib/Connection/Topology.js +3 -0
  19. package/lib/Connection/Topology.js.map +1 -0
  20. package/lib/Exchange/DeclarationOptions.d.ts +20 -0
  21. package/lib/Exchange/DeclarationOptions.js +3 -0
  22. package/lib/Exchange/DeclarationOptions.js.map +1 -0
  23. package/lib/Exchange/Exchange.d.ts +94 -0
  24. package/lib/Exchange/Exchange.js +279 -0
  25. package/lib/Exchange/Exchange.js.map +1 -0
  26. package/lib/Exchange/ExchangeType.d.ts +6 -0
  27. package/lib/Exchange/ExchangeType.js +3 -0
  28. package/lib/Exchange/ExchangeType.js.map +1 -0
  29. package/lib/Exchange/InitializeResult.d.ts +3 -0
  30. package/lib/Exchange/InitializeResult.js +3 -0
  31. package/lib/Exchange/InitializeResult.js.map +1 -0
  32. package/lib/LoggerFactory.d.ts +48 -0
  33. package/lib/LoggerFactory.js +13 -0
  34. package/lib/LoggerFactory.js.map +1 -0
  35. package/lib/Message.d.ts +52 -0
  36. package/lib/Message.js +125 -0
  37. package/lib/Message.js.map +1 -0
  38. package/lib/Queue/DeclarationOptions.d.ts +18 -0
  39. package/lib/Queue/DeclarationOptions.js +3 -0
  40. package/lib/Queue/DeclarationOptions.js.map +1 -0
  41. package/lib/Queue/DeleteResult.d.ts +3 -0
  42. package/lib/Queue/DeleteResult.js +3 -0
  43. package/lib/Queue/DeleteResult.js.map +1 -0
  44. package/lib/Queue/InitializeResult.d.ts +5 -0
  45. package/lib/Queue/InitializeResult.js +3 -0
  46. package/lib/Queue/InitializeResult.js.map +1 -0
  47. package/lib/Queue/Queue.d.ts +107 -0
  48. package/lib/Queue/Queue.js +339 -0
  49. package/lib/Queue/Queue.js.map +1 -0
  50. package/lib/Queue/StartConsumerResult.d.ts +5 -0
  51. package/lib/Queue/StartConsumerResult.js +3 -0
  52. package/lib/Queue/StartConsumerResult.js.map +1 -0
  53. package/lib/amqp-ts.d.ts +9 -0
  54. package/lib/amqp-ts.js +21 -0
  55. package/lib/amqp-ts.js.map +1 -0
  56. package/lib/index.d.ts +14 -0
  57. package/lib/index.js +14 -0
  58. package/lib/index.js.map +1 -0
  59. package/package.json +36 -28
  60. package/CHANGELOG.md +0 -159
@@ -0,0 +1,346 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Connection = void 0;
4
+ const AmqpLib = require("amqplib/callback_api");
5
+ const events_1 = require("events");
6
+ const Exchange_1 = require("../Exchange/Exchange");
7
+ const LoggerFactory_1 = require("../LoggerFactory");
8
+ const Queue_1 = require("../Queue/Queue");
9
+ class Connection extends events_1.EventEmitter {
10
+ /**
11
+ *
12
+ * @param url The url to RabbitMQ
13
+ * @param socketOptions Socket options are passed to {@link AmqpLib.connect}.
14
+ * @param reconnectStrategy Options specifying the reconnect strategy.
15
+ * @param loggerFactory A factory function that returns a logger.
16
+ */
17
+ constructor(url = "amqp://localhost", socketOptions = {}, reconnectStrategy = { retries: 0, interval: 1500 }, loggerFactory) {
18
+ super();
19
+ this.loggerFactory = loggerFactory;
20
+ this.isConnected = false;
21
+ this.connectedBefore = false;
22
+ this._rebuilding = false;
23
+ this._isClosing = false;
24
+ this._sentMessagesCounter = 0;
25
+ this._receivedMessagesCounter = 0;
26
+ if (!this.loggerFactory) {
27
+ this.loggerFactory = (_, __) => new LoggerFactory_1.EmtpyLogger();
28
+ }
29
+ this.log = this.loggerFactory(this.constructor, { module: "amqp-ts" });
30
+ this.url = url;
31
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
32
+ this.socketOptions = socketOptions;
33
+ this.reconnectStrategy = reconnectStrategy;
34
+ this._exchanges = {};
35
+ this._queues = {};
36
+ this._bindings = {};
37
+ this.rebuildConnection();
38
+ }
39
+ tryToConnect(thisConnection, retry, callback) {
40
+ AmqpLib.connect(thisConnection.url, thisConnection.socketOptions, (err, connection) => {
41
+ /* istanbul ignore if */
42
+ if (err) {
43
+ thisConnection.isConnected = false;
44
+ // only do every retry once, amqplib can return multiple connection errors for one connection request (error?)
45
+ if (retry <= this._retry) {
46
+ //amqpts_log.log("warn" , "Double retry " + retry + ", skipping.", {module: "amqp-ts"});
47
+ return;
48
+ }
49
+ this.log.warn({ err }, "Connection failed.");
50
+ this._retry = retry;
51
+ if (thisConnection.reconnectStrategy.retries === 0 || thisConnection.reconnectStrategy.retries > retry) {
52
+ this.log.warn("Connection retry %d in %d ms", retry + 1, thisConnection.reconnectStrategy.interval);
53
+ thisConnection.emit("trying_connect");
54
+ setTimeout(
55
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
56
+ thisConnection.tryToConnect.bind(this), thisConnection.reconnectStrategy.interval, thisConnection, retry + 1, callback);
57
+ }
58
+ else {
59
+ //no reconnect strategy, or retries exhausted, so return the error
60
+ this.log.warn("Connection failed, exiting: No connection retries left (retry %d).", retry);
61
+ callback(err);
62
+ }
63
+ }
64
+ else {
65
+ const restart = (err) => {
66
+ this.log.debug({ err }, "Connection error occurred.");
67
+ connection.removeListener("error", restart);
68
+ //connection.removeListener("end", restart); // not sure this is needed
69
+ thisConnection._rebuildAll(err); //try to rebuild the topology when the connection unexpectedly closes
70
+ };
71
+ const onClose = () => {
72
+ connection.removeListener("close", onClose);
73
+ if (!this._isClosing) {
74
+ thisConnection.emit("lost_connection");
75
+ restart(new Error("Connection closed by remote host"));
76
+ }
77
+ };
78
+ connection.on("error", restart);
79
+ connection.on("close", onClose);
80
+ //connection.on("end", restart); // not sure this is needed
81
+ thisConnection.connection = connection;
82
+ thisConnection.isConnected = true;
83
+ callback(null);
84
+ }
85
+ });
86
+ }
87
+ _rebuildAll(err) {
88
+ this.log.warn({ err }, "Connection error: %s", err.message);
89
+ this.log.debug("Rebuilding connection NOW.");
90
+ this.rebuildConnection();
91
+ //re initialize exchanges, queues and bindings if they exist
92
+ for (const exchangeId in this._exchanges) {
93
+ const exchange = this._exchanges[exchangeId];
94
+ this.log.debug("Re-initialize Exchange '%s'.", exchange.name);
95
+ exchange._initialize();
96
+ }
97
+ for (const queueId in this._queues) {
98
+ const queue = this._queues[queueId];
99
+ const consumer = queue._consumer;
100
+ this.log.debug("Re-initialize queue '%s'.", queue.name);
101
+ queue._initialize();
102
+ if (consumer) {
103
+ this.log.debug("Re-initialize consumer for queue '%s'.", queue.name);
104
+ queue._initializeConsumer();
105
+ }
106
+ }
107
+ for (const bindingId in this._bindings) {
108
+ const binding = this._bindings[bindingId];
109
+ this.log.debug("Re-initialize binding from '%s' to '%s'.", binding._source.name, binding._destination.name);
110
+ binding._initialize();
111
+ }
112
+ return new Promise((resolve, reject) => {
113
+ this.completeConfiguration().then(() => {
114
+ this.log.debug("Rebuild success.");
115
+ resolve(null);
116
+ },
117
+ /* istanbul ignore next */ (rejectReason) => {
118
+ this.log.debug("Rebuild failed.");
119
+ reject(rejectReason);
120
+ });
121
+ });
122
+ }
123
+ close() {
124
+ this._isClosing = true;
125
+ return new Promise((resolve, reject) => {
126
+ this.initialized.then(() => {
127
+ this.connection.close((err) => {
128
+ /* istanbul ignore if */
129
+ if (err) {
130
+ reject(err);
131
+ }
132
+ else {
133
+ this.isConnected = false;
134
+ this.emit("close_connection");
135
+ resolve(null);
136
+ }
137
+ });
138
+ });
139
+ });
140
+ }
141
+ /**
142
+ * Make sure the whole defined connection topology is configured. Goes through each exchange, queue and binding and
143
+ * waits until all of them are initialized.
144
+ *
145
+ * @return A promise that fulfills after all defined exchanges, queues and bindings are initialized.
146
+ */
147
+ completeConfiguration() {
148
+ const promises = [];
149
+ for (const exchangeId in this._exchanges) {
150
+ const exchange = this._exchanges[exchangeId];
151
+ promises.push(exchange.initialized);
152
+ }
153
+ for (const queueId in this._queues) {
154
+ const queue = this._queues[queueId];
155
+ promises.push(queue.initialized);
156
+ if (queue._consumerInitialized !== undefined) {
157
+ promises.push(queue._consumerInitialized);
158
+ }
159
+ }
160
+ for (const bindingId in this._bindings) {
161
+ const binding = this._bindings[bindingId];
162
+ promises.push(binding.initialized);
163
+ }
164
+ return Promise.all(promises);
165
+ }
166
+ /**
167
+ * Delete the whole defined connection topology:
168
+ * return promise that fulfills after all defined exchanges, queues and bindings have been removed
169
+ */
170
+ deleteConfiguration() {
171
+ const promises = [];
172
+ for (const bindingId in this._bindings) {
173
+ const binding = this._bindings[bindingId];
174
+ promises.push(binding.delete());
175
+ }
176
+ for (const queueId in this._queues) {
177
+ const queue = this._queues[queueId];
178
+ if (queue._consumerInitialized !== undefined) {
179
+ promises.push(queue.stopConsumer());
180
+ }
181
+ promises.push(queue.delete());
182
+ }
183
+ for (const exchangeId in this._exchanges) {
184
+ const exchange = this._exchanges[exchangeId];
185
+ promises.push(exchange.delete());
186
+ }
187
+ return Promise.all(promises);
188
+ }
189
+ /**
190
+ * Declares a new exchange. Dependent on the options it auto creates the exchange if it does not already exists.
191
+ *
192
+ * @param name The name of the exchange.
193
+ * @param type The type
194
+ * @param options Options that are passed to assertExchange in amqp-lib.
195
+ * @returns The declared queue. You need to await the initialization of the queue.
196
+ */
197
+ declareExchange(name, type, options) {
198
+ let exchange = this._exchanges[name];
199
+ if (exchange === undefined) {
200
+ exchange = new Exchange_1.Exchange(this, name, type, options);
201
+ }
202
+ return exchange;
203
+ }
204
+ /**
205
+ * Declares an exchange and waits until it is initialized.
206
+ *
207
+ * @param name The name of the exchange.
208
+ * @param type The exchange type.
209
+ * @param options Any declaration options.
210
+ * @returns A promise that resolves when the exchange is declared and initialized.
211
+ */
212
+ async declareExchangeAsync(name, type, options) {
213
+ const exchange = this.declareExchange(name, type, options);
214
+ await exchange.initialized;
215
+ return exchange;
216
+ }
217
+ /**
218
+ * Declares a new queue.
219
+ *
220
+ * @param name The name of the queue.
221
+ * @param options Any options for the queue declaration.
222
+ * @returns The queue.
223
+ */
224
+ declareQueue(name, options) {
225
+ let queue = this._queues[name];
226
+ if (queue === undefined) {
227
+ queue = new Queue_1.Queue(this, name, options);
228
+ }
229
+ return queue;
230
+ }
231
+ /**
232
+ * Declares a new queue and waits until it is intitialized.
233
+ *
234
+ * @param name The name of the queue.
235
+ * @param options Any options for the queue declaration.
236
+ * @returns A promise that resolves when the queue is declared and initialized.
237
+ */
238
+ async declareQueueAsync(name, options) {
239
+ const queue = this.declareQueue(name, options);
240
+ await queue.initialized;
241
+ return queue;
242
+ }
243
+ declareTopology(topology) {
244
+ const promises = [];
245
+ let i;
246
+ let len;
247
+ if (topology.exchanges !== undefined) {
248
+ for (i = 0, len = topology.exchanges.length; i < len; i++) {
249
+ const exchange = topology.exchanges[i];
250
+ promises.push(this.declareExchange(exchange.name, exchange.type, exchange.options)
251
+ .initialized);
252
+ }
253
+ }
254
+ if (topology.queues !== undefined) {
255
+ for (i = 0, len = topology.queues.length; i < len; i++) {
256
+ const queue = topology.queues[i];
257
+ promises.push(this.declareQueue(queue.name, queue.options).initialized);
258
+ }
259
+ }
260
+ if (topology.bindings !== undefined) {
261
+ for (i = 0, len = topology.bindings.length; i < len; i++) {
262
+ const binding = topology.bindings[i];
263
+ const source = this.declareExchange(binding.source);
264
+ let destination;
265
+ if (binding.exchange !== undefined) {
266
+ destination = this.declareExchange(binding.exchange);
267
+ }
268
+ else {
269
+ destination = this.declareQueue(binding.queue);
270
+ }
271
+ promises.push(destination.bind(source, binding.pattern, binding.args));
272
+ }
273
+ }
274
+ return Promise.all(promises);
275
+ }
276
+ get getConnection() {
277
+ return this.connection;
278
+ }
279
+ /**
280
+ * Returns an object with metrics about the connection.
281
+ * @returns An object containing metrics about the connection.
282
+ */
283
+ getMetrics() {
284
+ return {
285
+ receivedMessagesCounter: this._receivedMessagesCounter,
286
+ sentMessagesCounter: this._sentMessagesCounter,
287
+ };
288
+ }
289
+ rebuildConnection() {
290
+ if (this._rebuilding) {
291
+ // only one rebuild process can be active at any time
292
+ this.log.debug("AMQP Connection rebuild already in progress, joining active rebuild attempt.");
293
+ return this.initialized;
294
+ }
295
+ this._retry = -1;
296
+ this._rebuilding = true;
297
+ this._isClosing = false;
298
+ // rebuild the connection
299
+ this.initialized = new Promise((resolve, reject) => {
300
+ this.tryToConnect(this, 0, (err) => {
301
+ /* istanbul ignore if */
302
+ if (err) {
303
+ this._rebuilding = false;
304
+ reject(err);
305
+ }
306
+ else {
307
+ this._rebuilding = false;
308
+ if (this.connectedBefore) {
309
+ this.log.info("AMQP Connection re-established");
310
+ this.emit("re_established_connection");
311
+ }
312
+ else {
313
+ this.log.info("AQMP Connection established.");
314
+ this.emit("open_connection");
315
+ this.connectedBefore = true;
316
+ }
317
+ resolve(null);
318
+ }
319
+ });
320
+ });
321
+ /* istanbul ignore next */
322
+ this.initialized.catch((err) => {
323
+ this.log.warn({ err }, "Error creating connection!");
324
+ this.emit("error_connection", err);
325
+ //throw (err);
326
+ });
327
+ return this.initialized;
328
+ }
329
+ /**
330
+ * Increments an internal counter.
331
+ * @param counter The counter to increase.
332
+ * @param value The increment, default is 1.
333
+ */
334
+ _increaseCounter(counter, value = 1) {
335
+ switch (counter) {
336
+ case "receivedMessages":
337
+ this._receivedMessagesCounter += value;
338
+ break;
339
+ case "sentMessages":
340
+ this._sentMessagesCounter += value;
341
+ break;
342
+ }
343
+ }
344
+ }
345
+ exports.Connection = Connection;
346
+ //# sourceMappingURL=Connection.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Connection.js","sourceRoot":"","sources":["../../src/Connection/Connection.ts"],"names":[],"mappings":";;;AAAA,gDAAgD;AAChD,mCAAsC;AAGtC,mDAAgD;AAEhD,oDAA4E;AAE5E,0CAAuC;AAKvC,MAAa,UAAW,SAAQ,qBAAY;IAyB1C;;;;;;OAMG;IACH,YACE,GAAG,GAAG,kBAAkB,EACxB,gBAAqB,EAAE,EACvB,oBAAuC,EAAE,OAAO,EAAE,CAAC,EAAE,QAAQ,EAAE,IAAI,EAAE,EAC9D,aAA6B;QAEpC,KAAK,EAAE,CAAC;QAFD,kBAAa,GAAb,aAAa,CAAgB;QAjC/B,gBAAW,GAAG,KAAK,CAAC;QAcnB,oBAAe,GAAG,KAAK,CAAC;QAExB,gBAAW,GAAG,KAAK,CAAC;QACpB,eAAU,GAAG,KAAK,CAAC;QAEnB,yBAAoB,GAAG,CAAC,CAAC;QACzB,6BAAwB,GAAG,CAAC,CAAC;QAgBnC,IAAI,CAAC,IAAI,CAAC,aAAa,EAAE;YACvB,IAAI,CAAC,aAAa,GAAG,CAAC,CAAC,EAAE,EAAE,EAAgB,EAAE,CAAC,IAAI,2BAAW,EAAE,CAAC;SACjE;QACD,IAAI,CAAC,GAAG,GAAG,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,WAAW,EAAE,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC,CAAC;QAEvE,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;QACf,mEAAmE;QACnE,IAAI,CAAC,aAAa,GAAG,aAAa,CAAC;QACnC,IAAI,CAAC,iBAAiB,GAAG,iBAAiB,CAAC;QAC3C,IAAI,CAAC,UAAU,GAAG,EAAE,CAAC;QACrB,IAAI,CAAC,OAAO,GAAG,EAAE,CAAC;QAClB,IAAI,CAAC,SAAS,GAAG,EAAE,CAA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AC,CAAC;wBAC7B,IAAI,CAAC,eAAe,GAAG,IAAI,CAAC;qBAC7B;oBACD,OAAO,CAAC,IAAI,CAAC,CAAC;iBACf;YACH,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;QACH,0BAA0B;QAC1B,IAAI,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC,GAAU,EAAE,EAAE;YACpC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,GAAG,EAAE,EAAE,4BAA4B,CAAC,CAAC;YACrD,IAAI,CAAC,IAAI,CAAC,kBAAkB,EAAE,GAAG,CAAC,CAAC;YACnC,cAAc;QAChB,CAAC,CAAC,CAAC;QACH,OAAO,IAAI,CAAC,WAAW,CAAC;IAC1B,CAAC;IAED;;;;OAIG;IACI,gBAAgB,CAAC,OAA4C,EAAE,KAAK,GAAG,CAAC;QAC7E,QAAQ,OAAO,EAAE;YACf,KAAK,kBAAkB;gBACrB,IAAI,CAAC,wBAAwB,IAAI,KAAK,CAAC;gBACvC,MAAM;YACR,KAAK,cAAc;gBACjB,IAAI,CAAC,oBAAoB,IAAI,KAAK,CAAC;gBACnC,MAAM;SACT;IACH,CAAC;CACF;AA9XD,gCA8XC"}
@@ -0,0 +1,7 @@
1
+ /** Metrics of a connection. */
2
+ export interface ConnectionMetrics {
3
+ /** The amount of messages send via this connection. */
4
+ sentMessagesCounter: number;
5
+ /** The amount of messages received on this connection. */
6
+ receivedMessagesCounter: number;
7
+ }
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=ConnectionMetrics.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ConnectionMetrics.js","sourceRoot":"","sources":["../../src/Connection/ConnectionMetrics.ts"],"names":[],"mappings":""}
@@ -0,0 +1,4 @@
1
+ export interface ReconnectStrategy {
2
+ retries: number;
3
+ interval: number;
4
+ }
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=ReconnectStrategy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ReconnectStrategy.js","sourceRoot":"","sources":["../../src/Connection/ReconnectStrategy.ts"],"names":[],"mappings":""}
@@ -0,0 +1,19 @@
1
+ import { ExchangeType } from "../Exchange/ExchangeType";
2
+ export interface Topology {
3
+ exchanges: {
4
+ name: string;
5
+ type?: ExchangeType;
6
+ options?: any;
7
+ }[];
8
+ queues: {
9
+ name: string;
10
+ options?: any;
11
+ }[];
12
+ bindings: {
13
+ source: string;
14
+ queue?: string;
15
+ exchange?: string;
16
+ pattern?: string;
17
+ args?: any;
18
+ }[];
19
+ }
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=Topology.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Topology.js","sourceRoot":"","sources":["../../src/Connection/Topology.ts"],"names":[],"mappings":""}
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Options that will be used for for creating exchanges. For more info see
3
+ * {@link https://www.squaremobius.net/amqp.node/channel_api.html#channel_assertExchange}.
4
+ */
5
+ export interface DeclarationOptions {
6
+ /** If true, the exchange will survive broker restarts. Defaults to true. */
7
+ durable?: boolean;
8
+ /** If true, messages cannot be published directly to the exchange. Defaults to false. */
9
+ internal?: boolean;
10
+ /** If true, the exchange will be destroyed once the number of bindings drops to zero. Defaults to false. */
11
+ autoDelete?: boolean;
12
+ /** An exchange to send messages to when the this exchange can't route to any queues. */
13
+ alternateExchange?: string;
14
+ /** Any additional arguments for the exchange. may be needed for some exchange types. */
15
+ arguments?: any;
16
+ /**
17
+ * If true, exchange will not be created if it does not exist. Instead an error will be thrown on initialization. If
18
+ * false exchange will be created if it does not already exist. */
19
+ noCreate?: boolean;
20
+ }
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=DeclarationOptions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DeclarationOptions.js","sourceRoot":"","sources":["../../src/Exchange/DeclarationOptions.ts"],"names":[],"mappings":""}
@@ -0,0 +1,94 @@
1
+ import * as AmqpLib from "amqplib/callback_api";
2
+ import { Binding } from "../Binding";
3
+ import { Connection } from "../Connection/Connection";
4
+ import { Message } from "../Message";
5
+ import { DeclarationOptions } from "./DeclarationOptions";
6
+ import { ExchangeType } from "./ExchangeType";
7
+ import { InitializeResult } from "./InitializeResult";
8
+ /**
9
+ * Represents an exchange.
10
+ */
11
+ export declare class Exchange {
12
+ connection: Connection;
13
+ private options;
14
+ /** A promise that resolves once the exchange is initialized. */
15
+ initialized: Promise<InitializeResult>;
16
+ /** The underlying AMQP channel. */
17
+ _channel: AmqpLib.Channel;
18
+ /** Gets the name of the exchange. */
19
+ get name(): string;
20
+ /** Gets the type of the exchange. */
21
+ get type(): string;
22
+ private _consumer_handlers;
23
+ private _isConsumerInitializedRcp;
24
+ private _name;
25
+ private _type;
26
+ private _deleting;
27
+ private _closing;
28
+ private log;
29
+ /**
30
+ * Initializes a new instance of the @see Exchange class.
31
+ *
32
+ * @summary Initializes an exchange by creating a channel and asserting the exchange exists on it. Once done the
33
+ * {@link initialized} promise will resolve.
34
+ * @param connection The AMQP connection.
35
+ * @param name The name of the exchange.
36
+ * @param type The type of the exchange.
37
+ * @param options Any declaration otions.
38
+ */
39
+ constructor(connection: Connection, name: string, type?: ExchangeType, options?: DeclarationOptions);
40
+ /**
41
+ * Initializes the exchange and resolves the @see initialized promise once the initialization is complete.
42
+ */
43
+ _initialize(): void;
44
+ /**
45
+ * Directly sends a message to this exchange.
46
+ *
47
+ * @param message The message.
48
+ * @param routingKey The routing key of the message.
49
+ */
50
+ send(message: Message, routingKey?: string): void;
51
+ rpc(requestParameters: any, routingKey?: string, callback?: (err: any, message: Message) => void): Promise<Message>;
52
+ /**
53
+ * Removes any bindings of this exchange, deletes the exchange and then closes the channel.
54
+ */
55
+ delete(): Promise<void>;
56
+ /**
57
+ * Removes any bindings of this exchange and closes the channel.
58
+ */
59
+ close(): Promise<void>;
60
+ /**
61
+ * Creates a bew binding with this exchange to the given exchange.
62
+ *
63
+ * @param source The source exchange.
64
+ * @param pattern The pattern.
65
+ * @param args Any additonal arguments.
66
+ * @returns A promise that resolves when the binding is initialized.
67
+ */
68
+ bind(source: Exchange, pattern?: string, args?: any): Promise<Binding>;
69
+ /**
70
+ * Unbinds the given exchange from the connection.
71
+ *
72
+ * @param source The exchange to unbind from.
73
+ * @param pattern The pattern.
74
+ * @param _args Any addtional arguments.
75
+ */
76
+ unbind(source: Exchange, pattern?: string, _args?: any): Promise<void>;
77
+ /** Returns a string containing the name of the exchange, the name of the host and the process ID. */
78
+ consumerQueueName(): string;
79
+ /**
80
+ * Declares a queue, binds it to this exchange and activates the given consumer handler on the queue. The name of the
81
+ * queue is determined by the name of the exchange, the application, the host and the ID of the process. Calling this
82
+ * twice without @see stopConsumer between the calls will likely fail.
83
+ *
84
+ * @param onMessage The message handler.
85
+ * @param options Any consumer options. See
86
+ * @link https://www.squaremobius.net/amqp.node/channel_api.html#channel_consume for more information.
87
+ * @throws {Error} If a handler has already been registered for this queue.
88
+ */
89
+ activateConsumer(onMessage: (msg: Message) => any, options?: AmqpLib.Options.Consume): Promise<any>;
90
+ /**
91
+ * Removes the bindings, the consumer and the queue if any exists.
92
+ */
93
+ stopConsumer(): Promise<any>;
94
+ }