@autofleet/rabbit 1.1.0 → 1.2.1
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 +5 -1
- package/dist/index.js +14 -6
- package/package.json +1 -1
- package/src/index.ts +19 -6
package/dist/index.d.ts
CHANGED
|
@@ -1,18 +1,22 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { AmqpConnectionManager, ChannelWrapper } from 'amqp-connection-manager';
|
|
3
3
|
import { EventEmitter } from 'events';
|
|
4
|
+
interface ExchangesCache {
|
|
5
|
+
[key: string]: any;
|
|
6
|
+
}
|
|
4
7
|
declare class RabbitMq {
|
|
5
8
|
static parseMsg(msg: any): any;
|
|
6
9
|
channel: ChannelWrapper | null;
|
|
7
10
|
connection: AmqpConnectionManager | null;
|
|
8
11
|
em: EventEmitter;
|
|
9
12
|
creatingConnection: Boolean;
|
|
13
|
+
exchanges: ExchangesCache;
|
|
10
14
|
constructor();
|
|
11
15
|
ack: (msg: any) => Promise<void>;
|
|
12
16
|
nack: (queue: string) => (msg: any, retries: number) => Promise<void>;
|
|
13
17
|
getConnection(): Promise<AmqpConnectionManager>;
|
|
14
18
|
assertChannel(): Promise<ChannelWrapper>;
|
|
15
|
-
assertExchange(
|
|
19
|
+
assertExchange(exchangeName: string, options?: any): Promise<any>;
|
|
16
20
|
assertQueue(queue: string, options?: any): Promise<void>;
|
|
17
21
|
consume(queue: string, callback: (msg: any, ack: Function, nack: Function) => any, options?: any): Promise<void>;
|
|
18
22
|
consumeFromExchange(queue: string, exchange: string, callback: (msg: any, ack: Function, nack: Function) => any, options?: any): Promise<void>;
|
package/dist/index.js
CHANGED
|
@@ -28,6 +28,7 @@ class RabbitMq {
|
|
|
28
28
|
this.channel = null;
|
|
29
29
|
this.connection = null;
|
|
30
30
|
this.creatingConnection = false;
|
|
31
|
+
this.exchanges = {};
|
|
31
32
|
}
|
|
32
33
|
static parseMsg(msg) {
|
|
33
34
|
let { content } = msg;
|
|
@@ -73,9 +74,16 @@ class RabbitMq {
|
|
|
73
74
|
}
|
|
74
75
|
});
|
|
75
76
|
}
|
|
76
|
-
async assertExchange(
|
|
77
|
+
async assertExchange(exchangeName, options) {
|
|
77
78
|
const channel = await this.assertChannel();
|
|
78
|
-
|
|
79
|
+
if (this.exchanges[exchangeName]) {
|
|
80
|
+
return this.exchanges[exchangeName];
|
|
81
|
+
}
|
|
82
|
+
return channel.addSetup(async (c) => {
|
|
83
|
+
const exchange = await c.assertExchange(exchangeName, 'fanout');
|
|
84
|
+
this.exchanges[exchangeName] = exchange;
|
|
85
|
+
return exchange;
|
|
86
|
+
});
|
|
79
87
|
}
|
|
80
88
|
async assertQueue(queue, options) {
|
|
81
89
|
const channel = await this.assertChannel();
|
|
@@ -85,9 +93,9 @@ class RabbitMq {
|
|
|
85
93
|
const { limit } = options ? options : 1;
|
|
86
94
|
const channel = await this.assertChannel();
|
|
87
95
|
await this.assertQueue(queue);
|
|
88
|
-
return channel.addSetup((c) => {
|
|
96
|
+
return channel.addSetup(async (c) => {
|
|
97
|
+
await c.prefetch(limit, true);
|
|
89
98
|
return Promise.all([
|
|
90
|
-
c.prefetch(limit, true),
|
|
91
99
|
c.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue))),
|
|
92
100
|
]);
|
|
93
101
|
});
|
|
@@ -99,9 +107,9 @@ class RabbitMq {
|
|
|
99
107
|
this.assertExchange(exchange),
|
|
100
108
|
this.assertQueue(queue),
|
|
101
109
|
]);
|
|
102
|
-
return channel.addSetup((c) => {
|
|
110
|
+
return channel.addSetup(async (c) => {
|
|
111
|
+
await c.prefetch(limit, true);
|
|
103
112
|
return Promise.all([
|
|
104
|
-
c.prefetch(limit, true),
|
|
105
113
|
c.bindQueue(queue, exchange, ''),
|
|
106
114
|
c.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue))),
|
|
107
115
|
]);
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -2,6 +2,10 @@ import { ConfirmChannel } from 'amqplib';
|
|
|
2
2
|
import { connect, AmqpConnectionManager, ChannelWrapper } from 'amqp-connection-manager';
|
|
3
3
|
import { EventEmitter } from 'events';
|
|
4
4
|
|
|
5
|
+
interface ExchangesCache {
|
|
6
|
+
[key: string]: any
|
|
7
|
+
}
|
|
8
|
+
|
|
5
9
|
const connectionCreatedConst = 'connectionCreated';
|
|
6
10
|
class RabbitMq {
|
|
7
11
|
static parseMsg(msg: any) {
|
|
@@ -22,12 +26,14 @@ class RabbitMq {
|
|
|
22
26
|
connection: AmqpConnectionManager | null;
|
|
23
27
|
em: EventEmitter;
|
|
24
28
|
creatingConnection: Boolean;
|
|
29
|
+
exchanges: ExchangesCache;
|
|
25
30
|
|
|
26
31
|
constructor() {
|
|
27
32
|
this.em = new EventEmitter;
|
|
28
33
|
this.channel = null;
|
|
29
34
|
this.connection = null;
|
|
30
35
|
this.creatingConnection = false;
|
|
36
|
+
this.exchanges = {};
|
|
31
37
|
}
|
|
32
38
|
|
|
33
39
|
public ack = async (msg: any) => {
|
|
@@ -88,9 +94,16 @@ class RabbitMq {
|
|
|
88
94
|
})
|
|
89
95
|
}
|
|
90
96
|
|
|
91
|
-
async assertExchange(
|
|
97
|
+
async assertExchange(exchangeName: string, options?: any) {
|
|
92
98
|
const channel: ChannelWrapper = await this.assertChannel();
|
|
93
|
-
|
|
99
|
+
if(this.exchanges[exchangeName]) {
|
|
100
|
+
return this.exchanges[exchangeName]
|
|
101
|
+
}
|
|
102
|
+
return channel.addSetup(async (c: ConfirmChannel) => {
|
|
103
|
+
const exchange = await c.assertExchange(exchangeName, 'fanout')
|
|
104
|
+
this.exchanges[exchangeName] = exchange;
|
|
105
|
+
return exchange;
|
|
106
|
+
});
|
|
94
107
|
}
|
|
95
108
|
|
|
96
109
|
async assertQueue(queue: string, options?: any) {
|
|
@@ -102,9 +115,9 @@ class RabbitMq {
|
|
|
102
115
|
const { limit } = options ? options : 1;
|
|
103
116
|
const channel: ChannelWrapper = await this.assertChannel();
|
|
104
117
|
await this.assertQueue(queue);
|
|
105
|
-
return channel.addSetup((c: ConfirmChannel) => {
|
|
118
|
+
return channel.addSetup(async (c: ConfirmChannel) => {
|
|
119
|
+
await c.prefetch(limit, true);
|
|
106
120
|
return Promise.all([
|
|
107
|
-
c.prefetch(limit, true),
|
|
108
121
|
c.consume(queue, (msg: any) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue))),
|
|
109
122
|
]);
|
|
110
123
|
});
|
|
@@ -118,9 +131,9 @@ class RabbitMq {
|
|
|
118
131
|
this.assertQueue(queue),
|
|
119
132
|
]);
|
|
120
133
|
|
|
121
|
-
return channel.addSetup((c: ConfirmChannel) => {
|
|
134
|
+
return channel.addSetup(async (c: ConfirmChannel) => {
|
|
135
|
+
await c.prefetch(limit, true);
|
|
122
136
|
return Promise.all([
|
|
123
|
-
c.prefetch(limit, true),
|
|
124
137
|
c.bindQueue(queue, exchange, ''),
|
|
125
138
|
c.consume(queue, (msg: any) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue))),
|
|
126
139
|
])
|