UNPKG

fedapay-cli

Version:
92 lines (91 loc) 3.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); var amqp = require('amqplib/callback_api'); const chalk_1 = tslib_1.__importDefault(require("chalk")); /** * Class TailUtil */ class TailUtil { constructor(url, options) { this.url = url; this.options = options; } connect(filters = {}, keys = [], cb) { amqp.connect(this.url, (error0, connection) => { if (error0) { throw error0; } connection.createChannel((error1, channel) => { if (error1) { throw error1; } channel.assertQueue(this.options.queue, { nowait: this.options.nowait, durable: this.options.durable, autoDelete: this.options.auto_delete, arguments: { 'x-message-ttl': this.options.arguments['x-message-ttl'], 'x-expires': this.options.arguments['x-expires'], 'x-single-active-consumer': this.options.arguments['x-single-active-consumer'] } }, (error2) => { if (error2) { throw error2; } channel.consume(this.options.queue, (msg) => { if (msg.content) { const output = this.filterOutput(msg.content.toString(), filters, keys); if (output) { cb(output); } } }, { noAck: true }); }); }); }); } /** * Filter json output * @param {string} json * @param {any} filters * @return {string|null} */ filterOutput(json, filters, keys = []) { try { const data = JSON.parse(json); for (const filter in filters) { if (!data[filter] || data[filter] !== filters[filter]) { return null; } } return this.formatJsonOutput(data, keys); } catch (e) { return null; } } /** * Format queue messange json * @param {string} json * @return {string} */ formatJsonOutput(json, keys = []) { const date = (new Date()).toLocaleString(); let log = chalk_1.default.bold.gray(date) + ' ----> : '; for (const key in json) { if (keys.length > 0 && !keys.includes(key)) { continue; } let value = json[key]; if (typeof value !== 'string') { value = JSON.stringify(value); } log += chalk_1.default.bold.blue(`${key}: `) + `${value} `; } return log; } } exports.default = TailUtil;