@autofleet/rabbit 1.5.0 → 1.5.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/example.js +22 -13
- package/dist/index.js +119 -94
- package/dist/index.test.js +25 -16
- package/dist/mock.d.ts +9 -9
- package/package.json +8 -8
- package/src/example.ts +5 -5
- package/src/index.ts +7 -6
- package/tsconfig.json +2 -5
package/dist/example.js
CHANGED
|
@@ -1,24 +1,33 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
2
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
12
|
const index_1 = require("./index");
|
|
4
|
-
// process.env.RABBITMQ_SERVICE_HOST=
|
|
5
|
-
const init =
|
|
13
|
+
// process.env.RABBITMQ_SERVICE_HOST = '34.76.123.137:5672';
|
|
14
|
+
const init = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
6
15
|
const rabbit = new index_1.default();
|
|
7
16
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
8
|
-
|
|
9
|
-
console.log('msg2', msg.content);
|
|
10
|
-
|
|
17
|
+
yield rabbit.consumeFromExchange('test-q2', 'test-e', (msg, ack) => {
|
|
18
|
+
console.log('consumeFromExchange: msg2', msg.content);
|
|
19
|
+
ack(msg);
|
|
11
20
|
});
|
|
12
|
-
|
|
13
|
-
console.log('msg', msg.content);
|
|
21
|
+
yield rabbit.consumeFromExchange('test-q', 'test-e', (msg, ack) => {
|
|
22
|
+
console.log('consumeFromExchange: msg', msg.content);
|
|
14
23
|
ack(msg);
|
|
15
24
|
});
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
console.log('msg-q', msg.content);
|
|
25
|
+
yield rabbit.publish('test-e', 'hey');
|
|
26
|
+
yield rabbit.publish('test-e', 'hey1');
|
|
27
|
+
yield rabbit.consume('test-q-2', (msg, ack) => {
|
|
28
|
+
console.log('consume: msg-q', msg.content);
|
|
20
29
|
ack(msg);
|
|
21
30
|
});
|
|
22
|
-
|
|
23
|
-
};
|
|
31
|
+
yield rabbit.sendToQueue('test-q-2', 'hey-q');
|
|
32
|
+
});
|
|
24
33
|
init();
|
package/dist/index.js
CHANGED
|
@@ -1,36 +1,46 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
2
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
12
|
// eslint-disable-next-line max-classes-per-file
|
|
4
13
|
const bluebird = require("bluebird");
|
|
5
14
|
const amqp_connection_manager_1 = require("amqp-connection-manager");
|
|
6
15
|
const events_1 = require("events");
|
|
7
16
|
const rabbitError_1 = require("./rabbitError");
|
|
17
|
+
const assertExchangeFanout = (c, exchangeName) => c.assertExchange(exchangeName, 'fanout');
|
|
8
18
|
const connectionCreatedConst = 'connectionCreated';
|
|
9
19
|
class RabbitMq {
|
|
10
20
|
constructor() {
|
|
11
21
|
this.PUBLISH_TIMEOUT = Number(process.env.RABBITMQ_PUBLISH_TIMEOUT) || 60000;
|
|
12
22
|
this.PUBLISH_ERROR_MSG = `rabbit: publish timeout(${this.PUBLISH_TIMEOUT}ms) has pass, exchange: `;
|
|
13
23
|
this.DISCONNECT_MSG = 'rabbit: connection disconnect';
|
|
14
|
-
this.ack =
|
|
24
|
+
this.ack = (msg) => __awaiter(this, void 0, void 0, function* () {
|
|
15
25
|
if (this.channel) {
|
|
16
|
-
|
|
26
|
+
yield this.channel.ack(msg);
|
|
17
27
|
}
|
|
18
|
-
};
|
|
19
|
-
this.nack = (queue) =>
|
|
28
|
+
});
|
|
29
|
+
this.nack = (queue) => (msg, retries) => __awaiter(this, void 0, void 0, function* () {
|
|
20
30
|
if (this.channel) {
|
|
21
31
|
if (!msg.content['x-retry-count'] || msg.content['x-retry-count'] <= retries) {
|
|
22
32
|
// eslint-disable-next-line no-param-reassign
|
|
23
33
|
msg.content['x-retry-count'] = msg.content['x-retry-count'] ? msg.content['x-retry-count'] + 1 : 1;
|
|
24
|
-
|
|
34
|
+
yield this.sendToQueue(queue, msg.content);
|
|
25
35
|
}
|
|
26
36
|
else {
|
|
27
37
|
const deadQueue = `${queue}-dead`;
|
|
28
|
-
|
|
29
|
-
|
|
38
|
+
yield this.assertQueue(deadQueue);
|
|
39
|
+
yield this.sendToQueue(deadQueue, msg.content);
|
|
30
40
|
}
|
|
31
|
-
|
|
41
|
+
yield this.channel.ack(msg);
|
|
32
42
|
}
|
|
33
|
-
};
|
|
43
|
+
});
|
|
34
44
|
this.em = new events_1.EventEmitter();
|
|
35
45
|
this.channel = null;
|
|
36
46
|
this.connection = null;
|
|
@@ -51,108 +61,123 @@ class RabbitMq {
|
|
|
51
61
|
throw new rabbitError_1.default(`error while using ${type} with no name`);
|
|
52
62
|
}
|
|
53
63
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
this.
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
64
|
+
getConnection() {
|
|
65
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
66
|
+
// eslint-disable-next-line no-async-promise-executor
|
|
67
|
+
return new Promise((resolve) => __awaiter(this, void 0, void 0, function* () {
|
|
68
|
+
if (this.creatingConnection) {
|
|
69
|
+
this.em.on(connectionCreatedConst, resolve);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
if (this.connection !== null) {
|
|
73
|
+
// eslint-disable-next-line consistent-return
|
|
74
|
+
return this.connection;
|
|
75
|
+
}
|
|
76
|
+
this.creatingConnection = true;
|
|
77
|
+
const host = process.env.RABBITMQ_SERVICE_HOST || '';
|
|
78
|
+
const connection = yield amqp_connection_manager_1.connect([`amqp://${host}`]);
|
|
79
|
+
this.connection = connection;
|
|
80
|
+
this.connection.on('disconnect', ({ err }) => console.error(`${this.DISCONNECT_MSG}${err && ` - ${err}`}`));
|
|
81
|
+
this.creatingConnection = false;
|
|
82
|
+
this.em.emit(connectionCreatedConst, connection);
|
|
83
|
+
resolve(connection);
|
|
84
|
+
}));
|
|
73
85
|
});
|
|
74
86
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
const connection = await this.getConnection();
|
|
82
|
-
try {
|
|
83
|
-
if (this.channel === null) {
|
|
84
|
-
this.channel = connection.createChannel({});
|
|
87
|
+
assertChannel() {
|
|
88
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
89
|
+
// eslint-disable-next-line no-async-promise-executor,consistent-return
|
|
90
|
+
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
|
|
91
|
+
if (this.channel) {
|
|
92
|
+
return resolve(this.channel);
|
|
85
93
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
94
|
+
const connection = yield this.getConnection();
|
|
95
|
+
try {
|
|
96
|
+
if (this.channel === null) {
|
|
97
|
+
this.channel = connection.createChannel({});
|
|
98
|
+
}
|
|
99
|
+
resolve(this.channel);
|
|
100
|
+
}
|
|
101
|
+
catch (e) {
|
|
102
|
+
reject(e);
|
|
103
|
+
}
|
|
104
|
+
}));
|
|
91
105
|
});
|
|
92
106
|
}
|
|
93
107
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
108
|
+
assertExchange(exchangeName, options) {
|
|
109
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
110
|
+
const channel = yield this.assertChannel();
|
|
111
|
+
if (this.exchanges[exchangeName]) {
|
|
112
|
+
return this.exchanges[exchangeName];
|
|
113
|
+
}
|
|
114
|
+
return channel.addSetup((c) => __awaiter(this, void 0, void 0, function* () {
|
|
115
|
+
const exchange = yield assertExchangeFanout(c, exchangeName);
|
|
116
|
+
this.exchanges[exchangeName] = exchange;
|
|
117
|
+
return exchange;
|
|
118
|
+
}));
|
|
103
119
|
});
|
|
104
120
|
}
|
|
105
121
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
122
|
+
assertQueue(queue, options) {
|
|
123
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
124
|
+
RabbitMq.validateName('queue', queue);
|
|
125
|
+
const channel = yield this.assertChannel();
|
|
126
|
+
return channel.addSetup((c) => c.assertQueue(queue));
|
|
127
|
+
});
|
|
110
128
|
}
|
|
111
129
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
130
|
+
consume(queue, callback, options) {
|
|
131
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
132
|
+
RabbitMq.validateName('queue', queue);
|
|
133
|
+
const { limit } = (options && options.limit) ? options : { limit: 1 };
|
|
134
|
+
const channel = yield this.assertChannel();
|
|
135
|
+
return channel.addSetup((c) => __awaiter(this, void 0, void 0, function* () {
|
|
136
|
+
yield c.assertQueue(queue);
|
|
137
|
+
yield c.prefetch(limit, true);
|
|
138
|
+
return Promise.all([
|
|
139
|
+
c.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue))),
|
|
140
|
+
]);
|
|
141
|
+
}));
|
|
122
142
|
});
|
|
123
143
|
}
|
|
124
144
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
145
|
+
consumeFromExchange(queue, exchange, callback, options) {
|
|
146
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
147
|
+
RabbitMq.validateName('queue', queue);
|
|
148
|
+
RabbitMq.validateName('exchange', exchange);
|
|
149
|
+
const { limit } = (options && options.limit) ? options : { limit: 1 };
|
|
150
|
+
const channel = yield this.assertChannel();
|
|
151
|
+
return channel.addSetup((c) => __awaiter(this, void 0, void 0, function* () {
|
|
152
|
+
const assertExchange = yield assertExchangeFanout(c, exchange);
|
|
153
|
+
yield c.assertQueue(queue);
|
|
154
|
+
this.exchanges[exchange] = assertExchange;
|
|
155
|
+
yield c.prefetch(limit, true);
|
|
156
|
+
return Promise.all([
|
|
157
|
+
c.bindQueue(queue, exchange, ''),
|
|
158
|
+
c.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue))),
|
|
159
|
+
]);
|
|
160
|
+
}));
|
|
140
161
|
});
|
|
141
162
|
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
163
|
+
publish(exchange, content) {
|
|
164
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
165
|
+
RabbitMq.validateName('exchange', exchange);
|
|
166
|
+
const channel = yield this.assertChannel();
|
|
167
|
+
yield this.assertExchange(exchange);
|
|
168
|
+
return new bluebird.Promise((resolve) => __awaiter(this, void 0, void 0, function* () {
|
|
169
|
+
yield channel.publish(exchange, '', Buffer.from(JSON.stringify(content)));
|
|
170
|
+
return resolve();
|
|
171
|
+
})).timeout(this.PUBLISH_TIMEOUT, `${this.PUBLISH_ERROR_MSG}${exchange}`);
|
|
172
|
+
});
|
|
150
173
|
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
174
|
+
sendToQueue(queue, content) {
|
|
175
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
176
|
+
RabbitMq.validateName('queue', queue);
|
|
177
|
+
const channel = yield this.assertChannel();
|
|
178
|
+
yield this.assertQueue(queue);
|
|
179
|
+
return channel.sendToQueue(queue, Buffer.from(JSON.stringify(content)));
|
|
180
|
+
});
|
|
156
181
|
}
|
|
157
182
|
}
|
|
158
183
|
exports.default = RabbitMq;
|
package/dist/index.test.js
CHANGED
|
@@ -1,4 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
2
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
12
|
const index_1 = require("./index");
|
|
4
13
|
// import RabbitError from './rabbitError';
|
|
@@ -6,50 +15,50 @@ jest.setTimeout(120000);
|
|
|
6
15
|
const rabbit = new index_1.default();
|
|
7
16
|
const delay = () => new Promise((resolve) => setTimeout(() => resolve(), 500));
|
|
8
17
|
describe('RabbitMQ', () => {
|
|
9
|
-
it('check exchange',
|
|
18
|
+
it('check exchange', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
10
19
|
const callback = (msg, ack) => {
|
|
11
20
|
expect(msg.content).toEqual('hey');
|
|
12
21
|
ack(msg);
|
|
13
22
|
};
|
|
14
23
|
const mockFn = jest.fn(callback);
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
24
|
+
yield rabbit.consumeFromExchange('test-q2', 'test-e', mockFn);
|
|
25
|
+
yield rabbit.publish('test-e', 'hey');
|
|
26
|
+
yield delay();
|
|
18
27
|
expect(mockFn).toBeCalled();
|
|
19
|
-
});
|
|
20
|
-
it('check queue',
|
|
28
|
+
}));
|
|
29
|
+
it('check queue', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
21
30
|
const callback = (msg, ack) => {
|
|
22
31
|
expect(msg.content).toEqual('hey-q');
|
|
23
32
|
ack(msg);
|
|
24
33
|
};
|
|
25
34
|
const mockFn = jest.fn(callback);
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
35
|
+
yield rabbit.consume('test-q-2', mockFn);
|
|
36
|
+
yield rabbit.sendToQueue('test-q-2', 'hey-q');
|
|
37
|
+
yield delay();
|
|
29
38
|
expect(mockFn).toBeCalled();
|
|
30
|
-
});
|
|
31
|
-
it('cant publish to unknown exchange',
|
|
39
|
+
}));
|
|
40
|
+
it('cant publish to unknown exchange', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
32
41
|
let err;
|
|
33
42
|
try {
|
|
34
|
-
|
|
43
|
+
yield rabbit.publish('', 'hey');
|
|
35
44
|
}
|
|
36
45
|
catch (e) {
|
|
37
46
|
err = e;
|
|
38
47
|
}
|
|
39
48
|
expect(err).toBeDefined();
|
|
40
|
-
});
|
|
41
|
-
it('cant publish due timeout',
|
|
49
|
+
}));
|
|
50
|
+
it('cant publish due timeout', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
42
51
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
43
52
|
// @ts-ignore
|
|
44
53
|
process.env.RABBITMQ_PUBLISH_TIMEOUT = 1;
|
|
45
54
|
const rabbit2 = new index_1.default();
|
|
46
55
|
let err;
|
|
47
56
|
try {
|
|
48
|
-
|
|
57
|
+
yield rabbit2.publish('test-e', 'hey');
|
|
49
58
|
}
|
|
50
59
|
catch (e) {
|
|
51
60
|
err = e;
|
|
52
61
|
}
|
|
53
62
|
expect(err).toBeDefined();
|
|
54
|
-
});
|
|
63
|
+
}));
|
|
55
64
|
});
|
package/dist/mock.d.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import 'jest';
|
|
2
2
|
declare class RabbitMq {
|
|
3
|
-
ack: jest.Mock<
|
|
4
|
-
nack: jest.Mock<
|
|
5
|
-
assertChannel: jest.Mock<
|
|
6
|
-
assertExchange: jest.Mock<
|
|
7
|
-
assertQueue: jest.Mock<
|
|
8
|
-
consume: jest.Mock<
|
|
9
|
-
consumeFromExchange: jest.Mock<
|
|
10
|
-
publish: jest.Mock<
|
|
11
|
-
sendToQueue: jest.Mock<
|
|
3
|
+
ack: jest.Mock<unknown>;
|
|
4
|
+
nack: jest.Mock<unknown>;
|
|
5
|
+
assertChannel: jest.Mock<unknown>;
|
|
6
|
+
assertExchange: jest.Mock<unknown>;
|
|
7
|
+
assertQueue: jest.Mock<unknown>;
|
|
8
|
+
consume: jest.Mock<unknown>;
|
|
9
|
+
consumeFromExchange: jest.Mock<unknown>;
|
|
10
|
+
publish: jest.Mock<unknown>;
|
|
11
|
+
sendToQueue: jest.Mock<unknown>;
|
|
12
12
|
}
|
|
13
13
|
export default RabbitMq;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@autofleet/rabbit",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -9,27 +9,27 @@
|
|
|
9
9
|
"prepublish": "tsc",
|
|
10
10
|
"linter": "./node_modules/.bin/eslint .",
|
|
11
11
|
"test": "jest --forceExit",
|
|
12
|
-
"test-local": "RABBITMQ_SERVICE_HOST=34.76.
|
|
12
|
+
"test-local": "RABBITMQ_SERVICE_HOST=34.76.123.137:5672 jest --forceExit",
|
|
13
13
|
"coverage": "jest --coverage --forceExit --runInBand && rm -rf ./coverage",
|
|
14
14
|
"dev": "nodemon"
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
+
"@types/jest": "^22.0.0",
|
|
17
18
|
"@types/amqp-connection-manager": "^2.0.2",
|
|
18
19
|
"@types/amqplib": "^0.5.9",
|
|
19
|
-
"@types/jest": "^23.3.10",
|
|
20
20
|
"amqp-connection-manager": "^3.2.1",
|
|
21
21
|
"amqplib": "^0.6.0",
|
|
22
22
|
"bluebird": "^3.7.2",
|
|
23
|
-
"jest": "^
|
|
24
|
-
"ts-jest": "^26.4.4"
|
|
23
|
+
"jest": "^22.4.4"
|
|
25
24
|
},
|
|
26
25
|
"devDependencies": {
|
|
27
|
-
"ts-node": "^7.0.1",
|
|
28
|
-
"typescript": "^3.2.1",
|
|
29
26
|
"@typescript-eslint/eslint-plugin": "^4.8.1",
|
|
30
27
|
"eslint": "^7.13.0",
|
|
31
28
|
"eslint-config-airbnb-typescript": "^12.0.0",
|
|
32
|
-
"eslint-plugin-import": "^2.22.1"
|
|
29
|
+
"eslint-plugin-import": "^2.22.1",
|
|
30
|
+
"ts-jest": "^25.4.0",
|
|
31
|
+
"ts-node": "^8.6.2",
|
|
32
|
+
"typescript": "^3.8.3"
|
|
33
33
|
},
|
|
34
34
|
"author": "",
|
|
35
35
|
"license": "ISC"
|
package/src/example.ts
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
1
|
import RabbitMq from './index';
|
|
2
2
|
|
|
3
|
-
// process.env.RABBITMQ_SERVICE_HOST=
|
|
3
|
+
// process.env.RABBITMQ_SERVICE_HOST = '34.76.123.137:5672';
|
|
4
4
|
|
|
5
5
|
const init = async () => {
|
|
6
6
|
const rabbit = new RabbitMq();
|
|
7
7
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
8
8
|
await rabbit.consumeFromExchange('test-q2', 'test-e', (msg, ack) => {
|
|
9
|
-
console.log('msg2', msg.content);
|
|
10
|
-
|
|
9
|
+
console.log('consumeFromExchange: msg2', msg.content);
|
|
10
|
+
ack(msg);
|
|
11
11
|
});
|
|
12
12
|
|
|
13
13
|
await rabbit.consumeFromExchange('test-q', 'test-e', (msg, ack) => {
|
|
14
|
-
console.log('msg', msg.content);
|
|
14
|
+
console.log('consumeFromExchange: msg', msg.content);
|
|
15
15
|
ack(msg);
|
|
16
16
|
});
|
|
17
17
|
await rabbit.publish('test-e', 'hey');
|
|
18
18
|
await rabbit.publish('test-e', 'hey1');
|
|
19
19
|
|
|
20
20
|
await rabbit.consume('test-q-2', (msg, ack) => {
|
|
21
|
-
console.log('msg-q', msg.content);
|
|
21
|
+
console.log('consume: msg-q', msg.content);
|
|
22
22
|
ack(msg);
|
|
23
23
|
});
|
|
24
24
|
await rabbit.sendToQueue('test-q-2', 'hey-q');
|
package/src/index.ts
CHANGED
|
@@ -9,6 +9,8 @@ interface ExchangesCache {
|
|
|
9
9
|
[key: string]: any
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
+
const assertExchangeFanout = (c: any, exchangeName: string) => c.assertExchange(exchangeName, 'fanout');
|
|
13
|
+
|
|
12
14
|
const connectionCreatedConst = 'connectionCreated';
|
|
13
15
|
class RabbitMq {
|
|
14
16
|
static parseMsg(msg: any) {
|
|
@@ -126,7 +128,7 @@ class RabbitMq {
|
|
|
126
128
|
return this.exchanges[exchangeName];
|
|
127
129
|
}
|
|
128
130
|
return channel.addSetup(async (c: ConfirmChannel) => {
|
|
129
|
-
const exchange = await c
|
|
131
|
+
const exchange = await assertExchangeFanout(c, exchangeName);
|
|
130
132
|
this.exchanges[exchangeName] = exchange;
|
|
131
133
|
return exchange;
|
|
132
134
|
});
|
|
@@ -144,8 +146,8 @@ class RabbitMq {
|
|
|
144
146
|
RabbitMq.validateName('queue', queue);
|
|
145
147
|
const { limit } = (options && options.limit) ? options : { limit: 1 };
|
|
146
148
|
const channel: ChannelWrapper = await this.assertChannel();
|
|
147
|
-
await this.assertQueue(queue);
|
|
148
149
|
return channel.addSetup(async (c: ConfirmChannel) => {
|
|
150
|
+
await c.assertQueue(queue);
|
|
149
151
|
await c.prefetch(limit, true);
|
|
150
152
|
return Promise.all([
|
|
151
153
|
c.consume(queue, (msg: any) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue))),
|
|
@@ -159,12 +161,11 @@ class RabbitMq {
|
|
|
159
161
|
RabbitMq.validateName('exchange', exchange);
|
|
160
162
|
const { limit } = (options && options.limit) ? options : { limit: 1 };
|
|
161
163
|
const channel: ChannelWrapper = await this.assertChannel();
|
|
162
|
-
await Promise.all([
|
|
163
|
-
this.assertExchange(exchange),
|
|
164
|
-
this.assertQueue(queue),
|
|
165
|
-
]);
|
|
166
164
|
|
|
167
165
|
return channel.addSetup(async (c: ConfirmChannel) => {
|
|
166
|
+
const assertExchange = await assertExchangeFanout(c, exchange);
|
|
167
|
+
await c.assertQueue(queue);
|
|
168
|
+
this.exchanges[exchange] = assertExchange;
|
|
168
169
|
await c.prefetch(limit, true);
|
|
169
170
|
return Promise.all([
|
|
170
171
|
c.bindQueue(queue, exchange, ''),
|