@autofleet/rabbit 2.1.8-beta10 → 2.1.8-beta3

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
@@ -2,35 +2,15 @@
2
2
  import { Options } from 'amqplib';
3
3
  import { AmqpConnectionManager, ChannelWrapper } from 'amqp-connection-manager';
4
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
5
  interface ExchangesCache {
19
6
  [key: string]: any;
20
7
  }
21
- declare type CustomMessageHeaders = {
22
- redisTimestampValidationKey?: string;
23
- };
24
8
  export interface AfRabbitOptions {
25
9
  reconnect: boolean;
26
10
  }
27
- declare class RabbitMq implements IAfRabbitMq {
11
+ declare class RabbitMq {
28
12
  static parseMsg(msg: any): any;
29
13
  static validateName(type: string, name: string): void;
30
- static getPublishOptions(customHeaders?: CustomMessageHeaders): {
31
- timestamp: number;
32
- headers: CustomMessageHeaders;
33
- };
34
14
  PUBLISH_TIMEOUT: number;
35
15
  PUBLISH_ERROR_MSG: string;
36
16
  DISCONNECT_MSG: string;
@@ -41,22 +21,19 @@ declare class RabbitMq implements IAfRabbitMq {
41
21
  creatingConnection: boolean;
42
22
  exchanges: ExchangesCache;
43
23
  options: AfRabbitOptions | undefined;
44
- redisClient: any;
45
- constructor(options?: AfRabbitOptions, redisConfig?: RedisConfig);
46
- private shouldConsumeMessageByTimestamp;
47
- ack: (channel: ChannelWrapper, shouldUpdateRedisTimestamp?: boolean) => (msg: any) => Promise<void>;
48
- nack: (channel: ChannelWrapper, queue: string, options: any, deadQueueOptions: Options.AssertQueue) => (msg: any, { skipRetry, }?: any) => Promise<void>;
24
+ constructor(options?: AfRabbitOptions);
25
+ ack: (msg: any) => Promise<void>;
26
+ nack: (queue: string, options: any, deadQueueOptions: Options.AssertQueue) => (msg: any, { skipRetry, }?: any) => Promise<void>;
49
27
  getConnection(): Promise<AmqpConnectionManager>;
50
28
  getNewChannel(): Promise<ChannelWrapper>;
51
29
  assertChannel(): Promise<ChannelWrapper>;
52
30
  assertExchange(exchangeName: string, options?: any): Promise<any>;
53
- getQueueLength(queue: string): Promise<import("amqplib").Replies.AssertQueue>;
54
- bindQueue(queue: string, exchange: string): Promise<void>;
31
+ getQueueLength(queue: string): Promise<any>;
32
+ bindQueue(queue: string, exchange: string): Promise<any>;
55
33
  assertQueue(queue: string, options?: Options.AssertQueue): Promise<void>;
56
34
  consume(queue: string, callback: (msg: any, ack: Function, nack: Function) => any, options?: any): Promise<void>;
57
35
  consumeFromExchange(queue: string, exchange: string, callback: (msg: any, ack: Function, nack: Function) => any, options?: any): Promise<void>;
58
- publish(exchange: string, content: any, customHeaders?: any): Promise<unknown>;
59
- sendToQueue(queue: string, content: any, options?: any, customHeaders?: any): Promise<boolean>;
60
- isConnected(): Promise<boolean>;
36
+ publish(exchange: string, content: any): Promise<unknown>;
37
+ sendToQueue(queue: string, content: any, options?: any): Promise<void>;
61
38
  }
62
39
  export default RabbitMq;
package/dist/index.js CHANGED
@@ -34,49 +34,31 @@ Object.defineProperty(exports, "__esModule", { value: true });
34
34
  /* eslint-disable @typescript-eslint/ban-ts-comment,@typescript-eslint/ban-types,@typescript-eslint/no-unused-vars,consistent-return,no-async-promise-executor,no-param-reassign,no-empty */
35
35
  // eslint-disable-next-line max-classes-per-file
36
36
  const bluebird = __importStar(require("bluebird"));
37
- const moment_1 = __importDefault(require("moment"));
38
37
  const amqp_connection_manager_1 = require("amqp-connection-manager");
39
38
  const events_1 = require("events");
40
- // import { newTrace, traceTypes } from '@autofleet/outbreak';
41
- const logger_1 = __importDefault(require("./logger"));
39
+ const outbreak_1 = require("@autofleet/outbreak");
42
40
  const rabbitError_1 = __importDefault(require("./rabbitError"));
43
- const redis_1 = __importDefault(require("./redis"));
44
41
  const DEFAULT_DEAD_TTL_TWO_DAYS = 60000 * 60 * 48;
45
42
  const defaultOptions = {
46
43
  limit: 1,
47
44
  retries: 1,
48
45
  deadMessageTtl: DEFAULT_DEAD_TTL_TWO_DAYS,
49
46
  };
50
- const SECONDS_TO_MILLISECONDS = 1000;
51
47
  const assertExchangeFanout = (c, exchangeName) => c.assertExchange(exchangeName, 'fanout');
52
48
  const connectionCreatedConst = 'connectionCreated';
53
49
  class RabbitMq {
54
- constructor(options, redisConfig) {
50
+ constructor(options) {
55
51
  this.PUBLISH_TIMEOUT = Number(process.env.RABBITMQ_PUBLISH_TIMEOUT) || 60000;
56
52
  this.PUBLISH_ERROR_MSG = `rabbit: publish timeout(${this.PUBLISH_TIMEOUT}ms) has pass, exchange: `;
57
53
  this.DISCONNECT_MSG = 'rabbit: connection disconnect';
58
54
  this.RESCONNECT_MSG = 'rabbit: reconnecting';
59
- this.shouldConsumeMessageByTimestamp = (msg) => __awaiter(this, void 0, void 0, function* () {
60
- const { properties: { timestamp, headers } } = msg;
61
- if (timestamp && (headers === null || headers === void 0 ? void 0 : headers.redisTimestampValidationKey) && this.redisClient) {
62
- const lastMessageTimestamp = yield this.redisClient.getAsync(headers.redisTimestampValidationKey);
63
- logger_1.default.info('rabbit: checking if should consume params', {
64
- msg, timestamp, headers, lastMessageTimestamp,
65
- });
66
- return !lastMessageTimestamp || (parseInt(lastMessageTimestamp, 10) <= parseInt(timestamp, 10));
67
- }
68
- return true;
69
- });
70
- this.ack = (channel, shouldUpdateRedisTimestamp = false) => (msg) => __awaiter(this, void 0, void 0, function* () {
71
- yield channel.ack(msg);
72
- const { properties: { timestamp, headers } } = msg;
73
- if (shouldUpdateRedisTimestamp && timestamp && (headers === null || headers === void 0 ? void 0 : headers.redisTimestampValidationKey) && this.redisClient) {
74
- const parsedTimestamp = parseInt(timestamp, 10);
75
- yield this.redisClient.setAsync(headers.redisTimestampValidationKey, parsedTimestamp, 'EX', 3600);
55
+ this.ack = (msg) => __awaiter(this, void 0, void 0, function* () {
56
+ if (this.channel) {
57
+ yield this.channel.ack(msg);
76
58
  }
77
59
  });
78
- this.nack = (channel, queue, options, deadQueueOptions) => (msg, { skipRetry = false, } = {}) => __awaiter(this, void 0, void 0, function* () {
79
- if (channel) {
60
+ this.nack = (queue, options, deadQueueOptions) => (msg, { skipRetry = false, } = {}) => __awaiter(this, void 0, void 0, function* () {
61
+ if (this.channel) {
80
62
  if (!skipRetry
81
63
  && (!msg.content['x-retry-count']
82
64
  || msg.content['x-retry-count'] < options.retries)) {
@@ -88,7 +70,7 @@ class RabbitMq {
88
70
  yield this.assertQueue(deadQueue, deadQueueOptions);
89
71
  yield this.sendToQueue(deadQueue, msg.content, deadQueueOptions);
90
72
  }
91
- yield channel.ack(msg);
73
+ yield this.channel.ack(msg);
92
74
  }
93
75
  });
94
76
  this.em = new events_1.EventEmitter();
@@ -97,7 +79,6 @@ class RabbitMq {
97
79
  this.creatingConnection = false;
98
80
  this.exchanges = {};
99
81
  this.options = options;
100
- this.redisClient = redisConfig && redis_1.default(redisConfig);
101
82
  }
102
83
  static parseMsg(msg) {
103
84
  let { content } = msg;
@@ -113,27 +94,19 @@ class RabbitMq {
113
94
  throw new rabbitError_1.default(`error while using ${type} with no name`);
114
95
  }
115
96
  }
116
- static getPublishOptions(customHeaders = {}) {
117
- return {
118
- timestamp: moment_1.default().unix(),
119
- headers: customHeaders,
120
- };
121
- }
122
97
  getConnection() {
123
98
  return __awaiter(this, void 0, void 0, function* () {
124
99
  return new Promise((resolve) => __awaiter(this, void 0, void 0, function* () {
125
100
  if (this.creatingConnection) {
126
- this.em.once(connectionCreatedConst, resolve);
101
+ this.em.on(connectionCreatedConst, resolve);
127
102
  return;
128
103
  }
129
104
  if (this.connection !== null) {
130
- return resolve(this.connection);
105
+ return this.connection;
131
106
  }
132
107
  this.creatingConnection = true;
133
- const username = process.env.RABBITMQ_USERNAME || 'guest';
134
- const password = process.env.RABBITMQ_PASSWORD || 'guest';
135
108
  const host = process.env.RABBITMQ_SERVICE_HOST || '';
136
- const connection = yield amqp_connection_manager_1.connect([`amqp://${username}:${password}@${host}`]);
109
+ const connection = yield amqp_connection_manager_1.connect([`amqp://${host}`]);
137
110
  this.connection = connection;
138
111
  this.connection.on('disconnect', ({ err }) => {
139
112
  var _a;
@@ -163,8 +136,8 @@ class RabbitMq {
163
136
  if (this.channel) {
164
137
  return resolve(this.channel);
165
138
  }
139
+ const connection = yield this.getConnection();
166
140
  try {
167
- const connection = yield this.getConnection();
168
141
  if (this.channel === null) {
169
142
  this.channel = connection.createChannel({});
170
143
  }
@@ -221,21 +194,10 @@ class RabbitMq {
221
194
  yield c.assertQueue(queue);
222
195
  yield c.prefetch(limit, true);
223
196
  return Promise.all([
224
- c.consume(queue, (msg) => __awaiter(this, void 0, void 0, function* () {
225
- const parsedMessage = RabbitMq.parseMsg(msg);
226
- // newTrace(traceTypes.RABBIT);
227
- const shouldConsume = yield this.shouldConsumeMessageByTimestamp(parsedMessage);
228
- logger_1.default.info('rabbit: trying to consume', { queue, shouldConsume, msg });
229
- if (!shouldConsume) {
230
- return this.ack(channel)(msg);
231
- }
232
- try {
233
- yield callback(parsedMessage, this.ack(channel, true), this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl }));
234
- }
235
- catch (e) {
236
- yield this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl });
237
- }
238
- })),
197
+ c.consume(queue, (msg) => {
198
+ outbreak_1.newTrace(outbreak_1.traceTypes.RABBIT);
199
+ callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue, optionsWithDefaults, { messageTtl: deadMessageTtl }));
200
+ }),
239
201
  ]);
240
202
  }));
241
203
  });
@@ -254,49 +216,31 @@ class RabbitMq {
254
216
  yield c.prefetch(limit, true);
255
217
  return Promise.all([
256
218
  c.bindQueue(queue, exchange, ''),
257
- c.consume(queue, (msg) => __awaiter(this, void 0, void 0, function* () {
258
- const parsedMessage = RabbitMq.parseMsg(msg);
259
- // newTrace(traceTypes.RABBIT);
260
- const shouldConsume = yield this.shouldConsumeMessageByTimestamp(parsedMessage);
261
- logger_1.default.info('rabbit: trying to consume', { queue, shouldConsume, msg });
262
- if (!shouldConsume) {
263
- return this.ack(channel)(msg);
264
- }
265
- callback(parsedMessage, this.ack(channel, true), this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl }));
266
- })),
219
+ c.consume(queue, (msg) => {
220
+ outbreak_1.newTrace(outbreak_1.traceTypes.RABBIT);
221
+ callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue, optionsWithDefaults, { messageTtl: deadMessageTtl }));
222
+ }),
267
223
  ]);
268
224
  }));
269
225
  });
270
226
  }
271
- publish(exchange, content, customHeaders) {
227
+ publish(exchange, content) {
272
228
  return __awaiter(this, void 0, void 0, function* () {
273
229
  RabbitMq.validateName('exchange', exchange);
274
230
  const channel = yield this.assertChannel();
275
231
  yield this.assertExchange(exchange);
276
- return new bluebird.Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
277
- try {
278
- yield channel.publish(exchange, '', Buffer.from(JSON.stringify(content)), RabbitMq.getPublishOptions(customHeaders));
279
- return resolve();
280
- }
281
- catch (e) {
282
- reject(e);
283
- }
232
+ return new bluebird.Promise((resolve) => __awaiter(this, void 0, void 0, function* () {
233
+ yield channel.publish(exchange, '', Buffer.from(JSON.stringify(content)));
234
+ return resolve();
284
235
  })).timeout(this.PUBLISH_TIMEOUT, `${this.PUBLISH_ERROR_MSG}${exchange}`);
285
236
  });
286
237
  }
287
- sendToQueue(queue, content, options, customHeaders) {
238
+ sendToQueue(queue, content, options) {
288
239
  return __awaiter(this, void 0, void 0, function* () {
289
240
  RabbitMq.validateName('queue', queue);
290
241
  const channel = yield this.assertChannel();
291
242
  yield this.assertQueue(queue, options);
292
- logger_1.default.info('rabbit: sending to queue', { queue, content, customHeaders });
293
- return channel.sendToQueue(queue, Buffer.from(JSON.stringify(content)), RabbitMq.getPublishOptions(customHeaders));
294
- });
295
- }
296
- isConnected() {
297
- return __awaiter(this, void 0, void 0, function* () {
298
- const connection = yield this.getConnection();
299
- return connection.isConnected();
243
+ return channel.sendToQueue(queue, Buffer.from(JSON.stringify(content)));
300
244
  });
301
245
  }
302
246
  }
package/dist/mock.d.ts CHANGED
@@ -1,14 +1,13 @@
1
1
  import 'jest';
2
- import { IAfRabbitMq } from './index';
3
- declare class RabbitMq implements IAfRabbitMq {
4
- ack: any;
5
- nack: any;
6
- assertChannel: any;
7
- assertExchange: any;
8
- assertQueue: any;
9
- consume: any;
10
- consumeFromExchange: any;
11
- publish: any;
12
- sendToQueue: any;
2
+ declare class RabbitMq {
3
+ ack: jest.Mock<unknown>;
4
+ nack: jest.Mock<unknown>;
5
+ assertChannel: jest.Mock<unknown>;
6
+ assertExchange: jest.Mock<unknown>;
7
+ assertQueue: jest.Mock<unknown>;
8
+ consume: jest.Mock<unknown>;
9
+ consumeFromExchange: jest.Mock<unknown>;
10
+ publish: jest.Mock<unknown>;
11
+ sendToQueue: jest.Mock<unknown>;
13
12
  }
14
13
  export default RabbitMq;
package/dist/mock.js CHANGED
@@ -1,6 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- // eslint-disable-next-line import/no-extraneous-dependencies
4
3
  require("jest");
5
4
  class RabbitMq {
6
5
  constructor() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/rabbit",
3
- "version": "2.1.8-beta10",
3
+ "version": "2.1.8-beta3",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
package/src/index.ts CHANGED
@@ -259,15 +259,7 @@ class RabbitMq implements IAfRabbitMq {
259
259
  if (!shouldConsume) {
260
260
  return this.ack(channel)(msg);
261
261
  }
262
- try {
263
- await callback(
264
- parsedMessage,
265
- this.ack(channel, true),
266
- this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl }),
267
- );
268
- } catch (e) {
269
- await this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl });
270
- }
262
+ callback(parsedMessage, this.ack(channel, true), this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl }));
271
263
  },
272
264
  ),
273
265
  ]);
package/dist/logger.d.ts DELETED
@@ -1,2 +0,0 @@
1
- declare const logger: any;
2
- export default logger;
package/dist/logger.js DELETED
@@ -1,5 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const Logger = require('@autofleet/logger');
4
- const logger = Logger();
5
- exports.default = logger;
package/dist/redis.d.ts DELETED
@@ -1,7 +0,0 @@
1
- export declare type RedisConfig = {
2
- host: string;
3
- port: number | undefined;
4
- prefix?: string;
5
- };
6
- declare const getRedisInstance: (config: RedisConfig) => any;
7
- export default getRedisInstance;
package/dist/redis.js DELETED
@@ -1,11 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const bluebird_1 = __importDefault(require("bluebird"));
7
- const redis = require('redis');
8
- bluebird_1.default.promisifyAll(redis.RedisClient.prototype);
9
- bluebird_1.default.promisifyAll(redis.Multi.prototype);
10
- const getRedisInstance = (config) => redis.createClient(config);
11
- exports.default = getRedisInstance;