@autofleet/rabbit 3.2.25-beta.103 → 3.2.25

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