@autofleet/rabbit 2.3.6 → 2.3.7

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
@@ -1,13 +1,4 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
12
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
4
  };
@@ -34,68 +25,74 @@ const defaultOptions = {
34
25
  lockTimeout: DEFAULT_LOCK_TIMEOUT,
35
26
  useConsumeWithLock: DEFAULT_USE_CONSUME_WITH_LOCK,
36
27
  };
37
- const assertExchangeFanout = (c, exchangeName) => __awaiter(void 0, void 0, void 0, function* () { return c.assertExchange(exchangeName, 'fanout'); });
28
+ const assertExchangeFanout = async (c, exchangeName) => c.assertExchange(exchangeName, 'fanout');
38
29
  const connectionCreatedConst = 'connectionCreated';
39
- const wrapSetImmediate = (callback) => new Promise((resolve, reject) => __awaiter(void 0, void 0, void 0, function* () {
40
- setImmediate(() => __awaiter(void 0, void 0, void 0, function* () {
30
+ const wrapSetImmediate = (callback) => new Promise(async (resolve, reject) => {
31
+ setImmediate(async () => {
41
32
  try {
42
- const value = yield callback();
33
+ const value = await callback();
43
34
  resolve(value);
44
35
  }
45
36
  catch (error) {
46
37
  reject(error);
47
38
  }
48
- }));
49
- }));
39
+ });
40
+ });
50
41
  class RabbitMq {
51
42
  constructor(options, redisConfig) {
52
43
  this.PUBLISH_TIMEOUT = Number(process.env.RABBITMQ_PUBLISH_TIMEOUT) || 60000;
53
44
  this.PUBLISH_ERROR_MSG = `rabbit: publish timeout(${this.PUBLISH_TIMEOUT}ms) has pass, exchange: `;
54
45
  this.DISCONNECT_MSG = 'rabbit: connection disconnect';
55
46
  this.RESCONNECT_MSG = 'rabbit: reconnecting';
56
- this.shouldConsumeMessageByTimestamp = (msg) => __awaiter(this, void 0, void 0, function* () {
47
+ this.shouldConsumeMessageByTimestamp = async (msg) => {
57
48
  if (msg) {
58
49
  const { properties: { headers } } = msg;
59
- const timestamp = headers === null || headers === void 0 ? void 0 : headers.creationTimestamp;
60
- if (timestamp && (headers === null || headers === void 0 ? void 0 : headers.redisTimestampValidationKey) && this.redisClient) {
61
- const lastMessageTimestamp = yield this.redisClient.getAsync(headers.redisTimestampValidationKey);
50
+ const timestamp = headers?.creationTimestamp;
51
+ if (timestamp && headers?.redisTimestampValidationKey && this.redisClient) {
52
+ const lastMessageTimestamp = await this.redisClient.getAsync(headers.redisTimestampValidationKey);
62
53
  return !lastMessageTimestamp || (parseInt(lastMessageTimestamp, 10) <= parseInt(timestamp, 10));
63
54
  }
64
55
  return true;
65
56
  }
66
57
  return false;
67
- });
68
- this.ack = (channel, msg, shouldUpdateRedisTimestamp = false, releaseLock = null) => (userMsg) => __awaiter(this, void 0, void 0, function* () {
58
+ };
59
+ this.ack = (channel, msg, shouldUpdateRedisTimestamp = false, releaseLock = null) => async (userMsg) => {
69
60
  if (msg) {
70
- yield channel.ack(msg);
61
+ await channel.ack(msg);
71
62
  const { properties: { headers } } = msg;
72
- const timestamp = headers === null || headers === void 0 ? void 0 : headers.creationTimestamp;
73
- if (shouldUpdateRedisTimestamp && timestamp && (headers === null || headers === void 0 ? void 0 : headers.redisTimestampValidationKey) && this.redisClient) {
63
+ const timestamp = headers?.creationTimestamp;
64
+ if (shouldUpdateRedisTimestamp && timestamp && headers?.redisTimestampValidationKey && this.redisClient) {
74
65
  const parsedTimestamp = parseInt(timestamp, 10);
75
- yield this.redisClient.setAsync(headers.redisTimestampValidationKey, parsedTimestamp, 'EX', 3600);
76
- yield this.unlockRedisIfNeeded(releaseLock);
66
+ await this.redisClient.setAsync(headers.redisTimestampValidationKey, parsedTimestamp, 'EX', 3600);
67
+ await this.unlockRedisIfNeeded(releaseLock);
77
68
  }
78
69
  }
79
- });
80
- this.nack = (channel, queue, options, deadQueueOptions, msg, releaseLock) => (userMsg, { skipRetry = false, } = {}) => __awaiter(this, void 0, void 0, function* () {
81
- yield this.unlockRedisIfNeeded(releaseLock);
70
+ };
71
+ this.nack = (channel, queue, options, deadQueueOptions, msg, releaseLock) => async (userMsg, { skipRetry = false, } = {}) => {
72
+ await this.unlockRedisIfNeeded(releaseLock);
82
73
  if (channel && msg) {
83
74
  if (!skipRetry
84
75
  && (!msg.properties.headers[RETRY_HEADER]
85
76
  || parseInt(msg.properties.headers[RETRY_HEADER], 10) < options.retries)) {
86
- yield this.sendToQueue(queue, RabbitMq.parseMsg(msg).content, options, Object.assign(Object.assign({}, msg.properties.headers), { [RETRY_HEADER]: msg.properties.headers[RETRY_HEADER]
77
+ await this.sendToQueue(queue, RabbitMq.parseMsg(msg).content, options, {
78
+ ...msg.properties.headers,
79
+ [RETRY_HEADER]: msg.properties.headers[RETRY_HEADER]
87
80
  ? msg.properties.headers[RETRY_HEADER] + 1
88
- : 1 }));
81
+ : 1,
82
+ });
89
83
  }
90
84
  else {
91
85
  const deadQueue = `${queue}-dead`;
92
- yield this.sendToQueue(deadQueue, RabbitMq.parseMsg(msg).content, deadQueueOptions, Object.assign(Object.assign({}, msg.properties.headers), { [RETRY_HEADER]: msg.properties.headers[RETRY_HEADER]
86
+ await this.sendToQueue(deadQueue, RabbitMq.parseMsg(msg).content, deadQueueOptions, {
87
+ ...msg.properties.headers,
88
+ [RETRY_HEADER]: msg.properties.headers[RETRY_HEADER]
93
89
  ? msg.properties.headers[RETRY_HEADER] + 1
94
- : 1 }));
90
+ : 1,
91
+ });
95
92
  }
96
- yield channel.ack(msg);
93
+ await channel.ack(msg);
97
94
  }
98
- });
95
+ };
99
96
  this.em = new events_1.EventEmitter();
100
97
  this.channel = null;
101
98
  this.connection = null;
@@ -115,7 +112,10 @@ class RabbitMq {
115
112
  content = JSON.parse(content);
116
113
  }
117
114
  catch (e) { }
118
- return Object.assign(Object.assign({}, msg), { content });
115
+ return {
116
+ ...msg,
117
+ content,
118
+ };
119
119
  }
120
120
  static validateName(type, name) {
121
121
  if (!name || name === '') {
@@ -125,215 +125,186 @@ class RabbitMq {
125
125
  static getPublishOptions(customHeaders = {}) {
126
126
  return {
127
127
  timestamp: moment_1.default().unix(),
128
- headers: Object.assign({ creationTimestamp: moment_1.default().valueOf() }, customHeaders),
128
+ headers: {
129
+ creationTimestamp: moment_1.default().valueOf(),
130
+ ...customHeaders,
131
+ },
129
132
  };
130
133
  }
131
- getConnection() {
132
- return __awaiter(this, void 0, void 0, function* () {
133
- return new Promise((resolve) => __awaiter(this, void 0, void 0, function* () {
134
- if (this.connection !== null) {
135
- return resolve(this.connection);
134
+ async getConnection() {
135
+ return new Promise(async (resolve) => {
136
+ if (this.connection !== null) {
137
+ return resolve(this.connection);
138
+ }
139
+ if (this.creatingConnection) {
140
+ this.em.once(connectionCreatedConst, resolve);
141
+ return;
142
+ }
143
+ this.creatingConnection = true;
144
+ logger_1.default.info('rabbit: env username', { userName: (process.env.RABBITMQ_USERNAME || 'doesnt exist') });
145
+ const username = process.env.RABBITMQ_USERNAME || 'guest';
146
+ const password = process.env.RABBITMQ_PASSWORD || 'guest';
147
+ const host = process.env.RABBITMQ_SERVICE_HOST || '';
148
+ const connection = await amqp_connection_manager_1.connect([`amqp://${username}:${password}@${host}`]);
149
+ this.connection = connection;
150
+ this.connection.on('disconnect', ({ err }) => {
151
+ this.exchanges = {};
152
+ this.queues = {};
153
+ if (this.options?.disableReconnect) {
154
+ logger_1.default.error(`${this.DISCONNECT_MSG}${err && ` - ${err}`}`);
136
155
  }
137
- if (this.creatingConnection) {
138
- this.em.once(connectionCreatedConst, resolve);
139
- return;
156
+ else {
157
+ logger_1.default.error(`${this.RESCONNECT_MSG}${err && ` - ${err}`}`);
158
+ this.connection = null;
140
159
  }
141
- this.creatingConnection = true;
142
- logger_1.default.info('rabbit: env username', { userName: (process.env.RABBITMQ_USERNAME || 'doesnt exist') });
143
- const username = process.env.RABBITMQ_USERNAME || 'guest';
144
- const password = process.env.RABBITMQ_PASSWORD || 'guest';
145
- const host = process.env.RABBITMQ_SERVICE_HOST || '';
146
- const connection = yield amqp_connection_manager_1.connect([`amqp://${username}:${password}@${host}`]);
147
- this.connection = connection;
148
- this.connection.on('disconnect', ({ err }) => {
149
- var _a;
150
- this.exchanges = {};
151
- this.queues = {};
152
- if ((_a = this.options) === null || _a === void 0 ? void 0 : _a.disableReconnect) {
153
- logger_1.default.error(`${this.DISCONNECT_MSG}${err && ` - ${err}`}`);
154
- }
155
- else {
156
- logger_1.default.error(`${this.RESCONNECT_MSG}${err && ` - ${err}`}`);
157
- this.connection = null;
158
- }
159
- });
160
- this.creatingConnection = false;
161
- this.em.emit(connectionCreatedConst, connection);
162
- resolve(connection);
163
- }));
160
+ });
161
+ this.creatingConnection = false;
162
+ this.em.emit(connectionCreatedConst, connection);
163
+ resolve(connection);
164
164
  });
165
165
  }
166
- getNewChannel() {
167
- return __awaiter(this, void 0, void 0, function* () {
168
- const connection = yield this.getConnection();
169
- return connection.createChannel({});
170
- });
166
+ async getNewChannel() {
167
+ const connection = await this.getConnection();
168
+ return connection.createChannel({});
171
169
  }
172
- assertChannel() {
173
- return __awaiter(this, void 0, void 0, function* () {
174
- return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
175
- if (this.channel) {
176
- return resolve(this.channel);
177
- }
178
- try {
179
- const connection = yield this.getConnection();
180
- if (this.channel === null) {
181
- this.channel = yield connection.createChannel({});
182
- }
183
- resolve(this.channel);
184
- }
185
- catch (e) {
186
- reject(e);
170
+ async assertChannel() {
171
+ return new Promise(async (resolve, reject) => {
172
+ if (this.channel) {
173
+ return resolve(this.channel);
174
+ }
175
+ try {
176
+ const connection = await this.getConnection();
177
+ if (this.channel === null) {
178
+ this.channel = await connection.createChannel({});
187
179
  }
188
- }));
189
- });
190
- }
191
- assertExchange(exchangeName, options) {
192
- return __awaiter(this, void 0, void 0, function* () {
193
- const channel = yield this.assertChannel();
194
- if (this.exchanges[exchangeName]) {
195
- return this.exchanges[exchangeName];
180
+ resolve(this.channel);
181
+ }
182
+ catch (e) {
183
+ reject(e);
196
184
  }
197
- const exchange = yield assertExchangeFanout(channel, exchangeName);
198
- this.exchanges[exchangeName] = exchange;
199
- return exchange;
200
185
  });
201
186
  }
202
- getQueueLength(queue) {
203
- return __awaiter(this, void 0, void 0, function* () {
204
- RabbitMq.validateName('queue', queue);
205
- const channel = yield this.assertChannel();
206
- // @ts-ignore
207
- return channel.assertQueue(queue);
208
- });
187
+ async assertExchange(exchangeName, options) {
188
+ const channel = await this.assertChannel();
189
+ if (this.exchanges[exchangeName]) {
190
+ return this.exchanges[exchangeName];
191
+ }
192
+ const exchange = await assertExchangeFanout(channel, exchangeName);
193
+ this.exchanges[exchangeName] = exchange;
194
+ return exchange;
209
195
  }
210
- bindQueue(queue, exchange) {
211
- return __awaiter(this, void 0, void 0, function* () {
212
- const channel = yield this.assertChannel();
213
- yield channel.addSetup((setupChannel) => setupChannel.bindQueue(queue, exchange, ''));
214
- // @ts-ignore
215
- return channel.bindQueue(queue, exchange, '');
216
- });
196
+ async getQueueLength(queue) {
197
+ RabbitMq.validateName('queue', queue);
198
+ const channel = await this.assertChannel();
199
+ // @ts-ignore
200
+ return channel.checkQueue(queue);
217
201
  }
218
- assertQueue(queueName, options) {
219
- return __awaiter(this, void 0, void 0, function* () {
220
- RabbitMq.validateName('queue', queueName);
221
- const channel = yield this.assertChannel();
222
- if (this.queues[queueName]) {
223
- return this.queues[queueName];
224
- }
225
- yield channel.addSetup((setupChannel) => setupChannel.assertQueue(queueName, options));
226
- const queue = yield channel.assertQueue(queueName, options);
227
- this.queues[queueName] = queueName;
228
- return queue;
229
- });
202
+ async bindQueue(queue, exchange) {
203
+ const channel = await this.assertChannel();
204
+ await channel.addSetup((setupChannel) => setupChannel.bindQueue(queue, exchange, ''));
205
+ // @ts-ignore
206
+ return channel.bindQueue(queue, exchange, '');
230
207
  }
231
- consume(queue, callback, options) {
232
- return __awaiter(this, void 0, void 0, function* () {
233
- return this.consumeFromRabbit(queue, callback, options);
234
- });
208
+ async assertQueue(queueName, options) {
209
+ RabbitMq.validateName('queue', queueName);
210
+ const channel = await this.assertChannel();
211
+ if (this.queues[queueName]) {
212
+ return this.queues[queueName];
213
+ }
214
+ await channel.addSetup((setupChannel) => setupChannel.assertQueue(queueName, options));
215
+ const queue = await channel.assertQueue(queueName, options);
216
+ this.queues[queueName] = queueName;
217
+ return queue;
235
218
  }
236
- lockRedisIfNeeded(msg, options) {
237
- return __awaiter(this, void 0, void 0, function* () {
238
- const { properties: { headers } } = msg;
239
- const timestamp = headers === null || headers === void 0 ? void 0 : headers.creationTimestamp;
240
- let releaseLock = null;
241
- if (options.useConsumeWithLock && timestamp && (headers === null || headers === void 0 ? void 0 : headers.redisTimestampValidationKey) && this.redisLock) {
242
- releaseLock = yield this.redisLock(headers.redisTimestampValidationKey, (options === null || options === void 0 ? void 0 : options.lockTimeout) || DEFAULT_LOCK_TIMEOUT);
243
- }
244
- return releaseLock;
245
- });
219
+ async consume(queue, callback, options) {
220
+ return this.consumeFromRabbit(queue, callback, options);
246
221
  }
247
- unlockRedisIfNeeded(releaseLock) {
248
- return __awaiter(this, void 0, void 0, function* () {
249
- if (this.redisLock && releaseLock) {
250
- yield releaseLock();
251
- }
252
- });
222
+ async lockRedisIfNeeded(msg, options) {
223
+ const { properties: { headers } } = msg;
224
+ const timestamp = headers?.creationTimestamp;
225
+ let releaseLock = null;
226
+ if (options.useConsumeWithLock && timestamp && headers?.redisTimestampValidationKey && this.redisLock) {
227
+ releaseLock = await this.redisLock(headers.redisTimestampValidationKey, options?.lockTimeout || DEFAULT_LOCK_TIMEOUT);
228
+ }
229
+ return releaseLock;
253
230
  }
254
- consumeFromRabbit(queue, callback, options) {
255
- return __awaiter(this, void 0, void 0, function* () {
256
- const optionsWithDefaults = Object.assign(Object.assign({}, defaultOptions), options);
257
- RabbitMq.validateName('queue', queue);
258
- const { limit, deadMessageTtl, useConsumeWithLock, lockTimeout, } = optionsWithDefaults;
259
- if (useConsumeWithLock) {
260
- if (!this.redisLock) {
261
- throw new Error('Usage of consumeWithLock requires RedisInstance');
262
- }
263
- logger_1.default.info(`Consuming with lock from queue ${queue} with lockTimeout: ${lockTimeout}ms`);
231
+ async unlockRedisIfNeeded(releaseLock) {
232
+ if (this.redisLock && releaseLock) {
233
+ await releaseLock();
234
+ }
235
+ }
236
+ async consumeFromRabbit(queue, callback, options) {
237
+ const optionsWithDefaults = { ...defaultOptions, ...options };
238
+ RabbitMq.validateName('queue', queue);
239
+ const { limit, deadMessageTtl, useConsumeWithLock, lockTimeout, } = optionsWithDefaults;
240
+ if (useConsumeWithLock) {
241
+ if (!this.redisLock) {
242
+ throw new Error('Usage of consumeWithLock requires RedisInstance');
264
243
  }
265
- const channel = yield this.getNewChannel();
266
- return channel.addSetup((c) => __awaiter(this, void 0, void 0, function* () {
267
- console.log('addSetup');
268
- yield c.assertQueue(queue);
269
- yield c.prefetch(limit, true);
270
- return Promise.all([
271
- c.consume(queue, (msg) => __awaiter(this, void 0, void 0, function* () {
272
- if (!msg) {
273
- return null;
274
- }
275
- const parsedMessage = RabbitMq.parseMsg(msg);
276
- const releaseLock = yield this.lockRedisIfNeeded(parsedMessage, optionsWithDefaults);
277
- const shouldConsume = yield this.shouldConsumeMessageByTimestamp(parsedMessage);
278
- if (!shouldConsume) {
279
- yield this.unlockRedisIfNeeded(releaseLock);
280
- return this.ack(channel, msg)(msg);
281
- }
282
- try {
283
- yield callback(parsedMessage, this.ack(channel, msg, true, releaseLock), this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl }, msg, releaseLock));
284
- }
285
- catch (e) {
286
- yield this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl }, msg, releaseLock)(msg);
287
- }
288
- })),
289
- ]);
290
- }));
244
+ logger_1.default.info(`Consuming with lock from queue ${queue} with lockTimeout: ${lockTimeout}ms`);
245
+ }
246
+ const channel = await this.getNewChannel();
247
+ return channel.addSetup(async (c) => {
248
+ await c.assertQueue(queue);
249
+ await c.prefetch(limit, true);
250
+ return Promise.all([
251
+ c.consume(queue, async (msg) => {
252
+ if (!msg) {
253
+ return null;
254
+ }
255
+ const parsedMessage = RabbitMq.parseMsg(msg);
256
+ const releaseLock = await this.lockRedisIfNeeded(parsedMessage, optionsWithDefaults);
257
+ const shouldConsume = await this.shouldConsumeMessageByTimestamp(parsedMessage);
258
+ if (!shouldConsume) {
259
+ await this.unlockRedisIfNeeded(releaseLock);
260
+ return this.ack(channel, msg)(msg);
261
+ }
262
+ try {
263
+ await callback(parsedMessage, this.ack(channel, msg, true, releaseLock), this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl }, msg, releaseLock));
264
+ }
265
+ catch (e) {
266
+ await this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl }, msg, releaseLock)(msg);
267
+ }
268
+ }),
269
+ ]);
291
270
  });
292
271
  }
293
- consumeFromExchange(queue, exchange, callback, options) {
294
- return __awaiter(this, void 0, void 0, function* () {
295
- const optionsWithDefaults = Object.assign(Object.assign({}, defaultOptions), options);
296
- RabbitMq.validateName('exchange', exchange);
297
- RabbitMq.validateName('queue', queue);
298
- const { limit, deadMessageTtl } = optionsWithDefaults;
299
- const channel = yield this.getNewChannel();
300
- return channel.addSetup((c) => __awaiter(this, void 0, void 0, function* () {
301
- const assertExchange = yield assertExchangeFanout(c, exchange);
302
- yield c.assertQueue(queue);
303
- this.exchanges[exchange] = assertExchange;
304
- yield c.prefetch(limit, true);
305
- return Promise.all([
306
- c.bindQueue(queue, exchange, ''),
307
- this.consume(queue, callback, options),
308
- ]);
309
- }));
272
+ async consumeFromExchange(queue, exchange, callback, options) {
273
+ const optionsWithDefaults = { ...defaultOptions, ...options };
274
+ RabbitMq.validateName('exchange', exchange);
275
+ RabbitMq.validateName('queue', queue);
276
+ const { limit, deadMessageTtl } = optionsWithDefaults;
277
+ const channel = await this.getNewChannel();
278
+ return channel.addSetup(async (c) => {
279
+ const assertExchange = await assertExchangeFanout(c, exchange);
280
+ await c.assertQueue(queue);
281
+ this.exchanges[exchange] = assertExchange;
282
+ await c.prefetch(limit, true);
283
+ return Promise.all([
284
+ c.bindQueue(queue, exchange, ''),
285
+ this.consume(queue, callback, options),
286
+ ]);
310
287
  });
311
288
  }
312
- publish(exchange, content, customHeaders) {
313
- return __awaiter(this, void 0, void 0, function* () {
314
- return wrapSetImmediate(() => __awaiter(this, void 0, void 0, function* () {
315
- RabbitMq.validateName('exchange', exchange);
316
- const channel = yield this.assertChannel();
317
- yield this.assertExchange(exchange);
318
- yield channel.publish(exchange, '', Buffer.from(JSON.stringify(content)), RabbitMq.getPublishOptions(customHeaders));
319
- }));
289
+ async publish(exchange, content, customHeaders) {
290
+ return wrapSetImmediate(async () => {
291
+ RabbitMq.validateName('exchange', exchange);
292
+ const channel = await this.assertChannel();
293
+ await this.assertExchange(exchange);
294
+ await channel.publish(exchange, '', Buffer.from(JSON.stringify(content)), RabbitMq.getPublishOptions(customHeaders));
320
295
  });
321
296
  }
322
- sendToQueue(queue, content, options, customHeaders) {
323
- return __awaiter(this, void 0, void 0, function* () {
324
- return wrapSetImmediate(() => __awaiter(this, void 0, void 0, function* () {
325
- RabbitMq.validateName('queue', queue);
326
- const channel = yield this.assertChannel();
327
- yield this.assertQueue(queue, options);
328
- return channel.sendToQueue(queue, Buffer.from(JSON.stringify(content)), RabbitMq.getPublishOptions(customHeaders));
329
- }));
297
+ async sendToQueue(queue, content, options, customHeaders) {
298
+ return wrapSetImmediate(async () => {
299
+ RabbitMq.validateName('queue', queue);
300
+ const channel = await this.assertChannel();
301
+ await this.assertQueue(queue, options);
302
+ return channel.sendToQueue(queue, Buffer.from(JSON.stringify(content)), RabbitMq.getPublishOptions(customHeaders));
330
303
  });
331
304
  }
332
- isConnected() {
333
- return __awaiter(this, void 0, void 0, function* () {
334
- const connection = yield this.getConnection();
335
- return connection.isConnected();
336
- });
305
+ async isConnected() {
306
+ const connection = await this.getConnection();
307
+ return connection.isConnected();
337
308
  }
338
309
  }
339
310
  exports.default = RabbitMq;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/rabbit",
3
- "version": "2.3.6",
3
+ "version": "2.3.7",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -1,85 +0,0 @@
1
- /// <reference types="node" />
2
- import { Options, ConsumeMessage } from 'amqplib';
3
- import { AmqpConnectionManager, ChannelWrapper } from 'amqp-connection-manager';
4
- import { EventEmitter } from 'events';
5
- import { RedisConfig } from './redis';
6
- export interface IAfRabbitMq {
7
- ack: any;
8
- nack: any;
9
- assertChannel: any;
10
- assertExchange: any;
11
- assertQueue: any;
12
- consume: any;
13
- consumeFromExchange: any;
14
- publish: any;
15
- sendToQueue: any;
16
- redisClient?: any;
17
- }
18
- interface ExchangesCache {
19
- [key: string]: any;
20
- }
21
- interface QueuesCache {
22
- [key: string]: any;
23
- }
24
- declare type CustomMessageHeaders = {
25
- redisTimestampValidationKey?: string;
26
- };
27
- declare type RedisLockType = (args0?: string, arg1?: number) => Promise<any>;
28
- declare type ConsumeMessageOrNull = ConsumeMessage | null;
29
- interface ConsumeOptions {
30
- retries?: number;
31
- deadMessageTtl?: number;
32
- limit?: number;
33
- lockTimeout?: number;
34
- useConsumeWithLock?: boolean;
35
- }
36
- declare type CallbackFunction = (msg: ConsumeMessage, ack: Function, nack: Function) => Promise<any>;
37
- export interface AfRabbitOptions {
38
- disableReconnect?: boolean;
39
- host?: string;
40
- }
41
- declare class RabbitMq implements IAfRabbitMq {
42
- static parseMsg(msg: any): any;
43
- static validateName(type: string, name: string): void;
44
- static getPublishOptions(customHeaders?: CustomMessageHeaders): {
45
- timestamp: number;
46
- headers: {
47
- redisTimestampValidationKey?: string | undefined;
48
- creationTimestamp: number;
49
- };
50
- };
51
- PUBLISH_TIMEOUT: number;
52
- PUBLISH_ERROR_MSG: string;
53
- DISCONNECT_MSG: string;
54
- RESCONNECT_MSG: string;
55
- channel: ChannelWrapper | null;
56
- connection: AmqpConnectionManager | null;
57
- em: EventEmitter;
58
- creatingConnection: boolean;
59
- exchanges: ExchangesCache;
60
- queues: QueuesCache;
61
- host: string | null;
62
- options: AfRabbitOptions | undefined;
63
- redisClient: any;
64
- redisLock?: RedisLockType;
65
- constructor(options?: AfRabbitOptions, redisConfig?: RedisConfig);
66
- private shouldConsumeMessageByTimestamp;
67
- ack: (channel: ChannelWrapper, msg: ConsumeMessageOrNull, shouldUpdateRedisTimestamp?: boolean, releaseLock?: null) => (userMsg: ConsumeMessage) => Promise<any>;
68
- nack: (channel: ChannelWrapper, queue: string, options: any, deadQueueOptions: Options.AssertQueue, msg: ConsumeMessageOrNull, releaseLock: any) => (userMsg: ConsumeMessageOrNull, { skipRetry, }?: any) => Promise<any>;
69
- getConnection(): Promise<AmqpConnectionManager>;
70
- getNewChannel(): Promise<ChannelWrapper>;
71
- assertChannel(): Promise<ChannelWrapper>;
72
- assertExchange(exchangeName: string, options?: any): Promise<any>;
73
- getQueueLength(queue: string): Promise<import("amqplib").Replies.AssertQueue>;
74
- bindQueue(queue: string, exchange: string): Promise<void>;
75
- assertQueue(queueName: string, options?: Options.AssertQueue): Promise<any>;
76
- consume(queue: string, callback: CallbackFunction, options?: ConsumeOptions): Promise<any>;
77
- private lockRedisIfNeeded;
78
- private unlockRedisIfNeeded;
79
- private consumeFromRabbit;
80
- consumeFromExchange(queue: string, exchange: string, callback: CallbackFunction, options?: ConsumeOptions): Promise<any>;
81
- publish(exchange: string, content: any, customHeaders?: any): Promise<boolean>;
82
- sendToQueue(queue: string, content: any, options?: any, customHeaders?: any): Promise<boolean>;
83
- isConnected(): Promise<boolean>;
84
- }
85
- export default RabbitMq;