@autofleet/rabbit 2.1.7 → 2.1.8-beta1

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/.env ADDED
File without changes
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,11 +21,9 @@ 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>;
@@ -55,8 +33,7 @@ declare class RabbitMq implements IAfRabbitMq {
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<void>;
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
39
  const outbreak_1 = require("@autofleet/outbreak");
41
- const logger_1 = __importDefault(require("./logger"));
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 || moment_1.default(parseInt(lastMessageTimestamp, 10) * SECONDS_TO_MILLISECONDS).isBefore(moment_1.default(timestamp * SECONDS_TO_MILLISECONDS));
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,12 +94,6 @@ 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* () {
@@ -127,13 +102,11 @@ class RabbitMq {
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;
@@ -221,16 +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);
197
+ c.consume(queue, (msg) => {
226
198
  outbreak_1.newTrace(outbreak_1.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
- callback(parsedMessage, this.ack(channel, true), this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl }));
233
- })),
199
+ callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue, optionsWithDefaults, { messageTtl: deadMessageTtl }));
200
+ }),
234
201
  ]);
235
202
  }));
236
203
  });
@@ -249,49 +216,31 @@ class RabbitMq {
249
216
  yield c.prefetch(limit, true);
250
217
  return Promise.all([
251
218
  c.bindQueue(queue, exchange, ''),
252
- c.consume(queue, (msg) => __awaiter(this, void 0, void 0, function* () {
253
- const parsedMessage = RabbitMq.parseMsg(msg);
219
+ c.consume(queue, (msg) => {
254
220
  outbreak_1.newTrace(outbreak_1.traceTypes.RABBIT);
255
- const shouldConsume = yield this.shouldConsumeMessageByTimestamp(parsedMessage);
256
- logger_1.default.info('rabbit: trying to consume', { queue, shouldConsume, msg });
257
- if (!shouldConsume) {
258
- return this.ack(channel)(msg);
259
- }
260
- callback(parsedMessage, this.ack(channel, true), this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl }));
261
- })),
221
+ callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue, optionsWithDefaults, { messageTtl: deadMessageTtl }));
222
+ }),
262
223
  ]);
263
224
  }));
264
225
  });
265
226
  }
266
- publish(exchange, content, customHeaders) {
227
+ publish(exchange, content) {
267
228
  return __awaiter(this, void 0, void 0, function* () {
268
229
  RabbitMq.validateName('exchange', exchange);
269
230
  const channel = yield this.assertChannel();
270
231
  yield this.assertExchange(exchange);
271
- return new bluebird.Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
272
- try {
273
- yield channel.publish(exchange, '', Buffer.from(JSON.stringify(content)), RabbitMq.getPublishOptions(customHeaders));
274
- return resolve();
275
- }
276
- catch (e) {
277
- reject(e);
278
- }
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();
279
235
  })).timeout(this.PUBLISH_TIMEOUT, `${this.PUBLISH_ERROR_MSG}${exchange}`);
280
236
  });
281
237
  }
282
- sendToQueue(queue, content, options, customHeaders) {
238
+ sendToQueue(queue, content, options) {
283
239
  return __awaiter(this, void 0, void 0, function* () {
284
240
  RabbitMq.validateName('queue', queue);
285
241
  const channel = yield this.assertChannel();
286
242
  yield this.assertQueue(queue, options);
287
- logger_1.default.info('rabbit: sending to queue', { queue, content, customHeaders });
288
- return channel.sendToQueue(queue, Buffer.from(JSON.stringify(content)), RabbitMq.getPublishOptions(customHeaders));
289
- });
290
- }
291
- isConnected() {
292
- return __awaiter(this, void 0, void 0, function* () {
293
- const connection = yield this.getConnection();
294
- return connection.isConnected();
243
+ return channel.sendToQueue(queue, Buffer.from(JSON.stringify(content)));
295
244
  });
296
245
  }
297
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/dump.rdb ADDED
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/rabbit",
3
- "version": "2.1.7",
3
+ "version": "2.1.8-beta1",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
package/src/index.ts CHANGED
@@ -114,7 +114,7 @@ class RabbitMq implements IAfRabbitMq {
114
114
  logger.info('rabbit: checking if should consume params', {
115
115
  msg, timestamp, headers, lastMessageTimestamp,
116
116
  });
117
- return !lastMessageTimestamp || moment(parseInt(lastMessageTimestamp, 10) * SECONDS_TO_MILLISECONDS).isBefore(moment(timestamp * SECONDS_TO_MILLISECONDS));
117
+ return !lastMessageTimestamp || (parseInt(lastMessageTimestamp, 10) <= parseInt(timestamp, 10));
118
118
  }
119
119
  return true;
120
120
  }
@@ -159,7 +159,7 @@ class RabbitMq implements IAfRabbitMq {
159
159
  async getConnection() {
160
160
  return new Promise<AmqpConnectionManager>(async (resolve) => {
161
161
  if (this.creatingConnection) {
162
- this.em.on(connectionCreatedConst, resolve);
162
+ this.em.once(connectionCreatedConst, resolve);
163
163
  return;
164
164
  }
165
165
  if (this.connection !== null) {
@@ -197,9 +197,8 @@ class RabbitMq implements IAfRabbitMq {
197
197
  return resolve(this.channel);
198
198
  }
199
199
 
200
- const connection: AmqpConnectionManager = await this.getConnection();
201
-
202
200
  try {
201
+ const connection: AmqpConnectionManager = await this.getConnection();
203
202
  if (this.channel === null) {
204
203
  this.channel = connection.createChannel({});
205
204
  }
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;