@autofleet/rabbit 1.5.0-2 → 1.5.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/example.js CHANGED
@@ -1,33 +1,24 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  const index_1 = require("./index");
13
4
  // process.env.RABBITMQ_SERVICE_HOST="34.76.223.229:5672";
14
- const init = () => __awaiter(void 0, void 0, void 0, function* () {
5
+ const init = async () => {
15
6
  const rabbit = new index_1.default();
16
7
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
17
- yield rabbit.consumeFromExchange('test-q2', 'test-e', (msg, ack) => {
8
+ await rabbit.consumeFromExchange('test-q2', 'test-e', (msg, ack) => {
18
9
  console.log('msg2', msg.content);
19
10
  // ack(msg);
20
11
  });
21
- yield rabbit.consumeFromExchange('test-q', 'test-e', (msg, ack) => {
12
+ await rabbit.consumeFromExchange('test-q', 'test-e', (msg, ack) => {
22
13
  console.log('msg', msg.content);
23
14
  ack(msg);
24
15
  });
25
- yield rabbit.publish('test-e', 'hey');
26
- yield rabbit.publish('test-e', 'hey1');
27
- yield rabbit.consume('test-q-2', (msg, ack) => {
16
+ await rabbit.publish('test-e', 'hey');
17
+ await rabbit.publish('test-e', 'hey1');
18
+ await rabbit.consume('test-q-2', (msg, ack) => {
28
19
  console.log('msg-q', msg.content);
29
20
  ack(msg);
30
21
  });
31
- yield rabbit.sendToQueue('test-q-2', 'hey-q');
32
- });
22
+ await rabbit.sendToQueue('test-q-2', 'hey-q');
23
+ };
33
24
  init();
package/dist/index.js CHANGED
@@ -1,13 +1,4 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  // eslint-disable-next-line max-classes-per-file
13
4
  const bluebird = require("bluebird");
@@ -20,26 +11,26 @@ class RabbitMq {
20
11
  this.PUBLISH_TIMEOUT = Number(process.env.RABBITMQ_PUBLISH_TIMEOUT) || 60000;
21
12
  this.PUBLISH_ERROR_MSG = `rabbit: publish timeout(${this.PUBLISH_TIMEOUT}ms) has pass, exchange: `;
22
13
  this.DISCONNECT_MSG = 'rabbit: connection disconnect';
23
- this.ack = (msg) => __awaiter(this, void 0, void 0, function* () {
14
+ this.ack = async (msg) => {
24
15
  if (this.channel) {
25
- yield this.channel.ack(msg);
16
+ await this.channel.ack(msg);
26
17
  }
27
- });
28
- this.nack = (queue) => (msg, retries) => __awaiter(this, void 0, void 0, function* () {
18
+ };
19
+ this.nack = (queue) => async (msg, retries) => {
29
20
  if (this.channel) {
30
21
  if (!msg.content['x-retry-count'] || msg.content['x-retry-count'] <= retries) {
31
22
  // eslint-disable-next-line no-param-reassign
32
23
  msg.content['x-retry-count'] = msg.content['x-retry-count'] ? msg.content['x-retry-count'] + 1 : 1;
33
- yield this.sendToQueue(queue, msg.content);
24
+ await this.sendToQueue(queue, msg.content);
34
25
  }
35
26
  else {
36
27
  const deadQueue = `${queue}-dead`;
37
- yield this.assertQueue(deadQueue);
38
- yield this.sendToQueue(deadQueue, msg.content);
28
+ await this.assertQueue(deadQueue);
29
+ await this.sendToQueue(deadQueue, msg.content);
39
30
  }
40
- yield this.channel.ack(msg);
31
+ await this.channel.ack(msg);
41
32
  }
42
- });
33
+ };
43
34
  this.em = new events_1.EventEmitter();
44
35
  this.channel = null;
45
36
  this.connection = null;
@@ -60,124 +51,108 @@ class RabbitMq {
60
51
  throw new rabbitError_1.default(`error while using ${type} with no name`);
61
52
  }
62
53
  }
63
- getConnection() {
64
- return __awaiter(this, void 0, void 0, function* () {
65
- // eslint-disable-next-line no-async-promise-executor
66
- return new Promise((resolve) => __awaiter(this, void 0, void 0, function* () {
67
- if (this.creatingConnection) {
68
- this.em.on(connectionCreatedConst, resolve);
69
- return;
70
- }
71
- if (this.connection !== null) {
72
- // eslint-disable-next-line consistent-return
73
- return this.connection;
74
- }
75
- this.creatingConnection = true;
76
- const host = process.env.RABBITMQ_SERVICE_HOST || '';
77
- const connection = yield amqp_connection_manager_1.connect([`amqp://${host}`]);
78
- this.connection = connection;
79
- this.connection.on('disconnect', ({ err }) => console.error(`${this.DISCONNECT_MSG}${err && ` - ${err}`}`));
80
- this.creatingConnection = false;
81
- this.em.emit(connectionCreatedConst, connection);
82
- resolve(connection);
83
- }));
54
+ async getConnection() {
55
+ // eslint-disable-next-line no-async-promise-executor
56
+ return new Promise(async (resolve) => {
57
+ if (this.creatingConnection) {
58
+ this.em.on(connectionCreatedConst, resolve);
59
+ return;
60
+ }
61
+ if (this.connection !== null) {
62
+ // eslint-disable-next-line consistent-return
63
+ return this.connection;
64
+ }
65
+ this.creatingConnection = true;
66
+ const host = process.env.RABBITMQ_SERVICE_HOST || '';
67
+ const connection = await amqp_connection_manager_1.connect([`amqp://${host}`]);
68
+ this.connection = connection;
69
+ this.connection.on('disconnect', ({ err }) => console.error(`${this.DISCONNECT_MSG}${err && ` - ${err}`}`));
70
+ this.creatingConnection = false;
71
+ this.em.emit(connectionCreatedConst, connection);
72
+ resolve(connection);
84
73
  });
85
74
  }
86
- assertChannel() {
87
- return __awaiter(this, void 0, void 0, function* () {
88
- // eslint-disable-next-line no-async-promise-executor,consistent-return
89
- return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
90
- if (this.channel) {
91
- return resolve(this.channel);
92
- }
93
- const connection = yield this.getConnection();
94
- try {
95
- if (this.channel === null) {
96
- this.channel = connection.createChannel({});
97
- }
98
- resolve(this.channel);
99
- }
100
- catch (e) {
101
- reject(e);
75
+ async assertChannel() {
76
+ // eslint-disable-next-line no-async-promise-executor,consistent-return
77
+ return new Promise(async (resolve, reject) => {
78
+ if (this.channel) {
79
+ return resolve(this.channel);
80
+ }
81
+ const connection = await this.getConnection();
82
+ try {
83
+ if (this.channel === null) {
84
+ this.channel = connection.createChannel({});
102
85
  }
103
- }));
86
+ resolve(this.channel);
87
+ }
88
+ catch (e) {
89
+ reject(e);
90
+ }
104
91
  });
105
92
  }
106
93
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
107
- assertExchange(exchangeName, options) {
108
- return __awaiter(this, void 0, void 0, function* () {
109
- const channel = yield this.assertChannel();
110
- if (this.exchanges[exchangeName]) {
111
- return this.exchanges[exchangeName];
112
- }
113
- return channel.addSetup((c) => __awaiter(this, void 0, void 0, function* () {
114
- const exchange = yield c.assertExchange(exchangeName, 'fanout');
115
- this.exchanges[exchangeName] = exchange;
116
- return exchange;
117
- }));
94
+ async assertExchange(exchangeName, options) {
95
+ const channel = await this.assertChannel();
96
+ if (this.exchanges[exchangeName]) {
97
+ return this.exchanges[exchangeName];
98
+ }
99
+ return channel.addSetup(async (c) => {
100
+ const exchange = await c.assertExchange(exchangeName, 'fanout');
101
+ this.exchanges[exchangeName] = exchange;
102
+ return exchange;
118
103
  });
119
104
  }
120
105
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
121
- assertQueue(queue, options) {
122
- return __awaiter(this, void 0, void 0, function* () {
123
- RabbitMq.validateName('queue', queue);
124
- const channel = yield this.assertChannel();
125
- return channel.addSetup((c) => c.assertQueue(queue));
126
- });
106
+ async assertQueue(queue, options) {
107
+ RabbitMq.validateName('queue', queue);
108
+ const channel = await this.assertChannel();
109
+ return channel.addSetup((c) => c.assertQueue(queue));
127
110
  }
128
111
  // eslint-disable-next-line @typescript-eslint/ban-types
129
- consume(queue, callback, options) {
130
- return __awaiter(this, void 0, void 0, function* () {
131
- RabbitMq.validateName('queue', queue);
132
- const { limit } = (options && options.limit) ? options : { limit: 1 };
133
- const channel = yield this.assertChannel();
134
- yield this.assertQueue(queue);
135
- return channel.addSetup((c) => __awaiter(this, void 0, void 0, function* () {
136
- yield c.prefetch(limit, true);
137
- return Promise.all([
138
- c.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue))),
139
- ]);
140
- }));
112
+ async consume(queue, callback, options) {
113
+ RabbitMq.validateName('queue', queue);
114
+ const { limit } = (options && options.limit) ? options : { limit: 1 };
115
+ const channel = await this.assertChannel();
116
+ await this.assertQueue(queue);
117
+ return channel.addSetup(async (c) => {
118
+ await c.prefetch(limit, true);
119
+ return Promise.all([
120
+ c.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue))),
121
+ ]);
141
122
  });
142
123
  }
143
124
  // eslint-disable-next-line @typescript-eslint/ban-types
144
- consumeFromExchange(queue, exchange, callback, options) {
145
- return __awaiter(this, void 0, void 0, function* () {
146
- RabbitMq.validateName('queue', queue);
147
- RabbitMq.validateName('exchange', exchange);
148
- const { limit } = (options && options.limit) ? options : { limit: 1 };
149
- const channel = yield this.assertChannel();
150
- yield Promise.all([
151
- this.assertExchange(exchange),
152
- this.assertQueue(queue),
125
+ async consumeFromExchange(queue, exchange, callback, options) {
126
+ RabbitMq.validateName('queue', queue);
127
+ RabbitMq.validateName('exchange', exchange);
128
+ const { limit } = (options && options.limit) ? options : { limit: 1 };
129
+ const channel = await this.assertChannel();
130
+ await Promise.all([
131
+ this.assertExchange(exchange),
132
+ this.assertQueue(queue),
133
+ ]);
134
+ return channel.addSetup(async (c) => {
135
+ await c.prefetch(limit, true);
136
+ return Promise.all([
137
+ c.bindQueue(queue, exchange, ''),
138
+ c.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue))),
153
139
  ]);
154
- return channel.addSetup((c) => __awaiter(this, void 0, void 0, function* () {
155
- yield c.prefetch(limit, true);
156
- return Promise.all([
157
- c.bindQueue(queue, exchange, ''),
158
- c.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue))),
159
- ]);
160
- }));
161
140
  });
162
141
  }
163
- publish(exchange, content) {
164
- return __awaiter(this, void 0, void 0, function* () {
165
- RabbitMq.validateName('exchange', exchange);
166
- const channel = yield this.assertChannel();
167
- yield this.assertExchange(exchange);
168
- return new bluebird.Promise((resolve) => __awaiter(this, void 0, void 0, function* () {
169
- yield channel.publish(exchange, '', Buffer.from(JSON.stringify(content)));
170
- return resolve();
171
- })).timeout(this.PUBLISH_TIMEOUT, `${this.PUBLISH_ERROR_MSG}${exchange}`);
172
- });
142
+ async publish(exchange, content) {
143
+ RabbitMq.validateName('exchange', exchange);
144
+ const channel = await this.assertChannel();
145
+ await this.assertExchange(exchange);
146
+ return new bluebird.Promise(async (resolve) => {
147
+ await channel.publish(exchange, '', Buffer.from(JSON.stringify(content)));
148
+ return resolve();
149
+ }).timeout(this.PUBLISH_TIMEOUT, `${this.PUBLISH_ERROR_MSG}${exchange}`);
173
150
  }
174
- sendToQueue(queue, content) {
175
- return __awaiter(this, void 0, void 0, function* () {
176
- RabbitMq.validateName('queue', queue);
177
- const channel = yield this.assertChannel();
178
- yield this.assertQueue(queue);
179
- return channel.sendToQueue(queue, Buffer.from(JSON.stringify(content)));
180
- });
151
+ async sendToQueue(queue, content) {
152
+ RabbitMq.validateName('queue', queue);
153
+ const channel = await this.assertChannel();
154
+ await this.assertQueue(queue);
155
+ return channel.sendToQueue(queue, Buffer.from(JSON.stringify(content)));
181
156
  }
182
157
  }
183
158
  exports.default = RabbitMq;
@@ -1,13 +1,4 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  const index_1 = require("./index");
13
4
  // import RabbitError from './rabbitError';
@@ -15,50 +6,50 @@ jest.setTimeout(120000);
15
6
  const rabbit = new index_1.default();
16
7
  const delay = () => new Promise((resolve) => setTimeout(() => resolve(), 500));
17
8
  describe('RabbitMQ', () => {
18
- it('check exchange', () => __awaiter(void 0, void 0, void 0, function* () {
9
+ it('check exchange', async () => {
19
10
  const callback = (msg, ack) => {
20
11
  expect(msg.content).toEqual('hey');
21
12
  ack(msg);
22
13
  };
23
14
  const mockFn = jest.fn(callback);
24
- yield rabbit.consumeFromExchange('test-q2', 'test-e', mockFn);
25
- yield rabbit.publish('test-e', 'hey');
26
- yield delay();
15
+ await rabbit.consumeFromExchange('test-q2', 'test-e', mockFn);
16
+ await rabbit.publish('test-e', 'hey');
17
+ await delay();
27
18
  expect(mockFn).toBeCalled();
28
- }));
29
- it('check queue', () => __awaiter(void 0, void 0, void 0, function* () {
19
+ });
20
+ it('check queue', async () => {
30
21
  const callback = (msg, ack) => {
31
22
  expect(msg.content).toEqual('hey-q');
32
23
  ack(msg);
33
24
  };
34
25
  const mockFn = jest.fn(callback);
35
- yield rabbit.consume('test-q-2', mockFn);
36
- yield rabbit.sendToQueue('test-q-2', 'hey-q');
37
- yield delay();
26
+ await rabbit.consume('test-q-2', mockFn);
27
+ await rabbit.sendToQueue('test-q-2', 'hey-q');
28
+ await delay();
38
29
  expect(mockFn).toBeCalled();
39
- }));
40
- it('cant publish to unknown exchange', () => __awaiter(void 0, void 0, void 0, function* () {
30
+ });
31
+ it('cant publish to unknown exchange', async () => {
41
32
  let err;
42
33
  try {
43
- yield rabbit.publish('', 'hey');
34
+ await rabbit.publish('', 'hey');
44
35
  }
45
36
  catch (e) {
46
37
  err = e;
47
38
  }
48
39
  expect(err).toBeDefined();
49
- }));
50
- it('cant publish due timeout', () => __awaiter(void 0, void 0, void 0, function* () {
40
+ });
41
+ it('cant publish due timeout', async () => {
51
42
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
52
43
  // @ts-ignore
53
44
  process.env.RABBITMQ_PUBLISH_TIMEOUT = 1;
54
45
  const rabbit2 = new index_1.default();
55
46
  let err;
56
47
  try {
57
- yield rabbit2.publish('test-e', 'hey');
48
+ await rabbit2.publish('test-e', 'hey');
58
49
  }
59
50
  catch (e) {
60
51
  err = e;
61
52
  }
62
53
  expect(err).toBeDefined();
63
- }));
54
+ });
64
55
  });
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/rabbit",
3
- "version": "1.5.0-2",
3
+ "version": "1.5.0",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -14,22 +14,22 @@
14
14
  "dev": "nodemon"
15
15
  },
16
16
  "dependencies": {
17
- "@types/jest": "^22.0.0",
18
17
  "@types/amqp-connection-manager": "^2.0.2",
19
18
  "@types/amqplib": "^0.5.9",
19
+ "@types/jest": "^23.3.10",
20
20
  "amqp-connection-manager": "^3.2.1",
21
21
  "amqplib": "^0.6.0",
22
22
  "bluebird": "^3.7.2",
23
- "jest": "^22.4.4"
23
+ "jest": "^26.6.3",
24
+ "ts-jest": "^26.4.4"
24
25
  },
25
26
  "devDependencies": {
27
+ "ts-node": "^7.0.1",
28
+ "typescript": "^3.2.1",
26
29
  "@typescript-eslint/eslint-plugin": "^4.8.1",
27
30
  "eslint": "^7.13.0",
28
31
  "eslint-config-airbnb-typescript": "^12.0.0",
29
- "eslint-plugin-import": "^2.22.1",
30
- "ts-jest": "^25.4.0",
31
- "ts-node": "^8.6.2",
32
- "typescript": "^3.8.3"
32
+ "eslint-plugin-import": "^2.22.1"
33
33
  },
34
34
  "author": "",
35
35
  "license": "ISC"
package/tsconfig.json CHANGED
@@ -1,9 +1,12 @@
1
1
  {
2
2
  "compilerOptions": {
3
- "target": "es6",
3
+ "target": "es2017",
4
4
  "module": "commonjs",
5
+ "lib": [
6
+ "es2017"
7
+ ],
5
8
  "declaration": true,
6
9
  "outDir": "./dist",
7
10
  "strict": true
8
11
  }
9
- }
12
+ }