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