@autofleet/rabbit 3.2.19 → 3.2.20-beta-1

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 } 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;
@@ -66,6 +66,7 @@ declare class RabbitMq implements IAfRabbitMq {
66
66
  exchanges: ExchangesCache;
67
67
  queues: QueuesCache;
68
68
  queueSetupPromises: QueueSetupPromisesDictionary;
69
+ assertExchangePromises: AssertExchangePromisesDictionary;
69
70
  options: AfRabbitOptions | undefined;
70
71
  redisClient: any;
71
72
  redisLock?: RedisLockType;
package/dist/index.js CHANGED
@@ -123,6 +123,7 @@ class RabbitMq {
123
123
  this.exchanges = {};
124
124
  this.queues = {};
125
125
  this.queueSetupPromises = {};
126
+ this.assertExchangePromises = {};
126
127
  this.consumers = [];
127
128
  this.options = options;
128
129
  this.redisClient = redisConfig && (0, redis_1.default)(redisConfig);
@@ -266,11 +267,15 @@ class RabbitMq {
266
267
  async assertExchange(exchangeName, options) {
267
268
  const channel = await this.assertChannel();
268
269
  if (this.exchanges[exchangeName]) {
270
+ delete this.assertExchangePromises[exchangeName];
269
271
  return this.exchanges[exchangeName];
270
272
  }
271
- const exchange = await (0, utils_1.assertExchangeFanout)(channel, exchangeName);
272
- this.exchanges[exchangeName] = exchange;
273
- return exchange;
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];
274
279
  }
275
280
  async getQueueLength(queue) {
276
281
  RabbitMq.validateName('queue', queue);
@@ -383,6 +388,7 @@ class RabbitMq {
383
388
  }
384
389
  const traceId = msg.properties.headers[consts_1.TRACING_HEADER];
385
390
  const userId = msg.properties.headers[consts_1.USER_TRACING_HEADER];
391
+ const automationId = msg.properties.headers['x-af-automation-id'];
386
392
  const parsedMessage = RabbitMq.parseMsg(msg);
387
393
  const releaseLock = await this.lockRedisIfNeeded(parsedMessage, optionsWithDefaults);
388
394
  const trace = (0, zehut_1.newTrace)(zehut_1.traceTypes.RABBIT);
@@ -407,7 +413,10 @@ class RabbitMq {
407
413
  outbreakTrace?.context.set(consts_1.TRACING_HEADER, traceId);
408
414
  }
409
415
  if (auditContext) {
410
- await auditContext(queue);
416
+ await auditContext(queue, {
417
+ userId,
418
+ automationId,
419
+ });
411
420
  }
412
421
  const shouldConsume = await this.shouldConsumeMessageByTimestamp(parsedMessage);
413
422
  if (!shouldConsume) {
@@ -8,6 +8,9 @@ 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
+ }
11
14
  export type CustomMessageHeaders = {
12
15
  redisTimestampValidationKey?: string;
13
16
  };
@@ -1,5 +1,5 @@
1
1
  import { ChannelWrapper } from 'amqp-connection-manager';
2
- import { ConfirmChannel } from 'amqplib';
3
- export declare const assertExchangeFanout: (c: ChannelWrapper | ConfirmChannel, exchangeName: string) => Promise<import("amqplib").Replies.AssertExchange>;
2
+ import { ConfirmChannel, Replies } from 'amqplib';
3
+ export declare const assertExchangeFanout: (c: ChannelWrapper | ConfirmChannel, exchangeName: string) => Promise<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.19",
3
+ "version": "3.2.20-beta-1",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
package/src/index.ts CHANGED
@@ -35,7 +35,10 @@ import {
35
35
  CustomMessageHeaders,
36
36
  QueuesCache,
37
37
  RedisLockType,
38
- ExchangesCache, CONSUMER_DEFAULT_OPTIONS, QueueSetupPromisesDictionary,
38
+ ExchangesCache,
39
+ CONSUMER_DEFAULT_OPTIONS,
40
+ QueueSetupPromisesDictionary,
41
+ AssertExchangePromisesDictionary,
39
42
  } from './lib/types';
40
43
 
41
44
  // const debug = nodeDebug('af-rabbitmq')
@@ -157,6 +160,8 @@ class RabbitMq implements IAfRabbitMq {
157
160
 
158
161
  queueSetupPromises: QueueSetupPromisesDictionary;
159
162
 
163
+ assertExchangePromises: AssertExchangePromisesDictionary;
164
+
160
165
  options: AfRabbitOptions | undefined;
161
166
 
162
167
  redisClient: any;
@@ -177,6 +182,7 @@ class RabbitMq implements IAfRabbitMq {
177
182
  this.exchanges = {};
178
183
  this.queues = {};
179
184
  this.queueSetupPromises = {};
185
+ this.assertExchangePromises = {};
180
186
  this.consumers = [];
181
187
  this.options = options;
182
188
  this.redisClient = redisConfig && getRedisInstance(redisConfig);
@@ -403,12 +409,19 @@ class RabbitMq implements IAfRabbitMq {
403
409
 
404
410
  async assertExchange(exchangeName: string, options?: any) {
405
411
  const channel: ChannelWrapper = await this.assertChannel();
412
+
406
413
  if (this.exchanges[exchangeName]) {
414
+ delete this.assertExchangePromises[exchangeName];
407
415
  return this.exchanges[exchangeName];
408
416
  }
409
- const exchange = await assertExchangeFanout(channel, exchangeName);
410
- this.exchanges[exchangeName] = exchange;
411
- return exchange;
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];
412
425
  }
413
426
 
414
427
  async getQueueLength(queue: string) {
@@ -542,6 +555,7 @@ class RabbitMq implements IAfRabbitMq {
542
555
 
543
556
  const traceId = msg.properties.headers[TRACING_HEADER];
544
557
  const userId = msg.properties.headers[USER_TRACING_HEADER];
558
+ const automationId = msg.properties.headers['x-af-automation-id'];
545
559
  const parsedMessage = RabbitMq.parseMsg(msg);
546
560
  const releaseLock = await this.lockRedisIfNeeded(parsedMessage, optionsWithDefaults);
547
561
  const trace = newTrace(traceTypes.RABBIT);
@@ -567,7 +581,10 @@ class RabbitMq implements IAfRabbitMq {
567
581
  }
568
582
 
569
583
  if (auditContext) {
570
- await auditContext(queue);
584
+ await auditContext(queue, {
585
+ userId,
586
+ automationId,
587
+ });
571
588
  }
572
589
  const shouldConsume = await this.shouldConsumeMessageByTimestamp(parsedMessage);
573
590
  if (!shouldConsume) {
package/src/lib/types.ts CHANGED
@@ -12,6 +12,10 @@ 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
+
15
19
  export type CustomMessageHeaders = {
16
20
  redisTimestampValidationKey?: string;
17
21
  }
package/src/lib/utils.ts CHANGED
@@ -1,7 +1,11 @@
1
1
  import { ChannelWrapper } from 'amqp-connection-manager';
2
- import { ConfirmChannel } from 'amqplib';
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');
3
8
 
4
- export const assertExchangeFanout = async (c: ChannelWrapper | ConfirmChannel, exchangeName: string) => c.assertExchange(exchangeName, 'fanout');
5
9
  export const wrapSetImmediate = (callback: () => any) => new Promise<any>((resolve, reject) => {
6
10
  setImmediate(async () => {
7
11
  try {