@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 +8 -17
- package/dist/index.js +93 -118
- package/dist/index.test.js +16 -25
- package/dist/mock.d.ts +9 -9
- package/package.json +7 -7
- package/tsconfig.json +5 -2
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 = () =>
|
|
5
|
+
const init = async () => {
|
|
15
6
|
const rabbit = new index_1.default();
|
|
16
7
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
17
|
-
|
|
8
|
+
await rabbit.consumeFromExchange('test-q2', 'test-e', (msg, ack) => {
|
|
18
9
|
console.log('msg2', msg.content);
|
|
19
10
|
// ack(msg);
|
|
20
11
|
});
|
|
21
|
-
|
|
12
|
+
await rabbit.consumeFromExchange('test-q', 'test-e', (msg, ack) => {
|
|
22
13
|
console.log('msg', msg.content);
|
|
23
14
|
ack(msg);
|
|
24
15
|
});
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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
|
-
|
|
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) =>
|
|
14
|
+
this.ack = async (msg) => {
|
|
24
15
|
if (this.channel) {
|
|
25
|
-
|
|
16
|
+
await this.channel.ack(msg);
|
|
26
17
|
}
|
|
27
|
-
}
|
|
28
|
-
this.nack = (queue) => (msg, retries) =>
|
|
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
|
-
|
|
24
|
+
await this.sendToQueue(queue, msg.content);
|
|
34
25
|
}
|
|
35
26
|
else {
|
|
36
27
|
const deadQueue = `${queue}-dead`;
|
|
37
|
-
|
|
38
|
-
|
|
28
|
+
await this.assertQueue(deadQueue);
|
|
29
|
+
await this.sendToQueue(deadQueue, msg.content);
|
|
39
30
|
}
|
|
40
|
-
|
|
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
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
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
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
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
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
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
|
-
|
|
123
|
-
|
|
124
|
-
|
|
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
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
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
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
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
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
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
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
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;
|
package/dist/index.test.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
|
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', () =>
|
|
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
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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', () =>
|
|
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
|
-
|
|
36
|
-
|
|
37
|
-
|
|
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', () =>
|
|
30
|
+
});
|
|
31
|
+
it('cant publish to unknown exchange', async () => {
|
|
41
32
|
let err;
|
|
42
33
|
try {
|
|
43
|
-
|
|
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', () =>
|
|
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
|
-
|
|
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<
|
|
4
|
-
nack: jest.Mock<
|
|
5
|
-
assertChannel: jest.Mock<
|
|
6
|
-
assertExchange: jest.Mock<
|
|
7
|
-
assertQueue: jest.Mock<
|
|
8
|
-
consume: jest.Mock<
|
|
9
|
-
consumeFromExchange: jest.Mock<
|
|
10
|
-
publish: jest.Mock<
|
|
11
|
-
sendToQueue: jest.Mock<
|
|
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
|
|
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": "^
|
|
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"
|