UNPKG

@hotmeshio/hotmesh

Version:

Permanent-Memory Workflows & AI Agents

105 lines (104 loc) 4.09 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.NatsSubService = void 0; const key_1 = require("../../../../modules/key"); const index_1 = require("../../index"); class NatsSubService extends index_1.SubService { constructor(eventClient, storeClient) { super(eventClient, storeClient); this.subscriptions = new Map(); } async init(namespace = key_1.HMNS, appId, engineId, logger) { this.namespace = namespace; this.logger = logger; this.appId = appId; this.engineId = engineId; } transact() { // Return an empty object or throw an error if not supported. return {}; } mintKey(type, params) { if (!this.namespace) throw new Error('namespace not set'); return key_1.KeyService.mintKey(this.namespace, type, params) .replace(/:/g, '.') .replace(/\.$/g, '.X'); } async subscribe(keyType, callback, appId, topic) { const subject = this.mintKey(keyType, { appId, engineId: topic }); const subscription = this.eventClient.subscribe(subject); this.subscriptions.set(subject, subscription); this.logger.debug(`nats-subscribe ${subject}`); (async () => { for await (const msg of subscription) { try { const payload = JSON.parse(msg.data.toString()); callback(subject, payload); } catch (e) { this.logger.error(`nats-subscribe-message-parse-error: ${msg.data.toString()}`, e); } } })().catch((err) => { this.logger.error(`nats-subscribe-error ${subject}`, err); }); } async unsubscribe(keyType, appId, engineId) { const subject = this.mintKey(keyType, { appId, engineId }); const subscription = this.subscriptions.get(subject); if (subscription) { subscription.unsubscribe(); this.subscriptions.delete(subject); this.logger.debug(`nats-unsubscribe ${subject}`); } else { this.logger.warn(`nats-unsubscribe-error ${subject}`); } } async psubscribe(keyType, callback, appId, topic) { const subject = this.mintKey(keyType, { appId, engineId: topic }); const subscription = this.eventClient.subscribe(subject); this.subscriptions.set(subject, subscription); this.logger.debug(`nats-psubscribe ${subject}`); (async () => { for await (const msg of subscription) { try { const payload = JSON.parse(msg.data.toString()); callback(msg.subject, payload); } catch (e) { this.logger.error(`nats-parse-psubscription-message-error ${msg.data.toString()}`, e); } } })().catch((err) => { this.logger.error(`nats-pattern-psubscription-error ${subject}`, err); }); } async punsubscribe(keyType, appId, topic) { const subject = this.mintKey(keyType, { appId, engineId: topic }); const subscription = this.subscriptions.get(subject); if (subscription) { subscription.unsubscribe(); this.subscriptions.delete(subject); this.logger.debug(`nats-punsubscribe ${subject}`); } else { this.logger.warn(`nats-punsubscribe-error ${subject}`); } } async publish(keyType, message, appId, topic) { const subject = this.mintKey(keyType, { appId, engineId: topic }); try { // Use the storeClient for publishing if necessary this.storeClient.publish(subject, JSON.stringify(message)); this.logger.debug(`nats-publish ${subject}`); return true; } catch (err) { this.logger.error(`nats-publish-error ${subject}`, err); return false; } } } exports.NatsSubService = NatsSubService;