@autofleet/rabbit 1.0.13 → 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 -11
- package/dist/index.js +40 -41
- package/package.json +3 -1
- package/src/index.ts +54 -57
package/dist/index.d.ts
CHANGED
|
@@ -1,20 +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:
|
|
5
|
+
channel: ChannelWrapper | null;
|
|
6
|
+
connection: AmqpConnectionManager | null;
|
|
6
7
|
em: EventEmitter;
|
|
7
|
-
creatingChannel: boolean;
|
|
8
|
-
static parseMsg(msg: any): any;
|
|
9
8
|
constructor();
|
|
10
9
|
ack: (msg: any) => Promise<void>;
|
|
11
10
|
nack: (queue: string) => (msg: any, retries: number) => Promise<void>;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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>;
|
|
19
19
|
}
|
|
20
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,9 +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;
|
|
28
|
+
this.connection = null;
|
|
30
29
|
process.once('SIGINT', () => {
|
|
31
30
|
console.log('Removing channel');
|
|
32
31
|
if (this.channel) {
|
|
@@ -34,41 +33,29 @@ class RabbitMq {
|
|
|
34
33
|
}
|
|
35
34
|
});
|
|
36
35
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
try {
|
|
41
|
-
content = JSON.parse(content);
|
|
36
|
+
async getConnection() {
|
|
37
|
+
if (this.connection !== null) {
|
|
38
|
+
return this.connection;
|
|
42
39
|
}
|
|
43
|
-
|
|
44
|
-
|
|
40
|
+
let host = process.env.RABBITMQ_SERVICE_HOST || '35.240.13.28';
|
|
41
|
+
let connection = await amqp_connection_manager_1.connect([`amqp://${host}`]);
|
|
42
|
+
this.connection = connection;
|
|
43
|
+
return connection;
|
|
45
44
|
}
|
|
46
45
|
async assertChannel() {
|
|
47
46
|
return new Promise(async (resolve, reject) => {
|
|
48
47
|
if (this.channel) {
|
|
49
48
|
return resolve(this.channel);
|
|
50
49
|
}
|
|
51
|
-
|
|
52
|
-
return this.em.on(channelCreatedConst, resolve);
|
|
53
|
-
}
|
|
54
|
-
this.creatingChannel = true;
|
|
55
|
-
let connection, channel;
|
|
56
|
-
let host = process.env.RABBITMQ_SERVICE_HOST || '35.240.13.28';
|
|
50
|
+
let connection = await this.getConnection();
|
|
57
51
|
try {
|
|
58
|
-
connection = await
|
|
59
|
-
channel
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
connection.on('close', async () => {
|
|
66
|
-
this.channel = null;
|
|
67
|
-
await this.assertChannel();
|
|
68
|
-
});
|
|
69
|
-
this.creatingChannel = false;
|
|
70
|
-
this.em.emit(channelCreatedConst, channel);
|
|
71
|
-
resolve(channel);
|
|
52
|
+
connection = await this.getConnection();
|
|
53
|
+
if (this.channel === null) {
|
|
54
|
+
this.channel = connection.createChannel({
|
|
55
|
+
json: true,
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
resolve(this.channel);
|
|
72
59
|
}
|
|
73
60
|
catch (e) {
|
|
74
61
|
reject(e);
|
|
@@ -77,39 +64,51 @@ class RabbitMq {
|
|
|
77
64
|
}
|
|
78
65
|
async assertExchange(exchange, options) {
|
|
79
66
|
const channel = await this.assertChannel();
|
|
80
|
-
return channel.assertExchange(exchange, 'fanout');
|
|
67
|
+
return channel.addSetup((c) => c.assertExchange(exchange, 'fanout'));
|
|
81
68
|
}
|
|
82
69
|
async assertQueue(queue, options) {
|
|
83
70
|
const channel = await this.assertChannel();
|
|
84
|
-
return channel.assertQueue(queue);
|
|
71
|
+
return channel.addSetup((c) => c.assertQueue(queue));
|
|
85
72
|
}
|
|
86
|
-
async consume(queue, callback, options) {
|
|
73
|
+
async consume(queue, callback, options, isFirstTime = true) {
|
|
87
74
|
const { limit } = options ? options : 1;
|
|
88
75
|
const channel = await this.assertChannel();
|
|
89
76
|
await this.assertQueue(queue);
|
|
90
|
-
|
|
91
|
-
|
|
77
|
+
if (!isFirstTime) {
|
|
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
|
+
});
|
|
84
|
+
}
|
|
92
85
|
}
|
|
93
|
-
async consumeFromExchange(queue, exchange, callback, options) {
|
|
86
|
+
async consumeFromExchange(queue, exchange, callback, options, isFirstTime = true) {
|
|
94
87
|
const { limit } = options ? options : 1;
|
|
95
88
|
const channel = await this.assertChannel();
|
|
96
89
|
await Promise.all([
|
|
97
90
|
this.assertExchange(exchange),
|
|
98
91
|
this.assertQueue(queue),
|
|
99
92
|
]);
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
93
|
+
if (!isFirstTime) {
|
|
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
|
+
});
|
|
101
|
+
}
|
|
103
102
|
}
|
|
104
103
|
async publish(exchange, content) {
|
|
105
104
|
const channel = await this.assertChannel();
|
|
106
105
|
await this.assertExchange(exchange);
|
|
107
|
-
return channel.publish(exchange, '', Buffer.from(
|
|
106
|
+
return channel.publish(exchange, '', Buffer.from(content));
|
|
108
107
|
}
|
|
109
108
|
async sendToQueue(queue, content) {
|
|
110
109
|
const channel = await this.assertChannel();
|
|
111
110
|
await this.assertQueue(queue);
|
|
112
|
-
return channel.sendToQueue(queue, Buffer.from(
|
|
111
|
+
return channel.sendToQueue(queue, Buffer.from(content));
|
|
113
112
|
}
|
|
114
113
|
}
|
|
115
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,31 +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:
|
|
6
|
+
channel: ChannelWrapper | null;
|
|
7
|
+
connection: AmqpConnectionManager | null;
|
|
7
8
|
em: EventEmitter;
|
|
8
|
-
creatingChannel: boolean;
|
|
9
|
-
|
|
10
|
-
static parseMsg(msg: any) {
|
|
11
|
-
let { content } = msg;
|
|
12
|
-
content = content.toString();
|
|
13
|
-
|
|
14
|
-
try {
|
|
15
|
-
content = JSON.parse(content);
|
|
16
|
-
} catch(e) { }
|
|
17
|
-
|
|
18
|
-
return {
|
|
19
|
-
...msg,
|
|
20
|
-
content,
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
9
|
|
|
24
10
|
constructor() {
|
|
25
|
-
this.creatingChannel = false;
|
|
26
11
|
this.em = new EventEmitter;
|
|
27
12
|
this.channel = null;
|
|
28
|
-
|
|
13
|
+
this.connection = null;
|
|
29
14
|
process.once('SIGINT', () => {
|
|
30
15
|
console.log('Removing channel');
|
|
31
16
|
if(this.channel) {
|
|
@@ -54,33 +39,33 @@ class RabbitMq {
|
|
|
54
39
|
}
|
|
55
40
|
}
|
|
56
41
|
|
|
42
|
+
async getConnection() {
|
|
43
|
+
if (this.connection !== null) {
|
|
44
|
+
return this.connection;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
let host: string = process.env.RABBITMQ_SERVICE_HOST || '35.240.13.28';
|
|
48
|
+
let connection: AmqpConnectionManager = await connect([`amqp://${host}`]);
|
|
49
|
+
this.connection = connection;
|
|
50
|
+
return connection;
|
|
51
|
+
}
|
|
52
|
+
|
|
57
53
|
async assertChannel() {
|
|
58
|
-
return new Promise<
|
|
54
|
+
return new Promise<ChannelWrapper>(async (resolve, reject) => {
|
|
59
55
|
if(this.channel) {
|
|
60
56
|
return resolve(this.channel);
|
|
61
|
-
} if (this.creatingChannel) {
|
|
62
|
-
return this.em.on(channelCreatedConst, resolve);
|
|
63
57
|
}
|
|
64
58
|
|
|
65
|
-
|
|
66
|
-
let connection: Connection, channel: Channel;
|
|
67
|
-
let host: string = process.env.RABBITMQ_SERVICE_HOST || '35.240.13.28';
|
|
59
|
+
let connection: AmqpConnectionManager = await this.getConnection();
|
|
68
60
|
|
|
69
61
|
try {
|
|
70
|
-
connection = await
|
|
71
|
-
channel
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
connection.on('close', async () => {
|
|
78
|
-
this.channel = null;
|
|
79
|
-
await this.assertChannel();
|
|
80
|
-
});
|
|
81
|
-
this.creatingChannel = false;
|
|
82
|
-
this.em.emit(channelCreatedConst, channel);
|
|
83
|
-
resolve(channel);
|
|
62
|
+
connection = await this.getConnection();
|
|
63
|
+
if (this.channel === null) {
|
|
64
|
+
this.channel = connection.createChannel({
|
|
65
|
+
json: true,
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
resolve(this.channel);
|
|
84
69
|
} catch (e) {
|
|
85
70
|
reject(e)
|
|
86
71
|
}
|
|
@@ -88,45 +73,57 @@ class RabbitMq {
|
|
|
88
73
|
}
|
|
89
74
|
|
|
90
75
|
async assertExchange(exchange: string, options?: any) {
|
|
91
|
-
const channel:
|
|
92
|
-
return channel.assertExchange(exchange, 'fanout')
|
|
76
|
+
const channel: ChannelWrapper = await this.assertChannel();
|
|
77
|
+
return channel.addSetup((c: ConfirmChannel) => c.assertExchange(exchange, 'fanout'));
|
|
93
78
|
}
|
|
94
79
|
|
|
95
80
|
async assertQueue(queue: string, options?: any) {
|
|
96
|
-
const channel:
|
|
97
|
-
return channel.assertQueue(queue);
|
|
81
|
+
const channel: ChannelWrapper = await this.assertChannel();
|
|
82
|
+
return channel.addSetup((c: ConfirmChannel) => c.assertQueue(queue));
|
|
98
83
|
}
|
|
99
84
|
|
|
100
|
-
async consume(queue: string, callback: (msg: any, ack: Function, nack: Function) => any, options?: any) {
|
|
85
|
+
async consume(queue: string, callback: (msg: any, ack: Function, nack: Function) => any, options?: any, isFirstTime: boolean = true) {
|
|
101
86
|
const { limit } = options ? options : 1;
|
|
102
|
-
const channel:
|
|
87
|
+
const channel: ChannelWrapper = await this.assertChannel();
|
|
103
88
|
await this.assertQueue(queue);
|
|
104
|
-
|
|
105
|
-
|
|
89
|
+
if (!isFirstTime) {
|
|
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
|
+
});
|
|
96
|
+
}
|
|
106
97
|
}
|
|
107
98
|
|
|
108
|
-
async consumeFromExchange(queue: string, exchange: string, callback: (msg: any, ack: Function, nack: Function) => any, options?: any) {
|
|
99
|
+
async consumeFromExchange(queue: string, exchange: string, callback: (msg: any, ack: Function, nack: Function) => any, options?: any, isFirstTime: boolean = true) {
|
|
109
100
|
const { limit } = options ? options : 1;
|
|
110
|
-
const channel:
|
|
101
|
+
const channel: ChannelWrapper = await this.assertChannel();
|
|
111
102
|
await Promise.all([
|
|
112
103
|
this.assertExchange(exchange),
|
|
113
104
|
this.assertQueue(queue),
|
|
114
105
|
]);
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
106
|
+
if (!isFirstTime) {
|
|
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
|
+
})
|
|
114
|
+
}
|
|
118
115
|
}
|
|
119
116
|
|
|
120
117
|
async publish(exchange: string, content: any) {
|
|
121
|
-
const channel:
|
|
118
|
+
const channel: ChannelWrapper = await this.assertChannel();
|
|
122
119
|
await this.assertExchange(exchange);
|
|
123
|
-
return channel.publish(exchange, '', Buffer.from(
|
|
120
|
+
return channel.publish(exchange, '', Buffer.from(content));
|
|
124
121
|
}
|
|
125
122
|
|
|
126
123
|
async sendToQueue(queue: string, content: any) {
|
|
127
|
-
const channel:
|
|
124
|
+
const channel: ChannelWrapper = await this.assertChannel();
|
|
128
125
|
await this.assertQueue(queue);
|
|
129
|
-
return channel.sendToQueue(queue, Buffer.from(
|
|
126
|
+
return channel.sendToQueue(queue, Buffer.from(content));
|
|
130
127
|
}
|
|
131
128
|
}
|
|
132
129
|
|