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