@autofleet/rabbit 2.0.4-beta2 → 2.0.4-beta3
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/package.json +1 -1
- package/src/index.ts +1 -0
- package/dist/index.d.ts +0 -50
- package/dist/index.js +0 -278
- package/dist/mock.d.ts +0 -14
- package/dist/mock.js +0 -18
- package/dist/rabbitError.d.ts +0 -3
- package/dist/rabbitError.js +0 -9
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -44,6 +44,7 @@ const callbackWithTimeout = async (callback: any, args: any[], timeout: number,
|
|
|
44
44
|
new Promise((resolve, reject) => {
|
|
45
45
|
setTimeout(() => {
|
|
46
46
|
callbackPromise.cancel();
|
|
47
|
+
console.log('callbackPromise', callbackPromise.isCancelled());
|
|
47
48
|
reject(timeoutMessage);
|
|
48
49
|
}, timeout);
|
|
49
50
|
}),
|
package/dist/index.d.ts
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
import { Options } from 'amqplib';
|
|
3
|
-
import { AmqpConnectionManager, ChannelWrapper } from 'amqp-connection-manager';
|
|
4
|
-
import { EventEmitter } from 'events';
|
|
5
|
-
export interface IAfRabbitMq {
|
|
6
|
-
ack: any;
|
|
7
|
-
nack: any;
|
|
8
|
-
assertChannel: any;
|
|
9
|
-
assertExchange: any;
|
|
10
|
-
assertQueue: any;
|
|
11
|
-
consume: any;
|
|
12
|
-
consumeFromExchange: any;
|
|
13
|
-
publish: any;
|
|
14
|
-
sendToQueue: any;
|
|
15
|
-
}
|
|
16
|
-
interface ExchangesCache {
|
|
17
|
-
[key: string]: any;
|
|
18
|
-
}
|
|
19
|
-
export interface AfRabbitOptions {
|
|
20
|
-
reconnect: boolean;
|
|
21
|
-
}
|
|
22
|
-
declare class RabbitMq implements IAfRabbitMq {
|
|
23
|
-
static parseMsg(msg: any): any;
|
|
24
|
-
static validateName(type: string, name: string): void;
|
|
25
|
-
PUBLISH_TIMEOUT: number;
|
|
26
|
-
PUBLISH_ERROR_MSG: string;
|
|
27
|
-
DISCONNECT_MSG: string;
|
|
28
|
-
RESCONNECT_MSG: string;
|
|
29
|
-
channel: ChannelWrapper | null;
|
|
30
|
-
connection: AmqpConnectionManager | null;
|
|
31
|
-
em: EventEmitter;
|
|
32
|
-
creatingConnection: boolean;
|
|
33
|
-
exchanges: ExchangesCache;
|
|
34
|
-
options: AfRabbitOptions | undefined;
|
|
35
|
-
constructor(options?: AfRabbitOptions);
|
|
36
|
-
ack: (channel: ChannelWrapper) => (msg: any) => Promise<void>;
|
|
37
|
-
nack: (channel: ChannelWrapper, queue: string, options: any, deadQueueOptions: Options.AssertQueue) => (msg: any, { skipRetry, }?: any) => Promise<void>;
|
|
38
|
-
getConnection(): Promise<AmqpConnectionManager>;
|
|
39
|
-
getNewChannel(): Promise<ChannelWrapper>;
|
|
40
|
-
assertChannel(): Promise<ChannelWrapper>;
|
|
41
|
-
assertExchange(exchangeName: string, options?: any): Promise<any>;
|
|
42
|
-
getQueueLength(queue: string): Promise<any>;
|
|
43
|
-
bindQueue(queue: string, exchange: string): Promise<any>;
|
|
44
|
-
assertQueue(queue: string, options?: Options.AssertQueue): Promise<void>;
|
|
45
|
-
consume(queue: string, callback: (msg: any, ack: Function, nack: Function) => any, options?: any): Promise<void>;
|
|
46
|
-
consumeFromExchange(queue: string, exchange: string, callback: (msg: any, ack: Function, nack: Function) => any, options?: any): Promise<void>;
|
|
47
|
-
publish(exchange: string, content: any): Promise<unknown>;
|
|
48
|
-
sendToQueue(queue: string, content: any, options?: any): Promise<void>;
|
|
49
|
-
}
|
|
50
|
-
export default RabbitMq;
|
package/dist/index.js
DELETED
|
@@ -1,278 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
-
}) : (function(o, m, k, k2) {
|
|
6
|
-
if (k2 === undefined) k2 = k;
|
|
7
|
-
o[k2] = m[k];
|
|
8
|
-
}));
|
|
9
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
-
}) : function(o, v) {
|
|
12
|
-
o["default"] = v;
|
|
13
|
-
});
|
|
14
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
-
if (mod && mod.__esModule) return mod;
|
|
16
|
-
var result = {};
|
|
17
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
-
__setModuleDefault(result, mod);
|
|
19
|
-
return result;
|
|
20
|
-
};
|
|
21
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
22
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
23
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
24
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
25
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
26
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
27
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
28
|
-
});
|
|
29
|
-
};
|
|
30
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
31
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
32
|
-
};
|
|
33
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
34
|
-
/* eslint-disable @typescript-eslint/ban-ts-comment,@typescript-eslint/ban-types,@typescript-eslint/no-unused-vars,consistent-return,no-async-promise-executor,no-param-reassign,no-empty */
|
|
35
|
-
// eslint-disable-next-line max-classes-per-file
|
|
36
|
-
const bluebird = __importStar(require("bluebird"));
|
|
37
|
-
const amqp_connection_manager_1 = require("amqp-connection-manager");
|
|
38
|
-
const events_1 = require("events");
|
|
39
|
-
const outbreak_1 = require("@autofleet/outbreak");
|
|
40
|
-
const rabbitError_1 = __importDefault(require("./rabbitError"));
|
|
41
|
-
const DEFAULT_DEAD_TTL_TWO_DAYS = 60000 * 60 * 48;
|
|
42
|
-
const defaultOptions = {
|
|
43
|
-
limit: 1,
|
|
44
|
-
retries: 1,
|
|
45
|
-
deadMessageTtl: DEFAULT_DEAD_TTL_TWO_DAYS,
|
|
46
|
-
};
|
|
47
|
-
const assertExchangeFanout = (c, exchangeName) => c.assertExchange(exchangeName, 'fanout');
|
|
48
|
-
const callbackWithTimeout = (callback, args, timeout, timeoutMessage = 'Timeout while ack message') => __awaiter(void 0, void 0, void 0, function* () {
|
|
49
|
-
const callbackPromise = new bluebird.Promise(callback(...args));
|
|
50
|
-
Promise.race([
|
|
51
|
-
callbackPromise,
|
|
52
|
-
new Promise((resolve, reject) => {
|
|
53
|
-
setTimeout(() => {
|
|
54
|
-
callbackPromise.cancel();
|
|
55
|
-
reject(timeoutMessage);
|
|
56
|
-
}, timeout);
|
|
57
|
-
}),
|
|
58
|
-
]);
|
|
59
|
-
});
|
|
60
|
-
const connectionCreatedConst = 'connectionCreated';
|
|
61
|
-
class RabbitMq {
|
|
62
|
-
constructor(options) {
|
|
63
|
-
this.PUBLISH_TIMEOUT = Number(process.env.RABBITMQ_PUBLISH_TIMEOUT) || 60000;
|
|
64
|
-
this.PUBLISH_ERROR_MSG = `rabbit: publish timeout(${this.PUBLISH_TIMEOUT}ms) has pass, exchange: `;
|
|
65
|
-
this.DISCONNECT_MSG = 'rabbit: connection disconnect';
|
|
66
|
-
this.RESCONNECT_MSG = 'rabbit: reconnecting';
|
|
67
|
-
this.ack = (channel) => (msg) => __awaiter(this, void 0, void 0, function* () {
|
|
68
|
-
yield channel.ack(msg);
|
|
69
|
-
});
|
|
70
|
-
this.nack = (channel, queue, options, deadQueueOptions) => (msg, { skipRetry = false, } = {}) => __awaiter(this, void 0, void 0, function* () {
|
|
71
|
-
if (channel) {
|
|
72
|
-
if (!skipRetry
|
|
73
|
-
&& (!msg.content['x-retry-count']
|
|
74
|
-
|| msg.content['x-retry-count'] < options.retries)) {
|
|
75
|
-
msg.content['x-retry-count'] = msg.content['x-retry-count'] ? msg.content['x-retry-count'] + 1 : 1;
|
|
76
|
-
// msg.content.nackCount = msg.content.nackCount ? msg.content.nackCount + 1 : 1;
|
|
77
|
-
yield this.sendToQueue(queue, msg.content);
|
|
78
|
-
}
|
|
79
|
-
else {
|
|
80
|
-
const deadQueue = `${queue}-dead`;
|
|
81
|
-
yield this.assertQueue(deadQueue, deadQueueOptions);
|
|
82
|
-
yield this.sendToQueue(deadQueue, msg.content, deadQueueOptions);
|
|
83
|
-
}
|
|
84
|
-
yield channel.ack(msg);
|
|
85
|
-
}
|
|
86
|
-
});
|
|
87
|
-
this.em = new events_1.EventEmitter();
|
|
88
|
-
this.channel = null;
|
|
89
|
-
this.connection = null;
|
|
90
|
-
this.creatingConnection = false;
|
|
91
|
-
this.exchanges = {};
|
|
92
|
-
this.options = options;
|
|
93
|
-
}
|
|
94
|
-
static parseMsg(msg) {
|
|
95
|
-
let { content } = msg;
|
|
96
|
-
content = content.toString();
|
|
97
|
-
try {
|
|
98
|
-
content = JSON.parse(content);
|
|
99
|
-
}
|
|
100
|
-
catch (e) { }
|
|
101
|
-
return Object.assign(Object.assign({}, msg), { content });
|
|
102
|
-
}
|
|
103
|
-
static validateName(type, name) {
|
|
104
|
-
if (!name || name === '') {
|
|
105
|
-
throw new rabbitError_1.default(`error while using ${type} with no name`);
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
getConnection() {
|
|
109
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
110
|
-
return new Promise((resolve) => __awaiter(this, void 0, void 0, function* () {
|
|
111
|
-
if (this.creatingConnection) {
|
|
112
|
-
this.em.on(connectionCreatedConst, resolve);
|
|
113
|
-
return;
|
|
114
|
-
}
|
|
115
|
-
if (this.connection !== null) {
|
|
116
|
-
return resolve(this.connection);
|
|
117
|
-
}
|
|
118
|
-
this.creatingConnection = true;
|
|
119
|
-
const host = process.env.RABBITMQ_SERVICE_HOST || '';
|
|
120
|
-
const connection = yield amqp_connection_manager_1.connect([`amqp://${host}`]);
|
|
121
|
-
this.connection = connection;
|
|
122
|
-
this.connection.on('disconnect', ({ err }) => {
|
|
123
|
-
var _a;
|
|
124
|
-
if ((_a = this.options) === null || _a === void 0 ? void 0 : _a.reconnect) {
|
|
125
|
-
console.error(`${this.RESCONNECT_MSG}${err && ` - ${err}`}`);
|
|
126
|
-
this.connection = null;
|
|
127
|
-
}
|
|
128
|
-
else {
|
|
129
|
-
console.error(`${this.DISCONNECT_MSG}${err && ` - ${err}`}`);
|
|
130
|
-
}
|
|
131
|
-
});
|
|
132
|
-
this.creatingConnection = false;
|
|
133
|
-
this.em.emit(connectionCreatedConst, connection);
|
|
134
|
-
resolve(connection);
|
|
135
|
-
}));
|
|
136
|
-
});
|
|
137
|
-
}
|
|
138
|
-
getNewChannel() {
|
|
139
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
140
|
-
const connection = yield this.getConnection();
|
|
141
|
-
return connection.createChannel({});
|
|
142
|
-
});
|
|
143
|
-
}
|
|
144
|
-
assertChannel() {
|
|
145
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
146
|
-
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
|
|
147
|
-
if (this.channel) {
|
|
148
|
-
return resolve(this.channel);
|
|
149
|
-
}
|
|
150
|
-
const connection = yield this.getConnection();
|
|
151
|
-
try {
|
|
152
|
-
if (this.channel === null) {
|
|
153
|
-
this.channel = connection.createChannel({});
|
|
154
|
-
}
|
|
155
|
-
resolve(this.channel);
|
|
156
|
-
}
|
|
157
|
-
catch (e) {
|
|
158
|
-
reject(e);
|
|
159
|
-
}
|
|
160
|
-
}));
|
|
161
|
-
});
|
|
162
|
-
}
|
|
163
|
-
assertExchange(exchangeName, options) {
|
|
164
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
165
|
-
const channel = yield this.assertChannel();
|
|
166
|
-
if (this.exchanges[exchangeName]) {
|
|
167
|
-
return this.exchanges[exchangeName];
|
|
168
|
-
}
|
|
169
|
-
return channel.addSetup((c) => __awaiter(this, void 0, void 0, function* () {
|
|
170
|
-
const exchange = yield assertExchangeFanout(c, exchangeName);
|
|
171
|
-
this.exchanges[exchangeName] = exchange;
|
|
172
|
-
return exchange;
|
|
173
|
-
}));
|
|
174
|
-
});
|
|
175
|
-
}
|
|
176
|
-
getQueueLength(queue) {
|
|
177
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
178
|
-
RabbitMq.validateName('queue', queue);
|
|
179
|
-
const channel = yield this.assertChannel();
|
|
180
|
-
// @ts-ignore
|
|
181
|
-
return channel.checkQueue(queue);
|
|
182
|
-
});
|
|
183
|
-
}
|
|
184
|
-
bindQueue(queue, exchange) {
|
|
185
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
186
|
-
const channel = yield this.assertChannel();
|
|
187
|
-
// @ts-ignore
|
|
188
|
-
return channel.bindQueue(queue, exchange, '');
|
|
189
|
-
});
|
|
190
|
-
}
|
|
191
|
-
assertQueue(queue, options) {
|
|
192
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
193
|
-
RabbitMq.validateName('queue', queue);
|
|
194
|
-
const channel = yield this.assertChannel();
|
|
195
|
-
return channel.addSetup((c) => c.assertQueue(queue, options));
|
|
196
|
-
});
|
|
197
|
-
}
|
|
198
|
-
consume(queue, callback, options) {
|
|
199
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
200
|
-
const optionsWithDefaults = Object.assign(Object.assign({}, defaultOptions), options);
|
|
201
|
-
RabbitMq.validateName('queue', queue);
|
|
202
|
-
const { limit, deadMessageTtl } = optionsWithDefaults;
|
|
203
|
-
const channel = yield this.getNewChannel();
|
|
204
|
-
return channel.addSetup((c) => __awaiter(this, void 0, void 0, function* () {
|
|
205
|
-
yield c.assertQueue(queue);
|
|
206
|
-
yield c.prefetch(limit, true);
|
|
207
|
-
return Promise.all([
|
|
208
|
-
c.consume(queue, (msg) => __awaiter(this, void 0, void 0, function* () {
|
|
209
|
-
outbreak_1.newTrace(outbreak_1.traceTypes.RABBIT);
|
|
210
|
-
if (optionsWithDefaults.timeout) {
|
|
211
|
-
const args = [
|
|
212
|
-
RabbitMq.parseMsg(msg),
|
|
213
|
-
this.ack(channel),
|
|
214
|
-
this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl }),
|
|
215
|
-
];
|
|
216
|
-
try {
|
|
217
|
-
yield callbackWithTimeout(callback, args, optionsWithDefaults.timeout, optionsWithDefaults.timeoutMessage);
|
|
218
|
-
}
|
|
219
|
-
catch (e) {
|
|
220
|
-
this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl });
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
else {
|
|
224
|
-
callback(RabbitMq.parseMsg(msg), this.ack(channel), this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl }));
|
|
225
|
-
}
|
|
226
|
-
})),
|
|
227
|
-
]);
|
|
228
|
-
}));
|
|
229
|
-
});
|
|
230
|
-
}
|
|
231
|
-
consumeFromExchange(queue, exchange, callback, options) {
|
|
232
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
233
|
-
const optionsWithDefaults = Object.assign(Object.assign({}, defaultOptions), options);
|
|
234
|
-
RabbitMq.validateName('exchange', exchange);
|
|
235
|
-
RabbitMq.validateName('queue', queue);
|
|
236
|
-
const { limit, deadMessageTtl } = optionsWithDefaults;
|
|
237
|
-
const channel = yield this.getNewChannel();
|
|
238
|
-
return channel.addSetup((c) => __awaiter(this, void 0, void 0, function* () {
|
|
239
|
-
const assertExchange = yield assertExchangeFanout(c, exchange);
|
|
240
|
-
yield c.assertQueue(queue);
|
|
241
|
-
this.exchanges[exchange] = assertExchange;
|
|
242
|
-
yield c.prefetch(limit, true);
|
|
243
|
-
return Promise.all([
|
|
244
|
-
c.bindQueue(queue, exchange, ''),
|
|
245
|
-
c.consume(queue, (msg) => {
|
|
246
|
-
outbreak_1.newTrace(outbreak_1.traceTypes.RABBIT);
|
|
247
|
-
callback(RabbitMq.parseMsg(msg), this.ack(channel), this.nack(channel, queue, optionsWithDefaults, { messageTtl: deadMessageTtl }));
|
|
248
|
-
}),
|
|
249
|
-
]);
|
|
250
|
-
}));
|
|
251
|
-
});
|
|
252
|
-
}
|
|
253
|
-
publish(exchange, content) {
|
|
254
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
255
|
-
RabbitMq.validateName('exchange', exchange);
|
|
256
|
-
const channel = yield this.assertChannel();
|
|
257
|
-
yield this.assertExchange(exchange);
|
|
258
|
-
return new bluebird.Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
|
|
259
|
-
try {
|
|
260
|
-
yield channel.publish(exchange, '', Buffer.from(JSON.stringify(content)));
|
|
261
|
-
return resolve();
|
|
262
|
-
}
|
|
263
|
-
catch (e) {
|
|
264
|
-
reject(e);
|
|
265
|
-
}
|
|
266
|
-
})).timeout(this.PUBLISH_TIMEOUT, `${this.PUBLISH_ERROR_MSG}${exchange}`);
|
|
267
|
-
});
|
|
268
|
-
}
|
|
269
|
-
sendToQueue(queue, content, options) {
|
|
270
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
271
|
-
RabbitMq.validateName('queue', queue);
|
|
272
|
-
const channel = yield this.assertChannel();
|
|
273
|
-
yield this.assertQueue(queue, options);
|
|
274
|
-
return channel.sendToQueue(queue, Buffer.from(JSON.stringify(content)));
|
|
275
|
-
});
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
exports.default = RabbitMq;
|
package/dist/mock.d.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import 'jest';
|
|
2
|
-
import { IAfRabbitMq } from './index';
|
|
3
|
-
declare class RabbitMq implements IAfRabbitMq {
|
|
4
|
-
ack: any;
|
|
5
|
-
nack: any;
|
|
6
|
-
assertChannel: any;
|
|
7
|
-
assertExchange: any;
|
|
8
|
-
assertQueue: any;
|
|
9
|
-
consume: any;
|
|
10
|
-
consumeFromExchange: any;
|
|
11
|
-
publish: any;
|
|
12
|
-
sendToQueue: any;
|
|
13
|
-
}
|
|
14
|
-
export default RabbitMq;
|
package/dist/mock.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
// eslint-disable-next-line import/no-extraneous-dependencies
|
|
4
|
-
require("jest");
|
|
5
|
-
class RabbitMq {
|
|
6
|
-
constructor() {
|
|
7
|
-
this.ack = jest.fn();
|
|
8
|
-
this.nack = jest.fn();
|
|
9
|
-
this.assertChannel = jest.fn();
|
|
10
|
-
this.assertExchange = jest.fn();
|
|
11
|
-
this.assertQueue = jest.fn();
|
|
12
|
-
this.consume = jest.fn();
|
|
13
|
-
this.consumeFromExchange = jest.fn();
|
|
14
|
-
this.publish = jest.fn();
|
|
15
|
-
this.sendToQueue = jest.fn();
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
exports.default = RabbitMq;
|
package/dist/rabbitError.d.ts
DELETED