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