@autofleet/rabbit 1.0.14 → 1.0.16
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 +30 -70
- package/package.json +3 -1
- package/src/index.ts +42 -86
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
|
|
20
|
-
consumeFromExchange(queue: string, exchange: string, callback: (msg: any, ack: Function, nack: Function) => any, options?: any
|
|
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): Promise<void>;
|
|
16
|
+
consumeFromExchange(queue: string, exchange: string, callback: (msg: any, ack: Function, nack: Function) => any, options?: any): 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,45 +64,47 @@ 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
|
-
async consume(queue, callback, options
|
|
116
|
-
this.queues[queue] = this.queues[queue] ? this.queues[queue] : { callback, options };
|
|
73
|
+
async consume(queue, callback, options) {
|
|
117
74
|
const { limit } = options ? options : 1;
|
|
118
75
|
const channel = await this.assertChannel();
|
|
119
76
|
await this.assertQueue(queue);
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
77
|
+
return channel.addSetup((c) => {
|
|
78
|
+
return Promise.all([
|
|
79
|
+
c.prefetch(limit, false),
|
|
80
|
+
c.consume(queue, (msg) => callback(msg, this.ack, this.nack(queue))),
|
|
81
|
+
]);
|
|
82
|
+
});
|
|
124
83
|
}
|
|
125
|
-
async consumeFromExchange(queue, exchange, callback, options
|
|
126
|
-
this.exchanges[queue] = this.exchanges[queue] ? this.exchanges[queue] : { exchange, callback, options };
|
|
84
|
+
async consumeFromExchange(queue, exchange, callback, options) {
|
|
127
85
|
const { limit } = options ? options : 1;
|
|
128
86
|
const channel = await this.assertChannel();
|
|
129
87
|
await Promise.all([
|
|
130
88
|
this.assertExchange(exchange),
|
|
131
89
|
this.assertQueue(queue),
|
|
132
90
|
]);
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
91
|
+
return channel.addSetup((c) => {
|
|
92
|
+
return Promise.all([
|
|
93
|
+
c.prefetch(limit, false),
|
|
94
|
+
c.bindQueue(queue, exchange, ''),
|
|
95
|
+
c.consume(queue, (msg) => callback(msg, this.ack, this.nack(queue))),
|
|
96
|
+
]);
|
|
97
|
+
});
|
|
138
98
|
}
|
|
139
99
|
async publish(exchange, content) {
|
|
140
100
|
const channel = await this.assertChannel();
|
|
141
101
|
await this.assertExchange(exchange);
|
|
142
|
-
return channel.publish(exchange, '', Buffer.from(
|
|
102
|
+
return channel.publish(exchange, '', Buffer.from(content));
|
|
143
103
|
}
|
|
144
104
|
async sendToQueue(queue, content) {
|
|
145
105
|
const channel = await this.assertChannel();
|
|
146
106
|
await this.assertQueue(queue);
|
|
147
|
-
return channel.sendToQueue(queue, Buffer.from(
|
|
107
|
+
return channel.sendToQueue(queue, Buffer.from(content));
|
|
148
108
|
}
|
|
149
109
|
}
|
|
150
110
|
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.16",
|
|
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,54 @@ 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
|
-
async consume(queue: string, callback: (msg: any, ack: Function, nack: Function) => any, options?: any
|
|
133
|
-
this.queues[queue] = this.queues[queue] ? this.queues[queue] : { callback, options };
|
|
85
|
+
async consume(queue: string, callback: (msg: any, ack: Function, nack: Function) => any, options?: any) {
|
|
134
86
|
const { limit } = options ? options : 1;
|
|
135
|
-
const channel:
|
|
87
|
+
const channel: ChannelWrapper = await this.assertChannel();
|
|
136
88
|
await this.assertQueue(queue);
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
89
|
+
return channel.addSetup((c: ConfirmChannel) => {
|
|
90
|
+
return Promise.all([
|
|
91
|
+
c.prefetch(limit, false),
|
|
92
|
+
c.consume(queue, (msg : any) => callback(msg, this.ack, this.nack(queue))),
|
|
93
|
+
]);
|
|
94
|
+
});
|
|
141
95
|
}
|
|
142
96
|
|
|
143
|
-
async consumeFromExchange(queue: string, exchange: string, callback: (msg: any, ack: Function, nack: Function) => any, options?: any
|
|
144
|
-
this.exchanges[queue] = this.exchanges[queue] ? this.exchanges[queue] : { exchange, callback, options }
|
|
97
|
+
async consumeFromExchange(queue: string, exchange: string, callback: (msg: any, ack: Function, nack: Function) => any, options?: any) {
|
|
145
98
|
const { limit } = options ? options : 1;
|
|
146
|
-
const channel:
|
|
99
|
+
const channel: ChannelWrapper = await this.assertChannel();
|
|
147
100
|
await Promise.all([
|
|
148
101
|
this.assertExchange(exchange),
|
|
149
102
|
this.assertQueue(queue),
|
|
150
103
|
]);
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
104
|
+
|
|
105
|
+
return channel.addSetup((c: ConfirmChannel) => {
|
|
106
|
+
return Promise.all([
|
|
107
|
+
c.prefetch(limit, false),
|
|
108
|
+
c.bindQueue(queue, exchange, ''),
|
|
109
|
+
c.consume(queue, (msg : any) => callback(msg, this.ack, this.nack(queue))),
|
|
110
|
+
])
|
|
111
|
+
})
|
|
156
112
|
}
|
|
157
113
|
|
|
158
114
|
async publish(exchange: string, content: any) {
|
|
159
|
-
const channel:
|
|
115
|
+
const channel: ChannelWrapper = await this.assertChannel();
|
|
160
116
|
await this.assertExchange(exchange);
|
|
161
|
-
return channel.publish(exchange, '', Buffer.from(
|
|
117
|
+
return channel.publish(exchange, '', Buffer.from(content));
|
|
162
118
|
}
|
|
163
119
|
|
|
164
120
|
async sendToQueue(queue: string, content: any) {
|
|
165
|
-
const channel:
|
|
121
|
+
const channel: ChannelWrapper = await this.assertChannel();
|
|
166
122
|
await this.assertQueue(queue);
|
|
167
|
-
return channel.sendToQueue(queue, Buffer.from(
|
|
123
|
+
return channel.sendToQueue(queue, Buffer.from(content));
|
|
168
124
|
}
|
|
169
125
|
}
|
|
170
126
|
|