@droidsolutions-oss/amqp-ts 3.0.3 → 4.0.0-develop.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 (48) hide show
  1. package/README.md +186 -45
  2. package/lib/AmqpProperties.js +1 -2
  3. package/lib/Binding.d.ts +36 -6
  4. package/lib/Binding.js +244 -75
  5. package/lib/Binding.js.map +1 -1
  6. package/lib/Connection/Connection.d.ts +87 -46
  7. package/lib/Connection/Connection.js +360 -181
  8. package/lib/Connection/Connection.js.map +1 -1
  9. package/lib/Connection/ConnectionMetrics.js +1 -2
  10. package/lib/Connection/RecoveryOptions.d.ts +6 -0
  11. package/lib/Connection/RecoveryOptions.js +2 -0
  12. package/lib/Connection/RecoveryOptions.js.map +1 -0
  13. package/lib/Connection/Topology.d.ts +1 -1
  14. package/lib/Connection/Topology.js +1 -2
  15. package/lib/Deferred.d.ts +18 -0
  16. package/lib/Deferred.js +33 -0
  17. package/lib/Deferred.js.map +1 -0
  18. package/lib/Exchange/DeclarationOptions.js +1 -2
  19. package/lib/Exchange/Exchange.d.ts +38 -17
  20. package/lib/Exchange/Exchange.js +204 -159
  21. package/lib/Exchange/Exchange.js.map +1 -1
  22. package/lib/Exchange/ExchangeType.js +1 -2
  23. package/lib/Exchange/InitializeResult.js +1 -2
  24. package/lib/LoggerFactory.js +1 -5
  25. package/lib/LoggerFactory.js.map +1 -1
  26. package/lib/Message.d.ts +11 -10
  27. package/lib/Message.js +66 -37
  28. package/lib/Message.js.map +1 -1
  29. package/lib/Queue/DeclarationOptions.js +1 -2
  30. package/lib/Queue/DeleteResult.js +1 -2
  31. package/lib/Queue/InitializeResult.js +1 -2
  32. package/lib/Queue/Queue.d.ts +87 -26
  33. package/lib/Queue/Queue.js +440 -207
  34. package/lib/Queue/Queue.js.map +1 -1
  35. package/lib/Queue/StartConsumerResult.js +1 -2
  36. package/lib/amqp-ts.d.ts +0 -1
  37. package/lib/amqp-ts.js +2 -7
  38. package/lib/amqp-ts.js.map +1 -1
  39. package/lib/errorMessage.d.ts +1 -0
  40. package/lib/errorMessage.js +18 -0
  41. package/lib/errorMessage.js.map +1 -0
  42. package/lib/index.d.ts +19 -14
  43. package/lib/index.js +7 -13
  44. package/lib/index.js.map +1 -1
  45. package/package.json +38 -60
  46. package/lib/Connection/ReconnectStrategy.d.ts +0 -4
  47. package/lib/Connection/ReconnectStrategy.js +0 -3
  48. package/lib/Connection/ReconnectStrategy.js.map +0 -1
package/lib/Message.js CHANGED
@@ -1,11 +1,26 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Message = void 0;
4
- const Queue_1 = require("./Queue/Queue");
1
+ /* eslint-disable @typescript-eslint/no-unsafe-assignment */
2
+ import { IllegalOperationError } from "amqplib";
3
+ import { errorMessage } from "./errorMessage.js";
4
+ import { Queue } from "./Queue/Queue.js";
5
5
  /**
6
6
  * Represents a message that can be send via AMQP.
7
7
  */
