UNPKG

@diy0r/nestjs-rabbitmq

Version:
140 lines 6.73 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.RmqGlobalService = void 0; const common_1 = require("@nestjs/common"); const stream_1 = require("stream"); const rmq_connect_service_1 = require("./rmq-connect.service"); const interfaces_1 = require("./interfaces"); const constants_1 = require("./constants"); const common_2 = require("./common"); let RmqGlobalService = class RmqGlobalService { constructor(RMQOptions, rmqNestjsConnectService, rmqErrorGlobalService) { this.RMQOptions = RMQOptions; this.rmqNestjsConnectService = rmqNestjsConnectService; this.rmqErrorGlobalService = rmqErrorGlobalService; this.replyToQueue = null; this.extendedOptions = null; this.serDes = common_2.defaultSerDes; this.sendResponseEmitter = new stream_1.EventEmitter(); this.isInitialized = false; this.extendedOptions = RMQOptions.extendedOptions ?? {}; this.serDes = this.extendedOptions?.globalBroker?.serDes ?? common_2.defaultSerDes; this.logger = RMQOptions.extendedOptions?.appOptions?.logger ? RMQOptions.extendedOptions.appOptions.logger : new common_2.RQMColorLogger(this.extendedOptions?.appOptions?.logMessages); } async onModuleInit() { if (this.extendedOptions?.globalBroker?.replyTo) await this.replyQueue(); this.isInitialized = true; } get channel() { return this.rmqNestjsConnectService.getBaseChannel(); } async send(exchange, topic, message, options) { try { if (!this.replyToQueue) throw Error(constants_1.INDICATE_REPLY_QUEUE_GLOBAL); await this.initializationCheck(); const { messageTimeout, serviceName } = this.extendedOptions.globalBroker; return new Promise(async (resolve, reject) => { const correlationId = (0, common_2.getUniqId)(); const timeout = options?.timeout ?? messageTimeout ?? constants_1.DEFAULT_TIMEOUT; const timerId = setTimeout(() => reject(constants_1.TIMEOUT_ERROR), timeout); this.sendResponseEmitter.once(correlationId, (msg) => { clearTimeout(timerId); if (msg.properties?.headers?.['-x-error']) { return reject(this.rmqErrorGlobalService.errorHandler(msg)); } const content = msg.content; if (content.toString()) resolve(this.serDes.deserialize(content)); }); const confirmationFunction = (err, ok) => { if (err) { clearTimeout(timerId); reject(new common_2.RMQError(constants_1.NACKED)); } }; await this.rmqNestjsConnectService.publish({ exchange: exchange, routingKey: topic, content: this.serDes.serialize(message), options: { replyTo: this.replyToQueue.queue, appId: serviceName, correlationId, timestamp: new Date().getTime(), ...options, }, }, confirmationFunction); }); } catch (error) { this.logger.error(error); } } notify(exchange, topic, message, options) { return new Promise((resolve, reject) => { const confirmationFunction = (err, ok) => { if (err !== null) return reject(constants_1.NACKED); resolve({ status: 'ok' }); }; this.rmqNestjsConnectService.publish({ exchange, routingKey: topic, content: this.serDes.serialize(message), options: { appId: this.extendedOptions?.globalBroker?.serviceName ?? '', timestamp: new Date().getTime(), ...options, }, }, confirmationFunction); if (this.extendedOptions?.typeChannel !== interfaces_1.TypeChannel.CONFIRM_CHANNEL) { resolve({ status: 'ok' }); } }); } async sendToQueue(queue, content, options) { const status = await this.rmqNestjsConnectService.sendToQueue(queue, this.serDes.serialize(content), options); return status; } ack(...params) { return this.rmqNestjsConnectService.ack(...params); } async listenReplyQueue(message) { if (message.properties.correlationId) { this.sendResponseEmitter.emit(message.properties.correlationId, message); } } async replyQueue() { this.replyToQueue = await this.rmqNestjsConnectService.assertQueue(interfaces_1.TypeQueue.REPLY_QUEUE, this.extendedOptions.globalBroker.replyTo); await this.rmqNestjsConnectService.listenReplyQueue(this.replyToQueue.queue, this.listenReplyQueue.bind(this), this.extendedOptions.globalBroker.replyTo.consumeOptions); } async initializationCheck() { if (this.isInitialized) return; await new Promise(resolve => setTimeout(resolve, constants_1.INITIALIZATION_STEP_DELAY)); await this.initializationCheck(); } }; exports.RmqGlobalService = RmqGlobalService; exports.RmqGlobalService = RmqGlobalService = __decorate([ __param(0, (0, common_1.Inject)(constants_1.RMQ_OPTIONS)), __metadata("design:paramtypes", [Object, rmq_connect_service_1.RmqNestjsConnectService, common_2.RmqErrorGlobalService]) ], RmqGlobalService); //# sourceMappingURL=rmq.global.service.js.map