@autofleet/rabbit 3.2.24 → 3.2.25-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
@@ -1,9 +1,9 @@
1
1
  /// <reference types="node" />
2
2
  import { EventEmitter } from 'events';
3
- import { AmqpConnectionManager, ChannelWrapper, CreateChannelOpts } from 'amqp-connection-manager';
3
+ import { AmqpConnectionManager, ChannelWrapper } 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, AssertExchangePromisesDictionary, assertChannelOpts, newChannelOpts, PublishOptions } from './lib/types';
7
7
  export interface IAfRabbitMq {
8
8
  ack: any;
9
9
  nack: any;
@@ -33,15 +33,6 @@ export interface AfRabbitOptions {
33
33
  dontRetryAssert?: boolean;
34
34
  rabbitHost?: string;
35
35
  }
36
- type newChannelOpts = {
37
- name?: string;
38
- onClose?: null | ((args: any | null) => void);
39
- options?: CreateChannelOpts | undefined;
40
- };
41
- type assertChannelOpts = {
42
- channelName?: string;
43
- force?: boolean;
44
- };
45
36
  declare class RabbitMq implements IAfRabbitMq {
46
37
  static parseMsg(msg: any): any;
47
38
  static validateName(type: string, name: string): void;
@@ -79,8 +70,8 @@ declare class RabbitMq implements IAfRabbitMq {
79
70
  nack: (channel: ConfirmChannel, queue: string, options: any, deadQueueOptions: Options.AssertQueue, msg: ConsumeMessageOrNull, releaseLock: any) => (userMsg: ConsumeMessageOrNull, { skipRetry, }?: NackOptions) => Promise<any>;
80
71
  getConnection(): Promise<AmqpConnectionManager>;
81
72
  getNewChannel({ name, onClose, options }?: newChannelOpts): Promise<ChannelWrapper>;
82
- assertChannel({ force }?: assertChannelOpts): Promise<ChannelWrapper>;
83
- assertExchange(exchangeName: string, options?: any): Promise<any>;
73
+ assertChannel({ force, newChannelOptions }?: assertChannelOpts): Promise<ChannelWrapper>;
74
+ assertExchange(exchangeName: string): Promise<any>;
84
75
  getQueueLength(queue: string): Promise<Replies.AssertQueue>;
85
76
  private deleteQueue;
86
77
  bindQueue(queue: string, exchange: string): Promise<void>;
@@ -93,7 +84,7 @@ declare class RabbitMq implements IAfRabbitMq {
93
84
  private unlockRedisIfNeeded;
94
85
  private consumeFromRabbit;
95
86
  consumeFromExchange(queue: string, exchange: string, callback: CallbackFunction, options?: ConsumeOptions): Promise<any>;
96
- publish(exchange: string, content: any, customHeaders?: any): Promise<boolean>;
87
+ publish(exchange: string, content: any, customHeaders?: any, options?: PublishOptions): Promise<boolean>;
97
88
  sendToQueue(queue: string, content: any, options?: any, customHeaders?: any): Promise<boolean | undefined>;
98
89
  isConnected(): Promise<boolean>;
99
90
  gracefulShutdown(signal: string): Promise<void>;
package/dist/index.js CHANGED
@@ -7,6 +7,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
7
7
  exports.sendCeleryTaskViaHttp = void 0;
8
8
  const events_1 = require("events");
9
9
  const util_1 = require("util");
10
+ const lodash_1 = require("lodash");
10
11
  const moment_1 = __importDefault(require("moment"));
11
12
  const redis_lock_1 = __importDefault(require("redis-lock"));
12
13
  const amqp_connection_manager_1 = require("amqp-connection-manager");
@@ -244,14 +245,14 @@ class RabbitMq {
244
245
  throw err;
245
246
  }
246
247
  }
247
- async assertChannel({ force = false } = {}) {
248
+ async assertChannel({ force = false, newChannelOptions = {} } = {}) {
248
249
  if (!this.publishChannelSetupPromise) {
249
250
  this.publishChannelSetupPromise = new Promise(async (resolve, reject) => {
250
251
  if (this.channel && !force) {
251
252
  return resolve(this.channel);
252
253
  }
253
254
  try {
254
- const channel = await this.getNewChannel({});
255
+ const channel = await this.getNewChannel({ options: newChannelOptions });
255
256
  channel.on('error', (err) => {
256
257
  logger_1.default.error('rabbit: channel error', { err });
257
258
  });
@@ -265,7 +266,7 @@ class RabbitMq {
265
266
  }
266
267
  return this.publishChannelSetupPromise;
267
268
  }
268
- async assertExchange(exchangeName, options) {
269
+ async assertExchange(exchangeName) {
269
270
  const channel = await this.assertChannel();
270
271
  if (this.exchanges[exchangeName]) {
271
272
  delete this.assertExchangePromises[exchangeName];
@@ -404,6 +405,9 @@ class RabbitMq {
404
405
  return channel.addSetup(async (confirmChannel) => {
405
406
  const q = await this.assertQueue(queue, optionsWithDefaults);
406
407
  await confirmChannel.prefetch(limit, false);
408
+ const channelConsumerOptions = optionsWithDefaults.channelConsumeOptions
409
+ ? (0, lodash_1.merge)(types_1.CONSUMER_DEFAULT_OPTIONS, optionsWithDefaults.channelConsumeOptions)
410
+ : types_1.CONSUMER_DEFAULT_OPTIONS;
407
411
  const { consumerTag } = await confirmChannel.consume(queue, async (msg) => {
408
412
  if (!msg) {
409
413
  return null;
@@ -468,7 +472,7 @@ class RabbitMq {
468
472
  catch (e) {
469
473
  await localNack(msg);
470
474
  }
471
- }, types_1.CONSUMER_DEFAULT_OPTIONS);
475
+ }, channelConsumerOptions);
472
476
  if (!consumerTag) {
473
477
  logger_1.default.error(`rabbit: failed to consume from queue ${queue}`);
474
478
  }
@@ -489,24 +493,24 @@ class RabbitMq {
489
493
  const assertExchange = await (0, utils_1.assertExchangeFanout)(c, exchange);
490
494
  await c.assertQueue(queue);
491
495
  this.exchanges[exchange] = assertExchange;
492
- await c.prefetch(limit, false);
496
+ await c.prefetch(limit, true);
493
497
  return Promise.all([
494
498
  c.bindQueue(queue, exchange, ''),
495
499
  this.consume(queue, callback, options),
496
500
  ]);
497
501
  });
498
502
  }
499
- async publish(exchange, content, customHeaders) {
503
+ async publish(exchange, content, customHeaders, options) {
500
504
  return (0, utils_1.wrapSetImmediate)(async () => {
501
505
  RabbitMq.validateName('exchange', exchange);
502
- const channel = await this.assertChannel();
506
+ const channel = await this.assertChannel({ newChannelOptions: options?.newChannelOptions });
503
507
  await this.assertExchange(exchange);
504
508
  await channel.publish(exchange, '', Buffer.from(JSON.stringify(content)), RabbitMq.getPublishOptions(customHeaders));
505
509
  });
506
510
  }
507
511
  async sendToQueue(queue, content, options, customHeaders) {
508
512
  try {
509
- await this.assertChannel();
513
+ await this.assertChannel({ newChannelOptions: options?.newChannelOptions });
510
514
  }
511
515
  catch (e) {
512
516
  logger_1.default.error(`rabbit sendToQueue: failed to send assert channel when sending to queue ${queue}`, { e });
@@ -1,3 +1,4 @@
1
+ import { CreateChannelOpts } from 'amqp-connection-manager';
1
2
  import { ConsumeMessage, Options, Replies } from 'amqplib';
2
3
  export interface ExchangesCache {
3
4
  [key: string]: any;
@@ -25,16 +26,22 @@ export interface ConsumeOptions {
25
26
  useConsumeWithLock?: boolean;
26
27
  auditContext?: any;
27
28
  enableRabbitTrace?: boolean;
29
+ channelConsumeOptions?: Options.Consume;
28
30
  }
29
31
  export type CallbackFunction = (msg: ConsumeMessage, ack: any, nack: any) => Promise<any>;
30
32
  export type newChannelOpts = {
31
33
  name?: string;
32
34
  onClose?: null | ((args: any | null) => void);
35
+ options?: CreateChannelOpts | undefined;
33
36
  };
34
37
  export type assertChannelOpts = {
35
38
  channelName?: string;
36
39
  force?: boolean;
40
+ newChannelOptions?: newChannelOpts;
37
41
  };
42
+ export interface PublishOptions {
43
+ newChannelOptions?: newChannelOpts;
44
+ }
38
45
  export type AfConsumer = {
39
46
  queue: string;
40
47
  callback: CallbackFunction;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/rabbit",
3
- "version": "3.2.24",
3
+ "version": "3.2.25-beta.0",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "engines": {
@@ -22,6 +22,7 @@
22
22
  "amqp-connection-manager": "4.1.9",
23
23
  "amqplib": "0.10.3",
24
24
  "bluebird": "^3.7.2",
25
+ "lodash": "^4.17.21",
25
26
  "moment": "^2.29.1",
26
27
  "redis": "^3.1.2",
27
28
  "redis-lock": "^0.1.4"
@@ -33,6 +34,7 @@
33
34
  "@autofleet/logger": "4.0.6",
34
35
  "@types/amqplib": "0.8.2",
35
36
  "@types/jest": "^29.5.12",
37
+ "@types/lodash": "^4.17.12",
36
38
  "@types/node": "^20.14.11",
37
39
  "@typescript-eslint/eslint-plugin": "^4.8.1",
38
40
  "eslint": "^7.13.0",
package/src/index.ts CHANGED
@@ -2,10 +2,11 @@
2
2
 
3
3
  import { EventEmitter, once } from 'events';
4
4
  import { promisify } from 'util';
5
+ import { merge } from 'lodash';
5
6
  import moment from 'moment';
6
7
  import RedisLock from 'redis-lock';
7
8
  import {
8
- AmqpConnectionManager, ChannelWrapper, connect, CreateChannelOpts,
9
+ AmqpConnectionManager, ChannelWrapper, connect,
9
10
  } from 'amqp-connection-manager';
10
11
  import {
11
12
  ConfirmChannel, ConsumeMessage, Options, Replies,
@@ -40,6 +41,9 @@ import {
40
41
  CONSUMER_DEFAULT_OPTIONS,
41
42
  QueueSetupPromisesDictionary,
42
43
  AssertExchangePromisesDictionary,
44
+ assertChannelOpts,
45
+ newChannelOpts,
46
+ PublishOptions,
43
47
  } from './lib/types';
44
48
 
45
49
  // const debug = nodeDebug('af-rabbitmq')
@@ -82,17 +86,6 @@ export interface AfRabbitOptions {
82
86
  rabbitHost?: string;
83
87
  }
84
88
 
85
- type newChannelOpts = {
86
- name?: string;
87
- onClose?: null | ((args: any | null) => void);
88
- options?: CreateChannelOpts | undefined;
89
- };
90
-
91
- type assertChannelOpts = {
92
- channelName?: string;
93
- force?: boolean;
94
- }
95
-
96
89
  type AfConsumer = {
97
90
  queue: string;
98
91
  callback: CallbackFunction;
@@ -386,7 +379,7 @@ class RabbitMq implements IAfRabbitMq {
386
379
  }
387
380
  }
388
381
 
389
- async assertChannel({ force = false } : assertChannelOpts = {}): Promise<ChannelWrapper> {
382
+ async assertChannel({ force = false, newChannelOptions = {} }: assertChannelOpts = {}): Promise<ChannelWrapper> {
390
383
  if (!this.publishChannelSetupPromise) {
391
384
  this.publishChannelSetupPromise = new Promise<ChannelWrapper>(async (resolve, reject) => {
392
385
  if (this.channel && !force) {
@@ -394,7 +387,7 @@ class RabbitMq implements IAfRabbitMq {
394
387
  }
395
388
 
396
389
  try {
397
- const channel = await this.getNewChannel({});
390
+ const channel = await this.getNewChannel({ options: newChannelOptions });
398
391
  channel.on('error', (err) => {
399
392
  logger.error('rabbit: channel error', { err });
400
393
  });
@@ -408,7 +401,7 @@ class RabbitMq implements IAfRabbitMq {
408
401
  return this.publishChannelSetupPromise;
409
402
  }
410
403
 
411
- async assertExchange(exchangeName: string, options?: any) {
404
+ async assertExchange(exchangeName: string) {
412
405
  const channel: ChannelWrapper = await this.assertChannel();
413
406
 
414
407
  if (this.exchanges[exchangeName]) {
@@ -572,6 +565,10 @@ class RabbitMq implements IAfRabbitMq {
572
565
  return channel.addSetup(async (confirmChannel: ConfirmChannel) => {
573
566
  const q = await this.assertQueue(queue, optionsWithDefaults);
574
567
  await confirmChannel.prefetch(limit, false);
568
+ const channelConsumerOptions = optionsWithDefaults.channelConsumeOptions
569
+ ? merge(CONSUMER_DEFAULT_OPTIONS, optionsWithDefaults.channelConsumeOptions)
570
+ : CONSUMER_DEFAULT_OPTIONS;
571
+
575
572
  const { consumerTag } = await confirmChannel.consume(
576
573
  queue,
577
574
  async (msg: ConsumeMessageOrNull) => {
@@ -647,7 +644,8 @@ class RabbitMq implements IAfRabbitMq {
647
644
  } catch (e) {
648
645
  await localNack(msg);
649
646
  }
650
- }, CONSUMER_DEFAULT_OPTIONS,
647
+ },
648
+ channelConsumerOptions,
651
649
  );
652
650
  if (!consumerTag) {
653
651
  logger.error(`rabbit: failed to consume from queue ${queue}`);
@@ -670,7 +668,7 @@ class RabbitMq implements IAfRabbitMq {
670
668
  const assertExchange = await assertExchangeFanout(c, exchange);
671
669
  await c.assertQueue(queue);
672
670
  this.exchanges[exchange] = assertExchange;
673
- await c.prefetch(limit, false);
671
+ await c.prefetch(limit, true);
674
672
  return Promise.all([
675
673
  c.bindQueue(queue, exchange, ''),
676
674
  this.consume(
@@ -682,10 +680,10 @@ class RabbitMq implements IAfRabbitMq {
682
680
  });
683
681
  }
684
682
 
685
- async publish(exchange: string, content: any, customHeaders?: any) : Promise<boolean> {
683
+ async publish(exchange: string, content: any, customHeaders?: any, options?: PublishOptions): Promise<boolean> {
686
684
  return wrapSetImmediate(async () => {
687
685
  RabbitMq.validateName('exchange', exchange);
688
- const channel: ChannelWrapper = await this.assertChannel();
686
+ const channel: ChannelWrapper = await this.assertChannel({ newChannelOptions: options?.newChannelOptions });
689
687
  await this.assertExchange(exchange);
690
688
  await channel.publish(exchange, '',
691
689
  Buffer.from(JSON.stringify(content)),
@@ -700,7 +698,7 @@ class RabbitMq implements IAfRabbitMq {
700
698
  customHeaders?: any,
701
699
  ): Promise<boolean | undefined> {
702
700
  try {
703
- await this.assertChannel();
701
+ await this.assertChannel({ newChannelOptions: options?.newChannelOptions });
704
702
  } catch (e) {
705
703
  logger.error(`rabbit sendToQueue: failed to send assert channel when sending to queue ${queue}`, { e });
706
704
  throw e;
package/src/lib/types.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { CreateChannelOpts } from 'amqp-connection-manager';
1
2
  import { ConsumeMessage, Options, Replies } from 'amqplib';
2
3
 
3
4
  export interface ExchangesCache {
@@ -31,6 +32,7 @@ export interface ConsumeOptions {
31
32
  useConsumeWithLock?: boolean;
32
33
  auditContext?: any;
33
34
  enableRabbitTrace?: boolean;
35
+ channelConsumeOptions?: Options.Consume;
34
36
  }
35
37
 
36
38
  export type CallbackFunction = (msg: ConsumeMessage, ack: any, nack: any) => Promise<any>
@@ -38,11 +40,18 @@ export type CallbackFunction = (msg: ConsumeMessage, ack: any, nack: any) => Pro
38
40
  export type newChannelOpts = {
39
41
  name?: string;
40
42
  onClose?: null | ((args: any | null) => void);
43
+ options?: CreateChannelOpts | undefined;
41
44
  };
42
45
  export type assertChannelOpts = {
43
46
  channelName?: string;
44
47
  force?: boolean;
48
+ newChannelOptions?: newChannelOpts;
45
49
  }
50
+
51
+ export interface PublishOptions {
52
+ newChannelOptions?: newChannelOpts;
53
+ }
54
+
46
55
  export type AfConsumer = {
47
56
  queue: string;
48
57
  callback: CallbackFunction;