runmq 1.4.2 → 1.4.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.
package/dist/index.js CHANGED
@@ -27,8 +27,8 @@ var RunMQException = class extends Error {
27
27
  }
28
28
  };
29
29
 
30
- // src/core/clients/AmqplibClientAdapter.ts
31
- import amqplib from "amqplib";
30
+ // src/core/clients/RabbitMQClientAdapter.ts
31
+ import { Connection } from "rabbitmq-client";
32
32
 
33
33
  // src/core/exceptions/Exceptions.ts
34
34
  var Exceptions = class {
@@ -40,8 +40,8 @@ Exceptions.INVALID_MESSAGE_FORMAT = "MESSAGE_SHOULD_BE_VALID_RECORD";
40
40
  Exceptions.UNSUPPORTED_SCHEMA = "UNSUPPORTED_SCHEMA";
41
41
  Exceptions.FAILURE_TO_DEFINE_TTL_POLICY = "FAILURE_TO_DEFINE_TTL_POLICY";
42
42
 
43
- // src/core/clients/AmqplibChannel.ts
44
- var AmqplibChannel = class {
43
+ // src/core/clients/RabbitMQClientChannel.ts
44
+ var RabbitMQClientChannel = class {
45
45
  constructor(channel) {
46
46
  this.channel = channel;
47
47
  }
@@ -51,7 +51,8 @@ var AmqplibChannel = class {
51
51
  if (options == null ? void 0 : options.deadLetterRoutingKey) args["x-dead-letter-routing-key"] = options.deadLetterRoutingKey;
52
52
  if (options == null ? void 0 : options.messageTtl) args["x-message-ttl"] = options.messageTtl;
53
53
  if (options == null ? void 0 : options.arguments) Object.assign(args, options.arguments);
54
- const result = await this.channel.assertQueue(queue, {
54
+ const result = await this.channel.queueDeclare({
55
+ queue,
55
56
  durable: options == null ? void 0 : options.durable,
56
57
  exclusive: options == null ? void 0 : options.exclusive,
57
58
  autoDelete: options == null ? void 0 : options.autoDelete,
@@ -64,7 +65,10 @@ var AmqplibChannel = class {
64
65
  };
65
66
  }
66
67
  async checkQueue(queue) {
67
- const result = await this.channel.checkQueue(queue);
68
+ const result = await this.channel.queueDeclare({
69
+ queue,
70
+ passive: true
71
+ });
68
72
  return {
69
73
  queue: result.queue,
70
74
  messageCount: result.messageCount,
@@ -72,7 +76,8 @@ var AmqplibChannel = class {
72
76
  };
73
77
  }
74
78
  async deleteQueue(queue, options) {
75
- const result = await this.channel.deleteQueue(queue, {
79
+ const result = await this.channel.queueDelete({
80
+ queue,
76
81
  ifUnused: options == null ? void 0 : options.ifUnused,
77
82
  ifEmpty: options == null ? void 0 : options.ifEmpty
78
83
  });
@@ -84,7 +89,9 @@ var AmqplibChannel = class {
84
89
  const args = {};
85
90
  if (options == null ? void 0 : options.alternateExchange) args["alternate-exchange"] = options.alternateExchange;
86
91
  if (options == null ? void 0 : options.arguments) Object.assign(args, options.arguments);
87
- await this.channel.assertExchange(exchange, type, {
92
+ await this.channel.exchangeDeclare({
93
+ exchange,
94
+ type,
88
95
  durable: options == null ? void 0 : options.durable,
89
96
  internal: options == null ? void 0 : options.internal,
90
97
  autoDelete: options == null ? void 0 : options.autoDelete,
@@ -95,26 +102,37 @@ var AmqplibChannel = class {
95
102
  };
96
103
  }
97
104
  async checkExchange(exchange) {
98
- await this.channel.checkExchange(exchange);
105
+ await this.channel.exchangeDeclare({
106
+ exchange,
107
+ passive: true
108
+ });
99
109
  return {
100
110
  exchange
101
111
  };
102
112
  }
103
113
  async deleteExchange(exchange, options) {
104
- await this.channel.deleteExchange(exchange, {
114
+ await this.channel.exchangeDelete({
115
+ exchange,
105
116
  ifUnused: options == null ? void 0 : options.ifUnused
106
117
  });
107
118
  }
108
119
  async bindQueue(queue, source, pattern, args) {
109
- await this.channel.bindQueue(queue, source, pattern, args);
120
+ await this.channel.queueBind({
121
+ queue,
122
+ exchange: source,
123
+ routingKey: pattern,
124
+ arguments: args
125
+ });
110
126
  }
111
127
  publish(exchange, routingKey, content, options) {
112
128
  var _a2;
113
- return this.channel.publish(exchange, routingKey, content, {
129
+ this.channel.basicPublish({
130
+ exchange,
131
+ routingKey,
114
132
  correlationId: options == null ? void 0 : options.correlationId,
115
133
  messageId: options == null ? void 0 : options.messageId,
116
134
  headers: options == null ? void 0 : options.headers,
117
- persistent: options == null ? void 0 : options.persistent,
135
+ durable: options == null ? void 0 : options.persistent,
118
136
  expiration: (_a2 = options == null ? void 0 : options.expiration) == null ? void 0 : _a2.toString(),
119
137
  contentType: options == null ? void 0 : options.contentType,
120
138
  contentEncoding: options == null ? void 0 : options.contentEncoding,
@@ -124,67 +142,69 @@ var AmqplibChannel = class {
124
142
  type: options == null ? void 0 : options.type,
125
143
  userId: options == null ? void 0 : options.userId,
126
144
  appId: options == null ? void 0 : options.appId
127
- });
145
+ }, content);
146
+ return true;
128
147
  }
129
148
  async consume(queue, onMessage, options) {
130
- const result = await this.channel.consume(queue, (msg) => {
131
- if (msg === null) {
132
- onMessage(null);
133
- return;
134
- }
149
+ const result = await this.channel.basicConsume({
150
+ queue,
151
+ consumerTag: options == null ? void 0 : options.consumerTag,
152
+ noLocal: options == null ? void 0 : options.noLocal,
153
+ noAck: options == null ? void 0 : options.noAck,
154
+ exclusive: options == null ? void 0 : options.exclusive,
155
+ arguments: options == null ? void 0 : options.arguments
156
+ }, (msg) => {
157
+ const body = msg.body;
158
+ const content = Buffer.isBuffer(body) ? body : typeof body === "string" ? Buffer.from(body) : Buffer.from(JSON.stringify(body));
135
159
  const consumeMessage = {
136
- content: msg.content,
160
+ content,
137
161
  fields: {
138
- consumerTag: msg.fields.consumerTag,
139
- deliveryTag: msg.fields.deliveryTag,
140
- redelivered: msg.fields.redelivered,
141
- exchange: msg.fields.exchange,
142
- routingKey: msg.fields.routingKey
162
+ consumerTag: msg.consumerTag,
163
+ deliveryTag: msg.deliveryTag,
164
+ redelivered: msg.redelivered,
165
+ exchange: msg.exchange,
166
+ routingKey: msg.routingKey
143
167
  },
144
168
  properties: {
145
- contentType: msg.properties.contentType || void 0,
146
- contentEncoding: msg.properties.contentEncoding || void 0,
147
- headers: msg.properties.headers || {},
148
- deliveryMode: msg.properties.deliveryMode,
149
- priority: msg.properties.priority,
150
- correlationId: msg.properties.correlationId || void 0,
151
- replyTo: msg.properties.replyTo || void 0,
152
- expiration: msg.properties.expiration || void 0,
153
- messageId: msg.properties.messageId || void 0,
154
- timestamp: msg.properties.timestamp,
155
- type: msg.properties.type || void 0,
156
- userId: msg.properties.userId || void 0,
157
- appId: msg.properties.appId || void 0
169
+ contentType: msg.contentType,
170
+ contentEncoding: msg.contentEncoding,
171
+ headers: msg.headers || {},
172
+ deliveryMode: msg.durable ? 2 : 1,
173
+ priority: msg.priority,
174
+ correlationId: msg.correlationId,
175
+ replyTo: msg.replyTo,
176
+ expiration: msg.expiration,
177
+ messageId: msg.messageId,
178
+ timestamp: msg.timestamp,
179
+ type: msg.type,
180
+ userId: msg.userId,
181
+ appId: msg.appId
158
182
  }
159
183
  };
160
184
  onMessage(consumeMessage);
161
- }, {
162
- consumerTag: options == null ? void 0 : options.consumerTag,
163
- noLocal: options == null ? void 0 : options.noLocal,
164
- noAck: options == null ? void 0 : options.noAck,
165
- exclusive: options == null ? void 0 : options.exclusive,
166
- priority: options == null ? void 0 : options.priority,
167
- arguments: options == null ? void 0 : options.arguments
168
185
  });
169
186
  return {
170
187
  consumerTag: result.consumerTag
171
188
  };
172
189
  }
173
190
  ack(message, allUpTo) {
174
- this.channel.ack(
175
- { fields: message.fields, content: message.content, properties: message.properties },
176
- allUpTo
177
- );
191
+ this.channel.basicAck({
192
+ deliveryTag: message.fields.deliveryTag,
193
+ multiple: allUpTo
194
+ });
178
195
  }
179
196
  nack(message, allUpTo, requeue) {
180
- this.channel.nack(
181
- { fields: message.fields, content: message.content, properties: message.properties },
182
- allUpTo,
197
+ this.channel.basicNack({
198
+ deliveryTag: message.fields.deliveryTag,
199
+ multiple: allUpTo,
183
200
  requeue
184
- );
201
+ });
185
202
  }
186
203
  async prefetch(count, global) {
187
- await this.channel.prefetch(count, global);
204
+ await this.channel.basicQos({
205
+ prefetchCount: count,
206
+ global
207
+ });
188
208
  }
189
209
  async close() {
190
210
  await this.channel.close();
@@ -219,8 +239,8 @@ var RunMQConsoleLogger = class {
219
239
  }
220
240
  };
221
241
 
222
- // src/core/clients/AmqplibClientAdapter.ts
223
- var AmqplibClientAdapter = class {
242
+ // src/core/clients/RabbitMQClientAdapter.ts
243
+ var RabbitMQClientAdapter = class {
224
244
  constructor(config, logger = new RunMQConsoleLogger()) {
225
245
  this.config = config;
226
246
  this.logger = logger;
@@ -239,20 +259,38 @@ var AmqplibClientAdapter = class {
239
259
  }
240
260
  this.connection = void 0;
241
261
  }
242
- const connection = await amqplib.connect(this.config.url);
243
- this.connection = connection;
262
+ this.connection = new Connection({
263
+ url: this.config.url,
264
+ // Disable automatic retries - we handle retries at RunMQ level
265
+ retryLow: 100,
266
+ retryHigh: 200,
267
+ connectionTimeout: 5e3
268
+ });
244
269
  this.connection.on("error", (err) => {
245
270
  this.logger.error("RabbitMQ connection error:", { error: err });
246
271
  this.isConnected = false;
247
272
  });
248
- this.connection.on("close", () => {
249
- this.isConnected = false;
273
+ this.connection.on("connection", () => {
274
+ this.isConnected = true;
275
+ });
276
+ this.connection.on("connection.blocked", (reason) => {
277
+ this.logger.warn("RabbitMQ connection blocked:", { reason });
250
278
  });
279
+ this.connection.on("connection.unblocked", () => {
280
+ this.logger.info("RabbitMQ connection unblocked");
281
+ });
282
+ await this.connection.onConnect(5e3, true);
251
283
  this.isConnected = true;
252
284
  return this.connection;
253
285
  } catch (error) {
254
286
  this.isConnected = false;
255
- this.connection = void 0;
287
+ if (this.connection) {
288
+ try {
289
+ this.connection.close();
290
+ } catch (e) {
291
+ }
292
+ this.connection = void 0;
293
+ }
256
294
  throw new RunMQException(
257
295
  Exceptions.CONNECTION_NOT_ESTABLISHED,
258
296
  {
@@ -263,9 +301,9 @@ var AmqplibClientAdapter = class {
263
301
  }
264
302
  async getChannel() {
265
303
  const connection = await this.connect();
266
- const rawChannel = await connection.createChannel();
304
+ const rawChannel = await connection.acquire();
267
305
  this.acquiredChannels.push(rawChannel);
268
- return new AmqplibChannel(rawChannel);
306
+ return new RabbitMQClientChannel(rawChannel);
269
307
  }
270
308
  async getDefaultChannel() {
271
309
  if (!this.defaultChannel) {
@@ -282,7 +320,9 @@ var AmqplibClientAdapter = class {
282
320
  this.acquiredChannels = [];
283
321
  for (const channel of channels) {
284
322
  try {
285
- await channel.close();
323
+ if (channel.active) {
324
+ await channel.close();
325
+ }
286
326
  } catch (e) {
287
327
  }
288
328
  }
@@ -1291,7 +1331,7 @@ var RunMQ = class _RunMQ {
1291
1331
  reconnectDelay: (_a2 = config.reconnectDelay) != null ? _a2 : DEFAULTS.RECONNECT_DELAY,
1292
1332
  maxReconnectAttempts: (_b = config.maxReconnectAttempts) != null ? _b : DEFAULTS.MAX_RECONNECT_ATTEMPTS
1293
1333
  });
1294
- this.client = new AmqplibClientAdapter(this.config, this.logger);
1334
+ this.client = new RabbitMQClientAdapter(this.config, this.logger);
1295
1335
  this.consumer = new RunMQConsumerCreator(this.client, this.logger, this.config.management);
1296
1336
  }
1297
1337
  /**