amqpx 1.0.1 → 1.0.3

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 (40) hide show
  1. package/README.md +3 -3
  2. package/dist/cjs/connection/connection-implementation.d.ts +1 -1
  3. package/dist/cjs/exchange/exchange-implementation.d.ts +1 -0
  4. package/dist/cjs/exchange/exchange-implementation.js +6 -29
  5. package/dist/cjs/exchange/exchange.d.ts +13 -12
  6. package/dist/cjs/exchange/types.d.ts +4 -13
  7. package/dist/cjs/exchange/types.js +0 -6
  8. package/dist/cjs/extensions/vitest/test-batch-consumer.d.ts +3 -2
  9. package/dist/cjs/extensions/vitest/test-channel.d.ts +17 -26
  10. package/dist/cjs/extensions/vitest/test-connection.d.ts +12 -11
  11. package/dist/cjs/extensions/vitest/test-consumer.d.ts +4 -3
  12. package/dist/cjs/extensions/vitest/test-exchange.d.ts +8 -7
  13. package/dist/cjs/extensions/vitest/test-producer.d.ts +3 -2
  14. package/dist/cjs/extensions/vitest/test-queue.d.ts +7 -6
  15. package/dist/cjs/predefined.d.ts +26 -21
  16. package/dist/cjs/predefined.js +11 -5
  17. package/dist/cjs/queue/queue-implementation.d.ts +8 -0
  18. package/dist/cjs/queue/queue-implementation.js +62 -10
  19. package/dist/cjs/queue/queue.d.ts +3 -1
  20. package/dist/cjs/queue/types.d.ts +3 -3
  21. package/dist/esm/connection/connection-implementation.d.ts +1 -1
  22. package/dist/esm/exchange/exchange-implementation.d.ts +1 -0
  23. package/dist/esm/exchange/exchange-implementation.js +6 -29
  24. package/dist/esm/exchange/exchange.d.ts +13 -12
  25. package/dist/esm/exchange/types.d.ts +4 -13
  26. package/dist/esm/exchange/types.js +1 -5
  27. package/dist/esm/extensions/vitest/test-batch-consumer.d.ts +3 -2
  28. package/dist/esm/extensions/vitest/test-channel.d.ts +17 -26
  29. package/dist/esm/extensions/vitest/test-connection.d.ts +12 -11
  30. package/dist/esm/extensions/vitest/test-consumer.d.ts +4 -3
  31. package/dist/esm/extensions/vitest/test-exchange.d.ts +8 -7
  32. package/dist/esm/extensions/vitest/test-producer.d.ts +3 -2
  33. package/dist/esm/extensions/vitest/test-queue.d.ts +7 -6
  34. package/dist/esm/predefined.d.ts +26 -21
  35. package/dist/esm/predefined.js +11 -5
  36. package/dist/esm/queue/queue-implementation.d.ts +8 -0
  37. package/dist/esm/queue/queue-implementation.js +60 -11
  38. package/dist/esm/queue/queue.d.ts +3 -1
  39. package/dist/esm/queue/types.d.ts +3 -3
  40. package/package.json +19 -14
@@ -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.
@@ -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.1",
3
+ "version": "1.0.3",
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",
@@ -77,8 +77,8 @@
77
77
  "author": "Patrik Valkovic",
78
78
  "license": "MIT",
79
79
  "dependencies": {
80
- "amqplib": "^1.0.3",
81
- "safe-stable-stringify": "^2.5.0"
80
+ "amqplib": "^2.4.2",
81
+ "safe-stable-stringify": "^3.0.1"
82
82
  },
83
83
  "peerDependencies": {
84
84
  "jest": "x.x.x",
@@ -98,18 +98,23 @@
98
98
  },
99
99
  "devDependencies": {
100
100
  "@patrikvalkovic/eslint-config": "^1.0.9",
101
- "@types/amqplib": "^0.10.8",
102
101
  "@types/jest": "^30.0.0",
103
- "@vitest/coverage-v8": "^4.1.3",
104
- "csv-parse": "^6.2.1",
105
- "eslint": "^9.39.4",
106
- "jest": "^30.3.0",
107
- "rimraf": "^6.1.3",
108
- "typedoc": "^0.28.19",
109
- "typedoc-plugin-markdown": "^4.11.0",
110
- "typescript": "^6.0.2",
102
+ "@types/node": "^26.6.11",
103
+ "@vitest/coverage-v8": "^5.1.2",
104
+ "csv-parse": "^7.0.3",
105
+ "eslint": "^9.39.5",
106
+ "jest": "^30.5.2",
107
+ "rimraf": "^6.1.4",
108
+ "typedoc": "^0.28.20",
109
+ "typedoc-plugin-markdown": "^4.14.1",
110
+ "typescript": "^6.0.3",
111
111
  "vitepress": "^1.6.4",
112
- "vitest": "^4.1.3",
113
- "zod": "^4.3.6"
112
+ "vitest": "^5.1.2",
113
+ "zod": "^4.6.7"
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
  }