8
- class Message {
8
+ export class Message {
9
+ fields;
10
+ /**
11
+ * for received messages only: the channel it has been received on
12
+ * @internal
13
+ */
14
+ _channel;
15
+ /**
16
+ * received messages only: original amqplib message
17
+ * @internal
18
+ */
19
+ _message;
20
+ /** Additional AQMP properties of the message. */
21
+ properties;
22
+ /** The original contant of the message. */
23
+ content;
9
24
  /**
10
25
  * Initializes a new instance of the @see Message class.
11
26
  *
@@ -38,58 +53,73 @@ class Message {
38
53
  * @throws {Error} If the content type in the message properties is not set to application/json.
39
54
  */
40
55
  getJsonContent() {
41
- var _a;
42
56
  if (this.properties.contentType !== "application/json") {
43
57
  throw new Error("The content of the message is not set as application/json.");
44
58
  }
45
- return JSON.parse(this.content.toString((_a = this.properties.contentEncoding) !== null && _a !== void 0 ? _a : "utf-8"));
59
+ return JSON.parse(this.content.toString(this.properties.contentEncoding ?? "utf-8"));
46
60
  }
47
61
  /**
48
- * Tries to publish the message on the destination channel. Appends the current @see properties object and the
49
- * @see content of the message.
62
+ * Publishes the message on the destination's channel once the destination is initialized. Appends the current
63
+ * @see properties object and the @see content of the message.
64
+ *
65
+ * If the channel turns out to be closed or closing (the connection or the channel was lost, or amqplib is closing
66
+ * the connection after a protocol error), the message is published again once the destination is initialized
67
+ * again. A message that can never be published (the destination failed or was closed or deleted, recovery gave up
68
+ * or the connection was closed) is logged at warn level and dropped.
50
69
  *
51
70
  * @param destination The destination of the message.
52
71
  * @param routingKey An optional routing key.
53
72
  */
54
73
  sendTo(destination, routingKey = "") {
55
74
  let exchange;
56
- // inline function to send the message
57
- const sendMessage = () => {
75
+ if (destination instanceof Queue) {
76
+ exchange = "";
77
+ routingKey = destination.name;
78
+ }
79
+ else {
80
+ exchange = destination.name;
81
+ }
82
+ void this._publish(destination, exchange, routingKey);
83
+ }
84
+ async _publish(destination, exchange, routingKey) {
85
+ const connection = destination.connection;
86
+ const log = () => connection.loggerFactory(this.constructor);
87
+ for (;;) {
88
+ const ready = destination.initialized;
89
+ try {
90
+ await ready;
91
+ }
92
+ catch (err) {
93
+ const reason = errorMessage(err);
94
+ log().warn({ err }, "Dropping message to '%s', it cannot be initialized: %s", destination.name, reason);
95
+ return;
96
+ }
58
97
  try {
59
98
  if (!this.properties.timestamp) {
60
99
  // Set timestamp if not set by user
61
100
  this.properties.timestamp = Math.floor(new Date().getTime() / 1000);
62
101
  }
63
102
  destination._channel.publish(exchange, routingKey, this.content, this.properties);
64
- destination.connection._increaseCounter("sentMessages");
103
+ connection._increaseCounter("sentMessages");
104
+ return;
65
105
  }
66
106
  catch (err) {
67
- const log = destination.connection.loggerFactory(this.constructor);
68
- log.debug({ err }, "Publish error: %s", err.message);
69
- const destinationName = destination.name;
70
- const connection = destination.connection;
71
- log.debug("Try to rebuild connection, before Call.");
72
- connection._rebuildAll(err).then(() => {
73
- log.debug("Retransmitting message.");
74
- if (destination instanceof Queue_1.Queue) {
75
- // connection._queues[destinationName].publish(this.content, this.properties);
76
- connection._queues[destinationName].send(this);
77
- }
78
- else {
79
- // connection._exchanges[destinationName].publish(this.content, routingKey, this.properties);
80
- connection._exchanges[destinationName].send(this);
81
- }
82
- });
107
+ if (!(err instanceof IllegalOperationError)) {
108
+ const reason = err.message;
109
+ log().error({ err }, "Publishing to '%s' failed, dropping the message: %s", destination.name, reason);
110
+ return;
111
+ }
112
+ if (destination._isClosedOnPurpose) {
113
+ // close() or delete() of the destination or the connection: nothing re-initializes it
114
+ log().warn({ err }, "Dropping message to '%s', its channel is closed.", destination.name);
115
+ return;
116
+ }
117
+ // the channel or the connection is closed or closing otherwise (amqplib closes the connection itself on a
118
+ // protocol error, and publishing throws before the close arrives); once it is closed initialized is replaced
119
+ log().debug({ err }, "Channel of '%s' closed, publishing again once it is re-initialized.", destination.name);
120
+ await destination._initializedChanged(ready);
83
121
  }
84
- };
85
- if (destination instanceof Queue_1.Queue) {
86
- exchange = "";
87
- routingKey = destination.name;
88
- }
89
- else {
90
- exchange = destination.name;
91
122
  }
92
- destination.initialized.then(sendMessage);
93
123
  }
94
124
  ack(allUpTo) {
95
125
  if (this._channel !== undefined) {
@@ -120,5 +150,4 @@ class Message {
120
150
  }
121
151
  }
122
152
  }
123
- exports.Message = Message;
124
153
  //# sourceMappingURL=Message.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"Message.js","sourceRoot":"","sources":["../src/Message.ts"],"names":[],"mappings":";;;AAKA,yCAAsC;AAEtC;;GAEG;AACH,MAAa,OAAO;IAWlB;;;;;;;;OAQG;IACH,YAAY,OAAa,EAAE,UAA0B,EAAE;QACrD,IAAI,CAAC,UAAU,GAAG,OAAO,CAAC;QAC1B,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;YAC1B,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;QAC3B,CAAC;IACH,CAAC;IAED;;;OAGG;IACI,UAAU;QACf,IAAI,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC;QACtC,IAAI,IAAI,CAAC,UAAU,CAAC,WAAW,KAAK,kBAAkB,EAAE,CAAC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,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,CAAC;YACpD,IAAI,CAAC,UAAU,CAAC,WAAW,GAAG,kBAAkB,CAAC;YACjD,IAAI,CAAC,UAAU,CAAC,eAAe,GAAG,OAAO,CAAC;QAC5C,CAAC;aAAM,CAAC;YACN,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACzB,CAAC;IACH,CAAC;CACF"}
@@ -1,3 +1,2 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ export {};
3
2
  //# sourceMappingURL=DeclarationOptions.js.map
@@ -1,3 +1,2 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ export {};
3
2
  //# sourceMappingURL=DeleteResult.js.map
@@ -1,3 +1,2 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ export {};
3
2
  //# sourceMappingURL=InitializeResult.js.map
@@ -1,31 +1,63 @@
1
- import * as AmqpLib from "amqplib/callback_api";
2
- import type { AmqpProperties } from "../AmqpProperties";
3
- import { Binding } from "../Binding";
4
- import { Connection } from "../Connection/Connection";
5
- import { Exchange } from "../Exchange/Exchange";
6
- import { Message } from "../Message";
7
- import { DeclarationOptions } from "./DeclarationOptions";
8
- import { DeleteResult } from "./DeleteResult";
9
- import { InitializeResult } from "./InitializeResult";
10
- import { StartConsumerResult } from "./StartConsumerResult";
1
+ import type { Options } from "amqplib";
2
+ import type { AmqpProperties } from "../AmqpProperties.js";
3
+ import { Binding } from "../Binding.js";
4
+ import { Connection } from "../Connection/Connection.js";
5
+ import { Exchange } from "../Exchange/Exchange.js";
6
+ import { Message } from "../Message.js";
7
+ import { DeclarationOptions } from "./DeclarationOptions.js";
8
+ import { DeleteResult } from "./DeleteResult.js";
9
+ import { InitializeResult } from "./InitializeResult.js";
10
+ import { StartConsumerResult } from "./StartConsumerResult.js";
11
11
  /** Represents an AQMP queue. */
12
12
  export declare class Queue {
13
13
  connection: Connection;
14
14
  private options;
15
- /** A promise that resolves once the conneciton is initialized and the channel is created. */
15
+ /** A broker-side consumer cancel this soon after the consumer was registered again does not re-initialize. */
16
+ private static readonly CancelLoopWindowMs;
17
+ /**
18
+ * A promise that resolves once the connection is initialized and the queue is declared on its channel. It is
19
+ * replaced by a pending promise while the queue is re-initialized after a lost connection or a closed channel. It
20
+ * rejects when the queue fails on its own, or when the connection is closed or recovery gives up while it is pending.
21
+ */
16
22
  initialized: Promise<InitializeResult>;
17
- /** The AMQP channel. */
18
- _channel: AmqpLib.Channel;
19
- /** A promise that resolves when a consumer handler is registered and initialized. */
20
- _consumerInitialized: Promise<StartConsumerResult>;
21
23
  private _name;
22
- _consumer: (msg: any, channel?: AmqpLib.Channel) => any;
23
24
  private _consumerOptions;
24
25
  private _consumerTag;
25
26
  private _consumerStopping;
26
27
  private _deleting;
27
28
  private _closing;
28
29
  private log;
30
+ private _deferred;
31
+ private _consumerDeferred;
32
+ /** The generation of the last initialization, and that initialization. */
33
+ private _generation;
34
+ private _initializing;
35
+ /** Changes with every initialization, so a superseded one can tell. */
36
+ private _attempt;
37
+ /** The channel the queue was successfully declared on, and its model. */
38
+ private _readyChannel;
39
+ private _readyModel;
40
+ /** The channel the consumer is registered on, or being registered on. */
41
+ private _consumerChannel;
42
+ private _consuming;
43
+ /** The generation in which a broker-side consumer cancel made the queue re-initialize, until it re-consumed. */
44
+ private _cancelReinitGeneration;
45
+ /**
46
+ * When the current consumer was registered by a re-initialization after a broker-side cancel. Another cancel within
47
+ * {@link Queue.CancelLoopWindowMs} of that does not re-initialize again, which stops a cancel loop.
48
+ */
49
+ private _cancelRecoveredAt;
50
+ /**
51
+ * The generation in which the broker refused the consumer. Registering it again on the same connection would be
52
+ * refused again, so it is only registered again after the next reconnect.
53
+ */
54
+ private _consumerRefusedIn;
55
+ /** Why the queue failed on its own, after which it is no longer declared. */
56
+ private _failure;
57
+ /** Settles {@link _initializedChanged} for those waiting on it. */
58
+ private _changed;
59
+ /** Counts deliveries; bound once so it can be removed again. */
60
+ private readonly _onDelivery;
29
61
  /** Returns the name of the queue. */
30
62
  get name(): string;
31
63
  /**
@@ -39,15 +71,35 @@ export declare class Queue {
39
71
  * @constructor
40
72
  */
41
73
  constructor(connection: Connection, name: string, options?: DeclarationOptions);
42
- /** Waits until the connection is initialized then creates a channel and declares the queue on it. */
43
- _initialize(): void;
74
+ /** Replaces a settled {@link initialized} with a pending one. */
75
+ private _pendInitialized;
76
+ /**
77
+ * Re-creates the channel after it closed while the connection stayed up, declares the queue again and restores its
78
+ * bindings, which a broker-side delete removes as well. Only then {@link initialized} resolves. The caller registers
79
+ * the consumer again.
80
+ */
81
+ private _reinitialize;
82
+ private _declare;
83
+ /**
84
+ * Registers the consumer on the given channel. A refusal (for example 403 because another connection holds an
85
+ * exclusive consumer) is also reported through `error_connection`, since during recovery the consumer would
86
+ * otherwise stop silently.
87
+ */
88
+ private _consume;
89
+ /** Cancels a consumer registration that completed after it was superseded, so it does not keep receiving messages. */
90
+ private _cancelSuperseded;
91
+ /**
92
+ * A channel that closes while the connection stays up (for example after a double ack) is re-created, the queue
93
+ * declared again and its consumer registered again. Closes during a failed declaration or consumer registration, on
94
+ * a lost connection or caused by {@link close} or {@link delete} are left alone.
95
+ */
96
+ private _onChannelClose;
44
97
  /**
45
98
  * Directly sends a message to the underlying queue.
46
99
  *
47
100
  * @param message The AMQP message.
48
101
  */
49
102
  send(message: Message): void;
50
- rpc(requestParameters: any): Promise<Message>;
51
103
  /**
52
104
  * Sets the prefetch of the current quere. This limits the amount of unacknowledged messages on a channel. If you
53
105
  * already specified this in the options in the constructor you don't need to set it here again.
@@ -67,16 +119,25 @@ export declare class Queue {
67
119
  * @throws {Error} Rejects when a consumer is already bound to this queue.
68
120
  * @returns A promise that resolves once the handler is bound to the queue.
69
121
  */
70
- activateConsumer(onMessage: (msg: Message) => any, options?: AmqpLib.Options.Consume & {
122
+ activateConsumer(onMessage: (msg: Message) => any, options?: Options.Consume & {
71
123
  properties?: AmqpProperties;
72
124
  }): Promise<StartConsumerResult>;
73
125
  /**
74
- * Initializes the consumer with a handler function. Resolves the @see _consumerInitialized promise once the handler
75
- * is bound.
126
+ * Wraps the consumer handler for messages delivered on the given channel. A message that arrives after the consumer
127
+ * was stopped, before the broker confirmed the cancel, is given back to the queue for another consumer; one that
128
+ * needs no acknowledgement cannot be given back.
129
+ */
130
+ private _createConsumerWrapper;
131
+ /**
132
+ * The broker cancelled the consumer, for example because the queue was deleted. The channel stays open, so nothing
133
+ * else would notice: close it, which re-declares the queue, restores its bindings and registers the consumer again
134
+ * through the channel close handling. A `noCreate` queue that is gone then fails like any failed declaration.
76
135
  */
77
- _initializeConsumer(): void;
136
+ private _onConsumerCancelled;
78
137
  /**
79
- * Completely removes the consumer handler and cancels the channel.
138
+ * Completely removes the consumer handler and cancels the consumer on the broker. A consumer the broker refused has
139
+ * nothing to cancel, it is only removed. A consumer whose channel closes meanwhile is registered again on a new one,
140
+ * so that registration is waited for and cancelled instead.
80
141
  */
81
142
  stopConsumer(): Promise<void>;
82
143
  /**
@@ -96,12 +157,12 @@ export declare class Queue {
96
157
  */
97
158
  bind(source: Exchange, pattern?: string, args?: any): Promise<Binding>;
98
159
  /**
99
- * Unbinds the queue from the given exchange by deleting the binding.
160
+ * Unbinds the queue from the given exchange by deleting the binding. Resolves right away if there is no such
161
+ * binding, for example because it was unbound before.
100
162
  *
101
163
  * @param source The exchange to unbind from.
102
164
  * @param pattern The pattern the queue was bind with.
103
165
  * @param _args Any additional arguments.
104
166
  */
105
167
  unbind(source: Exchange, pattern?: string, _args?: any): Promise<void>;
106
- private handleReceivedMessageEvent;
107
168
  }