@relaymesh/relaybus-amqp 0.0.6 → 0.0.8

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 (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.ts +38 -4
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@relaymesh/relaybus-amqp",
3
- "version": "0.0.6",
3
+ "version": "0.0.8",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "scripts": {
package/src/index.ts CHANGED
@@ -28,6 +28,11 @@ export type AmqpChannel = {
28
28
  type: string,
29
29
  options?: { durable?: boolean; autoDelete?: boolean; internal?: boolean }
30
30
  ) => Promise<unknown> | unknown;
31
+ bindQueue?: (
32
+ queue: string,
33
+ exchange: string,
34
+ routingKey: string
35
+ ) => Promise<unknown> | unknown;
31
36
  };
32
37
 
33
38
  export type AmqpSubscriberConfig = {
@@ -40,6 +45,7 @@ export type AmqpPublisherConfig = {
40
45
  routingKeyTemplate?: string;
41
46
  exchangeType?: string;
42
47
  queue?: string;
48
+ queueOptions?: { durable?: boolean; autoDelete?: boolean; exclusive?: boolean };
43
49
  };
44
50
 
45
51
  export type AmqpPublisherConnectConfig = {
@@ -48,6 +54,7 @@ export type AmqpPublisherConnectConfig = {
48
54
  routingKeyTemplate?: string;
49
55
  exchangeType?: string;
50
56
  queue?: string;
57
+ queueOptions?: { durable?: boolean; autoDelete?: boolean; exclusive?: boolean };
51
58
  };
52
59
 
53
60
  export type AmqpSubscriberConnectConfig = {
@@ -57,6 +64,7 @@ export type AmqpSubscriberConnectConfig = {
57
64
  exchangeType?: string;
58
65
  routingKeyTemplate?: string;
59
66
  queue?: string;
67
+ queueOptions?: { durable?: boolean; autoDelete?: boolean; exclusive?: boolean };
60
68
  };
61
69
 
62
70
  export class AmqpSubscriber {
@@ -67,6 +75,7 @@ export class AmqpSubscriber {
67
75
  private exchangeType?: string;
68
76
  private routingKeyTemplate?: string;
69
77
  private queue?: string;
78
+ private queueOptions?: { durable?: boolean; autoDelete?: boolean; exclusive?: boolean };
70
79
 
71
80
  constructor(config: AmqpSubscriberConfig) {
72
81
  this.onMessage = config.onMessage;
@@ -82,6 +91,7 @@ export class AmqpSubscriber {
82
91
  subscriber.exchangeType = config.exchangeType ?? "topic";
83
92
  subscriber.routingKeyTemplate = config.routingKeyTemplate ?? "{topic}";
84
93
  subscriber.queue = config.queue;
94
+ subscriber.queueOptions = config.queueOptions;
85
95
  return subscriber;
86
96
  }
87
97
 
@@ -101,7 +111,7 @@ export class AmqpSubscriber {
101
111
  });
102
112
  }
103
113
  const queueName = this.queue ?? topic;
104
- await this.channel.assertQueue(queueName, { durable: false, autoDelete: true });
114
+ await this.channel.assertQueue(queueName, normalizeQueueOptions(this.queueOptions));
105
115
  if (this.exchange) {
106
116
  const key = buildRoutingKey(this.routingKeyTemplate ?? "{topic}", topic);
107
117
  await this.channel.bindQueue(queueName, this.exchange, key);
@@ -140,8 +150,10 @@ export class AmqpPublisher {
140
150
  private readonly routingKeyTemplate: string;
141
151
  private readonly exchangeType: string;
142
152
  private readonly queue?: string;
153
+ private readonly queueOptions?: { durable?: boolean; autoDelete?: boolean; exclusive?: boolean };
143
154
  private readonly ensuredQueues = new Set<string>();
144
155
  private readonly ensuredExchanges = new Set<string>();
156
+ private readonly ensuredBindings = new Set<string>();
145
157
  private connection?: ChannelModel;
146
158
  private confirmChannel?: ConfirmChannel;
147
159
 
@@ -151,6 +163,7 @@ export class AmqpPublisher {
151
163
  this.routingKeyTemplate = config.routingKeyTemplate ?? "{topic}";
152
164
  this.exchangeType = config.exchangeType ?? "topic";
153
165
  this.queue = config.queue;
166
+ this.queueOptions = config.queueOptions;
154
167
  }
155
168
 
156
169
  static async connect(config: AmqpPublisherConnectConfig): Promise<AmqpPublisher> {
@@ -170,12 +183,14 @@ export class AmqpPublisher {
170
183
  });
171
184
  },
172
185
  assertQueue: (queue, options) => channel.assertQueue(queue, options),
173
- assertExchange: (exchange, type, options) => channel.assertExchange(exchange, type, options)
186
+ assertExchange: (exchange, type, options) => channel.assertExchange(exchange, type, options),
187
+ bindQueue: (queue, exchange, routingKey) => channel.bindQueue(queue, exchange, routingKey)
174
188
  },
175
189
  exchange: config.exchange,
176
190
  routingKeyTemplate: config.routingKeyTemplate,
177
191
  exchangeType: config.exchangeType,
178
- queue: config.queue
192
+ queue: config.queue,
193
+ queueOptions: config.queueOptions
179
194
  });
180
195
  publisher.connection = connection;
181
196
  publisher.confirmChannel = channel;
@@ -222,11 +237,20 @@ export class AmqpPublisher {
222
237
  if (queueName && this.channel.assertQueue) {
223
238
  if (!this.ensuredQueues.has(queueName)) {
224
239
  await Promise.resolve(
225
- this.channel.assertQueue(queueName, { durable: false, autoDelete: true })
240
+ this.channel.assertQueue(queueName, normalizeQueueOptions(this.queueOptions))
226
241
  );
227
242
  this.ensuredQueues.add(queueName);
228
243
  }
229
244
  }
245
+
246
+ if (this.exchange && queueName && this.channel.bindQueue) {
247
+ const routingKey = buildRoutingKey(this.routingKeyTemplate, topic);
248
+ const bindingKey = `${queueName}::${this.exchange}::${routingKey}`;
249
+ if (!this.ensuredBindings.has(bindingKey)) {
250
+ await Promise.resolve(this.channel.bindQueue(queueName, this.exchange, routingKey));
251
+ this.ensuredBindings.add(bindingKey);
252
+ }
253
+ }
230
254
  }
231
255
  }
232
256
 
@@ -250,3 +274,13 @@ function buildRoutingKey(template: string, topic: string): string {
250
274
  }
251
275
  return template;
252
276
  }
277
+
278
+ function normalizeQueueOptions(
279
+ options?: { durable?: boolean; autoDelete?: boolean; exclusive?: boolean }
280
+ ): { durable: boolean; autoDelete: boolean; exclusive: boolean } {
281
+ return {
282
+ durable: options?.durable ?? false,
283
+ autoDelete: options?.autoDelete ?? false,
284
+ exclusive: options?.exclusive ?? false
285
+ };
286
+ }