UNPKG

@mdf.js/openc2-core

Version:

MMS - API Core - OpenC2

89 lines 3.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.HealthWrapper = void 0; /** * 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. */ const core_1 = require("@mdf.js/core"); const crash_1 = require("@mdf.js/crash"); const events_1 = require("events"); const uuid_1 = require("uuid"); class HealthWrapper extends events_1.EventEmitter { /** * Regular OpenC2 consumer implementation. This class allows the management of incoming command * and the underlayer Adapter. The main task of this class is to filter incoming commands that are * not related with the instance or are not supported * @param name - Component name used as node identifier for OpenC2 * @param components - Health components to be monitored */ constructor(name, components) { super(); this.name = name; this.components = components; /** Component identification */ this.componentId = (0, uuid_1.v4)(); /** Emit the status if it's different from the last emitted status */ this.emitStatus = () => { const actualStatus = core_1.Health.overallStatus(this.checks); if (this.lastStatusEmitted !== actualStatus) { this.lastStatusEmitted = this.status; this.emit('status', this.status); } }; /** * Manage the error in the producer interface * @param error - error to be processed */ this.onErrorHandler = (error) => { const crash = crash_1.Crash.from(error); if (this.listenerCount('error') > 0) { this.emit('error', crash); } }; for (const component of this.components) { component.on('status', this.emitStatus); component.on('error', this.onErrorHandler); } } /** * Add a new component to health wrapper * @param component - component to be added to the health wrapper */ add(component) { this.components.push(component); component.on('status', this.emitStatus); component.on('error', this.onErrorHandler); this.emitStatus(); } /** * Return the status of the Consumer in a standard format * @returns _check object_ as defined in the draft standard * https://datatracker.ietf.org/doc/html/draft-inadarei-api-health-check-05 */ get checks() { return this.components.reduce((checks, component) => { return { ...checks, ...component.checks }; }, {}); } /** Overall component status */ get status() { return core_1.Health.overallStatus(this.checks); } /** Fake start method used to implement the Resource interface */ async start() { return Promise.resolve(); } /** Fake stop method used to implement the Resource interface */ async stop() { return Promise.resolve(); } /** Fake close method used to implement the Resource interface */ async close() { return Promise.resolve(); } } exports.HealthWrapper = HealthWrapper; //# sourceMappingURL=HealthWrapper.js.map