@autofleet/rabbit 3.2.20 → 3.2.21-beta.0

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.d.ts CHANGED
@@ -3,7 +3,7 @@ import { EventEmitter } from 'events';
3
3
  import { AmqpConnectionManager, ChannelWrapper, CreateChannelOpts } from 'amqp-connection-manager';
4
4
  import { ConfirmChannel, ConsumeMessage, Options, Replies } from 'amqplib';
5
5
  import { RedisConfig } from './lib/redis';
6
- import { CallbackFunction, ConsumeMessageOrNull, ConsumeOptions, CustomMessageHeaders, QueuesCache, RedisLockType, ExchangesCache, QueueSetupPromisesDictionary, AssertExchangePromisesDictionary } from './lib/types';
6
+ import { CallbackFunction, ConsumeMessageOrNull, ConsumeOptions, CustomMessageHeaders, QueuesCache, RedisLockType, ExchangesCache, QueueSetupPromisesDictionary } from './lib/types';
7
7
  export interface IAfRabbitMq {
8
8
  ack: any;
9
9
  nack: any;
@@ -66,7 +66,6 @@ declare class RabbitMq implements IAfRabbitMq {
66
66
  exchanges: ExchangesCache;
67
67
  queues: QueuesCache;
68
68
  queueSetupPromises: QueueSetupPromisesDictionary;
69
- assertExchangePromises: AssertExchangePromisesDictionary;
70
69
  options: AfRabbitOptions | undefined;
71
70
  redisClient: any;
72
71
  redisLock?: RedisLockType;
package/dist/index.js CHANGED
@@ -6,6 +6,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
6
6
  Object.defineProperty(exports, "__esModule", { value: true });
7
7
  const events_1 = require("events");
8
8
  const util_1 = require("util");
9
+ const lodash_1 = __importDefault(require("lodash"));
9
10
  const moment_1 = __importDefault(require("moment"));
10
11
  const redis_lock_1 = __importDefault(require("redis-lock"));
11
12
  const amqp_connection_manager_1 = require("amqp-connection-manager");
@@ -123,7 +124,6 @@ class RabbitMq {
123
124
  this.exchanges = {};
124
125
  this.queues = {};
125
126
  this.queueSetupPromises = {};
126
- this.assertExchangePromises = {};
127
127
  this.consumers = [];
128
128
  this.options = options;
129
129
  this.redisClient = redisConfig && (0, redis_1.default)(redisConfig);
@@ -199,7 +199,6 @@ class RabbitMq {
199
199
  }
200
200
  });
201
201
  this.connection.on('disconnect', ({ err }) => {
202
- // this.channel = null;
203
202
  this.consumersTags = [];
204
203
  debug('rabbit: connection closed');
205
204
  if (this.options?.disableReconnect) {
@@ -267,15 +266,11 @@ class RabbitMq {
267
266
  async assertExchange(exchangeName, options) {
268
267
  const channel = await this.assertChannel();
269
268
  if (this.exchanges[exchangeName]) {
270
- delete this.assertExchangePromises[exchangeName];
271
269
  return this.exchanges[exchangeName];
272
270
  }
273
- if (this.assertExchangePromises[exchangeName]) {
274
- return this.assertExchangePromises[exchangeName];
275
- }
276
- this.assertExchangePromises[exchangeName] = (0, utils_1.assertExchangeFanout)(channel, exchangeName);
277
- this.exchanges[exchangeName] = await this.assertExchangePromises[exchangeName];
278
- return this.exchanges[exchangeName];
271
+ const exchange = await (0, utils_1.assertExchangeFanout)(channel, exchangeName);
272
+ this.exchanges[exchangeName] = exchange;
273
+ return exchange;
279
274
  }
280
275
  async getQueueLength(queue) {
281
276
  RabbitMq.validateName('queue', queue);
@@ -382,6 +377,7 @@ class RabbitMq {
382
377
  return channel.addSetup(async (confirmChannel) => {
383
378
  await confirmChannel.assertQueue(queue, options ? { messageTtl: options.messageTtl } : undefined);
384
379
  await confirmChannel.prefetch(limit, true);
380
+ const channelConsumerOptions = optionsWithDefaults.channelConsumeOptions ? lodash_1.default.merge(types_1.CONSUMER_DEFAULT_OPTIONS, optionsWithDefaults.channelConsumeOptions) : types_1.CONSUMER_DEFAULT_OPTIONS;
385
381
  const { consumerTag } = await confirmChannel.consume(queue, async (msg) => {
386
382
  if (!msg) {
387
383
  return null;
@@ -442,7 +438,7 @@ class RabbitMq {
442
438
  catch (e) {
443
439
  await localNack(msg);
444
440
  }
445
- }, types_1.CONSUMER_DEFAULT_OPTIONS);
441
+ }, channelConsumerOptions);
446
442
  if (!consumerTag) {
447
443
  logger_1.default.error(`rabbit: failed to consume from queue ${queue}`);
448
444
  }
@@ -8,9 +8,6 @@ export interface QueuesCache {
8
8
  export interface QueueSetupPromisesDictionary {
9
9
  [key: string]: Promise<Replies.AssertQueue> | undefined;
10
10
  }
11
- export interface AssertExchangePromisesDictionary {
12
- [key: string]: Promise<Replies.AssertExchange> | undefined;
13
- }
14
11
  export type CustomMessageHeaders = {
15
12
  redisTimestampValidationKey?: string;
16
13
  };
@@ -25,6 +22,7 @@ export interface ConsumeOptions {
25
22
  useConsumeWithLock?: boolean;
26
23
  auditContext?: any;
27
24
  enableRabbitTrace?: boolean;
25
+ channelConsumeOptions?: Options.Consume;
28
26
  }
29
27
  export type CallbackFunction = (msg: ConsumeMessage, ack: any, nack: any) => Promise<any>;
30
28
  export type newChannelOpts = {
@@ -1,5 +1,5 @@
1
1
  import { ChannelWrapper } from 'amqp-connection-manager';
2
- import { ConfirmChannel, Replies } from 'amqplib';
3
- export declare const assertExchangeFanout: (c: ChannelWrapper | ConfirmChannel, exchangeName: string) => Promise<Replies.AssertExchange>;
2
+ import { ConfirmChannel } from 'amqplib';
3
+ export declare const assertExchangeFanout: (c: ChannelWrapper | ConfirmChannel, exchangeName: string) => Promise<import("amqplib").Replies.AssertExchange>;
4
4
  export declare const wrapSetImmediate: (callback: () => any) => Promise<any>;
5
5
  export declare const rand: () => number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/rabbit",
3
- "version": "3.2.20",
3
+ "version": "3.2.21-beta.0",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -19,6 +19,7 @@
19
19
  "amqp-connection-manager": "4.1.9",
20
20
  "amqplib": "0.10.3",
21
21
  "bluebird": "^3.7.2",
22
+ "lodash": "^4.17.21",
22
23
  "moment": "^2.29.1",
23
24
  "redis": "^3.1.2",
24
25
  "redis-lock": "^0.1.4"
@@ -30,6 +31,7 @@
30
31
  "@autofleet/logger": "4.0.6",
31
32
  "@types/amqplib": "0.8.2",
32
33
  "@types/jest": "^29.5.12",
34
+ "@types/lodash": "^4.17.12",
33
35
  "@types/node": "^20.14.11",
34
36
  "@typescript-eslint/eslint-plugin": "^4.8.1",
35
37
  "eslint": "^7.13.0",
@@ -43,4 +45,4 @@
43
45
  },
44
46
  "author": "",
45
47
  "license": "ISC"
46
- }
48
+ }
package/src/index.ts CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  import { EventEmitter, once } from 'events';
4
4
  import { promisify } from 'util';
5
+ import _ from 'lodash';
5
6
  import moment from 'moment';
6
7
  import RedisLock from 'redis-lock';
7
8
  import {
@@ -35,10 +36,7 @@ import {
35
36
  CustomMessageHeaders,
36
37
  QueuesCache,
37
38
  RedisLockType,
38
- ExchangesCache,
39
- CONSUMER_DEFAULT_OPTIONS,
40
- QueueSetupPromisesDictionary,
41
- AssertExchangePromisesDictionary,
39
+ ExchangesCache, CONSUMER_DEFAULT_OPTIONS, QueueSetupPromisesDictionary,
42
40
  } from './lib/types';
43
41
 
44
42
  // const debug = nodeDebug('af-rabbitmq')
@@ -160,8 +158,6 @@ class RabbitMq implements IAfRabbitMq {
160
158
 
161
159
  queueSetupPromises: QueueSetupPromisesDictionary;
162
160
 
163
- assertExchangePromises: AssertExchangePromisesDictionary;
164
-
165
161
  options: AfRabbitOptions | undefined;
166
162
 
167
163
  redisClient: any;
@@ -182,7 +178,6 @@ class RabbitMq implements IAfRabbitMq {
182
178
  this.exchanges = {};
183
179
  this.queues = {};
184
180
  this.queueSetupPromises = {};
185
- this.assertExchangePromises = {};
186
181
  this.consumers = [];
187
182
  this.options = options;
188
183
  this.redisClient = redisConfig && getRedisInstance(redisConfig);
@@ -341,7 +336,6 @@ class RabbitMq implements IAfRabbitMq {
341
336
  });
342
337
 
343
338
  this.connection.on('disconnect', ({ err }) => {
344
- // this.channel = null;
345
339
  this.consumersTags = [];
346
340
  debug('rabbit: connection closed');
347
341
  if (this.options?.disableReconnect) {
@@ -351,7 +345,6 @@ class RabbitMq implements IAfRabbitMq {
351
345
  logger.error(`${this.RECONNECT_MSG}${err && ` - ${err}`}`);
352
346
  }
353
347
  });
354
-
355
348
  this.connection.once('connect', async () => {
356
349
  debug('rabbit: connection established');
357
350
  this.creatingConnection = false;
@@ -409,19 +402,12 @@ class RabbitMq implements IAfRabbitMq {
409
402
 
410
403
  async assertExchange(exchangeName: string, options?: any) {
411
404
  const channel: ChannelWrapper = await this.assertChannel();
412
-
413
405
  if (this.exchanges[exchangeName]) {
414
- delete this.assertExchangePromises[exchangeName];
415
406
  return this.exchanges[exchangeName];
416
407
  }
417
-
418
- if (this.assertExchangePromises[exchangeName]) {
419
- return this.assertExchangePromises[exchangeName];
420
- }
421
-
422
- this.assertExchangePromises[exchangeName] = assertExchangeFanout(channel, exchangeName);
423
- this.exchanges[exchangeName] = await this.assertExchangePromises[exchangeName];
424
- return this.exchanges[exchangeName];
408
+ const exchange = await assertExchangeFanout(channel, exchangeName);
409
+ this.exchanges[exchangeName] = exchange;
410
+ return exchange;
425
411
  }
426
412
 
427
413
  async getQueueLength(queue: string) {
@@ -546,6 +532,7 @@ class RabbitMq implements IAfRabbitMq {
546
532
  return channel.addSetup(async (confirmChannel: ConfirmChannel) => {
547
533
  await confirmChannel.assertQueue(queue, options ? { messageTtl: options.messageTtl } : undefined);
548
534
  await confirmChannel.prefetch(limit, true);
535
+ const channelConsumerOptions = optionsWithDefaults.channelConsumeOptions ? _.merge(CONSUMER_DEFAULT_OPTIONS, optionsWithDefaults.channelConsumeOptions) : CONSUMER_DEFAULT_OPTIONS;
549
536
  const { consumerTag } = await confirmChannel.consume(
550
537
  queue,
551
538
  async (msg: ConsumeMessageOrNull) => {
@@ -617,7 +604,8 @@ class RabbitMq implements IAfRabbitMq {
617
604
  } catch (e) {
618
605
  await localNack(msg);
619
606
  }
620
- }, CONSUMER_DEFAULT_OPTIONS,
607
+ },
608
+ channelConsumerOptions,
621
609
  );
622
610
  if (!consumerTag) {
623
611
  logger.error(`rabbit: failed to consume from queue ${queue}`);
package/src/lib/types.ts CHANGED
@@ -12,10 +12,6 @@ export interface QueueSetupPromisesDictionary {
12
12
  [key: string]: Promise<Replies.AssertQueue> | undefined
13
13
  }
14
14
 
15
- export interface AssertExchangePromisesDictionary {
16
- [key: string]: Promise<Replies.AssertExchange> | undefined
17
- }
18
-
19
15
  export type CustomMessageHeaders = {
20
16
  redisTimestampValidationKey?: string;
21
17
  }
@@ -31,6 +27,7 @@ export interface ConsumeOptions {
31
27
  useConsumeWithLock?: boolean;
32
28
  auditContext?: any;
33
29
  enableRabbitTrace?: boolean;
30
+ channelConsumeOptions?: Options.Consume;
34
31
  }
35
32
 
36
33
  export type CallbackFunction = (msg: ConsumeMessage, ack: any, nack: any) => Promise<any>
package/src/lib/utils.ts CHANGED
@@ -1,11 +1,7 @@
1
1
  import { ChannelWrapper } from 'amqp-connection-manager';
2
- import { ConfirmChannel, Replies } from 'amqplib';
3
-
4
- export const assertExchangeFanout = async (
5
- c: ChannelWrapper | ConfirmChannel,
6
- exchangeName: string,
7
- ): Promise<Replies.AssertExchange> => c.assertExchange(exchangeName, 'fanout');
2
+ import { ConfirmChannel } from 'amqplib';
8
3
 
4
+ export const assertExchangeFanout = async (c: ChannelWrapper | ConfirmChannel, exchangeName: string) => c.assertExchange(exchangeName, 'fanout');
9
5
  export const wrapSetImmediate = (callback: () => any) => new Promise<any>((resolve, reject) => {
10
6
  setImmediate(async () => {
11
7
  try {