UNPKG

@glowlabs-org/events-sdk

Version:

Typed event SDK for Glow, powered by RabbitMQ and Zod.

67 lines (66 loc) 2.78 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createGlowEventEmitter = createGlowEventEmitter; const amqplib_1 = __importDefault(require("amqplib")); const utils_1 = require("./utils"); const uuid_1 = require("uuid"); const zones_1 = require("./zones"); function createGlowEventEmitter({ username, password, zoneId, exchangePrefix = "glow.zone-", host = "turntable.proxy.rlwy.net:50784", }) { let amqpConnection = null; let amqpChannel = null; // Exchanges for global and specific zone const globalExchangeName = `${exchangePrefix}0.events`; const zoneExchangeName = `${exchangePrefix}${zoneId}.events`; function buildUrl() { return (0, utils_1.buildAmqpUrl)({ username, password, host }); } async function connectIfNeeded() { if (!amqpConnection) { amqpConnection = (await amqplib_1.default.connect(buildUrl())); amqpChannel = (await amqpConnection.createChannel()); } if (amqpChannel) { await amqpChannel.assertExchange(globalExchangeName, "topic", { durable: true, }); await amqpChannel.assertExchange(zoneExchangeName, "topic", { durable: true, }); } } async function emit(args) { const { eventType, schemaVersion, payload } = args; const zoneName = zones_1.zoneMap[zoneId]; if (!zoneName) throw new Error(`Invalid zoneId: ${zoneId}`); if (zoneId === 0) throw new Error("Cannot emit events with zoneId 0. Use a specific zone emitter."); const event = { id: (0, uuid_1.v4)(), eventType, schemaVersion, zoneId, zoneName, timeStamp: Date.now(), payload, }; (0, utils_1.validateZoneNameAndId)(event.zoneId, event.zoneName); (0, utils_1.validateEventPayload)(event.eventType, schemaVersion, event); const routingKey = `${event.eventType}.${event.schemaVersion.toString()}`; await connectIfNeeded(); // Emit to both the global and the specific zone exchange amqpChannel.publish(globalExchangeName, routingKey, Buffer.from(JSON.stringify(event)), { persistent: true }); amqpChannel.publish(zoneExchangeName, routingKey, Buffer.from(JSON.stringify(event)), { persistent: true }); } async function disconnect() { if (amqpConnection) { await amqpConnection.close(); amqpConnection = null; amqpChannel = null; } } return { emit, disconnect }; }