@autofleet/rabbit 1.5.1 → 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 +3 -2
- package/dist/index.d.ts +2 -0
- package/dist/index.js +17 -9
- package/dist/index.test.js +22 -2
- package/package.json +3 -3
- package/src/example.ts +6 -2
- package/src/index.test.ts +25 -2
- package/src/index.ts +15 -9
package/dist/example.js
CHANGED
|
@@ -10,10 +10,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
const index_1 = require("./index");
|
|
13
|
-
|
|
13
|
+
process.env.RABBITMQ_SERVICE_HOST = '34.76.123.137:5672';
|
|
14
14
|
const init = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
15
15
|
const rabbit = new index_1.default();
|
|
16
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
17
16
|
yield rabbit.consumeFromExchange('test-q2', 'test-e', (msg, ack) => {
|
|
18
17
|
console.log('consumeFromExchange: msg2', msg.content);
|
|
19
18
|
ack(msg);
|
|
@@ -29,5 +28,7 @@ const init = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
|
29
28
|
ack(msg);
|
|
30
29
|
});
|
|
31
30
|
yield rabbit.sendToQueue('test-q-2', 'hey-q');
|
|
31
|
+
const getQueueLength = yield rabbit.getQueueLength('test-q2');
|
|
32
|
+
console.log(`getQueueLength:${getQueueLength}`);
|
|
32
33
|
});
|
|
33
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
|
@@ -9,6 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
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 */
|
|
12
13
|
// eslint-disable-next-line max-classes-per-file
|
|
13
14
|
const bluebird = require("bluebird");
|
|
14
15
|
const amqp_connection_manager_1 = require("amqp-connection-manager");
|
|
@@ -29,7 +30,6 @@ class RabbitMq {
|
|
|
29
30
|
this.nack = (queue) => (msg, retries) => __awaiter(this, void 0, void 0, function* () {
|
|
30
31
|
if (this.channel) {
|
|
31
32
|
if (!msg.content['x-retry-count'] || msg.content['x-retry-count'] <= retries) {
|
|
32
|
-
// eslint-disable-next-line no-param-reassign
|
|
33
33
|
msg.content['x-retry-count'] = msg.content['x-retry-count'] ? msg.content['x-retry-count'] + 1 : 1;
|
|
34
34
|
yield this.sendToQueue(queue, msg.content);
|
|
35
35
|
}
|
|
@@ -53,7 +53,7 @@ class RabbitMq {
|
|
|
53
53
|
try {
|
|
54
54
|
content = JSON.parse(content);
|
|
55
55
|
}
|
|
56
|
-
catch (e) { }
|
|
56
|
+
catch (e) { }
|
|
57
57
|
return Object.assign(Object.assign({}, msg), { content });
|
|
58
58
|
}
|
|
59
59
|
static validateName(type, name) {
|
|
@@ -63,14 +63,12 @@ class RabbitMq {
|
|
|
63
63
|
}
|
|
64
64
|
getConnection() {
|
|
65
65
|
return __awaiter(this, void 0, void 0, function* () {
|
|
66
|
-
// eslint-disable-next-line no-async-promise-executor
|
|
67
66
|
return new Promise((resolve) => __awaiter(this, void 0, void 0, function* () {
|
|
68
67
|
if (this.creatingConnection) {
|
|
69
68
|
this.em.on(connectionCreatedConst, resolve);
|
|
70
69
|
return;
|
|
71
70
|
}
|
|
72
71
|
if (this.connection !== null) {
|
|
73
|
-
// eslint-disable-next-line consistent-return
|
|
74
72
|
return this.connection;
|
|
75
73
|
}
|
|
76
74
|
this.creatingConnection = true;
|
|
@@ -86,7 +84,6 @@ class RabbitMq {
|
|
|
86
84
|
}
|
|
87
85
|
assertChannel() {
|
|
88
86
|
return __awaiter(this, void 0, void 0, function* () {
|
|
89
|
-
// eslint-disable-next-line no-async-promise-executor,consistent-return
|
|
90
87
|
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
|
|
91
88
|
if (this.channel) {
|
|
92
89
|
return resolve(this.channel);
|
|
@@ -104,7 +101,6 @@ class RabbitMq {
|
|
|
104
101
|
}));
|
|
105
102
|
});
|
|
106
103
|
}
|
|
107
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
108
104
|
assertExchange(exchangeName, options) {
|
|
109
105
|
return __awaiter(this, void 0, void 0, function* () {
|
|
110
106
|
const channel = yield this.assertChannel();
|
|
@@ -118,7 +114,21 @@ class RabbitMq {
|
|
|
118
114
|
}));
|
|
119
115
|
});
|
|
120
116
|
}
|
|
121
|
-
|
|
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);
|
|
123
|
+
});
|
|
124
|
+
}
|
|
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
|
+
});
|
|
131
|
+
}
|
|
122
132
|
assertQueue(queue, options) {
|
|
123
133
|
return __awaiter(this, void 0, void 0, function* () {
|
|
124
134
|
RabbitMq.validateName('queue', queue);
|
|
@@ -126,7 +136,6 @@ class RabbitMq {
|
|
|
126
136
|
return channel.addSetup((c) => c.assertQueue(queue));
|
|
127
137
|
});
|
|
128
138
|
}
|
|
129
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
130
139
|
consume(queue, callback, options) {
|
|
131
140
|
return __awaiter(this, void 0, void 0, function* () {
|
|
132
141
|
RabbitMq.validateName('queue', queue);
|
|
@@ -141,7 +150,6 @@ class RabbitMq {
|
|
|
141
150
|
}));
|
|
142
151
|
});
|
|
143
152
|
}
|
|
144
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
145
153
|
consumeFromExchange(queue, exchange, callback, options) {
|
|
146
154
|
return __awaiter(this, void 0, void 0, function* () {
|
|
147
155
|
RabbitMq.validateName('queue', queue);
|
package/dist/index.test.js
CHANGED
|
@@ -9,8 +9,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
/* eslint-disable @typescript-eslint/ban-ts-comment,@typescript-eslint/no-unused-vars */
|
|
12
13
|
const index_1 = require("./index");
|
|
13
|
-
// import RabbitError from './rabbitError';
|
|
14
14
|
jest.setTimeout(120000);
|
|
15
15
|
const rabbit = new index_1.default();
|
|
16
16
|
const delay = () => new Promise((resolve) => setTimeout(() => resolve(), 500));
|
|
@@ -48,7 +48,6 @@ describe('RabbitMQ', () => {
|
|
|
48
48
|
expect(err).toBeDefined();
|
|
49
49
|
}));
|
|
50
50
|
it('cant publish due timeout', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
51
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
52
51
|
// @ts-ignore
|
|
53
52
|
process.env.RABBITMQ_PUBLISH_TIMEOUT = 1;
|
|
54
53
|
const rabbit2 = new index_1.default();
|
|
@@ -61,4 +60,25 @@ describe('RabbitMQ', () => {
|
|
|
61
60
|
}
|
|
62
61
|
expect(err).toBeDefined();
|
|
63
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
|
+
}));
|
|
64
84
|
});
|
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": {
|
|
@@ -14,9 +14,9 @@
|
|
|
14
14
|
"dev": "nodemon"
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
+
"@types/amqp-connection-manager": "^2.0.10",
|
|
18
|
+
"@types/amqplib": "^0.5.16",
|
|
17
19
|
"@types/jest": "^22.0.0",
|
|
18
|
-
"@types/amqp-connection-manager": "^2.0.2",
|
|
19
|
-
"@types/amqplib": "^0.5.9",
|
|
20
20
|
"amqp-connection-manager": "^3.2.1",
|
|
21
21
|
"amqplib": "^0.6.0",
|
|
22
22
|
"bluebird": "^3.7.2",
|
package/src/example.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
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
9
|
console.log('consumeFromExchange: msg2', msg.content);
|
|
10
10
|
ack(msg);
|
|
@@ -21,7 +21,11 @@ const init = async () => {
|
|
|
21
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';
|
|
@@ -19,7 +20,7 @@ class RabbitMq {
|
|
|
19
20
|
|
|
20
21
|
try {
|
|
21
22
|
content = JSON.parse(content);
|
|
22
|
-
} catch (e) { }
|
|
23
|
+
} catch (e) { }
|
|
23
24
|
|
|
24
25
|
return {
|
|
25
26
|
...msg,
|
|
@@ -66,7 +67,6 @@ class RabbitMq {
|
|
|
66
67
|
public nack = (queue: string) => async (msg: any, retries: number) => {
|
|
67
68
|
if (this.channel) {
|
|
68
69
|
if (!msg.content['x-retry-count'] || msg.content['x-retry-count'] <= retries) {
|
|
69
|
-
// eslint-disable-next-line no-param-reassign
|
|
70
70
|
msg.content['x-retry-count'] = msg.content['x-retry-count'] ? msg.content['x-retry-count'] + 1 : 1;
|
|
71
71
|
await this.sendToQueue(queue, msg.content);
|
|
72
72
|
} else {
|
|
@@ -79,14 +79,12 @@ class RabbitMq {
|
|
|
79
79
|
}
|
|
80
80
|
|
|
81
81
|
async getConnection() {
|
|
82
|
-
// eslint-disable-next-line no-async-promise-executor
|
|
83
82
|
return new Promise<AmqpConnectionManager>(async (resolve) => {
|
|
84
83
|
if (this.creatingConnection) {
|
|
85
84
|
this.em.on(connectionCreatedConst, resolve);
|
|
86
85
|
return;
|
|
87
86
|
}
|
|
88
87
|
if (this.connection !== null) {
|
|
89
|
-
// eslint-disable-next-line consistent-return
|
|
90
88
|
return this.connection;
|
|
91
89
|
}
|
|
92
90
|
this.creatingConnection = true;
|
|
@@ -102,7 +100,6 @@ class RabbitMq {
|
|
|
102
100
|
}
|
|
103
101
|
|
|
104
102
|
async assertChannel() {
|
|
105
|
-
// eslint-disable-next-line no-async-promise-executor,consistent-return
|
|
106
103
|
return new Promise<ChannelWrapper>(async (resolve, reject) => {
|
|
107
104
|
if (this.channel) {
|
|
108
105
|
return resolve(this.channel);
|
|
@@ -121,7 +118,6 @@ class RabbitMq {
|
|
|
121
118
|
});
|
|
122
119
|
}
|
|
123
120
|
|
|
124
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
125
121
|
async assertExchange(exchangeName: string, options?: any) {
|
|
126
122
|
const channel: ChannelWrapper = await this.assertChannel();
|
|
127
123
|
if (this.exchanges[exchangeName]) {
|
|
@@ -134,14 +130,25 @@ class RabbitMq {
|
|
|
134
130
|
});
|
|
135
131
|
}
|
|
136
132
|
|
|
137
|
-
|
|
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
|
+
|
|
138
146
|
async assertQueue(queue: string, options?: any) {
|
|
139
147
|
RabbitMq.validateName('queue', queue);
|
|
140
148
|
const channel: ChannelWrapper = await this.assertChannel();
|
|
141
149
|
return channel.addSetup((c: ConfirmChannel) => c.assertQueue(queue));
|
|
142
150
|
}
|
|
143
151
|
|
|
144
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
145
152
|
async consume(queue: string, callback: (msg: any, ack: Function, nack: Function) => any, options?: any) {
|
|
146
153
|
RabbitMq.validateName('queue', queue);
|
|
147
154
|
const { limit } = (options && options.limit) ? options : { limit: 1 };
|
|
@@ -155,7 +162,6 @@ class RabbitMq {
|
|
|
155
162
|
});
|
|
156
163
|
}
|
|
157
164
|
|
|
158
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
159
165
|
async consumeFromExchange(queue: string, exchange: string, callback: (msg: any, ack: Function, nack: Function) => any, options?: any) {
|
|
160
166
|
RabbitMq.validateName('queue', queue);
|
|
161
167
|
RabbitMq.validateName('exchange', exchange);
|