UNPKG

@mdf.js/openc2-core

Version:

MMS - API Core - OpenC2

133 lines 4.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Component = void 0; const tslib_1 = require("tslib"); const Health_1 = require("@mdf.js/core/dist/Health"); const crash_1 = require("@mdf.js/crash"); const logger_1 = require("@mdf.js/logger"); const events_1 = tslib_1.__importDefault(require("events")); const uuid_1 = require("uuid"); const Router_1 = require("../Router"); const modules_1 = require("../modules"); class Component extends events_1.default { /** * Abstract OpenC2 component implementation. * @param adapter - transport adapter * @param options - configuration options */ constructor(adapter, options) { var _a, _b; super(); this.adapter = adapter; this.options = options; /** Component identification */ this.componentId = (0, uuid_1.v4)(); /** * Manage the error in the producer interface * @param error - error to be processed */ this.onErrorHandler = (error) => { const crash = crash_1.Crash.from(error); this.logger.crash(crash); if (this.listenerCount('error') > 0) { this.emit('error', crash); } }; /** * Manage the status change in the producer interface * @param status - status to be processed */ this.onStatusHandler = (status) => { if (this.listenerCount('status') > 0) { this.emit('status', status); } }; this.logger = (0, logger_1.SetContext)((_a = this.options.logger) !== null && _a !== void 0 ? _a : new logger_1.DebugLogger(`mdf:oc2:component:${this.name}`), this.constructor.name, this.componentId); this.register = (_b = this.options.registry) !== null && _b !== void 0 ? _b : new modules_1.Registry(this.options.id, this.options.maxInactivityTime, this.options.registerLimit); this._router = new Router_1.Router(this.register); this.health = new modules_1.HealthWrapper(this.options.id, [this.register, this.adapter]); this.started = false; } /** Component name */ get name() { return this.options.id; } /** Component health status */ get status() { return (0, Health_1.overallStatus)(this.health.checks); } /** * 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.health.checks; } /** Return an Express router with access to OpenC2 information */ get router() { return this._router.router; } /** Return links offered by this service */ get links() { return { openc2: { jobs: '/openc2/jobs', pendingJobs: '/openc2/pendingJobs', messages: '/openc2/messages', }, }; } /** * Connect the OpenC2 Adapter to the underlayer transport system and perform the startup of the * component */ start() { if (this.started) { return Promise.resolve(); } return new Promise((resolve, reject) => { this.adapter .start() .then(() => this.startup()) .then(() => { this.health.on('error', this.onErrorHandler); this.health.on('status', this.onStatusHandler); }) .then(() => { this.started = true; resolve(); }) .catch(reject); }); } /** * Disconnect the OpenC2 Adapter to the underlayer transport system and perform the shutdown of * the component */ stop() { if (!this.started) { return Promise.resolve(); } return new Promise((resolve, reject) => { this.health.off('error', this.onErrorHandler); this.health.off('status', this.onStatusHandler); this.adapter .stop() .then(() => this.shutdown()) .then(() => { this.started = false; this.register.clear(); resolve(); }) .catch(reject); }); } /** Close the OpenC2 component */ close() { return this.stop(); } } exports.Component = Component; //# sourceMappingURL=Component.js.map