@golemio/cli
Version:
Collection of executables intended for use with Golemio services and modules
116 lines • 4.63 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.printCommandHelpReceive = exports.printCommandHelpSend = exports.printCommandHelp = exports.receiveData = exports.sendData = void 0;
const amqplib_1 = __importDefault(require("amqplib"));
const fs_1 = __importDefault(require("fs"));
const gluegun_1 = require("gluegun");
const sendData = async ({ exchangeName, queueKey, messageOrPathToFile }) => {
var _a;
let drained = false;
const conn = await amqplib_1.default.connect((_a = process.env.RABBIT_CONN) !== null && _a !== void 0 ? _a : " ");
let channel = await conn.createChannel();
channel.on("drain", () => {
drained = true;
});
gluegun_1.print.info("Connected to Queue!");
conn.on("close", () => {
gluegun_1.print.info("Queue disconnected");
});
// from file if exists
let buffer;
try {
const stats = fs_1.default.statSync(messageOrPathToFile);
if (!stats.isDirectory()) {
buffer = fs_1.default.readFileSync(messageOrPathToFile);
}
else {
throw new Error("Is dir");
}
}
catch (err) {
gluegun_1.print.warning("File not found. Input sent as string.");
buffer = Buffer.from(messageOrPathToFile);
}
// send message
let ok = channel.publish(exchangeName, queueKey, buffer, {
persistent: true,
});
if (!ok) {
while (!drained) {
await new Promise((resolve) => setTimeout(() => resolve(), 10));
}
}
gluegun_1.print.highlight(`Message sent! Key: "${queueKey}", messageOrFile: "${messageOrPathToFile}"`);
await new Promise((resolve) => setTimeout(() => {
conn.close();
resolve();
}, 500));
};
exports.sendData = sendData;
const receiveData = async ({ queueName, dequeue }) => {
var _a;
const conn = await amqplib_1.default.connect((_a = process.env.RABBIT_CONN) !== null && _a !== void 0 ? _a : " ");
let channel = await conn.createChannel();
gluegun_1.print.info("Connected to Queue!");
conn.on("close", () => {
gluegun_1.print.info("Queue disconnected");
});
const opts = { noAck: false };
if (dequeue) {
opts.noAck = true;
}
const msg = await channel.get(queueName, opts);
if (msg) {
gluegun_1.print.highlight(`Headers:\n ${msg.properties ? JSON.stringify(msg.properties.headers) : "undefined"}`);
gluegun_1.print.highlight(`Content:\n ${msg.content.toString()}`);
if (dequeue) {
gluegun_1.print.success("Message was dequeued.");
}
}
else {
gluegun_1.print.warning("Queue is empty.");
}
await new Promise((resolve) => setTimeout(() => {
conn.close();
resolve();
}, 500));
};
exports.receiveData = receiveData;
/**
* Print command help for rabbitmq
*/
const printCommandHelp = () => {
gluegun_1.print.info(gluegun_1.print.colors.blue("\ngolemio rabbitmq (rmq)"));
gluegun_1.print.info(" Commands\n");
gluegun_1.print.info(" send (s)\t\t Send message to exchange");
gluegun_1.print.info(" receive (r)\t\t Receive message from queue");
gluegun_1.print.info(" help (h)\t\t -");
gluegun_1.print.info("\n Flags\n");
gluegun_1.print.info(" --help\t\t Show help for specific command (send, receive)");
};
exports.printCommandHelp = printCommandHelp;
/**
* Print command help for rabbitmq (send command)
*/
const printCommandHelpSend = () => {
gluegun_1.print.info(gluegun_1.print.colors.blue("\ngolemio rabbitmq (rmq) send (s)"));
gluegun_1.print.info("Flags\n");
gluegun_1.print.info(" --exchange <name>\t\t\t Set exchange name");
gluegun_1.print.info(" --queueKey <key>\t\t\t Set queue key to send to");
gluegun_1.print.info(" --messageOrPathToFile <messageOrFile>\t Set message to send or path to file");
};
exports.printCommandHelpSend = printCommandHelpSend;
/**
* Print command help for rabbitmq (receive command)
*/
const printCommandHelpReceive = () => {
gluegun_1.print.info(gluegun_1.print.colors.blue("\ngolemio rabbitmq (rmq) receive (r)"));
gluegun_1.print.info("Flags\n");
gluegun_1.print.info(" --queueName <name>\t\t Set queue name to receive from");
gluegun_1.print.info(" --dequeue <true|false>\t Optional, if true, the message will be assumed by the server to be acknowledged");
};
exports.printCommandHelpReceive = printCommandHelpReceive;
//# sourceMappingURL=rabbitmq.utils.js.map