@autofleet/rabbit 1.0.22 → 1.0.24
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 +1 -0
- package/dist/index.js +11 -3
- package/package.json +1 -1
- package/src/index.ts +16 -3
package/dist/index.d.ts
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import { AmqpConnectionManager, ChannelWrapper } from 'amqp-connection-manager';
|
|
3
3
|
import { EventEmitter } from 'events';
|
|
4
4
|
declare class RabbitMq {
|
|
5
|
+
static parseMsg(msg: any): any;
|
|
5
6
|
channel: ChannelWrapper | null;
|
|
6
7
|
connection: AmqpConnectionManager | null;
|
|
7
8
|
em: EventEmitter;
|
package/dist/index.js
CHANGED
|
@@ -29,6 +29,15 @@ class RabbitMq {
|
|
|
29
29
|
this.connection = null;
|
|
30
30
|
this.creatingConnection = false;
|
|
31
31
|
}
|
|
32
|
+
static parseMsg(msg) {
|
|
33
|
+
let { content } = msg;
|
|
34
|
+
content = content.toString();
|
|
35
|
+
try {
|
|
36
|
+
content = JSON.parse(content);
|
|
37
|
+
}
|
|
38
|
+
catch (e) { }
|
|
39
|
+
return Object.assign({}, msg, { content });
|
|
40
|
+
}
|
|
32
41
|
async getConnection() {
|
|
33
42
|
return new Promise(async (resolve) => {
|
|
34
43
|
if (this.creatingConnection) {
|
|
@@ -54,7 +63,6 @@ class RabbitMq {
|
|
|
54
63
|
}
|
|
55
64
|
let connection = await this.getConnection();
|
|
56
65
|
try {
|
|
57
|
-
connection = await this.getConnection();
|
|
58
66
|
if (this.channel === null) {
|
|
59
67
|
this.channel = connection.createChannel({});
|
|
60
68
|
}
|
|
@@ -80,7 +88,7 @@ class RabbitMq {
|
|
|
80
88
|
return channel.addSetup((c) => {
|
|
81
89
|
return Promise.all([
|
|
82
90
|
c.prefetch(limit, false),
|
|
83
|
-
c.consume(queue, (msg) => callback(
|
|
91
|
+
c.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue))),
|
|
84
92
|
]);
|
|
85
93
|
});
|
|
86
94
|
}
|
|
@@ -95,7 +103,7 @@ class RabbitMq {
|
|
|
95
103
|
return Promise.all([
|
|
96
104
|
c.prefetch(limit, false),
|
|
97
105
|
c.bindQueue(queue, exchange, ''),
|
|
98
|
-
c.consume(queue, (msg) => callback(
|
|
106
|
+
c.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue))),
|
|
99
107
|
]);
|
|
100
108
|
});
|
|
101
109
|
}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -4,6 +4,20 @@ import { EventEmitter } from 'events';
|
|
|
4
4
|
|
|
5
5
|
const connectionCreatedConst = 'connectionCreated';
|
|
6
6
|
class RabbitMq {
|
|
7
|
+
static parseMsg(msg: any) {
|
|
8
|
+
let { content } = msg;
|
|
9
|
+
content = content.toString();
|
|
10
|
+
|
|
11
|
+
try {
|
|
12
|
+
content = JSON.parse(content);
|
|
13
|
+
} catch (e) { }
|
|
14
|
+
|
|
15
|
+
return {
|
|
16
|
+
...msg,
|
|
17
|
+
content,
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
7
21
|
channel: ChannelWrapper | null;
|
|
8
22
|
connection: AmqpConnectionManager | null;
|
|
9
23
|
em: EventEmitter;
|
|
@@ -64,7 +78,6 @@ class RabbitMq {
|
|
|
64
78
|
let connection: AmqpConnectionManager = await this.getConnection();
|
|
65
79
|
|
|
66
80
|
try {
|
|
67
|
-
connection = await this.getConnection();
|
|
68
81
|
if (this.channel === null) {
|
|
69
82
|
this.channel = connection.createChannel({});
|
|
70
83
|
}
|
|
@@ -92,7 +105,7 @@ class RabbitMq {
|
|
|
92
105
|
return channel.addSetup((c: ConfirmChannel) => {
|
|
93
106
|
return Promise.all([
|
|
94
107
|
c.prefetch(limit, false),
|
|
95
|
-
c.consume(queue, (msg: any) => callback(
|
|
108
|
+
c.consume(queue, (msg: any) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue))),
|
|
96
109
|
]);
|
|
97
110
|
});
|
|
98
111
|
}
|
|
@@ -109,7 +122,7 @@ class RabbitMq {
|
|
|
109
122
|
return Promise.all([
|
|
110
123
|
c.prefetch(limit, false),
|
|
111
124
|
c.bindQueue(queue, exchange, ''),
|
|
112
|
-
c.consume(queue, (msg: any) => callback(
|
|
125
|
+
c.consume(queue, (msg: any) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue))),
|
|
113
126
|
])
|
|
114
127
|
})
|
|
115
128
|
}
|