@autofleet/rabbit 1.0.14 → 1.0.15
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 +11 -15
- package/dist/index.js +28 -64
- package/package.json +3 -1
- package/src/index.ts +39 -80
package/dist/index.d.ts
CHANGED
|
@@ -1,24 +1,20 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import {
|
|
2
|
+
import { AmqpConnectionManager, ChannelWrapper } from 'amqp-connection-manager';
|
|
3
3
|
import { EventEmitter } from 'events';
|
|
4
4
|
declare class RabbitMq {
|
|
5
|
-
channel:
|
|
6
|
-
connection:
|
|
5
|
+
channel: ChannelWrapper | null;
|
|
6
|
+
connection: AmqpConnectionManager | null;
|
|
7
7
|
em: EventEmitter;
|
|
8
|
-
creatingChannel: boolean;
|
|
9
|
-
queues: any;
|
|
10
|
-
exchanges: any;
|
|
11
|
-
static parseMsg(msg: any): any;
|
|
12
8
|
constructor();
|
|
13
9
|
ack: (msg: any) => Promise<void>;
|
|
14
10
|
nack: (queue: string) => (msg: any, retries: number) => Promise<void>;
|
|
15
|
-
getConnection(): Promise<
|
|
16
|
-
assertChannel(): Promise<
|
|
17
|
-
assertExchange(exchange: string, options?: any): Promise<
|
|
18
|
-
assertQueue(queue: string, options?: any): Promise<
|
|
19
|
-
consume(queue: string, callback: (msg: any, ack: Function, nack: Function) => any, options?: any, isFirstTime?: boolean): Promise<
|
|
20
|
-
consumeFromExchange(queue: string, exchange: string, callback: (msg: any, ack: Function, nack: Function) => any, options?: any, isFirstTime?: boolean): Promise<
|
|
21
|
-
publish(exchange: string, content: any): Promise<
|
|
22
|
-
sendToQueue(queue: string, content: any): Promise<
|
|
11
|
+
getConnection(): Promise<AmqpConnectionManager>;
|
|
12
|
+
assertChannel(): Promise<ChannelWrapper>;
|
|
13
|
+
assertExchange(exchange: string, options?: any): Promise<void>;
|
|
14
|
+
assertQueue(queue: string, options?: any): Promise<void>;
|
|
15
|
+
consume(queue: string, callback: (msg: any, ack: Function, nack: Function) => any, options?: any, isFirstTime?: boolean): Promise<void>;
|
|
16
|
+
consumeFromExchange(queue: string, exchange: string, callback: (msg: any, ack: Function, nack: Function) => any, options?: any, isFirstTime?: boolean): Promise<void>;
|
|
17
|
+
publish(exchange: string, content: any): Promise<void>;
|
|
18
|
+
sendToQueue(queue: string, content: any): Promise<void>;
|
|
23
19
|
}
|
|
24
20
|
export default RabbitMq;
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const
|
|
3
|
+
const amqp_connection_manager_1 = require("amqp-connection-manager");
|
|
4
4
|
const events_1 = require("events");
|
|
5
|
-
const channelCreatedConst = 'channelCreated';
|
|
6
5
|
class RabbitMq {
|
|
7
6
|
constructor() {
|
|
8
7
|
this.ack = async (msg) => {
|
|
@@ -24,12 +23,9 @@ class RabbitMq {
|
|
|
24
23
|
await this.channel.ack(msg);
|
|
25
24
|
}
|
|
26
25
|
};
|
|
27
|
-
this.creatingChannel = false;
|
|
28
26
|
this.em = new events_1.EventEmitter;
|
|
29
27
|
this.channel = null;
|
|
30
28
|
this.connection = null;
|
|
31
|
-
this.queues = {};
|
|
32
|
-
this.exchanges = {};
|
|
33
29
|
process.once('SIGINT', () => {
|
|
34
30
|
console.log('Removing channel');
|
|
35
31
|
if (this.channel) {
|
|
@@ -37,18 +33,12 @@ class RabbitMq {
|
|
|
37
33
|
}
|
|
38
34
|
});
|
|
39
35
|
}
|
|
40
|
-
static parseMsg(msg) {
|
|
41
|
-
let { content } = msg;
|
|
42
|
-
content = content.toString();
|
|
43
|
-
try {
|
|
44
|
-
content = JSON.parse(content);
|
|
45
|
-
}
|
|
46
|
-
catch (e) { }
|
|
47
|
-
return Object.assign({}, msg, { content });
|
|
48
|
-
}
|
|
49
36
|
async getConnection() {
|
|
37
|
+
if (this.connection !== null) {
|
|
38
|
+
return this.connection;
|
|
39
|
+
}
|
|
50
40
|
let host = process.env.RABBITMQ_SERVICE_HOST || '35.240.13.28';
|
|
51
|
-
let connection =
|
|
41
|
+
let connection = await amqp_connection_manager_1.connect([`amqp://${host}`]);
|
|
52
42
|
this.connection = connection;
|
|
53
43
|
return connection;
|
|
54
44
|
}
|
|
@@ -57,47 +47,15 @@ class RabbitMq {
|
|
|
57
47
|
if (this.channel) {
|
|
58
48
|
return resolve(this.channel);
|
|
59
49
|
}
|
|
60
|
-
|
|
61
|
-
return this.em.on(channelCreatedConst, resolve);
|
|
62
|
-
}
|
|
63
|
-
this.creatingChannel = true;
|
|
64
|
-
let connection;
|
|
65
|
-
let channel = this.channel;
|
|
50
|
+
let connection = await this.getConnection();
|
|
66
51
|
try {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
this.channel = null;
|
|
72
|
-
this.connection = null;
|
|
73
|
-
await this.assertChannel();
|
|
52
|
+
connection = await this.getConnection();
|
|
53
|
+
if (this.channel === null) {
|
|
54
|
+
this.channel = connection.createChannel({
|
|
55
|
+
json: true,
|
|
74
56
|
});
|
|
75
57
|
}
|
|
76
|
-
|
|
77
|
-
channel = await connection.createChannel();
|
|
78
|
-
this.channel = channel;
|
|
79
|
-
this.creatingChannel = false;
|
|
80
|
-
const queueJobs = [];
|
|
81
|
-
const exchangeJobs = [];
|
|
82
|
-
const queueKeys = Object.keys(this.queues);
|
|
83
|
-
queueKeys.forEach(queue => {
|
|
84
|
-
const { callback, options } = this.queues[queue];
|
|
85
|
-
queueJobs.push(this.consume(queue, callback, options, false));
|
|
86
|
-
});
|
|
87
|
-
const exchangeKeys = Object.keys(this.exchanges);
|
|
88
|
-
exchangeKeys.forEach(queue => {
|
|
89
|
-
const { exchange, callback, options } = this.exchanges[queue];
|
|
90
|
-
exchangeJobs.push(this.consumeFromExchange(queue, exchange, callback, options, false));
|
|
91
|
-
});
|
|
92
|
-
await Promise.all(queueJobs);
|
|
93
|
-
await Promise.all(exchangeJobs);
|
|
94
|
-
channel.on('error', async () => {
|
|
95
|
-
console.log('closed channel');
|
|
96
|
-
this.channel = null;
|
|
97
|
-
await this.assertChannel();
|
|
98
|
-
});
|
|
99
|
-
this.em.emit(channelCreatedConst, channel);
|
|
100
|
-
resolve(channel);
|
|
58
|
+
resolve(this.channel);
|
|
101
59
|
}
|
|
102
60
|
catch (e) {
|
|
103
61
|
reject(e);
|
|
@@ -106,24 +64,26 @@ class RabbitMq {
|
|
|
106
64
|
}
|
|
107
65
|
async assertExchange(exchange, options) {
|
|
108
66
|
const channel = await this.assertChannel();
|
|
109
|
-
return channel.assertExchange(exchange, 'fanout');
|
|
67
|
+
return channel.addSetup((c) => c.assertExchange(exchange, 'fanout'));
|
|
110
68
|
}
|
|
111
69
|
async assertQueue(queue, options) {
|
|
112
70
|
const channel = await this.assertChannel();
|
|
113
|
-
return channel.assertQueue(queue);
|
|
71
|
+
return channel.addSetup((c) => c.assertQueue(queue));
|
|
114
72
|
}
|
|
115
73
|
async consume(queue, callback, options, isFirstTime = true) {
|
|
116
|
-
this.queues[queue] = this.queues[queue] ? this.queues[queue] : { callback, options };
|
|
117
74
|
const { limit } = options ? options : 1;
|
|
118
75
|
const channel = await this.assertChannel();
|
|
119
76
|
await this.assertQueue(queue);
|
|
120
77
|
if (!isFirstTime) {
|
|
121
|
-
channel.
|
|
122
|
-
|
|
78
|
+
return channel.addSetup((c) => {
|
|
79
|
+
return Promise.all([
|
|
80
|
+
c.prefetch(limit, false),
|
|
81
|
+
c.consume(queue, (msg) => callback(msg, this.ack, this.nack(queue))),
|
|
82
|
+
]);
|
|
83
|
+
});
|
|
123
84
|
}
|
|
124
85
|
}
|
|
125
86
|
async consumeFromExchange(queue, exchange, callback, options, isFirstTime = true) {
|
|
126
|
-
this.exchanges[queue] = this.exchanges[queue] ? this.exchanges[queue] : { exchange, callback, options };
|
|
127
87
|
const { limit } = options ? options : 1;
|
|
128
88
|
const channel = await this.assertChannel();
|
|
129
89
|
await Promise.all([
|
|
@@ -131,20 +91,24 @@ class RabbitMq {
|
|
|
131
91
|
this.assertQueue(queue),
|
|
132
92
|
]);
|
|
133
93
|
if (!isFirstTime) {
|
|
134
|
-
channel.
|
|
135
|
-
|
|
136
|
-
|
|
94
|
+
return channel.addSetup((c) => {
|
|
95
|
+
return Promise.all([
|
|
96
|
+
c.prefetch(limit, false),
|
|
97
|
+
c.bindQueue(queue, exchange, ''),
|
|
98
|
+
c.consume(queue, (msg) => callback(msg, this.ack, this.nack(queue))),
|
|
99
|
+
]);
|
|
100
|
+
});
|
|
137
101
|
}
|
|
138
102
|
}
|
|
139
103
|
async publish(exchange, content) {
|
|
140
104
|
const channel = await this.assertChannel();
|
|
141
105
|
await this.assertExchange(exchange);
|
|
142
|
-
return channel.publish(exchange, '', Buffer.from(
|
|
106
|
+
return channel.publish(exchange, '', Buffer.from(content));
|
|
143
107
|
}
|
|
144
108
|
async sendToQueue(queue, content) {
|
|
145
109
|
const channel = await this.assertChannel();
|
|
146
110
|
await this.assertQueue(queue);
|
|
147
|
-
return channel.sendToQueue(queue, Buffer.from(
|
|
111
|
+
return channel.sendToQueue(queue, Buffer.from(content));
|
|
148
112
|
}
|
|
149
113
|
}
|
|
150
114
|
exports.default = RabbitMq;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@autofleet/rabbit",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.15",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -10,8 +10,10 @@
|
|
|
10
10
|
"dev": "nodemon"
|
|
11
11
|
},
|
|
12
12
|
"dependencies": {
|
|
13
|
+
"@types/amqp-connection-manager": "^2.0.2",
|
|
13
14
|
"@types/amqplib": "^0.5.9",
|
|
14
15
|
"@types/jest": "^23.3.10",
|
|
16
|
+
"amqp-connection-manager": "^2.3.0",
|
|
15
17
|
"amqplib": "^0.5.3",
|
|
16
18
|
"jest": "^23.6.0"
|
|
17
19
|
},
|
package/src/index.ts
CHANGED
|
@@ -1,36 +1,16 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ConfirmChannel } from 'amqplib';
|
|
2
|
+
import { connect, AmqpConnectionManager, ChannelWrapper } from 'amqp-connection-manager';
|
|
2
3
|
import { EventEmitter } from 'events';
|
|
3
4
|
|
|
4
|
-
const channelCreatedConst: string = 'channelCreated';
|
|
5
5
|
class RabbitMq {
|
|
6
|
-
channel:
|
|
7
|
-
connection:
|
|
6
|
+
channel: ChannelWrapper | null;
|
|
7
|
+
connection: AmqpConnectionManager | null;
|
|
8
8
|
em: EventEmitter;
|
|
9
|
-
creatingChannel: boolean;
|
|
10
|
-
queues: any;
|
|
11
|
-
exchanges: any;
|
|
12
|
-
|
|
13
|
-
static parseMsg(msg: any) {
|
|
14
|
-
let { content } = msg;
|
|
15
|
-
content = content.toString();
|
|
16
|
-
|
|
17
|
-
try {
|
|
18
|
-
content = JSON.parse(content);
|
|
19
|
-
} catch(e) { }
|
|
20
|
-
|
|
21
|
-
return {
|
|
22
|
-
...msg,
|
|
23
|
-
content,
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
9
|
|
|
27
10
|
constructor() {
|
|
28
|
-
this.creatingChannel = false;
|
|
29
11
|
this.em = new EventEmitter;
|
|
30
12
|
this.channel = null;
|
|
31
13
|
this.connection = null;
|
|
32
|
-
this.queues = {};
|
|
33
|
-
this.exchanges = {};
|
|
34
14
|
process.once('SIGINT', () => {
|
|
35
15
|
console.log('Removing channel');
|
|
36
16
|
if(this.channel) {
|
|
@@ -60,59 +40,32 @@ class RabbitMq {
|
|
|
60
40
|
}
|
|
61
41
|
|
|
62
42
|
async getConnection() {
|
|
43
|
+
if (this.connection !== null) {
|
|
44
|
+
return this.connection;
|
|
45
|
+
}
|
|
46
|
+
|
|
63
47
|
let host: string = process.env.RABBITMQ_SERVICE_HOST || '35.240.13.28';
|
|
64
|
-
let connection:
|
|
48
|
+
let connection: AmqpConnectionManager = await connect([`amqp://${host}`]);
|
|
65
49
|
this.connection = connection;
|
|
66
50
|
return connection;
|
|
67
51
|
}
|
|
68
52
|
|
|
69
53
|
async assertChannel() {
|
|
70
|
-
return new Promise<
|
|
54
|
+
return new Promise<ChannelWrapper>(async (resolve, reject) => {
|
|
71
55
|
if(this.channel) {
|
|
72
56
|
return resolve(this.channel);
|
|
73
|
-
} if (this.creatingChannel) {
|
|
74
|
-
return this.em.on(channelCreatedConst, resolve);
|
|
75
57
|
}
|
|
76
58
|
|
|
77
|
-
|
|
78
|
-
let connection: Connection;
|
|
79
|
-
let channel: Channel | null = this.channel;
|
|
59
|
+
let connection: AmqpConnectionManager = await this.getConnection();
|
|
80
60
|
|
|
81
61
|
try {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
this.channel = null;
|
|
87
|
-
this.connection = null;
|
|
88
|
-
await this.assertChannel();
|
|
62
|
+
connection = await this.getConnection();
|
|
63
|
+
if (this.channel === null) {
|
|
64
|
+
this.channel = connection.createChannel({
|
|
65
|
+
json: true,
|
|
89
66
|
});
|
|
90
67
|
}
|
|
91
|
-
|
|
92
|
-
channel = await connection.createChannel();
|
|
93
|
-
this.channel = channel;
|
|
94
|
-
this.creatingChannel = false;
|
|
95
|
-
const queueJobs : Array<any> = [];
|
|
96
|
-
const exchangeJobs : Array<any> = [];
|
|
97
|
-
const queueKeys = Object.keys(this.queues);
|
|
98
|
-
queueKeys.forEach(queue => {
|
|
99
|
-
const { callback, options } = this.queues[queue];
|
|
100
|
-
queueJobs.push(this.consume(queue, callback, options, false));
|
|
101
|
-
});
|
|
102
|
-
const exchangeKeys = Object.keys(this.exchanges);
|
|
103
|
-
exchangeKeys.forEach(queue => {
|
|
104
|
-
const { exchange, callback, options } = this.exchanges[queue];
|
|
105
|
-
exchangeJobs.push(this.consumeFromExchange(queue, exchange, callback, options, false));
|
|
106
|
-
});
|
|
107
|
-
await Promise.all(queueJobs);
|
|
108
|
-
await Promise.all(exchangeJobs);
|
|
109
|
-
channel.on('error', async () => {
|
|
110
|
-
console.log('closed channel');
|
|
111
|
-
this.channel = null;
|
|
112
|
-
await this.assertChannel();
|
|
113
|
-
});
|
|
114
|
-
this.em.emit(channelCreatedConst, channel);
|
|
115
|
-
resolve(channel);
|
|
68
|
+
resolve(this.channel);
|
|
116
69
|
} catch (e) {
|
|
117
70
|
reject(e)
|
|
118
71
|
}
|
|
@@ -120,51 +73,57 @@ class RabbitMq {
|
|
|
120
73
|
}
|
|
121
74
|
|
|
122
75
|
async assertExchange(exchange: string, options?: any) {
|
|
123
|
-
const channel:
|
|
124
|
-
return channel.assertExchange(exchange, 'fanout')
|
|
76
|
+
const channel: ChannelWrapper = await this.assertChannel();
|
|
77
|
+
return channel.addSetup((c: ConfirmChannel) => c.assertExchange(exchange, 'fanout'));
|
|
125
78
|
}
|
|
126
79
|
|
|
127
80
|
async assertQueue(queue: string, options?: any) {
|
|
128
|
-
const channel:
|
|
129
|
-
return channel.assertQueue(queue);
|
|
81
|
+
const channel: ChannelWrapper = await this.assertChannel();
|
|
82
|
+
return channel.addSetup((c: ConfirmChannel) => c.assertQueue(queue));
|
|
130
83
|
}
|
|
131
84
|
|
|
132
85
|
async consume(queue: string, callback: (msg: any, ack: Function, nack: Function) => any, options?: any, isFirstTime: boolean = true) {
|
|
133
|
-
this.queues[queue] = this.queues[queue] ? this.queues[queue] : { callback, options };
|
|
134
86
|
const { limit } = options ? options : 1;
|
|
135
|
-
const channel:
|
|
87
|
+
const channel: ChannelWrapper = await this.assertChannel();
|
|
136
88
|
await this.assertQueue(queue);
|
|
137
89
|
if (!isFirstTime) {
|
|
138
|
-
channel.
|
|
139
|
-
|
|
90
|
+
return channel.addSetup((c: ConfirmChannel) => {
|
|
91
|
+
return Promise.all([
|
|
92
|
+
c.prefetch(limit, false),
|
|
93
|
+
c.consume(queue, (msg : any) => callback(msg, this.ack, this.nack(queue))),
|
|
94
|
+
]);
|
|
95
|
+
});
|
|
140
96
|
}
|
|
141
97
|
}
|
|
142
98
|
|
|
143
99
|
async consumeFromExchange(queue: string, exchange: string, callback: (msg: any, ack: Function, nack: Function) => any, options?: any, isFirstTime: boolean = true) {
|
|
144
|
-
this.exchanges[queue] = this.exchanges[queue] ? this.exchanges[queue] : { exchange, callback, options }
|
|
145
100
|
const { limit } = options ? options : 1;
|
|
146
|
-
const channel:
|
|
101
|
+
const channel: ChannelWrapper = await this.assertChannel();
|
|
147
102
|
await Promise.all([
|
|
148
103
|
this.assertExchange(exchange),
|
|
149
104
|
this.assertQueue(queue),
|
|
150
105
|
]);
|
|
151
106
|
if (!isFirstTime) {
|
|
152
|
-
channel.
|
|
153
|
-
|
|
154
|
-
|
|
107
|
+
return channel.addSetup((c: ConfirmChannel) => {
|
|
108
|
+
return Promise.all([
|
|
109
|
+
c.prefetch(limit, false),
|
|
110
|
+
c.bindQueue(queue, exchange, ''),
|
|
111
|
+
c.consume(queue, (msg : any) => callback(msg, this.ack, this.nack(queue))),
|
|
112
|
+
])
|
|
113
|
+
})
|
|
155
114
|
}
|
|
156
115
|
}
|
|
157
116
|
|
|
158
117
|
async publish(exchange: string, content: any) {
|
|
159
|
-
const channel:
|
|
118
|
+
const channel: ChannelWrapper = await this.assertChannel();
|
|
160
119
|
await this.assertExchange(exchange);
|
|
161
|
-
return channel.publish(exchange, '', Buffer.from(
|
|
120
|
+
return channel.publish(exchange, '', Buffer.from(content));
|
|
162
121
|
}
|
|
163
122
|
|
|
164
123
|
async sendToQueue(queue: string, content: any) {
|
|
165
|
-
const channel:
|
|
124
|
+
const channel: ChannelWrapper = await this.assertChannel();
|
|
166
125
|
await this.assertQueue(queue);
|
|
167
|
-
return channel.sendToQueue(queue, Buffer.from(
|
|
126
|
+
return channel.sendToQueue(queue, Buffer.from(content));
|
|
168
127
|
}
|
|
169
128
|
}
|
|
170
129
|
|