@autofleet/rabbit 1.7.0 → 2.0.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
@@ -22,9 +22,10 @@ declare class RabbitMq {
22
22
  exchanges: ExchangesCache;
23
23
  options: AfRabbitOptions | undefined;
24
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>;
25
+ ack: (channel: ChannelWrapper) => (msg: any) => Promise<void>;
26
+ nack: (channel: ChannelWrapper, queue: string, options: any, deadQueueOptions: Options.AssertQueue) => (msg: any, { skipRetry, }?: any) => Promise<void>;
27
27
  getConnection(): Promise<AmqpConnectionManager>;
28
+ getNewChannel(): Promise<ChannelWrapper>;
28
29
  assertChannel(): Promise<ChannelWrapper>;
29
30
  assertExchange(exchangeName: string, options?: any): Promise<any>;
30
31
  getQueueLength(queue: string): Promise<any>;
package/dist/index.js CHANGED
@@ -52,13 +52,11 @@ class RabbitMq {
52
52
  this.PUBLISH_ERROR_MSG = `rabbit: publish timeout(${this.PUBLISH_TIMEOUT}ms) has pass, exchange: `;
53
53
  this.DISCONNECT_MSG = 'rabbit: connection disconnect';
54
54
  this.RESCONNECT_MSG = 'rabbit: reconnecting';
55
- this.ack = (msg) => __awaiter(this, void 0, void 0, function* () {
56
- if (this.channel) {
57
- yield this.channel.ack(msg);
58
- }
55
+ this.ack = (channel) => (msg) => __awaiter(this, void 0, void 0, function* () {
56
+ yield channel.ack(msg);
59
57
  });
60
- this.nack = (queue, options, deadQueueOptions) => (msg, { skipRetry = false, } = {}) => __awaiter(this, void 0, void 0, function* () {
61
- if (this.channel) {
58
+ this.nack = (channel, queue, options, deadQueueOptions) => (msg, { skipRetry = false, } = {}) => __awaiter(this, void 0, void 0, function* () {
59
+ if (channel) {
62
60
  if (!skipRetry
63
61
  && (!msg.content['x-retry-count']
64
62
  || msg.content['x-retry-count'] < options.retries)) {
@@ -70,7 +68,7 @@ class RabbitMq {
70
68
  yield this.assertQueue(deadQueue, deadQueueOptions);
71
69
  yield this.sendToQueue(deadQueue, msg.content, deadQueueOptions);
72
70
  }
73
- yield this.channel.ack(msg);
71
+ yield channel.ack(msg);
74
72
  }
75
73
  });
76
74
  this.em = new events_1.EventEmitter();
@@ -102,7 +100,7 @@ class RabbitMq {
102
100
  return;
103
101
  }
104
102
  if (this.connection !== null) {
105
- return this.connection;
103
+ return resolve(this.connection);
106
104
  }
107
105
  this.creatingConnection = true;
108
106
  const host = process.env.RABBITMQ_SERVICE_HOST || '';
@@ -124,6 +122,12 @@ class RabbitMq {
124
122
  }));
125
123
  });
126
124
  }
125
+ getNewChannel() {
126
+ return __awaiter(this, void 0, void 0, function* () {
127
+ const connection = yield this.getConnection();
128
+ return connection.createChannel({});
129
+ });
130
+ }
127
131
  assertChannel() {
128
132
  return __awaiter(this, void 0, void 0, function* () {
129
133
  return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
@@ -183,14 +187,14 @@ class RabbitMq {
183
187
  const optionsWithDefaults = Object.assign(Object.assign({}, defaultOptions), options);
184
188
  RabbitMq.validateName('queue', queue);
185
189
  const { limit, deadMessageTtl } = optionsWithDefaults;
186
- const channel = yield this.assertChannel();
190
+ const channel = yield this.getNewChannel();
187
191
  return channel.addSetup((c) => __awaiter(this, void 0, void 0, function* () {
188
192
  yield c.assertQueue(queue);
189
193
  yield c.prefetch(limit, true);
190
194
  return Promise.all([
191
195
  c.consume(queue, (msg) => {
192
196
  outbreak_1.newTrace(outbreak_1.traceTypes.RABBIT);
193
- callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue, optionsWithDefaults, { messageTtl: deadMessageTtl }));
197
+ callback(RabbitMq.parseMsg(msg), this.ack(channel), this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl }));
194
198
  }),
195
199
  ]);
196
200
  }));
@@ -202,7 +206,7 @@ class RabbitMq {
202
206
  RabbitMq.validateName('exchange', exchange);
203
207
  RabbitMq.validateName('queue', queue);
204
208
  const { limit, deadMessageTtl } = optionsWithDefaults;
205
- const channel = yield this.assertChannel();
209
+ const channel = yield this.getNewChannel();
206
210
  return channel.addSetup((c) => __awaiter(this, void 0, void 0, function* () {
207
211
  const assertExchange = yield assertExchangeFanout(c, exchange);
208
212
  yield c.assertQueue(queue);
@@ -212,7 +216,7 @@ class RabbitMq {
212
216
  c.bindQueue(queue, exchange, ''),
213
217
  c.consume(queue, (msg) => {
214
218
  outbreak_1.newTrace(outbreak_1.traceTypes.RABBIT);
215
- callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue, optionsWithDefaults, { messageTtl: deadMessageTtl }));
219
+ callback(RabbitMq.parseMsg(msg), this.ack(channel), this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl }));
216
220
  }),
217
221
  ]);
218
222
  }));
