UNPKG

@mdf.js/openc2

Version:

MMS - API - Observability

65 lines 2.54 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.SocketIOConsumerAdapter = void 0; const crash_1 = require("@mdf.js/crash"); const SocketIOAdapter_1 = require("./SocketIOAdapter"); class SocketIOConsumerAdapter extends SocketIOAdapter_1.SocketIOAdapter { /** * Create a new OpenC2 adapter for Socket.IO * @param adapterOptions - Adapter configuration options * @param options - Socket.IO client configuration options */ constructor(adapterOptions, options) { super(adapterOptions, 'consumer', options); /** Wrapper function for message adaptation */ this.subscriptionAdapter = (incomingMessage, acknowledge) => { if (this.handler) { try { const onDone = async (error, message) => { if (!error && message) { acknowledge(message); } else if (error) { this.onErrorHandler(error); } }; this.handler(incomingMessage, onDone); } 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 })); } } }; } /** * Subscribe the incoming message handler to the underlayer transport system * @param handler - handler to be used * @returns */ async subscribe(handler) { this.handler = handler; for (const channel of this.subscriptions) { this.provider.client.on(channel, this.subscriptionAdapter); } } /** * Unsubscribe the incoming message handler from the underlayer transport system * @param handler - handler to be used * @returns */ async unsubscribe(handler) { this.handler = undefined; for (const channel of this.subscriptions) { this.provider.client.off(channel, this.subscriptionAdapter); } } } exports.SocketIOConsumerAdapter = SocketIOConsumerAdapter; //# sourceMappingURL=SocketIOConsumerAdapter.js.map