UNPKG

@addapptables/microservice

Version:
113 lines 5.65 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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var RabbitMQBusAdapter_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.RabbitMQBusAdapter = void 0; const R = require("ramda"); const load_package_util_1 = require("../utils/load-package.util"); const logger_service_1 = require("../logger/services/logger.service"); const common_1 = require("@nestjs/common"); let RabbitMQBusAdapter = RabbitMQBusAdapter_1 = class RabbitMQBusAdapter { constructor(logger) { this.logger = logger; this.options = {}; this.subscribeHandle = (handle) => (message) => __awaiter(this, void 0, void 0, function* () { try { this.logger.verbose('handle message'); yield handle(JSON.parse(message.content.toString())); this.subChannel.ack(message); this.logger.verbose('ack message'); } catch (error) { this.logger.error('subscribe handle:', error); this.subChannel.nack(message); } }); this.queueDefault = R.mergeDeepRight({ durable: true, autoDelete: false, exclusive: false, }); this.subscribeDefault = R.mergeDeepRight({ noAck: false, exclusive: false, }); this.exchangeDefault = R.mergeDeepRight({ durable: true, autoDelete: false, }); this.publishDefault = R.mergeDeepRight({ persistent: true, }); logger.setContext(RabbitMQBusAdapter_1.name); this.rabbitmqPackage = (0, load_package_util_1.loadPackage)('amqplib', RabbitMQBusAdapter_1.name); } onInit() { return __awaiter(this, void 0, void 0, function* () { this.logger.verbose('init connection'); this.rabbitmq = yield this.rabbitmqPackage.connect(this.options.host); this.subChannel = yield this.rabbitmq.createChannel(); this.pubChannel = yield this.rabbitmq.createChannel(); }); } publish(data, options) { return __awaiter(this, void 0, void 0, function* () { options = options || {}; const { action, context } = data; const exchange = R.or(context, this.options.exchange); const type = R.or(this.options.type, 'topic'); yield this.pubChannel .assertExchange(exchange, type, this.exchangeDefault(options.exchange || {})) .then(() => this.pubChannel.publish(exchange, action, Buffer.from(JSON.stringify(data)), this.publishDefault(options.publish || {}))); }); } subscribe(handle, data, options) { return __awaiter(this, void 0, void 0, function* () { options = options || {}; const { action, context } = data; const exchange = R.or(context, this.options.exchange); const service = R.or(options.service, this.options.service); const queue = `${service}.${exchange}.${action}`; const type = R.or(this.options.type, 'topic'); const prefetch = 1; this.subChannel.prefetch(R.or(options.prefetch, prefetch)); yield this.subChannel .assertExchange(exchange, type, this.exchangeDefault(options.exchange || {})) .then(() => this.subChannel.assertQueue(queue, this.queueDefault(options.queue || {}))) .then(() => this.subChannel.bindQueue(queue, exchange, action)) .then(() => this.subChannel.consume(queue, this.subscribeHandle(handle), this.subscribeDefault(options.subscribe || {}))); }); } setOptions(options) { this.options = options; } close() { return __awaiter(this, void 0, void 0, function* () { this.logger.verbose('close connection'); yield this.rabbitmq.close(); }); } }; RabbitMQBusAdapter = RabbitMQBusAdapter_1 = __decorate([ (0, common_1.Injectable)({ scope: common_1.Scope.TRANSIENT }), __metadata("design:paramtypes", [logger_service_1.CraftsLogger]) ], RabbitMQBusAdapter); exports.RabbitMQBusAdapter = RabbitMQBusAdapter; //# sourceMappingURL=rabbitmq-bus.adapter.js.map