@glowlabs-org/events-sdk
Version:
Typed event SDK for Glow, powered by RabbitMQ and Zod.
75 lines (74 loc) • 3.23 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createExchange = createExchange;
exports.bindQueueToExchange = bindQueueToExchange;
exports.deleteExchange = deleteExchange;
exports.deleteQueue = deleteQueue;
const amqplib_1 = __importDefault(require("amqplib"));
const utils_1 = require("./utils");
/**
* Create a RabbitMQ topic exchange (admin credentials required).
*
* @param options - Connection and exchange options
* @returns Promise<void>
*/
async function createExchange({ username, password, exchange, exchangeType = "topic", exchangeOptions = { durable: true }, host = "turntable.proxy.rlwy.net:50784", }) {
(0, utils_1.validateName)(exchange, "exchange");
const url = (0, utils_1.buildAmqpUrl)({ username, password, host });
const conn = await amqplib_1.default.connect(url);
const channel = await conn.createChannel();
await channel.assertExchange(exchange, exchangeType, exchangeOptions);
await channel.close();
await conn.close();
}
/**
* Create a RabbitMQ queue and bind it to a topic exchange (admin credentials required).
*
* @param options - Connection, queue, and exchange options
* @returns Promise<void>
*/
async function bindQueueToExchange({ username, password, exchange, queue, exchangeType = "topic", queueOptions = { durable: false }, exchangeOptions = { durable: true }, routingKey = "#", host = "turntable.proxy.rlwy.net:50784", }) {
(0, utils_1.validateName)(exchange, "exchange");
(0, utils_1.validateName)(queue, "queue");
const url = (0, utils_1.buildAmqpUrl)({ username, password, host });
const conn = await amqplib_1.default.connect(url);
const channel = await conn.createChannel();
await channel.assertExchange(exchange, exchangeType, exchangeOptions);
await channel.assertQueue(queue, queueOptions);
await channel.bindQueue(queue, exchange, routingKey);
await channel.close();
await conn.close();
}
/**
* Delete a RabbitMQ exchange (admin credentials required).
*
* @param options - Connection and exchange options
* @returns Promise<void>
*/
async function deleteExchange({ username, password, exchange, host = "turntable.proxy.rlwy.net:50784", }) {
(0, utils_1.validateName)(exchange, "exchange");
const url = (0, utils_1.buildAmqpUrl)({ username, password, host });
const conn = await amqplib_1.default.connect(url);
const channel = await conn.createChannel();
await channel.deleteExchange(exchange);
await channel.close();
await conn.close();
}
/**
* Delete a RabbitMQ queue (admin credentials required).
*
* @param options - Connection and queue options
* @returns Promise<void>
*/
async function deleteQueue({ username, password, queue, host = "turntable.proxy.rlwy.net:50784", }) {
(0, utils_1.validateName)(queue, "queue");
const url = (0, utils_1.buildAmqpUrl)({ username, password, host });
const conn = await amqplib_1.default.connect(url);
const channel = await conn.createChannel();
await channel.deleteQueue(queue);
await channel.close();
await conn.close();
}