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.cjs CHANGED
@@ -60,8 +60,8 @@ var RunMQException = class extends Error {
60
60
  }
61
61
  };
62
62
 
63
- // src/core/clients/AmqplibClientAdapter.ts
64
- var import_amqplib = __toESM(require("amqplib"), 1);
63
+ // src/core/clients/RabbitMQClientAdapter.ts
64
+ var import_rabbitmq_client = require("rabbitmq-client");
65
65
 
66
66
  // src/core/exceptions/Exceptions.ts
67
67
  var Exceptions = class {
@@ -73,8 +73,8 @@ Exceptions.INVALID_MESSAGE_FORMAT = "MESSAGE_SHOULD_BE_VALID_RECORD";
73
73
  Exceptions.UNSUPPORTED_SCHEMA = "UNSUPPORTED_SCHEMA";
74
74
  Exceptions.FAILURE_TO_DEFINE_TTL_POLICY = "FAILURE_TO_DEFINE_TTL_POLICY";
75
75
 
76
- // src/core/clients/AmqplibChannel.ts
77
- var AmqplibChannel = class {
76
+ // src/core/clients/RabbitMQClientChannel.ts
77
+ var RabbitMQClientChannel = class {
78
78
  constructor(channel) {
79
79
  this.channel = channel;
80
80
  }
@@ -84,7 +84,8 @@ var AmqplibChannel = class {
84
84
  if (options == null ? void 0 : options.deadLetterRoutingKey) args["x-dead-letter-routing-key"] = options.deadLetterRoutingKey;
85
85
  if (options == null ? void 0 : options.messageTtl) args["x-message-ttl"] = options.messageTtl;
86
86
  if (options == null ? void 0 : options.arguments) Object.assign(args, options.arguments);
87
- const result = await this.channel.assertQueue(queue, {
87
+ const result = await this.channel.queueDeclare({
88
+ queue,
88
89
  durable: options == null ? void 0 : options.durable,
89
90
  exclusive: options == null ? void 0 : options.exclusive,
90
91
  autoDelete: options == null ? void 0 : options.autoDelete,
@@ -97,7 +98,10 @@ var AmqplibChannel = class {
97
98
  };
98
99
  }
99
100
  async checkQueue(queue) {
100
- const result = await this.channel.checkQueue(queue);
101
+ const result = await this.channel.queueDeclare({
102
+ queue,
103
+ passive: true
104
+ });
101
105
  return {
102
106
  queue: result.queue,
103
107
  messageCount: result.messageCount,
@@ -105,7 +109,8 @@ var AmqplibChannel = class {
105
109
  };
106
110
  }
107
111
  async deleteQueue(queue, options) {
108
- const result = await this.channel.deleteQueue(queue, {
112
+ const result = await this.channel.queueDelete({
113
+ queue,
109
114
  ifUnused: options == null ? void 0 : options.ifUnused,
110
115
  ifEmpty: options == null ? void 0 : options.ifEmpty
111
116
  });
@@ -117,7 +122,9 @@ var AmqplibChannel = class {
117
122
  const args = {};
118
123
  if (options == null ? void 0 : options.alternateExchange) args["alternate-exchange"] = options.alternateExchange;
119
124
  if (options == null ? void 0 : options.arguments) Object.assign(args, options.arguments);
120
- await this.channel.assertExchange(exchange, type, {
125
+ await this.channel.exchangeDeclare({
126
+ exchange,
127
+ type,
121
128
  durable: options == null ? void 0 : options.durable,
122
129
  internal: options == null ? void 0 : options.internal,
123
130
  autoDelete: options == null ? void 0 : options.autoDelete,
@@ -128,26 +135,37 @@ var AmqplibChannel = class {
128
135
  };
129
136
  }
130
137
  async checkExchange(exchange) {
131
- await this.channel.checkExchange(exchange);
138
+ await this.channel.exchangeDeclare({
139
+ exchange,
140
+ passive: true
141
+ });
132
142
  return {
133
143
  exchange
134
144
  };
135
145
  }
136
146
  async deleteExchange(exchange, options) {
137
- await this.channel.deleteExchange(exchange, {
147
+ await this.channel.exchangeDelete({
148
+ exchange,
138
149
  ifUnused: options == null ? void 0 : options.ifUnused
139
150
  });
140
151
  }
141
152
  async bindQueue(queue, source, pattern, args) {
142
- await this.channel.bindQueue(queue, source, pattern, args);
153
+ await this.channel.queueBind({
154
+ queue,
155
+ exchange: source,
156
+ routingKey: pattern,
157
+ arguments: args
158
+ });
143
159
  }
144
160
  publish(exchange, routingKey, content, options) {
145
161
  var _a2;
146
- return this.channel.publish(exchange, routingKey, content, {
162
+ this.channel.basicPublish({
163
+ exchange,
164
+ routingKey,
147
165
  correlationId: options == null ? void 0 : options.correlationId,
148
166
  messageId: options == null ? void 0 : options.messageId,
149
167
  headers: options == null ? void 0 : options.headers,
150
- persistent: options == null ? void 0 : options.persistent,
168
+ durable: options == null ? void 0 : options.persistent,
151
169
  expiration: (_a2 = options == null ? void 0 : options.expiration) == null ? void 0 : _a2.toString(),
152
170
  contentType: options == null ? void 0 : options.contentType,
153
171
  contentEncoding: options == null ? void 0 : options.contentEncoding,
@@ -157,67 +175,69 @@ var AmqplibChannel = class {
157
175
  type: options == null ? void 0 : options.type,
158
176
  userId: options == null ? void 0 : options.userId,
159
177
  appId: options == null ? void 0 : options.appId
160
- });
178
+ }, content);
179
+ return true;
161
180
  }
162
181
  async consume(queue, onMessage, options) {
163
- const result = await this.channel.consume(queue, (msg) => {
164
- if (msg === null) {
165
- onMessage(null);
166
- return;
167
- }
182
+ const result = await this.channel.basicConsume({
183
+ queue,
184
+ consumerTag: options == null ? void 0 : options.consumerTag,
185
+ noLocal: options == null ? void 0 : options.noLocal,
186
+ noAck: options == null ? void 0 : options.noAck,
187
+ exclusive: options == null ? void 0 : options.exclusive,
188
+ arguments: options == null ? void 0 : options.arguments
189
+ }, (msg) => {
190
+ const body = msg.body;
191
+ const content = Buffer.isBuffer(body) ? body : typeof body === "string" ? Buffer.from(body) : Buffer.from(JSON.stringify(body));
168
192
  const consumeMessage = {
169
- content: msg.content,
193
+ content,
170
194
  fields: {
171
- consumerTag: msg.fields.consumerTag,
172
- deliveryTag: msg.fields.deliveryTag,
173
- redelivered: msg.fields.redelivered,
174
- exchange: msg.fields.exchange,
175
- routingKey: msg.fields.routingKey
195
+ consumerTag: msg.consumerTag,
196
+ deliveryTag: msg.deliveryTag,
197
+ redelivered: msg.redelivered,
198
+ exchange: msg.exchange,
199
+ routingKey: msg.routingKey
176
200
  },
177
201
  properties: {
178
- contentType: msg.properties.contentType || void 0,
179
- contentEncoding: msg.properties.contentEncoding || void 0,
180
- headers: msg.properties.headers || {},
181
- deliveryMode: msg.properties.deliveryMode,
182
- priority: msg.properties.priority,
183
- correlationId: msg.properties.correlationId || void 0,
184
- replyTo: msg.properties.replyTo || void 0,
185
- expiration: msg.properties.expiration || void 0,
186
- messageId: msg.properties.messageId || void 0,
187
- timestamp: msg.properties.timestamp,
188
- type: msg.properties.type || void 0,
189
- userId: msg.properties.userId || void 0,
190
- appId: msg.properties.appId || void 0
202
+ contentType: msg.contentType,
203
+ contentEncoding: msg.contentEncoding,
204
+ headers: msg.headers || {},
205
+ deliveryMode: msg.durable ? 2 : 1,
206
+ priority: msg.priority,
207
+ correlationId: msg.correlationId,
208
+ replyTo: msg.replyTo,
209
+ expiration: msg.expiration,
210
+ messageId: msg.messageId,
211
+ timestamp: msg.timestamp,
212
+ type: msg.type,
213
+ userId: msg.userId,
214
+ appId: msg.appId
191
215
  }
192
216
  };
193
217
  onMessage(consumeMessage);
194
- }, {
195
- consumerTag: options == null ? void 0 : options.consumerTag,
196
- noLocal: options == null ? void 0 : options.noLocal,
197
- noAck: options == null ? void 0 : options.noAck,
198
- exclusive: options == null ? void 0 : options.exclusive,
199
- priority: options == null ? void 0 : options.priority,
200
- arguments: options == null ? void 0 : options.arguments
201
218
  });
202
219
  return {
203
220
  consumerTag: result.consumerTag
204
221
  };
205
222
  }
206
223
  ack(message, allUpTo) {
207
- this.channel.ack(
208
- { fields: message.fields, content: message.content, properties: message.properties },
209
- allUpTo
210
- );
224
+ this.channel.basicAck({
225
+ deliveryTag: message.fields.deliveryTag,
226
+ multiple: allUpTo
227
+ });
211
228
  }
212
229
  nack(message, allUpTo, requeue) {
213
- this.channel.nack(
214
- { fields: message.fields, content: message.content, properties: message.properties },
215
- allUpTo,
230
+ this.channel.basicNack({
231
+ deliveryTag: message.fields.deliveryTag,
232
+ multiple: allUpTo,
216
233
  requeue
217
- );
234
+ });
218
235
  }
219
236
  async prefetch(count, global) {
220
- await this.channel.prefetch(count, global);
237
+ await this.channel.basicQos({
238
+ prefetchCount: count,
239
+ global
240
+ });
221
241
  }
222
242
  async close() {
223
243
  await this.channel.close();
@@ -252,8 +272,8 @@ var RunMQConsoleLogger = class {
252
272
  }
253
273
  };
254
274
 
255
- // src/core/clients/AmqplibClientAdapter.ts
256
- var AmqplibClientAdapter = class {
275
+ // src/core/clients/RabbitMQClientAdapter.ts
276
+ var RabbitMQClientAdapter = class {
257
277
  constructor(config, logger = new RunMQConsoleLogger()) {
258
278
  this.config = config;
259
279
  this.logger = logger;
@@ -272,20 +292,38 @@ var AmqplibClientAdapter = class {
272
292
  }
273
293
  this.connection = void 0;
274
294
  }
275
- const connection = await import_amqplib.default.connect(this.config.url);
276
- this.connection = connection;
295
+ this.connection = new import_rabbitmq_client.Connection({
296
+ url: this.config.url,
297
+ // Disable automatic retries - we handle retries at RunMQ level
298
+ retryLow: 100,
299
+ retryHigh: 200,
300
+ connectionTimeout: 5e3
301
+ });
277
302
  this.connection.on("error", (err) => {
278
303
  this.logger.error("RabbitMQ connection error:", { error: err });
279
304
  this.isConnected = false;
280
305
  });
281
- this.connection.on("close", () => {
282
- this.isConnected = false;
306
+ this.connection.on("connection", () => {
307
+ this.isConnected = true;
308
+ });
309
+ this.connection.on("connection.blocked", (reason) => {
310
+ this.logger.warn("RabbitMQ connection blocked:", { reason });
283
311
  });
312
+ this.connection.on("connection.unblocked", () => {
313
+ this.logger.info("RabbitMQ connection unblocked");
314
+ });
315
+ await this.connection.onConnect(5e3, true);
284
316
  this.isConnected = true;
285
317
  return this.connection;
286
318
  } catch (error) {
287
319
  this.isConnected = false;
288
- this.connection = void 0;
320
+ if (this.connection) {
321
+ try {
322
+ this.connection.close();
323
+ } catch (e) {
324
+ }
325
+ this.connection = void 0;
326
+ }
289
327
  throw new RunMQException(
290
328
  Exceptions.CONNECTION_NOT_ESTABLISHED,
291
329
  {
@@ -296,9 +334,9 @@ var AmqplibClientAdapter = class {
296
334
  }
297
335
  async getChannel() {
298
336
  const connection = await this.connect();
299
- const rawChannel = await connection.createChannel();
337
+ const rawChannel = await connection.acquire();
300
338
  this.acquiredChannels.push(rawChannel);
301
- return new AmqplibChannel(rawChannel);
339
+ return new RabbitMQClientChannel(rawChannel);
302
340
  }
303
341
  async getDefaultChannel() {
304
342
  if (!this.defaultChannel) {
@@ -315,7 +353,9 @@ var AmqplibClientAdapter = class {
315
353
  this.acquiredChannels = [];
316
354
  for (const channel of channels) {
317
355
  try {
318
- await channel.close();
356
+ if (channel.active) {
357
+ await channel.close();
358
+ }
319
359
  } catch (e) {
320
360
  }
321
361
  }
@@ -1324,7 +1364,7 @@ var RunMQ = class _RunMQ {
1324
1364
  reconnectDelay: (_a2 = config.reconnectDelay) != null ? _a2 : DEFAULTS.RECONNECT_DELAY,
1325
1365
  maxReconnectAttempts: (_b = config.maxReconnectAttempts) != null ? _b : DEFAULTS.MAX_RECONNECT_ATTEMPTS
1326
1366
  });
1327
- this.client = new AmqplibClientAdapter(this.config, this.logger);
1367
+ this.client = new RabbitMQClientAdapter(this.config, this.logger);
1328
1368
  this.consumer = new RunMQConsumerCreator(this.client, this.logger, this.config.management);
1329
1369
  }
1330
1370
  /**