@autofleet/rabbit 3.2.14 → 3.2.15
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/.nvmrc +1 -1
- package/dist/index.d.ts +2 -3
- package/dist/index.js +68 -80
- package/dist/lib/redis.d.ts +1 -1
- package/dist/lib/types.d.ts +7 -7
- package/dist/lib/utils.js +6 -3
- package/dist/logger.d.ts +1 -1
- package/dist/logger.js +5 -2
- package/package.json +10 -11
- package/src/index.ts +24 -38
- package/src/logger.ts +1 -1
- package/tsconfig.json +5 -2
package/.nvmrc
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
v20
|
package/dist/index.d.ts
CHANGED
|
@@ -3,7 +3,6 @@ import { EventEmitter } from 'events';
|
|
|
3
3
|
import { AmqpConnectionManager, ChannelWrapper, CreateChannelOpts } from 'amqp-connection-manager';
|
|
4
4
|
import { ConfirmChannel, ConsumeMessage, Options, Replies } from 'amqplib';
|
|
5
5
|
import { RedisConfig } from './lib/redis';
|
|
6
|
-
import { TRACING_HEADER, USER_TRACING_HEADER } from './lib/consts';
|
|
7
6
|
import { CallbackFunction, ConsumeMessageOrNull, ConsumeOptions, CustomMessageHeaders, QueuesCache, RedisLockType, ExchangesCache } from './lib/types';
|
|
8
7
|
export interface IAfRabbitMq {
|
|
9
8
|
ack: any;
|
|
@@ -34,12 +33,12 @@ export interface AfRabbitOptions {
|
|
|
34
33
|
dontRetryAssert?: boolean;
|
|
35
34
|
rabbitHost?: string;
|
|
36
35
|
}
|
|
37
|
-
|
|
36
|
+
type newChannelOpts = {
|
|
38
37
|
name?: string;
|
|
39
38
|
onClose?: null | ((args: any | null) => void);
|
|
40
39
|
options?: CreateChannelOpts | undefined;
|
|
41
40
|
};
|
|
42
|
-
|
|
41
|
+
type assertChannelOpts = {
|
|
43
42
|
channelName?: string;
|
|
44
43
|
force?: boolean;
|
|
45
44
|
};
|
package/dist/index.js
CHANGED
|
@@ -10,7 +10,7 @@ const moment_1 = __importDefault(require("moment"));
|
|
|
10
10
|
const redis_lock_1 = __importDefault(require("redis-lock"));
|
|
11
11
|
const amqp_connection_manager_1 = require("amqp-connection-manager");
|
|
12
12
|
const zehut_1 = require("@autofleet/zehut");
|
|
13
|
-
const
|
|
13
|
+
const node_crypto_1 = require("node:crypto");
|
|
14
14
|
const logger_1 = __importDefault(require("./logger"));
|
|
15
15
|
const rabbitError_1 = __importDefault(require("./lib/rabbitError"));
|
|
16
16
|
const redis_1 = __importDefault(require("./lib/redis"));
|
|
@@ -18,10 +18,43 @@ const utils_1 = require("./lib/utils");
|
|
|
18
18
|
const consts_1 = require("./lib/consts");
|
|
19
19
|
const types_1 = require("./lib/types");
|
|
20
20
|
// const debug = nodeDebug('af-rabbitmq')
|
|
21
|
-
const debug = logger_1.default.info;
|
|
21
|
+
const debug = logger_1.default.info.bind(logger_1.default);
|
|
22
22
|
const PUBLISH_TIMEOUT = 1000 * 10;
|
|
23
23
|
const HEARTBEAT = '60';
|
|
24
24
|
class RabbitMq {
|
|
25
|
+
static parseMsg(msg) {
|
|
26
|
+
let { content } = msg;
|
|
27
|
+
content = content.toString();
|
|
28
|
+
try {
|
|
29
|
+
content = JSON.parse(content);
|
|
30
|
+
}
|
|
31
|
+
catch (e) { }
|
|
32
|
+
return {
|
|
33
|
+
...msg,
|
|
34
|
+
content,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
static validateName(type, name) {
|
|
38
|
+
if (!name || name === '') {
|
|
39
|
+
throw new rabbitError_1.default(`error while using ${type} with no name`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
static getPublishOptions(customHeaders = {}) {
|
|
43
|
+
const trace = (0, zehut_1.getCurrentPayload)();
|
|
44
|
+
const user = trace?.context?.get(consts_1.USER_OBJECT);
|
|
45
|
+
const traceId = trace?.context?.get(consts_1.TRACING_HEADER);
|
|
46
|
+
const outbreakTrace = zehut_1.outbreak.getCurrentContext();
|
|
47
|
+
return {
|
|
48
|
+
timestamp: (0, moment_1.default)().unix(),
|
|
49
|
+
timeout: PUBLISH_TIMEOUT,
|
|
50
|
+
headers: {
|
|
51
|
+
creationTimestamp: (0, moment_1.default)().valueOf(),
|
|
52
|
+
...customHeaders,
|
|
53
|
+
[consts_1.USER_TRACING_HEADER]: user?.id,
|
|
54
|
+
[consts_1.TRACING_HEADER]: traceId || outbreakTrace?.context?.get(consts_1.TRACING_HEADER),
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
}
|
|
25
58
|
constructor(options, redisConfig) {
|
|
26
59
|
this.DISCONNECT_MSG = 'rabbit: connection disconnect';
|
|
27
60
|
this.RECONNECT_MSG = 'rabbit: connection disconnect - reconnecting';
|
|
@@ -90,9 +123,9 @@ class RabbitMq {
|
|
|
90
123
|
this.queues = {};
|
|
91
124
|
this.consumers = [];
|
|
92
125
|
this.options = options;
|
|
93
|
-
this.redisClient = redisConfig && redis_1.default(redisConfig);
|
|
126
|
+
this.redisClient = redisConfig && (0, redis_1.default)(redisConfig);
|
|
94
127
|
if (this.redisClient) {
|
|
95
|
-
this.redisLock = util_1.promisify(redis_lock_1.default(this.redisClient));
|
|
128
|
+
this.redisLock = (0, util_1.promisify)((0, redis_lock_1.default)(this.redisClient));
|
|
96
129
|
}
|
|
97
130
|
this.consumersTags = [];
|
|
98
131
|
logger_1.default.info(`rabbit: [gracefully-shutdown] adding gracefully shutdown for process.pid ${process.pid}`);
|
|
@@ -105,39 +138,6 @@ class RabbitMq {
|
|
|
105
138
|
});
|
|
106
139
|
}
|
|
107
140
|
}
|
|
108
|
-
static parseMsg(msg) {
|
|
109
|
-
let { content } = msg;
|
|
110
|
-
content = content.toString();
|
|
111
|
-
try {
|
|
112
|
-
content = JSON.parse(content);
|
|
113
|
-
}
|
|
114
|
-
catch (e) { }
|
|
115
|
-
return {
|
|
116
|
-
...msg,
|
|
117
|
-
content,
|
|
118
|
-
};
|
|
119
|
-
}
|
|
120
|
-
static validateName(type, name) {
|
|
121
|
-
if (!name || name === '') {
|
|
122
|
-
throw new rabbitError_1.default(`error while using ${type} with no name`);
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
static getPublishOptions(customHeaders = {}) {
|
|
126
|
-
const trace = zehut_1.getCurrentPayload();
|
|
127
|
-
const user = trace?.context?.get(consts_1.USER_OBJECT);
|
|
128
|
-
const traceId = trace?.context?.get(consts_1.TRACING_HEADER);
|
|
129
|
-
const outbreakTrace = zehut_1.outbreak.getCurrentContext();
|
|
130
|
-
return {
|
|
131
|
-
timestamp: moment_1.default().unix(),
|
|
132
|
-
timeout: PUBLISH_TIMEOUT,
|
|
133
|
-
headers: {
|
|
134
|
-
creationTimestamp: moment_1.default().valueOf(),
|
|
135
|
-
...customHeaders,
|
|
136
|
-
[consts_1.USER_TRACING_HEADER]: user?.id,
|
|
137
|
-
[consts_1.TRACING_HEADER]: traceId || outbreakTrace?.context?.get(consts_1.TRACING_HEADER),
|
|
138
|
-
},
|
|
139
|
-
};
|
|
140
|
-
}
|
|
141
141
|
async getConnection() {
|
|
142
142
|
return new Promise(async (resolve, reject) => {
|
|
143
143
|
if (this.blockReconnect) {
|
|
@@ -174,7 +174,7 @@ class RabbitMq {
|
|
|
174
174
|
return [`amqp://${userName}:${password}@${host}?heartbeat=${HEARTBEAT}`];
|
|
175
175
|
};
|
|
176
176
|
const defaultUrls = findServers();
|
|
177
|
-
const connection = await amqp_connection_manager_1.connect(defaultUrls, {
|
|
177
|
+
const connection = await (0, amqp_connection_manager_1.connect)(defaultUrls, {
|
|
178
178
|
findServers,
|
|
179
179
|
});
|
|
180
180
|
this.connection = connection;
|
|
@@ -215,41 +215,29 @@ class RabbitMq {
|
|
|
215
215
|
});
|
|
216
216
|
});
|
|
217
217
|
}
|
|
218
|
-
async getNewChannel({ name = utils_1.rand().toString(), onClose = null, options = {} } = {}) {
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
let isResolved = false;
|
|
232
|
-
if (channel) {
|
|
233
|
-
channel.on('error', (err) => {
|
|
234
|
-
logger_1.default.error(`rabbit: channel error ${name} error`, { err });
|
|
235
|
-
if (!isResolved) {
|
|
236
|
-
isResolved = true;
|
|
237
|
-
reject(err);
|
|
238
|
-
}
|
|
239
|
-
});
|
|
240
|
-
channel.on('close', (...args) => {
|
|
241
|
-
logger_1.default.error(`rabbit: channel ${name} closed`, { args });
|
|
242
|
-
if (onClose) {
|
|
243
|
-
onClose(args);
|
|
244
|
-
}
|
|
245
|
-
});
|
|
246
|
-
channel.once('connect', () => {
|
|
247
|
-
debug(`rabbit: channel ${name} CONNECTED`);
|
|
248
|
-
isResolved = true;
|
|
249
|
-
resolve(channel);
|
|
250
|
-
});
|
|
251
|
-
}
|
|
218
|
+
async getNewChannel({ name = (0, utils_1.rand)().toString(), onClose = null, options = {} } = {}) {
|
|
219
|
+
let connection;
|
|
220
|
+
try {
|
|
221
|
+
connection = await this.getConnection();
|
|
222
|
+
}
|
|
223
|
+
catch (e) {
|
|
224
|
+
logger_1.default.error(`rabbit: error on get connection for new channel ${name} `, { e });
|
|
225
|
+
throw e;
|
|
226
|
+
}
|
|
227
|
+
const channel = connection.createChannel({ ...options });
|
|
228
|
+
(0, events_1.once)(channel, 'close').then((args) => {
|
|
229
|
+
logger_1.default.error(`rabbit: channel ${name} closed`);
|
|
230
|
+
onClose?.(args);
|
|
252
231
|
});
|
|
232
|
+
try {
|
|
233
|
+
await (0, events_1.once)(channel, 'connect');
|
|
234
|
+
debug(`rabbit: channel ${name} CONNECTED`);
|
|
235
|
+
return channel;
|
|
236
|
+
}
|
|
237
|
+
catch (err) {
|
|
238
|
+
logger_1.default.error(`rabbit: channel error ${name} error`, { err });
|
|
239
|
+
throw err;
|
|
240
|
+
}
|
|
253
241
|
}
|
|
254
242
|
async assertChannel({ force = false } = {}) {
|
|
255
243
|
return new Promise(async (resolve, reject) => {
|
|
@@ -274,7 +262,7 @@ class RabbitMq {
|
|
|
274
262
|
if (this.exchanges[exchangeName]) {
|
|
275
263
|
return this.exchanges[exchangeName];
|
|
276
264
|
}
|
|
277
|
-
const exchange = await utils_1.assertExchangeFanout(channel, exchangeName);
|
|
265
|
+
const exchange = await (0, utils_1.assertExchangeFanout)(channel, exchangeName);
|
|
278
266
|
this.exchanges[exchangeName] = exchange;
|
|
279
267
|
return exchange;
|
|
280
268
|
}
|
|
@@ -363,7 +351,7 @@ class RabbitMq {
|
|
|
363
351
|
const optionsWithDefaults = { ...consts_1.DEFAULT_OPTIONS, ...options };
|
|
364
352
|
RabbitMq.validateName('queue', queue);
|
|
365
353
|
this.saveConsumer(queue, callback, options);
|
|
366
|
-
const uniqueId =
|
|
354
|
+
const uniqueId = (0, node_crypto_1.randomUUID)();
|
|
367
355
|
const { limit, deadMessageTtl, useConsumeWithLock, lockTimeout, auditContext, enableRabbitTrace, } = optionsWithDefaults;
|
|
368
356
|
if (useConsumeWithLock) {
|
|
369
357
|
if (!this.redisLock) {
|
|
@@ -383,7 +371,7 @@ class RabbitMq {
|
|
|
383
371
|
const userId = msg.properties.headers[consts_1.USER_TRACING_HEADER];
|
|
384
372
|
const parsedMessage = RabbitMq.parseMsg(msg);
|
|
385
373
|
const releaseLock = await this.lockRedisIfNeeded(parsedMessage, optionsWithDefaults);
|
|
386
|
-
const trace = zehut_1.newTrace(zehut_1.traceTypes.RABBIT);
|
|
374
|
+
const trace = (0, zehut_1.newTrace)(zehut_1.traceTypes.RABBIT);
|
|
387
375
|
// setting also outbreak trace as part of legacy code
|
|
388
376
|
const outbreakTrace = zehut_1.outbreak.newTrace(zehut_1.traceTypes.RABBIT);
|
|
389
377
|
// enableRabbitTrace is a flag to protect from different flows that doesn't work with permission
|
|
@@ -391,8 +379,8 @@ class RabbitMq {
|
|
|
391
379
|
if (userId && enableRabbitTrace) {
|
|
392
380
|
try {
|
|
393
381
|
await Promise.all([
|
|
394
|
-
zehut_1.createOrSetRabbitTrace(trace, userId),
|
|
395
|
-
zehut_1.createOrSetRabbitTrace(outbreakTrace, userId),
|
|
382
|
+
(0, zehut_1.createOrSetRabbitTrace)(trace, userId),
|
|
383
|
+
(0, zehut_1.createOrSetRabbitTrace)(outbreakTrace, userId),
|
|
396
384
|
]);
|
|
397
385
|
}
|
|
398
386
|
catch (e) {
|
|
@@ -454,7 +442,7 @@ class RabbitMq {
|
|
|
454
442
|
await this.saveConsumer(queue, callback, options);
|
|
455
443
|
const channel = await this.getNewChannel({ name: `consume-exchange-${exchange}-queue-${queue}` });
|
|
456
444
|
return channel.addSetup(async (c) => {
|
|
457
|
-
const assertExchange = await utils_1.assertExchangeFanout(c, exchange);
|
|
445
|
+
const assertExchange = await (0, utils_1.assertExchangeFanout)(c, exchange);
|
|
458
446
|
await c.assertQueue(queue);
|
|
459
447
|
this.exchanges[exchange] = assertExchange;
|
|
460
448
|
await c.prefetch(limit, true);
|
|
@@ -465,7 +453,7 @@ class RabbitMq {
|
|
|
465
453
|
});
|
|
466
454
|
}
|
|
467
455
|
async publish(exchange, content, customHeaders) {
|
|
468
|
-
return utils_1.wrapSetImmediate(async () => {
|
|
456
|
+
return (0, utils_1.wrapSetImmediate)(async () => {
|
|
469
457
|
RabbitMq.validateName('exchange', exchange);
|
|
470
458
|
const channel = await this.assertChannel();
|
|
471
459
|
await this.assertExchange(exchange);
|
|
@@ -490,7 +478,7 @@ class RabbitMq {
|
|
|
490
478
|
if (isBlocking) {
|
|
491
479
|
return callback();
|
|
492
480
|
}
|
|
493
|
-
return utils_1.wrapSetImmediate(callback);
|
|
481
|
+
return (0, utils_1.wrapSetImmediate)(callback);
|
|
494
482
|
}
|
|
495
483
|
async isConnected() {
|
|
496
484
|
const connection = await this.getConnection();
|
package/dist/lib/redis.d.ts
CHANGED
package/dist/lib/types.d.ts
CHANGED
|
@@ -5,11 +5,11 @@ export interface ExchangesCache {
|
|
|
5
5
|
export interface QueuesCache {
|
|
6
6
|
[key: string]: any;
|
|
7
7
|
}
|
|
8
|
-
export
|
|
8
|
+
export type CustomMessageHeaders = {
|
|
9
9
|
redisTimestampValidationKey?: string;
|
|
10
10
|
};
|
|
11
|
-
export
|
|
12
|
-
export
|
|
11
|
+
export type RedisLockType = (args0?: string, arg1?: number) => Promise<any>;
|
|
12
|
+
export type ConsumeMessageOrNull = ConsumeMessage | null;
|
|
13
13
|
export interface ConsumeOptions {
|
|
14
14
|
retries?: number;
|
|
15
15
|
deadMessageTtl?: number;
|
|
@@ -20,16 +20,16 @@ export interface ConsumeOptions {
|
|
|
20
20
|
auditContext?: any;
|
|
21
21
|
enableRabbitTrace?: boolean;
|
|
22
22
|
}
|
|
23
|
-
export
|
|
24
|
-
export
|
|
23
|
+
export type CallbackFunction = (msg: ConsumeMessage, ack: any, nack: any) => Promise<any>;
|
|
24
|
+
export type newChannelOpts = {
|
|
25
25
|
name?: string;
|
|
26
26
|
onClose?: null | ((args: any | null) => void);
|
|
27
27
|
};
|
|
28
|
-
export
|
|
28
|
+
export type assertChannelOpts = {
|
|
29
29
|
channelName?: string;
|
|
30
30
|
force?: boolean;
|
|
31
31
|
};
|
|
32
|
-
export
|
|
32
|
+
export type AfConsumer = {
|
|
33
33
|
queue: string;
|
|
34
34
|
callback: CallbackFunction;
|
|
35
35
|
options: ConsumeOptions | undefined;
|
package/dist/lib/utils.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.rand = exports.wrapSetImmediate = exports.assertExchangeFanout = void 0;
|
|
4
|
-
|
|
5
|
-
exports.
|
|
4
|
+
const assertExchangeFanout = async (c, exchangeName) => c.assertExchange(exchangeName, 'fanout');
|
|
5
|
+
exports.assertExchangeFanout = assertExchangeFanout;
|
|
6
|
+
const wrapSetImmediate = (callback) => new Promise((resolve, reject) => {
|
|
6
7
|
setImmediate(async () => {
|
|
7
8
|
try {
|
|
8
9
|
const value = await callback();
|
|
@@ -13,4 +14,6 @@ exports.wrapSetImmediate = (callback) => new Promise((resolve, reject) => {
|
|
|
13
14
|
}
|
|
14
15
|
});
|
|
15
16
|
});
|
|
16
|
-
exports.
|
|
17
|
+
exports.wrapSetImmediate = wrapSetImmediate;
|
|
18
|
+
const rand = () => Math.floor(Math.random() * 100000);
|
|
19
|
+
exports.rand = rand;
|
package/dist/logger.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
declare const logger:
|
|
1
|
+
declare const logger: import("@autofleet/logger").LoggerInstanceManager;
|
|
2
2
|
export default logger;
|
package/dist/logger.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const
|
|
4
|
-
const logger =
|
|
6
|
+
const logger_1 = __importDefault(require("@autofleet/logger"));
|
|
7
|
+
const logger = (0, logger_1.default)();
|
|
5
8
|
exports.default = logger;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@autofleet/rabbit",
|
|
3
|
-
"version": "3.2.
|
|
3
|
+
"version": "3.2.15",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -15,32 +15,31 @@
|
|
|
15
15
|
"dev": "nodemon"
|
|
16
16
|
},
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@autofleet/logger": "^1.2.8",
|
|
19
18
|
"@autofleet/zehut": "3.0.8",
|
|
20
|
-
"@types/amqplib": "0.8.2",
|
|
21
|
-
"@types/uuid": "^8.3.3",
|
|
22
19
|
"amqp-connection-manager": "4.1.9",
|
|
23
20
|
"amqplib": "0.10.3",
|
|
24
21
|
"bluebird": "^3.7.2",
|
|
25
22
|
"moment": "^2.29.1",
|
|
26
23
|
"redis": "^3.1.2",
|
|
27
|
-
"redis-lock": "^0.1.4"
|
|
28
|
-
"uuid": "^8.3.2"
|
|
24
|
+
"redis-lock": "^0.1.4"
|
|
29
25
|
},
|
|
30
26
|
"peerDependencies": {
|
|
31
|
-
"@autofleet/
|
|
27
|
+
"@autofleet/logger": "*"
|
|
32
28
|
},
|
|
33
29
|
"devDependencies": {
|
|
34
|
-
"@
|
|
30
|
+
"@autofleet/logger": "4.0.6",
|
|
31
|
+
"@types/amqplib": "0.8.2",
|
|
32
|
+
"@types/jest": "^29.5.12",
|
|
33
|
+
"@types/node": "^20.14.11",
|
|
35
34
|
"@typescript-eslint/eslint-plugin": "^4.8.1",
|
|
36
35
|
"eslint": "^7.13.0",
|
|
37
36
|
"eslint-config-airbnb-typescript": "^12.0.0",
|
|
38
37
|
"eslint-plugin-import": "^2.22.1",
|
|
39
|
-
"jest": "^
|
|
38
|
+
"jest": "^29.7.0",
|
|
40
39
|
"node-tcp-proxy": "^0.0.28",
|
|
41
|
-
"ts-jest": "^
|
|
40
|
+
"ts-jest": "^29.2.3",
|
|
42
41
|
"ts-node": "^8.6.2",
|
|
43
|
-
"typescript": "^
|
|
42
|
+
"typescript": "^4.9.5"
|
|
44
43
|
},
|
|
45
44
|
"author": "",
|
|
46
45
|
"license": "ISC"
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/* eslint-disable no-empty,consistent-return,no-async-promise-executor,@typescript-eslint/no-unused-vars */
|
|
2
2
|
|
|
3
|
-
import { EventEmitter } from 'events';
|
|
3
|
+
import { EventEmitter, once } from 'events';
|
|
4
4
|
import { promisify } from 'util';
|
|
5
5
|
import moment from 'moment';
|
|
6
6
|
import RedisLock from 'redis-lock';
|
|
@@ -13,7 +13,7 @@ import {
|
|
|
13
13
|
import {
|
|
14
14
|
getCurrentPayload, newTrace, traceTypes, createOrSetRabbitTrace, outbreak,
|
|
15
15
|
} from '@autofleet/zehut';
|
|
16
|
-
import {
|
|
16
|
+
import { randomUUID } from 'node:crypto';
|
|
17
17
|
import logger from './logger';
|
|
18
18
|
import RabbitError from './lib/rabbitError';
|
|
19
19
|
import getRedisInstance, { RedisConfig } from './lib/redis';
|
|
@@ -39,7 +39,7 @@ import {
|
|
|
39
39
|
} from './lib/types';
|
|
40
40
|
|
|
41
41
|
// const debug = nodeDebug('af-rabbitmq')
|
|
42
|
-
const debug = logger.info;
|
|
42
|
+
const debug = logger.info.bind(logger);
|
|
43
43
|
|
|
44
44
|
const PUBLISH_TIMEOUT = 1000 * 10;
|
|
45
45
|
|
|
@@ -112,7 +112,7 @@ class RabbitMq implements IAfRabbitMq {
|
|
|
112
112
|
};
|
|
113
113
|
}
|
|
114
114
|
|
|
115
|
-
static validateName(type: string, name: string) {
|
|
115
|
+
static validateName(type: string, name: string): void {
|
|
116
116
|
if (!name || name === '') {
|
|
117
117
|
throw new RabbitError(`error while using ${type} with no name`);
|
|
118
118
|
}
|
|
@@ -349,40 +349,26 @@ class RabbitMq implements IAfRabbitMq {
|
|
|
349
349
|
}
|
|
350
350
|
|
|
351
351
|
async getNewChannel({ name = rand().toString(), onClose = null, options = {} }: newChannelOpts = {}) {
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
let isResolved = false;
|
|
365
|
-
if (channel) {
|
|
366
|
-
channel.on('error', (err) => {
|
|
367
|
-
logger.error(`rabbit: channel error ${name} error`, { err });
|
|
368
|
-
if (!isResolved) {
|
|
369
|
-
isResolved = true;
|
|
370
|
-
reject(err);
|
|
371
|
-
}
|
|
372
|
-
});
|
|
373
|
-
channel.on('close', (...args) => {
|
|
374
|
-
logger.error(`rabbit: channel ${name} closed`, { args });
|
|
375
|
-
if (onClose) {
|
|
376
|
-
onClose(args);
|
|
377
|
-
}
|
|
378
|
-
});
|
|
379
|
-
channel.once('connect', () => {
|
|
380
|
-
debug(`rabbit: channel ${name} CONNECTED`);
|
|
381
|
-
isResolved = true;
|
|
382
|
-
resolve(channel);
|
|
383
|
-
});
|
|
384
|
-
}
|
|
352
|
+
let connection!: AmqpConnectionManager;
|
|
353
|
+
try {
|
|
354
|
+
connection = await this.getConnection();
|
|
355
|
+
} catch (e) {
|
|
356
|
+
logger.error(`rabbit: error on get connection for new channel ${name} `, { e });
|
|
357
|
+
throw e;
|
|
358
|
+
}
|
|
359
|
+
const channel = connection.createChannel({ ...options });
|
|
360
|
+
once(channel, 'close').then((args) => {
|
|
361
|
+
logger.error(`rabbit: channel ${name} closed`);
|
|
362
|
+
onClose?.(args);
|
|
385
363
|
});
|
|
364
|
+
try {
|
|
365
|
+
await once(channel, 'connect');
|
|
366
|
+
debug(`rabbit: channel ${name} CONNECTED`);
|
|
367
|
+
return channel;
|
|
368
|
+
} catch (err) {
|
|
369
|
+
logger.error(`rabbit: channel error ${name} error`, { err });
|
|
370
|
+
throw err;
|
|
371
|
+
}
|
|
386
372
|
}
|
|
387
373
|
|
|
388
374
|
async assertChannel({ force = false } : assertChannelOpts = {}): Promise<ChannelWrapper> {
|
|
@@ -511,7 +497,7 @@ class RabbitMq implements IAfRabbitMq {
|
|
|
511
497
|
const optionsWithDefaults = { ...DEFAULT_OPTIONS, ...options };
|
|
512
498
|
RabbitMq.validateName('queue', queue);
|
|
513
499
|
this.saveConsumer(queue, callback, options);
|
|
514
|
-
const uniqueId =
|
|
500
|
+
const uniqueId = randomUUID();
|
|
515
501
|
const {
|
|
516
502
|
limit, deadMessageTtl, useConsumeWithLock, lockTimeout, auditContext, enableRabbitTrace,
|
|
517
503
|
} = optionsWithDefaults;
|
package/src/logger.ts
CHANGED
package/tsconfig.json
CHANGED
|
@@ -7,7 +7,10 @@
|
|
|
7
7
|
"strict": true,
|
|
8
8
|
"esModuleInterop": true,
|
|
9
9
|
"experimentalDecorators": true,
|
|
10
|
-
"emitDecoratorMetadata": true
|
|
10
|
+
"emitDecoratorMetadata": true,
|
|
11
|
+
"skipLibCheck": true,
|
|
12
|
+
"types": ["@types/jest"]
|
|
11
13
|
},
|
|
12
|
-
"exclude": ["node_modules",
|
|
14
|
+
"exclude": ["node_modules"],
|
|
15
|
+
"include": ["src/**/*.ts"]
|
|
13
16
|
}
|