package/dist/mock.d.ts CHANGED
@@ -1,13 +1,13 @@
1
1
  import 'jest';
2
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>;
3
+ ack: jest.Mock<any, any>;
4
+ nack: jest.Mock<any, any>;
5
+ assertChannel: jest.Mock<any, any>;
6
+ assertExchange: jest.Mock<any, any>;
7
+ assertQueue: jest.Mock<any, any>;
8
+ consume: jest.Mock<any, any>;
9
+ consumeFromExchange: jest.Mock<any, any>;
10
+ publish: jest.Mock<any, any>;
11
+ sendToQueue: jest.Mock<any, any>;
12
12
  }
13
13
  export default RabbitMq;
package/dist/mock.js CHANGED
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ // eslint-disable-next-line import/no-extraneous-dependencies
3
4
  require("jest");
4
5
  class RabbitMq {
5
6
  constructor() {
package/package.json CHANGED
@@ -1,15 +1,15 @@
1
1
  {
2
2
  "name": "@autofleet/rabbit",
3
- "version": "1.7.0",
3
+ "version": "2.0.0",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
7
7
  "start": "ts-node src/index.ts",
8
8
  "example": "ts-node src/example.ts",
9
- "prepublish": "tsc",
9
+ "build": "tsc",
10
10
  "linter": "./node_modules/.bin/eslint .",
11
11
  "test": "jest --forceExit",
12
- "test-local": "RABBITMQ_SERVICE_HOST=34.76.123.137:5672 jest --forceExit",
12
+ "test-local": "RABBITMQ_SERVICE_HOST=10.132.0.98:5672 jest --forceExit",
13
13
  "coverage": "jest --coverage --forceExit --runInBand && rm -rf ./coverage",
14
14
  "dev": "nodemon"
15
15
  },
@@ -17,18 +17,18 @@
17
17
  "@autofleet/outbreak": "0.0.6",
18
18
  "@types/amqp-connection-manager": "^2.0.10",
19
19
  "@types/amqplib": "^0.5.16",
20
- "@types/jest": "^22.0.0",
21
20
  "amqp-connection-manager": "^3.2.1",
22
21
  "amqplib": "^0.6.0",
23
- "bluebird": "^3.7.2",
24
- "jest": "^22.4.4"
22
+ "bluebird": "^3.7.2"
25
23
  },
26
24
  "devDependencies": {
25
+ "@types/jest": "^26.0.23",
27
26
  "@typescript-eslint/eslint-plugin": "^4.8.1",
28
27
  "eslint": "^7.13.0",
29
28
  "eslint-config-airbnb-typescript": "^12.0.0",
30
29
  "eslint-plugin-import": "^2.22.1",
31
- "ts-jest": "^25.4.0",
30
+ "jest": "^27.0.5",
31
+ "ts-jest": "^27.0.3",
32
32
  "ts-node": "^8.6.2",
33
33
  "typescript": "^3.8.3"
34
34
  },
package/src/index.ts CHANGED
@@ -76,13 +76,12 @@ class RabbitMq {
76
76
  this.options = options;
77
77
  }
78
78
 
79
- public ack = async (msg: any) => {
80
- if (this.channel) {
81
- await this.channel.ack(msg);
82
- }
79
+ public ack = (channel: ChannelWrapper) => async (msg: any) => {
80
+ await channel.ack(msg);
83
81
  }
84
82
 
85
83
  public nack = (
84
+ channel: ChannelWrapper,
86
85
  queue: string,
87
86
  options: any,
88
87
  deadQueueOptions: Options.AssertQueue,
@@ -92,7 +91,7 @@ class RabbitMq {
92
91
  skipRetry = false,
93
92
  }: any = { },
94
93
  ) => {
95
- if (this.channel) {
94
+ if (channel) {
96
95
  if (
97
96
  !skipRetry
98
97
  && (!msg.content['x-retry-count']
@@ -105,7 +104,7 @@ class RabbitMq {
105
104
  await this.assertQueue(deadQueue, deadQueueOptions);
106
105
  await this.sendToQueue(deadQueue, msg.content, deadQueueOptions);
107
106
  }
108
- await this.channel.ack(msg);
107
+ await channel.ack(msg);
109
108
  }
110
109
  }
111
110
 
@@ -116,7 +115,7 @@ class RabbitMq {
116
115
  return;
117
116
  }
118
117
  if (this.connection !== null) {
119
- return this.connection;
118
+ return resolve(this.connection);
120
119
  }
121
120
  this.creatingConnection = true;
122
121
  const host: string = process.env.RABBITMQ_SERVICE_HOST || '';
@@ -137,6 +136,11 @@ class RabbitMq {
137
136
  });
138
137
  }
139
138
 
139
+ async getNewChannel() {
140
+ const connection: AmqpConnectionManager = await this.getConnection();
141
+ return connection.createChannel({});
142
+ }
143
+
140
144
  async assertChannel() {
141
145
  return new Promise<ChannelWrapper>(async (resolve, reject) => {
142
146
  if (this.channel) {
@@ -191,7 +195,7 @@ class RabbitMq {
191
195
  const optionsWithDefaults = { ...defaultOptions, ...options };
192
196
  RabbitMq.validateName('queue', queue);
193
197
  const { limit, deadMessageTtl } = optionsWithDefaults;
194
- const channel: ChannelWrapper = await this.assertChannel();
198
+ const channel: ChannelWrapper = await this.getNewChannel();
195
199
  return channel.addSetup(async (c: ConfirmChannel) => {
196
200
  await c.assertQueue(queue);
197
201
  await c.prefetch(limit, true);
@@ -200,7 +204,7 @@ class RabbitMq {
200
204
  queue,
201
205
  (msg: any) => {
202
206
  newTrace(traceTypes.RABBIT);
203
- callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue, optionsWithDefaults, { messageTtl: deadMessageTtl }));
207
+ callback(RabbitMq.parseMsg(msg), this.ack(channel), this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl }));
204
208
  },
205
209
  ),
206
210
  ]);
@@ -212,7 +216,7 @@ class RabbitMq {
212
216
  RabbitMq.validateName('exchange', exchange);
213
217
  RabbitMq.validateName('queue', queue);
214
218
  const { limit, deadMessageTtl } = optionsWithDefaults;
215
- const channel: ChannelWrapper = await this.assertChannel();
219
+ const channel: ChannelWrapper = await this.getNewChannel();
216
220
 
217
221
  return channel.addSetup(async (c: ConfirmChannel) => {
218
222
  const assertExchange = await assertExchangeFanout(c, exchange);
@@ -225,7 +229,7 @@ class RabbitMq {
225
229
  queue,
226
230
  (msg: any) => {
227
231
  newTrace(traceTypes.RABBIT);
228
- callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue, optionsWithDefaults, { messageTtl: deadMessageTtl }));
232
+ callback(RabbitMq.parseMsg(msg), this.ack(channel), this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl }));
229
233
  },
230
234
  ),
231
235
  ]);
package/src/mock.ts CHANGED
@@ -1,3 +1,4 @@
1
+ // eslint-disable-next-line import/no-extraneous-dependencies
1
2
  import 'jest';
2
3
 
3
4
  class RabbitMq {
package/tsconfig.json CHANGED
@@ -6,5 +6,6 @@
6
6
  "outDir": "./dist",
7
7
  "strict": true,
8
8
  "esModuleInterop": true
9
- }
9
+ },
10
+ "exclude": ["node_modules", "**/*.test.ts"]
10
11
  }