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