UNPKG

@mdf.js/openc2

Version:

MMS - API - Observability

53 lines 2.21 kB
"use strict"; /** * Copyright 2024 Mytra Control S.L. All rights reserved. * * Use of this source code is governed by an MIT-style license that can be found in the LICENSE file * or at https://opensource.org/licenses/MIT. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.RedisProducerAdapter = void 0; const crash_1 = require("@mdf.js/crash"); const RedisAdapter_1 = require("./RedisAdapter"); class RedisProducerAdapter extends RedisAdapter_1.RedisAdapter { /** * Create a new OpenC2 adapter for Redis * @param redisOptions - Redis configuration options * @param adapterOptions - Adapter configuration options */ constructor(adapterOptions, redisOptions) { super(adapterOptions, 'producer', redisOptions); /** Wrapper function for message adaptation */ this.subscriptionAdapter = (pattern, topic, incomingMessage) => { try { const message = JSON.parse(incomingMessage); this.emit(message.request_id, message); } catch (rawError) { const error = crash_1.Crash.from(rawError); this.onErrorHandler(new crash_1.Crash(`Error performing the adaptation of the incoming message: ${error.message}`, error.uuid, { cause: error })); } }; this.subscriber.client.on('pmessage', this.subscriptionAdapter); } /** * Perform the publication of the message in the underlayer transport system * @param message - message to be published * @returns */ async publish(message) { try { const topics = this.defineTopics(message); const parsedMessage = JSON.stringify(message); for (const topic of topics) { await this.publisher.client.publish(topic, parsedMessage); } } catch (rawError) { const error = crash_1.Crash.from(rawError); throw new crash_1.Crash(`Error performing the publication of the message: ${error.message}`, error.uuid, { cause: error }); } } } exports.RedisProducerAdapter = RedisProducerAdapter; //# sourceMappingURL=RedisProducerAdapter.js.map