@mdf.js/openc2
Version:
MMS - API - Observability
62 lines • 2.61 kB
JavaScript
;
/**
* 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.SocketIOProducerAdapter = void 0;
const crash_1 = require("@mdf.js/crash");
const openc2_core_1 = require("@mdf.js/openc2-core");
const SocketIOAdapter_1 = require("./SocketIOAdapter");
const DEFAULT_EXTRA_DELAY_TIME_FOR_RESPONSE = 100;
class SocketIOProducerAdapter 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, 'producer', options);
/** Wrapper function for message adaptation */
this.subscriptionAdapter = (error, incomingMessage) => {
if (error) {
this.onErrorHandler(crash_1.Crash.from(error));
return;
}
else if (!incomingMessage) {
this.onErrorHandler(new crash_1.Crash(`No response was received, but we didn't receive any error either, check the consumers`, this.componentId));
return;
}
if (Array.isArray(incomingMessage)) {
for (const message of incomingMessage) {
this.emit(message.request_id, message);
}
}
else {
this.emit(incomingMessage.request_id, incomingMessage);
}
};
}
/**
* Perform the publication of the message in the underlayer transport system
* @param message - message to be published
* @returns
*/
async publish(message) {
try {
const timeout = openc2_core_1.Accessors.getDelayFromCommandMessage(message) + DEFAULT_EXTRA_DELAY_TIME_FOR_RESPONSE;
const topics = this.defineTopics(message);
for (const topic of topics) {
this.provider.client.timeout(timeout).emit(topic, message, this.subscriptionAdapter);
}
}
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.SocketIOProducerAdapter = SocketIOProducerAdapter;
//# sourceMappingURL=SocketIOProducerAdapter.js.map