@autofleet/rabbit 1.0.4 → 1.0.6
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/example.js +8 -16
- package/dist/index.js +48 -68
- package/package.json +1 -1
- package/src/index.ts +2 -0
- package/tsconfig.json +4 -1
package/dist/example.js
CHANGED
|
@@ -1,30 +1,22 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
3
|
const index_1 = require("./index");
|
|
12
|
-
const init = () =>
|
|
4
|
+
const init = async () => {
|
|
13
5
|
const rabbit = new index_1.default();
|
|
14
|
-
|
|
6
|
+
await rabbit.consumeFromExchange('test-q2', 'test-e', (msg, ack) => {
|
|
15
7
|
console.log('msg2', msg.content);
|
|
16
8
|
// ack(msg);
|
|
17
9
|
});
|
|
18
|
-
|
|
10
|
+
await rabbit.consumeFromExchange('test-q', 'test-e', (msg, ack) => {
|
|
19
11
|
console.log('msg', msg.content);
|
|
20
12
|
ack(msg);
|
|
21
13
|
});
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
14
|
+
await rabbit.publish('test-e', 'hey');
|
|
15
|
+
await rabbit.publish('test-e', 'hey1');
|
|
16
|
+
await rabbit.consume('test-q-2', (msg, ack) => {
|
|
25
17
|
console.log('msg-q', msg.content);
|
|
26
18
|
ack(msg);
|
|
27
19
|
});
|
|
28
|
-
|
|
29
|
-
}
|
|
20
|
+
await rabbit.sendToQueue('test-q-2', 'hey-q');
|
|
21
|
+
};
|
|
30
22
|
init();
|
package/dist/index.js
CHANGED
|
@@ -1,12 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
3
|
const amqplib_1 = require("amqplib");
|
|
12
4
|
const events_1 = require("events");
|
|
@@ -37,73 +29,61 @@ class RabbitMq {
|
|
|
37
29
|
catch (e) { }
|
|
38
30
|
return Object.assign({}, msg, { content });
|
|
39
31
|
}
|
|
40
|
-
assertChannel() {
|
|
41
|
-
return
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
}
|
|
63
|
-
}));
|
|
32
|
+
async assertChannel() {
|
|
33
|
+
return new Promise(async (resolve, reject) => {
|
|
34
|
+
if (this.channel) {
|
|
35
|
+
return resolve(this.channel);
|
|
36
|
+
}
|
|
37
|
+
if (this.creatingChannel) {
|
|
38
|
+
return this.em.on(channelCreatedConst, resolve);
|
|
39
|
+
}
|
|
40
|
+
this.creatingChannel = true;
|
|
41
|
+
let connection, channel;
|
|
42
|
+
let host = process.env.RABBITMQ_SERVICE_HOST || '35.240.13.28';
|
|
43
|
+
try {
|
|
44
|
+
connection = await amqplib_1.connect(`amqp://${host}`);
|
|
45
|
+
channel = await connection.createChannel();
|
|
46
|
+
this.channel = channel;
|
|
47
|
+
this.creatingChannel = false;
|
|
48
|
+
this.em.emit(channelCreatedConst, channel);
|
|
49
|
+
resolve(channel);
|
|
50
|
+
}
|
|
51
|
+
catch (e) {
|
|
52
|
+
reject(e);
|
|
53
|
+
}
|
|
64
54
|
});
|
|
65
55
|
}
|
|
66
|
-
assertExchange(exchange, options) {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
return channel.assertExchange(exchange, 'fanout');
|
|
70
|
-
});
|
|
56
|
+
async assertExchange(exchange, options) {
|
|
57
|
+
const channel = await this.assertChannel();
|
|
58
|
+
return channel.assertExchange(exchange, 'fanout');
|
|
71
59
|
}
|
|
72
|
-
assertQueue(queue, options) {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
return channel.assertQueue(queue);
|
|
76
|
-
});
|
|
60
|
+
async assertQueue(queue, options) {
|
|
61
|
+
const channel = await this.assertChannel();
|
|
62
|
+
return channel.assertQueue(queue);
|
|
77
63
|
}
|
|
78
|
-
consume(queue, callback) {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
return channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack));
|
|
83
|
-
});
|
|
64
|
+
async consume(queue, callback) {
|
|
65
|
+
const channel = await this.assertChannel();
|
|
66
|
+
await this.assertQueue(queue);
|
|
67
|
+
return channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack));
|
|
84
68
|
}
|
|
85
|
-
consumeFromExchange(queue, exchange, callback) {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
return channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack));
|
|
94
|
-
});
|
|
69
|
+
async consumeFromExchange(queue, exchange, callback) {
|
|
70
|
+
const channel = await this.assertChannel();
|
|
71
|
+
await Promise.all([
|
|
72
|
+
this.assertExchange(exchange),
|
|
73
|
+
this.assertQueue(queue),
|
|
74
|
+
]);
|
|
75
|
+
await channel.bindQueue(queue, exchange, '');
|
|
76
|
+
return channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack));
|
|
95
77
|
}
|
|
96
|
-
publish(exchange, content) {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
});
|
|
78
|
+
async publish(exchange, content) {
|
|
79
|
+
const channel = await this.assertChannel();
|
|
80
|
+
await this.assertExchange(exchange);
|
|
81
|
+
return channel.publish(exchange, '', Buffer.from(content));
|
|
101
82
|
}
|
|
102
|
-
sendToQueue(queue, content) {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
});
|
|
83
|
+
async sendToQueue(queue, content) {
|
|
84
|
+
const channel = await this.assertChannel();
|
|
85
|
+
await this.assertQueue(queue);
|
|
86
|
+
return channel.sendToQueue(queue, Buffer.from(content));
|
|
107
87
|
}
|
|
108
88
|
}
|
|
109
89
|
exports.default = RabbitMq;
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -94,11 +94,13 @@ class RabbitMq {
|
|
|
94
94
|
|
|
95
95
|
async publish(exchange: string, content: any) {
|
|
96
96
|
const channel: Channel = await this.assertChannel();
|
|
97
|
+
await this.assertExchange(exchange);
|
|
97
98
|
return channel.publish(exchange, '', Buffer.from(content));
|
|
98
99
|
}
|
|
99
100
|
|
|
100
101
|
async sendToQueue(queue: string, content: any) {
|
|
101
102
|
const channel: Channel = await this.assertChannel();
|
|
103
|
+
await this.assertQueue(queue);
|
|
102
104
|
return channel.sendToQueue(queue, Buffer.from(content));
|
|
103
105
|
}
|
|
104
106
|
}
|