amqpx 1.0.0 → 1.0.2

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.
package/README.md CHANGED
@@ -25,7 +25,7 @@ npm install amqpx
25
25
  ## Quick example
26
26
 
27
27
  ```typescript
28
- import { connect, AssertionMode } from 'amqpx'
28
+ import { connect } from 'amqpx'
29
29
 
30
30
  type Order = { orderId: string; total: number }
31
31
 
@@ -36,7 +36,7 @@ const connection = await connect({
36
36
  })
37
37
 
38
38
  // Topology
39
- const channel = await connection.createChannel()
39
+ const channel = connection.createChannel()
40
40
  const queue = await channel.createQueue('orders', { durable: true })
41
41
 
42
42
  // Produce
@@ -44,7 +44,7 @@ const producer = await queue.createProducer<Order>()
44
44
  await producer.publish({ orderId: 'abc-123', total: 49.99 })
45
45
 
46
46
  // Consume
47
- const consumer = await consumerQueue.createConsumer<Order>({ prefetch: 10 })
47
+ const consumer = await queue.createConsumer<Order>({ prefetch: 10 })
48
48
  await consumer.listen(async ({ message }) => {
49
49
  console.log('order', message.orderId, 'total', message.total)
50
50
  })
@@ -9,6 +9,7 @@ export declare class ExchangeImplementation implements Exchange {
9
9
  private readonly exchangeType;
10
10
  private readonly options?;
11
11
  private assertPromise;
12
+ /** Exchange bindings, re-applied when the exchange is asserted again. */
12
13
  private bindings;
13
14
  constructor(channel: Channel, exchangeName: string, exchangeType: ExchangeTypes, options?: ExchangeOptions);
14
15
  assert(): Promise<this>;
@@ -18,7 +18,6 @@ const safe_stable_stringify_1 = __importDefault(require("safe-stable-stringify")
18
18
  const producer_1 = require("../producer");
19
19
  const utils_1 = require("../utils");
20
20
  const types_1 = require("../types");
21
- const types_2 = require("./types");
22
21
  const debug = (0, util_1.debuglog)(`${utils_1.LIB_NAME}:exchange`);
23
22
  const DEFAULT_EXCHANGE_OPTIONS = {
24
23
  assertionMode: types_1.AssertionMode.Assert,
@@ -75,26 +74,8 @@ class ExchangeImplementation {
75
74
  }
76
75
  bindQueue(queue, pattern, args) {
77
76
  return __awaiter(this, void 0, void 0, function* () {
78
- const [thisName, queueName] = yield Promise.all([
79
- this.name(),
80
- queue.name(),
81
- ]);
82
- debug('bind-queue exchange=%s queue=%s pattern=%s', thisName, queueName, pattern);
83
- const doesBindingMatch = (binding) => binding.type === types_2.BindingType.queue &&
84
- binding.pattern === pattern &&
85
- binding.queue === queue &&
86
- (0, safe_stable_stringify_1.default)(binding.args) === (0, safe_stable_stringify_1.default)(args);
87
- if (this.bindings.every(binding => !doesBindingMatch(binding))) {
88
- this.bindings.push({
89
- args,
90
- pattern,
91
- type: types_2.BindingType.queue,
92
- queue,
93
- });
94
- }
95
- const channel = yield this.channel.native();
96
- yield channel.bindQueue(queueName, thisName, pattern, args);
97
- debug('bind-queue-done exchange=%s queue=%s pattern=%s', thisName, queueName, pattern);
77
+ debug('bind-queue exchange=%s pattern=%s', this.exchangeName, pattern);
78
+ yield queue.bindExchange(this, pattern, args);
98
79
  return this;
99
80
  });
100
81
  }
@@ -105,15 +86,13 @@ class ExchangeImplementation {
105
86
  exchange.name(),
106
87
  ]);
107
88
  debug('bind-exchange dest=%s source=%s pattern=%s', thisName, exchangeName, pattern);
108
- const doesBindingMatch = (binding) => binding.type === types_2.BindingType.exchange &&
109
- binding.pattern === pattern &&
89
+ const doesBindingMatch = (binding) => binding.pattern === pattern &&
110
90
  binding.exchange === exchange &&
111
91
  (0, safe_stable_stringify_1.default)(binding.args) === (0, safe_stable_stringify_1.default)(args);
112
92
  if (this.bindings.every(binding => !doesBindingMatch(binding))) {
113
93
  this.bindings.push({
114
94
  args,
115
95
  pattern,
116
- type: types_2.BindingType.exchange,
117
96
  exchange,
118
97
  });
119
98
  }
@@ -126,9 +105,7 @@ class ExchangeImplementation {
126
105
  rebind() {
127
106
  return __awaiter(this, void 0, void 0, function* () {
128
107
  debug('rebind name=%s bindings=%d', this.exchangeName, this.bindings.length);
129
- const promises = yield Promise.allSettled(this.bindings.map(binding => (binding.type === types_2.BindingType.queue
130
- ? this.bindQueue(binding.queue, binding.pattern, binding.args)
131
- : this.bindExchange(binding.exchange, binding.pattern, binding.args))));
108
+ const promises = yield Promise.allSettled(this.bindings.map(binding => this.bindExchange(binding.exchange, binding.pattern, binding.args)));
132
109
  const failedPromise = promises.find(p => p.status === 'rejected');
133
110
  if (failedPromise) {
134
111
  debug('rebind-failed error=%s', (0, utils_1.errToMessage)(failedPromise.reason));
@@ -140,14 +117,14 @@ class ExchangeImplementation {
140
117
  createConsumer(options_1) {
141
118
  return __awaiter(this, arguments, void 0, function* (options, queueOptions = {}) {
142
119
  const queue = yield this.channel.createQueue('', Object.assign(Object.assign({}, queueOptions), { durable: false, exclusive: true }));
143
- yield this.bindQueue(queue, options.pattern, options.bindingArgs);
120
+ yield queue.bindExchange(this, options.pattern, options.bindingArgs);
144
121
  return queue.createConsumer(options);
145
122
  });
146
123
  }
147
124
  createBatchConsumer(options_1) {
148
125
  return __awaiter(this, arguments, void 0, function* (options, queueOptions = {}) {
149
126
  const queue = yield this.channel.createQueue('', Object.assign(Object.assign({}, queueOptions), { durable: false, exclusive: true }));
150
- yield this.bindQueue(queue, options.pattern, options.bindingArgs);
127
+ yield queue.bindExchange(this, options.pattern, options.bindingArgs);
151
128
  return queue.createBatchConsumer(options);
152
129
  });
153
130
  }
@@ -21,7 +21,9 @@ export interface Exchange {
21
21
  name(): Promise<string>;
22
22
  /**
23
23
  * Bind a queue to this exchange.
24
- * Asserts the exchange first if it has not been asserted yet.
24
+ * Asserts the queue and the exchange first if they have not been asserted yet.
25
+ * Equivalent to `queue.bindExchange(exchange, pattern, args)`.
26
+ * The binding is restored when the queue is asserted again after a reconnect.
25
27
  * @param queue - Queue to bind to this exchange.
26
28
  * @param pattern - Routing key pattern under which to bind.
27
29
  * @param args - Optional AMQP headers or x-arguments for the binding.
@@ -37,25 +39,28 @@ export interface Exchange {
37
39
  bindExchange(exchange: Exchange, pattern: string, args?: BindingArgs): Promise<Exchange>;
38
40
  /**
39
41
  * Creates a consumer for the current exchange.
40
- * Internally asserts an exclusive queue and binds it to the exchange.
41
- * Unless specified otherwise, uses the same channel as was used to declare the exchange.
42
- * This is generally not recommended, as each consumer should have its own dedicated channel.
43
- * Consider using `connection.createConsumerForExchange` instead.
42
+ * The consumer reads from a temporary exclusive queue bound to the exchange under `options.pattern`
43
+ * and keeps receiving messages after a reconnect.
44
+ * By default, creates a new dedicated channel for the consumer.
45
+ * Pass a channel in `options` to use a specific one instead.
44
46
  */
45
47
  createConsumer<T>(options: ExchangeConsumerOptions<T>, queueOptions?: ExchangeConsumerQueueOptions): Promise<Consumer<T>>;
46
48
  /**
47
49
  * Creates a batch consumer for the current exchange.
48
- * Internally asserts an exclusive queue and binds it to the exchange.
49
- * Unless specified otherwise, uses the same channel as was used to declare the exchange.
50
- * This is generally not recommended, as each consumer should have its own dedicated channel.
51
- * Consider using `connection.createBatchConsumerForExchange` instead.
50
+ * The consumer reads from a temporary exclusive queue bound to the exchange under `options.pattern`
51
+ * and keeps receiving messages after a reconnect.
52
+ * By default, creates a new dedicated channel for the consumer.
53
+ * Pass a channel in `options` to use a specific one instead.
52
54
  */
53
55
  createBatchConsumer<T>(options: ExchangeBatchConsumerOptions<T>, queueOptions?: ExchangeConsumerQueueOptions): Promise<BatchConsumer<T>>;
54
56
  /**
55
57
  * Creates a producer for the current exchange.
56
- * Unless specified otherwise, uses the same channel as was used to declare the exchange.
57
- * This is generally not recommended, as each producer should have its own dedicated channel.
58
- * Consider using `connection.createProducerForExchange` instead.
58
+ * By default, creates a new dedicated channel for the producer.
59
+ * Pass a channel in `options` to use a specific one instead.
60
+ *
61
+ * @param options - Optional producer options (serialization, routing key, hooks, …).
62
+ * Set `options.isConfirmed` to `true` to open the dedicated channel in confirm mode.
63
+ * Has no effect when `options.channel` is supplied.
59
64
  */
60
65
  createProducer<T>(options?: ProducerOptions<T> & {
61
66
  isConfirmed?: boolean;
@@ -1,7 +1,6 @@
1
1
  import * as amqp from 'amqplib';
2
2
  import { AssertionMode } from '../types';
3
3
  import { ConsumerOptions, BatchConsumerOptions } from '../consumer';
4
- import { Queue } from '../queue';
5
4
  import { Exchange } from './exchange';
6
5
  /**
7
6
  * The type of exchange to create. Accepted values: `'direct'`, `'fanout'`, `'topic'`, `'headers'`, `'match'`.
@@ -31,21 +30,13 @@ export type ExchangeConsumerOptions<T> = ExchangeConsumerBindingOptions & Consum
31
30
  */
32
31
  export type ExchangeBatchConsumerOptions<T> = ExchangeConsumerBindingOptions & BatchConsumerOptions<T>;
33
32
  export type BindingArgs = Record<string, string | number | boolean>;
34
- export declare enum BindingType {
35
- queue = "queue",
36
- exchange = "exchange"
37
- }
38
33
  type Pattern = string;
39
- export type Binding = {
34
+ /** A binding of another exchange to this exchange. */
35
+ export type ExchangeBinding = {
36
+ exchange: Exchange;
40
37
  pattern: Pattern;
41
38
  args?: BindingArgs;
42
- } & ({
43
- type: BindingType.queue;
44
- queue: Queue;
45
- } | {
46
- type: BindingType.exchange;
47
- exchange: Exchange;
48
- });
39
+ };
49
40
  export type ExchangeOptions = amqp.Options.AssertExchange & {
50
41
  /**
51
42
  * Controls how the exchange is verified against the broker when `assert()` is called.
@@ -1,8 +1,2 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.BindingType = void 0;
4
- var BindingType;
5
- (function (BindingType) {
6
- BindingType["queue"] = "queue";
7
- BindingType["exchange"] = "exchange";
8
- })(BindingType || (exports.BindingType = BindingType = {}));
@@ -1,49 +1,54 @@
1
1
  import { Exchange } from './exchange';
2
2
  import { Channel } from './channel';
3
3
  /**
4
- * This will create a direct exchange that is created by default by RabbitMQ container.
4
+ * Returns the direct exchange that RabbitMQ creates by default in every virtual host.
5
5
  *
6
- * The direct exchange has name *amqp.direct*, is durable, and has type *direct*.
6
+ * The exchange has name *amq.direct*, is durable, and has type *direct*.
7
+ * The exchange always exists on the broker, so it is never declared.
7
8
  *
8
- * @param channel The channel that should assert the exchange
9
- * @returns The exchange that was created
9
+ * @param channel The channel the exchange belongs to
10
+ * @returns The predefined exchange
10
11
  */
11
12
  export declare const directExchange: (channel: Channel) => Exchange;
12
13
  /**
13
- * This will create a fanout exchange that is created by default by RabbitMQ container.
14
+ * Returns the fanout exchange that RabbitMQ creates by default in every virtual host.
14
15
  *
15
- * The fanout exchange has name *amqp.fanout*, is durable, and has type *fanout*.
16
+ * The exchange has name *amq.fanout*, is durable, and has type *fanout*.
17
+ * The exchange always exists on the broker, so it is never declared.
16
18
  *
17
- * @param channel The channel that should assert the exchange
18
- * @returns The exchange that was created
19
+ * @param channel The channel the exchange belongs to
20
+ * @returns The predefined exchange
19
21
  */
20
22
  export declare const fanoutExchange: (channel: Channel) => Exchange;
21
23
  /**
22
- * This will create a headers exchange that is created by default by RabbitMQ container.
24
+ * Returns the headers exchange that RabbitMQ creates by default in every virtual host.
23
25
  *
24
- * The headers exchange has name *amqp.headers*, is durable, and has type *headers*.
26
+ * The exchange has name *amq.headers*, is durable, and has type *headers*.
27
+ * The exchange always exists on the broker, so it is never declared.
25
28
  *
26
- * @param channel The channel that should assert the exchange
27
- * @returns The exchange that was created
29
+ * @param channel The channel the exchange belongs to
30
+ * @returns The predefined exchange
28
31
  */
29
32
  export declare const headersExchange: (channel: Channel) => Exchange;
30
33
  /**
31
- * This will create a match exchange that is created by default by RabbitMQ container.
32
- * Behaves the same as headers exchange, but has a different name.
34
+ * Returns the match exchange that RabbitMQ creates by default in every virtual host.
35
+ * Behaves the same as {@link headersExchange}, but has a different name.
33
36
  *
34
- * The match exchange has name *amqp.match*, is durable, and has type *match*.
37
+ * The exchange has name *amq.match*, is durable, and has type *headers*.
38
+ * The exchange always exists on the broker, so it is never declared.
35
39
  *
36
- * @param channel The channel that should assert the exchange
37
- * @returns The exchange that was created
40
+ * @param channel The channel the exchange belongs to
41
+ * @returns The predefined exchange
38
42
  */
39
43
  export declare const matchExchange: (channel: Channel) => Exchange;
40
44
  /**
41
- * This will create a topic exchange that is created by default by RabbitMQ container.
45
+ * Returns the topic exchange that RabbitMQ creates by default in every virtual host.
42
46
  *
43
- * The topic exchange has name *amqp.topic*, is durable, and has type *topic*.
47
+ * The exchange has name *amq.topic*, is durable, and has type *topic*.
48
+ * The exchange always exists on the broker, so it is never declared.
44
49
  *
45
- * @param channel The channel that should assert the exchange
46
- * @returns The exchange that was created
50
+ * @param channel The channel the exchange belongs to
51
+ * @returns The predefined exchange
47
52
  */
48
53
  export declare const topicExchange: (channel: Channel) => Exchange;
49
54
  /**
@@ -3,24 +3,30 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.defaultExchange = exports.topicExchange = exports.matchExchange = exports.headersExchange = exports.fanoutExchange = exports.directExchange = void 0;
4
4
  const exchange_1 = require("./exchange");
5
5
  const default_exchange_1 = require("./exchange/default-exchange");
6
- const directExchange = (channel) => new exchange_1.ExchangeImplementation(channel, 'amqp.direct', 'direct', {
6
+ const types_1 = require("./types");
7
+ const directExchange = (channel) => new exchange_1.ExchangeImplementation(channel, 'amq.direct', 'direct', {
7
8
  durable: true,
9
+ assertionMode: types_1.AssertionMode.Passive,
8
10
  });
9
11
  exports.directExchange = directExchange;
10
- const fanoutExchange = (channel) => new exchange_1.ExchangeImplementation(channel, 'amqp.fanout', 'fanout', {
12
+ const fanoutExchange = (channel) => new exchange_1.ExchangeImplementation(channel, 'amq.fanout', 'fanout', {
11
13
  durable: true,
14
+ assertionMode: types_1.AssertionMode.Passive,
12
15
  });
13
16
  exports.fanoutExchange = fanoutExchange;
14
- const headersExchange = (channel) => new exchange_1.ExchangeImplementation(channel, 'amqp.headers', 'headers', {
17
+ const headersExchange = (channel) => new exchange_1.ExchangeImplementation(channel, 'amq.headers', 'headers', {
15
18
  durable: true,
19
+ assertionMode: types_1.AssertionMode.Passive,
16
20
  });
17
21
  exports.headersExchange = headersExchange;
18
- const matchExchange = (channel) => new exchange_1.ExchangeImplementation(channel, 'amqp.match', 'match', {
22
+ const matchExchange = (channel) => new exchange_1.ExchangeImplementation(channel, 'amq.match', 'headers', {
19
23
  durable: true,
24
+ assertionMode: types_1.AssertionMode.Passive,
20
25
  });
21
26
  exports.matchExchange = matchExchange;
22
- const topicExchange = (channel) => new exchange_1.ExchangeImplementation(channel, 'amqp.topic', 'topic', {
27
+ const topicExchange = (channel) => new exchange_1.ExchangeImplementation(channel, 'amq.topic', 'topic', {
23
28
  durable: true,
29
+ assertionMode: types_1.AssertionMode.Passive,
24
30
  });
25
31
  exports.topicExchange = topicExchange;
26
32
  const defaultExchange = (channel) => new default_exchange_1.DefaultExchange(channel);
@@ -9,10 +9,18 @@ export declare class QueueImplementation implements Queue {
9
9
  private readonly queueName;
10
10
  private readonly options?;
11
11
  private assertPromise;
12
+ /** Queue name resolved by the last assert; null until then and after the channel closes. */
13
+ private declaredName;
14
+ private bindings;
12
15
  constructor(channel: Channel, queueName: string, options?: QueueOptions);
13
16
  assert(): Promise<this>;
14
17
  name(): Promise<string>;
15
18
  bindExchange(exchange: Exchange, pattern: string, args?: BindingArgs): Promise<this>;
19
+ /** Creates the binding on the broker; the queue must already be asserted. */
20
+ private nativeBind;
21
+ private currentName;
22
+ /** Re-applies the remembered bindings after the queue is asserted. */
23
+ private rebind;
16
24
  createConsumer<T>(options?: ConsumerOptions<T>): Promise<Consumer<T>>;
17
25
  createBatchConsumer<T>(options?: BatchConsumerOptions<T>): Promise<BatchConsumer<T>>;
18
26
  createProducer<T>(options?: ProducerOptions<T> & {
@@ -8,9 +8,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
11
14
  Object.defineProperty(exports, "__esModule", { value: true });
12
15
  exports.QueueImplementation = void 0;
13
16
  const util_1 = require("util");
17
+ const safe_stable_stringify_1 = __importDefault(require("safe-stable-stringify"));
14
18
  const consumer_1 = require("../consumer");
15
19
  const producer_1 = require("../producer");
16
20
  const index_1 = require("../index");
@@ -25,10 +29,13 @@ class QueueImplementation {
25
29
  this.channel = channel;
26
30
  this.queueName = queueName;
27
31
  this.assertPromise = null;
32
+ this.declaredName = null;
33
+ this.bindings = [];
28
34
  this.options = (0, utils_1.deepMerge)({}, DEFAULT_QUEUE_OPTIONS, options !== null && options !== void 0 ? options : {});
29
35
  this.channel.on('close', () => {
30
36
  debug('native-close');
31
37
  this.assertPromise = null;
38
+ this.declaredName = null;
32
39
  });
33
40
  }
34
41
  assert() {
@@ -37,43 +44,88 @@ class QueueImplementation {
37
44
  yield this.assertPromise;
38
45
  return this;
39
46
  }
40
- this.assertPromise = (() => __awaiter(this, void 0, void 0, function* () {
47
+ const promise = (() => __awaiter(this, void 0, void 0, function* () {
41
48
  var _a, _b, _c;
42
49
  debug('assert name=%s mode=%s', this.queueName, (_a = this.options) === null || _a === void 0 ? void 0 : _a.assertionMode);
43
50
  const channel = yield this.channel.native();
44
51
  switch ((_b = this.options) === null || _b === void 0 ? void 0 : _b.assertionMode) {
45
52
  case types_1.AssertionMode.Check:
46
53
  yield channel.checkQueue(this.queueName);
54
+ this.declaredName = this.queueName;
47
55
  debug('native-check-done name=%s', this.queueName);
48
56
  break;
49
- case types_1.AssertionMode.Assert:
50
- yield channel.assertQueue(this.queueName, this.options);
51
- debug('native-assert-done name=%s', this.queueName);
57
+ case types_1.AssertionMode.Assert: {
58
+ const reply = yield channel.assertQueue(this.queueName, this.options);
59
+ this.declaredName = reply.queue;
60
+ debug('native-assert-done name=%s declared-name=%s', this.queueName, this.declaredName);
52
61
  break;
62
+ }
53
63
  case types_1.AssertionMode.Passive:
64
+ this.declaredName = this.queueName;
54
65
  debug('no-assert name=%s mode=%s', this.queueName, this.options.assertionMode);
55
66
  break;
56
67
  default:
57
68
  throw new Error(`Unknown assertion mode: ${(_c = this.options) === null || _c === void 0 ? void 0 : _c.assertionMode}`);
58
69
  }
59
- }))();
60
- yield this.assertPromise;
61
- debug('assert-complete name=%s', this.queueName);
70
+ yield this.rebind();
71
+ debug('assert-complete name=%s', this.currentName());
72
+ }))().catch((error) => {
73
+ if (this.assertPromise === promise) {
74
+ this.assertPromise = null;
75
+ this.declaredName = null;
76
+ }
77
+ throw error;
78
+ });
79
+ this.assertPromise = promise;
80
+ yield promise;
62
81
  return this;
63
82
  });
64
83
  }
65
84
  name() {
66
85
  return __awaiter(this, void 0, void 0, function* () {
67
- return this.assert().then(self => self.queueName);
86
+ return this.assert().then(self => self.currentName());
68
87
  });
69
88
  }
70
89
  bindExchange(exchange, pattern, args) {
71
90
  return __awaiter(this, void 0, void 0, function* () {
72
- debug('bind-exchange queue=%s pattern=%s', this.queueName, pattern);
73
- yield exchange.bindQueue(this, pattern, args);
91
+ yield this.assert();
92
+ yield this.nativeBind({ exchange, pattern, args });
93
+ const isSameBinding = (binding) => binding.exchange === exchange &&
94
+ binding.pattern === pattern &&
95
+ (0, safe_stable_stringify_1.default)(binding.args) === (0, safe_stable_stringify_1.default)(args);
96
+ if (this.bindings.every(binding => !isSameBinding(binding)))
97
+ this.bindings.push({ exchange, pattern, args });
74
98
  return this;
75
99
  });
76
100
  }
101
+ nativeBind(_a) {
102
+ return __awaiter(this, arguments, void 0, function* ({ exchange, pattern, args }) {
103
+ const exchangeName = yield exchange.name();
104
+ const queueName = this.currentName();
105
+ debug('bind-exchange queue=%s exchange=%s pattern=%s', queueName, exchangeName, pattern);
106
+ const channel = yield this.channel.native();
107
+ yield channel.bindQueue(queueName, exchangeName, pattern, args);
108
+ debug('bind-exchange-done queue=%s exchange=%s pattern=%s', queueName, exchangeName, pattern);
109
+ });
110
+ }
111
+ currentName() {
112
+ var _a;
113
+ return (_a = this.declaredName) !== null && _a !== void 0 ? _a : this.queueName;
114
+ }
115
+ rebind() {
116
+ return __awaiter(this, void 0, void 0, function* () {
117
+ if (this.bindings.length === 0)
118
+ return;
119
+ debug('rebind name=%s bindings=%d', this.currentName(), this.bindings.length);
120
+ const results = yield Promise.allSettled(this.bindings.map(binding => this.nativeBind(binding)));
121
+ const failed = results.find(result => result.status === 'rejected');
122
+ if (failed) {
123
+ debug('rebind-failed error=%s', (0, utils_1.errToMessage)(failed.reason));
124
+ throw failed.reason;
125
+ }
126
+ debug('rebind-complete name=%s', this.currentName());
127
+ });
128
+ }
77
129
  createConsumer(options = {}) {
78
130
  var _a;
79
131
  return Promise.resolve(new consumer_1.ConsumerImplementation((_a = options.channel) !== null && _a !== void 0 ? _a : this.channel.connection.createChannel(), this, options));
@@ -21,7 +21,9 @@ export interface Queue {
21
21
  name(): Promise<string>;
22
22
  /**
23
23
  * Bind this queue to an exchange.
24
- * Asserts the queue first if it has not been asserted yet.
24
+ * Asserts the queue and the exchange first if they have not been asserted yet.
25
+ * Equivalent to `exchange.bindQueue(queue, pattern, args)`.
26
+ * The binding is restored when the queue is asserted again after a reconnect.
25
27
  * @param exchange - Exchange to bind this queue to.
26
28
  * @param pattern - Routing key pattern under which to bind.
27
29
  * @param args - Optional AMQP headers or x-arguments for the binding.
@@ -44,6 +46,10 @@ export interface Queue {
44
46
  * Publishes via the default RabbitMQ exchange, routing messages directly to this queue by name.
45
47
  * By default, creates a new dedicated channel for the producer.
46
48
  * Pass a channel in `options` to use a specific one instead.
49
+ *
50
+ * @param options - Optional producer options (serialization, routing key, hooks, …).
51
+ * Set `options.isConfirmed` to `true` to open the dedicated channel in confirm mode.
52
+ * Has no effect when `options.channel` is supplied.
47
53
  */
48
54
  createProducer<T>(options?: ProducerOptions<T> & {
49
55
  isConfirmed?: boolean;
@@ -15,11 +15,11 @@ export type QueueOptions = amqp.Options.AssertQueue & {
15
15
  */
16
16
  assertionMode?: AssertionMode;
17
17
  };
18
- type ExchangeName = string;
19
18
  type Pattern = string;
20
- export type Bindings = Record<ExchangeName, Record<Pattern, {
19
+ /** A binding of a queue to an exchange. */
20
+ export type QueueBinding = {
21
21
  exchange: Exchange;
22
22
  pattern: Pattern;
23
23
  args?: BindingArgs;
24
- }>>;
24
+ };
25
25
  export {};
@@ -9,6 +9,7 @@ export declare class ExchangeImplementation implements Exchange {
9
9
  private readonly exchangeType;
10
10
  private readonly options?;
11
11
  private assertPromise;
12
+ /** Exchange bindings, re-applied when the exchange is asserted again. */
12
13
  private bindings;
13
14
  constructor(channel: Channel, exchangeName: string, exchangeType: ExchangeTypes, options?: ExchangeOptions);
14
15
  assert(): Promise<this>;
@@ -12,7 +12,6 @@ import stableJsonStringify from 'safe-stable-stringify';
12
12
  import { ProducerImplementation } from '../producer';
13
13
  import { deepMerge, errToMessage, LIB_NAME } from '../utils';
14
14
  import { AssertionMode } from '../types';
15
- import { BindingType } from './types';
16
15
  const debug = debuglog(`${LIB_NAME}:exchange`);
17
16
  const DEFAULT_EXCHANGE_OPTIONS = {
18
17
  assertionMode: AssertionMode.Assert,
@@ -69,26 +68,8 @@ export class ExchangeImplementation {
69
68
  }
70
69
  bindQueue(queue, pattern, args) {
71
70
  return __awaiter(this, void 0, void 0, function* () {
72
- const [thisName, queueName] = yield Promise.all([
73
- this.name(),
74
- queue.name(),
75
- ]);
76
- debug('bind-queue exchange=%s queue=%s pattern=%s', thisName, queueName, pattern);
77
- const doesBindingMatch = (binding) => binding.type === BindingType.queue &&
78
- binding.pattern === pattern &&
79
- binding.queue === queue &&
80
- stableJsonStringify(binding.args) === stableJsonStringify(args);
81
- if (this.bindings.every(binding => !doesBindingMatch(binding))) {
82
- this.bindings.push({
83
- args,
84
- pattern,
85
- type: BindingType.queue,
86
- queue,
87
- });
88
- }
89
- const channel = yield this.channel.native();
90
- yield channel.bindQueue(queueName, thisName, pattern, args);
91
- debug('bind-queue-done exchange=%s queue=%s pattern=%s', thisName, queueName, pattern);
71
+ debug('bind-queue exchange=%s pattern=%s', this.exchangeName, pattern);
72
+ yield queue.bindExchange(this, pattern, args);
92
73
  return this;
93
74
  });
94
75
  }
@@ -99,15 +80,13 @@ export class ExchangeImplementation {
99
80
  exchange.name(),
100
81
  ]);
101
82
  debug('bind-exchange dest=%s source=%s pattern=%s', thisName, exchangeName, pattern);
102
- const doesBindingMatch = (binding) => binding.type === BindingType.exchange &&
103
- binding.pattern === pattern &&
83
+ const doesBindingMatch = (binding) => binding.pattern === pattern &&
104
84
  binding.exchange === exchange &&
105
85
  stableJsonStringify(binding.args) === stableJsonStringify(args);
106
86
  if (this.bindings.every(binding => !doesBindingMatch(binding))) {
107
87
  this.bindings.push({
108
88
  args,
109
89
  pattern,
110
- type: BindingType.exchange,
111
90
  exchange,
112
91
  });
113
92
  }
@@ -120,9 +99,7 @@ export class ExchangeImplementation {
120
99
  rebind() {
121
100
  return __awaiter(this, void 0, void 0, function* () {
122
101
  debug('rebind name=%s bindings=%d', this.exchangeName, this.bindings.length);
123
- const promises = yield Promise.allSettled(this.bindings.map(binding => (binding.type === BindingType.queue
124
- ? this.bindQueue(binding.queue, binding.pattern, binding.args)
125
- : this.bindExchange(binding.exchange, binding.pattern, binding.args))));
102
+ const promises = yield Promise.allSettled(this.bindings.map(binding => this.bindExchange(binding.exchange, binding.pattern, binding.args)));
126
103
  const failedPromise = promises.find(p => p.status === 'rejected');
127
104
  if (failedPromise) {
128
105
  debug('rebind-failed error=%s', errToMessage(failedPromise.reason));
@@ -134,14 +111,14 @@ export class ExchangeImplementation {
134
111
  createConsumer(options_1) {
135
112
  return __awaiter(this, arguments, void 0, function* (options, queueOptions = {}) {
136
113
  const queue = yield this.channel.createQueue('', Object.assign(Object.assign({}, queueOptions), { durable: false, exclusive: true }));
137
- yield this.bindQueue(queue, options.pattern, options.bindingArgs);
114
+ yield queue.bindExchange(this, options.pattern, options.bindingArgs);
138
115
  return queue.createConsumer(options);
139
116
  });
140
117
  }
141
118
  createBatchConsumer(options_1) {
142
119
  return __awaiter(this, arguments, void 0, function* (options, queueOptions = {}) {
143
120
  const queue = yield this.channel.createQueue('', Object.assign(Object.assign({}, queueOptions), { durable: false, exclusive: true }));
144
- yield this.bindQueue(queue, options.pattern, options.bindingArgs);
121
+ yield queue.bindExchange(this, options.pattern, options.bindingArgs);
145
122
  return queue.createBatchConsumer(options);
146
123
  });
147
124
  }
@@ -21,7 +21,9 @@ export interface Exchange {
21
21
  name(): Promise<string>;
22
22
  /**
23
23
  * Bind a queue to this exchange.
24
- * Asserts the exchange first if it has not been asserted yet.
24
+ * Asserts the queue and the exchange first if they have not been asserted yet.
25
+ * Equivalent to `queue.bindExchange(exchange, pattern, args)`.
26
+ * The binding is restored when the queue is asserted again after a reconnect.
25
27
  * @param queue - Queue to bind to this exchange.
26
28
  * @param pattern - Routing key pattern under which to bind.
27
29
  * @param args - Optional AMQP headers or x-arguments for the binding.
@@ -37,25 +39,28 @@ export interface Exchange {
37
39
  bindExchange(exchange: Exchange, pattern: string, args?: BindingArgs): Promise<Exchange>;
38
40
  /**
39
41
  * Creates a consumer for the current exchange.
40
- * Internally asserts an exclusive queue and binds it to the exchange.
41
- * Unless specified otherwise, uses the same channel as was used to declare the exchange.
42
- * This is generally not recommended, as each consumer should have its own dedicated channel.
43
- * Consider using `connection.createConsumerForExchange` instead.
42
+ * The consumer reads from a temporary exclusive queue bound to the exchange under `options.pattern`
43
+ * and keeps receiving messages after a reconnect.
44
+ * By default, creates a new dedicated channel for the consumer.
45
+ * Pass a channel in `options` to use a specific one instead.
44
46
  */
45
47
  createConsumer<T>(options: ExchangeConsumerOptions<T>, queueOptions?: ExchangeConsumerQueueOptions): Promise<Consumer<T>>;
46
48
  /**
47
49
  * Creates a batch consumer for the current exchange.
48
- * Internally asserts an exclusive queue and binds it to the exchange.
49
- * Unless specified otherwise, uses the same channel as was used to declare the exchange.
50
- * This is generally not recommended, as each consumer should have its own dedicated channel.
51
- * Consider using `connection.createBatchConsumerForExchange` instead.
50
+ * The consumer reads from a temporary exclusive queue bound to the exchange under `options.pattern`
51
+ * and keeps receiving messages after a reconnect.
52
+ * By default, creates a new dedicated channel for the consumer.
53
+ * Pass a channel in `options` to use a specific one instead.
52
54
  */
53
55
  createBatchConsumer<T>(options: ExchangeBatchConsumerOptions<T>, queueOptions?: ExchangeConsumerQueueOptions): Promise<BatchConsumer<T>>;
54
56
  /**
55
57
  * Creates a producer for the current exchange.
56
- * Unless specified otherwise, uses the same channel as was used to declare the exchange.
57
- * This is generally not recommended, as each producer should have its own dedicated channel.
58
- * Consider using `connection.createProducerForExchange` instead.
58
+ * By default, creates a new dedicated channel for the producer.
59
+ * Pass a channel in `options` to use a specific one instead.
60
+ *
61
+ * @param options - Optional producer options (serialization, routing key, hooks, …).
62
+ * Set `options.isConfirmed` to `true` to open the dedicated channel in confirm mode.
63
+ * Has no effect when `options.channel` is supplied.
59
64
  */
60
65
  createProducer<T>(options?: ProducerOptions<T> & {
61
66
  isConfirmed?: boolean;
@@ -1,7 +1,6 @@
1
1
  import * as amqp from 'amqplib';
2
2
  import { AssertionMode } from '../types';
3
3
  import { ConsumerOptions, BatchConsumerOptions } from '../consumer';
4
- import { Queue } from '../queue';
5
4
  import { Exchange } from './exchange';
6
5
  /**
7
6
  * The type of exchange to create. Accepted values: `'direct'`, `'fanout'`, `'topic'`, `'headers'`, `'match'`.
@@ -31,21 +30,13 @@ export type ExchangeConsumerOptions<T> = ExchangeConsumerBindingOptions & Consum
31
30
  */
32
31
  export type ExchangeBatchConsumerOptions<T> = ExchangeConsumerBindingOptions & BatchConsumerOptions<T>;
33
32
  export type BindingArgs = Record<string, string | number | boolean>;
34
- export declare enum BindingType {
35
- queue = "queue",
36
- exchange = "exchange"
37
- }
38
33
  type Pattern = string;
39
- export type Binding = {
34
+ /** A binding of another exchange to this exchange. */
35
+ export type ExchangeBinding = {
36
+ exchange: Exchange;
40
37
  pattern: Pattern;
41
38
  args?: BindingArgs;
42
- } & ({
43
- type: BindingType.queue;
44
- queue: Queue;
45
- } | {
46
- type: BindingType.exchange;
47
- exchange: Exchange;
48
- });
39
+ };
49
40
  export type ExchangeOptions = amqp.Options.AssertExchange & {
50
41
  /**
51
42
  * Controls how the exchange is verified against the broker when `assert()` is called.
@@ -1,5 +1 @@
1
- export var BindingType;
2
- (function (BindingType) {
3
- BindingType["queue"] = "queue";
4
- BindingType["exchange"] = "exchange";
5
- })(BindingType || (BindingType = {}));
1
+ export {};
@@ -1,49 +1,54 @@
1
1
  import { Exchange } from './exchange';
2
2
  import { Channel } from './channel';
3
3
  /**
4
- * This will create a direct exchange that is created by default by RabbitMQ container.
4
+ * Returns the direct exchange that RabbitMQ creates by default in every virtual host.
5
5
  *
6
- * The direct exchange has name *amqp.direct*, is durable, and has type *direct*.
6
+ * The exchange has name *amq.direct*, is durable, and has type *direct*.
7
+ * The exchange always exists on the broker, so it is never declared.
7
8
  *
8
- * @param channel The channel that should assert the exchange
9
- * @returns The exchange that was created
9
+ * @param channel The channel the exchange belongs to
10
+ * @returns The predefined exchange
10
11
  */
11
12
  export declare const directExchange: (channel: Channel) => Exchange;
12
13
  /**
13
- * This will create a fanout exchange that is created by default by RabbitMQ container.
14
+ * Returns the fanout exchange that RabbitMQ creates by default in every virtual host.
14
15
  *
15
- * The fanout exchange has name *amqp.fanout*, is durable, and has type *fanout*.
16
+ * The exchange has name *amq.fanout*, is durable, and has type *fanout*.
17
+ * The exchange always exists on the broker, so it is never declared.
16
18
  *
17
- * @param channel The channel that should assert the exchange
18
- * @returns The exchange that was created
19
+ * @param channel The channel the exchange belongs to
20
+ * @returns The predefined exchange
19
21
  */
20
22
  export declare const fanoutExchange: (channel: Channel) => Exchange;
21
23
  /**
22
- * This will create a headers exchange that is created by default by RabbitMQ container.
24
+ * Returns the headers exchange that RabbitMQ creates by default in every virtual host.
23
25
  *
24
- * The headers exchange has name *amqp.headers*, is durable, and has type *headers*.
26
+ * The exchange has name *amq.headers*, is durable, and has type *headers*.
27
+ * The exchange always exists on the broker, so it is never declared.
25
28
  *
26
- * @param channel The channel that should assert the exchange
27
- * @returns The exchange that was created
29
+ * @param channel The channel the exchange belongs to
30
+ * @returns The predefined exchange
28
31
  */
29
32
  export declare const headersExchange: (channel: Channel) => Exchange;
30
33
  /**
31
- * This will create a match exchange that is created by default by RabbitMQ container.
32
- * Behaves the same as headers exchange, but has a different name.
34
+ * Returns the match exchange that RabbitMQ creates by default in every virtual host.
35
+ * Behaves the same as {@link headersExchange}, but has a different name.
33
36
  *
34
- * The match exchange has name *amqp.match*, is durable, and has type *match*.
37
+ * The exchange has name *amq.match*, is durable, and has type *headers*.
38
+ * The exchange always exists on the broker, so it is never declared.
35
39
  *
36
- * @param channel The channel that should assert the exchange
37
- * @returns The exchange that was created
40
+ * @param channel The channel the exchange belongs to
41
+ * @returns The predefined exchange
38
42
  */
39
43
  export declare const matchExchange: (channel: Channel) => Exchange;
40
44
  /**
41
- * This will create a topic exchange that is created by default by RabbitMQ container.
45
+ * Returns the topic exchange that RabbitMQ creates by default in every virtual host.
42
46
  *
43
- * The topic exchange has name *amqp.topic*, is durable, and has type *topic*.
47
+ * The exchange has name *amq.topic*, is durable, and has type *topic*.
48
+ * The exchange always exists on the broker, so it is never declared.
44
49
  *
45
- * @param channel The channel that should assert the exchange
46
- * @returns The exchange that was created
50
+ * @param channel The channel the exchange belongs to
51
+ * @returns The predefined exchange
47
52
  */
48
53
  export declare const topicExchange: (channel: Channel) => Exchange;
49
54
  /**
@@ -1,18 +1,24 @@
1
1
  import { ExchangeImplementation } from './exchange';
2
2
  import { DefaultExchange } from './exchange/default-exchange';
3
- export const directExchange = (channel) => new ExchangeImplementation(channel, 'amqp.direct', 'direct', {
3
+ import { AssertionMode } from './types';
4
+ export const directExchange = (channel) => new ExchangeImplementation(channel, 'amq.direct', 'direct', {
4
5
  durable: true,
6
+ assertionMode: AssertionMode.Passive,
5
7
  });
6
- export const fanoutExchange = (channel) => new ExchangeImplementation(channel, 'amqp.fanout', 'fanout', {
8
+ export const fanoutExchange = (channel) => new ExchangeImplementation(channel, 'amq.fanout', 'fanout', {
7
9
  durable: true,
10
+ assertionMode: AssertionMode.Passive,
8
11
  });
9
- export const headersExchange = (channel) => new ExchangeImplementation(channel, 'amqp.headers', 'headers', {
12
+ export const headersExchange = (channel) => new ExchangeImplementation(channel, 'amq.headers', 'headers', {
10
13
  durable: true,
14
+ assertionMode: AssertionMode.Passive,
11
15
  });
12
- export const matchExchange = (channel) => new ExchangeImplementation(channel, 'amqp.match', 'match', {
16
+ export const matchExchange = (channel) => new ExchangeImplementation(channel, 'amq.match', 'headers', {
13
17
  durable: true,
18
+ assertionMode: AssertionMode.Passive,
14
19
  });
15
- export const topicExchange = (channel) => new ExchangeImplementation(channel, 'amqp.topic', 'topic', {
20
+ export const topicExchange = (channel) => new ExchangeImplementation(channel, 'amq.topic', 'topic', {
16
21
  durable: true,
22
+ assertionMode: AssertionMode.Passive,
17
23
  });
18
24
  export const defaultExchange = (channel) => new DefaultExchange(channel);
@@ -9,10 +9,18 @@ export declare class QueueImplementation implements Queue {
9
9
  private readonly queueName;
10
10
  private readonly options?;
11
11
  private assertPromise;
12
+ /** Queue name resolved by the last assert; null until then and after the channel closes. */
13
+ private declaredName;
14
+ private bindings;
12
15
  constructor(channel: Channel, queueName: string, options?: QueueOptions);
13
16
  assert(): Promise<this>;
14
17
  name(): Promise<string>;
15
18
  bindExchange(exchange: Exchange, pattern: string, args?: BindingArgs): Promise<this>;
19
+ /** Creates the binding on the broker; the queue must already be asserted. */
20
+ private nativeBind;
21
+ private currentName;
22
+ /** Re-applies the remembered bindings after the queue is asserted. */
23
+ private rebind;
16
24
  createConsumer<T>(options?: ConsumerOptions<T>): Promise<Consumer<T>>;
17
25
  createBatchConsumer<T>(options?: BatchConsumerOptions<T>): Promise<BatchConsumer<T>>;
18
26
  createProducer<T>(options?: ProducerOptions<T> & {
@@ -8,10 +8,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import { debuglog } from 'util';
11
+ import stableJsonStringify from 'safe-stable-stringify';
11
12
  import { ConsumerImplementation, BatchConsumerImplementation } from '../consumer';
12
13
  import { ProducerImplementation } from '../producer';
13
14
  import { predefined } from '../index';
14
- import { deepMerge, LIB_NAME } from '../utils';
15
+ import { deepMerge, errToMessage, LIB_NAME } from '../utils';
15
16
  import { AssertionMode } from '../types';
16
17
  const debug = debuglog(`${LIB_NAME}:queue`);
17
18
  const DEFAULT_QUEUE_OPTIONS = {
@@ -22,10 +23,13 @@ export class QueueImplementation {
22
23
  this.channel = channel;
23
24
  this.queueName = queueName;
24
25
  this.assertPromise = null;
26
+ this.declaredName = null;
27
+ this.bindings = [];
25
28
  this.options = deepMerge({}, DEFAULT_QUEUE_OPTIONS, options !== null && options !== void 0 ? options : {});
26
29
  this.channel.on('close', () => {
27
30
  debug('native-close');
28
31
  this.assertPromise = null;
32
+ this.declaredName = null;
29
33
  });
30
34
  }
31
35
  assert() {
@@ -34,43 +38,88 @@ export class QueueImplementation {
34
38
  yield this.assertPromise;
35
39
  return this;
36
40
  }
37
- this.assertPromise = (() => __awaiter(this, void 0, void 0, function* () {
41
+ const promise = (() => __awaiter(this, void 0, void 0, function* () {
38
42
  var _a, _b, _c;
39
43
  debug('assert name=%s mode=%s', this.queueName, (_a = this.options) === null || _a === void 0 ? void 0 : _a.assertionMode);
40
44
  const channel = yield this.channel.native();
41
45
  switch ((_b = this.options) === null || _b === void 0 ? void 0 : _b.assertionMode) {
42
46
  case AssertionMode.Check:
43
47
  yield channel.checkQueue(this.queueName);
48
+ this.declaredName = this.queueName;
44
49
  debug('native-check-done name=%s', this.queueName);
45
50
  break;
46
- case AssertionMode.Assert:
47
- yield channel.assertQueue(this.queueName, this.options);
48
- debug('native-assert-done name=%s', this.queueName);
51
+ case AssertionMode.Assert: {
52
+ const reply = yield channel.assertQueue(this.queueName, this.options);
53
+ this.declaredName = reply.queue;
54
+ debug('native-assert-done name=%s declared-name=%s', this.queueName, this.declaredName);
49
55
  break;
56
+ }
50
57
  case AssertionMode.Passive:
58
+ this.declaredName = this.queueName;
51
59
  debug('no-assert name=%s mode=%s', this.queueName, this.options.assertionMode);
52
60
  break;
53
61
  default:
54
62
  throw new Error(`Unknown assertion mode: ${(_c = this.options) === null || _c === void 0 ? void 0 : _c.assertionMode}`);
55
63
  }
56
- }))();
57
- yield this.assertPromise;
58
- debug('assert-complete name=%s', this.queueName);
64
+ yield this.rebind();
65
+ debug('assert-complete name=%s', this.currentName());
66
+ }))().catch((error) => {
67
+ if (this.assertPromise === promise) {
68
+ this.assertPromise = null;
69
+ this.declaredName = null;
70
+ }
71
+ throw error;
72
+ });
73
+ this.assertPromise = promise;
74
+ yield promise;
59
75
  return this;
60
76
  });
61
77
  }
62
78
  name() {
63
79
  return __awaiter(this, void 0, void 0, function* () {
64
- return this.assert().then(self => self.queueName);
80
+ return this.assert().then(self => self.currentName());
65
81
  });
66
82
  }
67
83
  bindExchange(exchange, pattern, args) {
68
84
  return __awaiter(this, void 0, void 0, function* () {
69
- debug('bind-exchange queue=%s pattern=%s', this.queueName, pattern);
70
- yield exchange.bindQueue(this, pattern, args);
85
+ yield this.assert();
86
+ yield this.nativeBind({ exchange, pattern, args });
87
+ const isSameBinding = (binding) => binding.exchange === exchange &&
88
+ binding.pattern === pattern &&
89
+ stableJsonStringify(binding.args) === stableJsonStringify(args);
90
+ if (this.bindings.every(binding => !isSameBinding(binding)))
91
+ this.bindings.push({ exchange, pattern, args });
71
92
  return this;
72
93
  });
73
94
  }
95
+ nativeBind(_a) {
96
+ return __awaiter(this, arguments, void 0, function* ({ exchange, pattern, args }) {
97
+ const exchangeName = yield exchange.name();
98
+ const queueName = this.currentName();
99
+ debug('bind-exchange queue=%s exchange=%s pattern=%s', queueName, exchangeName, pattern);
100
+ const channel = yield this.channel.native();
101
+ yield channel.bindQueue(queueName, exchangeName, pattern, args);
102
+ debug('bind-exchange-done queue=%s exchange=%s pattern=%s', queueName, exchangeName, pattern);
103
+ });
104
+ }
105
+ currentName() {
106
+ var _a;
107
+ return (_a = this.declaredName) !== null && _a !== void 0 ? _a : this.queueName;
108
+ }
109
+ rebind() {
110
+ return __awaiter(this, void 0, void 0, function* () {
111
+ if (this.bindings.length === 0)
112
+ return;
113
+ debug('rebind name=%s bindings=%d', this.currentName(), this.bindings.length);
114
+ const results = yield Promise.allSettled(this.bindings.map(binding => this.nativeBind(binding)));
115
+ const failed = results.find(result => result.status === 'rejected');
116
+ if (failed) {
117
+ debug('rebind-failed error=%s', errToMessage(failed.reason));
118
+ throw failed.reason;
119
+ }
120
+ debug('rebind-complete name=%s', this.currentName());
121
+ });
122
+ }
74
123
  createConsumer(options = {}) {
75
124
  var _a;
76
125
  return Promise.resolve(new ConsumerImplementation((_a = options.channel) !== null && _a !== void 0 ? _a : this.channel.connection.createChannel(), this, options));
@@ -21,7 +21,9 @@ export interface Queue {
21
21
  name(): Promise<string>;
22
22
  /**
23
23
  * Bind this queue to an exchange.
24
- * Asserts the queue first if it has not been asserted yet.
24
+ * Asserts the queue and the exchange first if they have not been asserted yet.
25
+ * Equivalent to `exchange.bindQueue(queue, pattern, args)`.
26
+ * The binding is restored when the queue is asserted again after a reconnect.
25
27
  * @param exchange - Exchange to bind this queue to.
26
28
  * @param pattern - Routing key pattern under which to bind.
27
29
  * @param args - Optional AMQP headers or x-arguments for the binding.
@@ -44,6 +46,10 @@ export interface Queue {
44
46
  * Publishes via the default RabbitMQ exchange, routing messages directly to this queue by name.
45
47
  * By default, creates a new dedicated channel for the producer.
46
48
  * Pass a channel in `options` to use a specific one instead.
49
+ *
50
+ * @param options - Optional producer options (serialization, routing key, hooks, …).
51
+ * Set `options.isConfirmed` to `true` to open the dedicated channel in confirm mode.
52
+ * Has no effect when `options.channel` is supplied.
47
53
  */
48
54
  createProducer<T>(options?: ProducerOptions<T> & {
49
55
  isConfirmed?: boolean;
@@ -15,11 +15,11 @@ export type QueueOptions = amqp.Options.AssertQueue & {
15
15
  */
16
16
  assertionMode?: AssertionMode;
17
17
  };
18
- type ExchangeName = string;
19
18
  type Pattern = string;
20
- export type Bindings = Record<ExchangeName, Record<Pattern, {
19
+ /** A binding of a queue to an exchange. */
20
+ export type QueueBinding = {
21
21
  exchange: Exchange;
22
22
  pattern: Pattern;
23
23
  args?: BindingArgs;
24
- }>>;
24
+ };
25
25
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "amqpx",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "Wrapper around amqplib with connection retries, topology definition, batching, and validation",
5
5
  "homepage": "https://patrikvalkovic.github.io/amqpx/",
6
6
  "main": "dist/cjs/index.js",
@@ -111,5 +111,10 @@
111
111
  "vitepress": "^1.6.4",
112
112
  "vitest": "^4.1.3",
113
113
  "zod": "^4.3.6"
114
+ },
115
+ "allowScripts": {
116
+ "fsevents@2.3.3": true,
117
+ "unrs-resolver@1.11.1": true,
118
+ "esbuild@0.21.5": true
114
119
  }
115
120
  }