UNPKG

@mdf.js/openc2-core

Version:

MMS - API Core - OpenC2

66 lines 2.11 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.Service = void 0; const tslib_1 = require("tslib"); const crash_1 = require("@mdf.js/crash"); const events_1 = tslib_1.__importDefault(require("events")); const helpers_1 = require("../helpers"); /** Service class */ class Service extends events_1.default { /** * Create an instance of service * @param model - model instance */ constructor(model) { super(); this.model = model; } /** Return array of messages used as fifo registry */ async messages() { return this.model.messages(); } /** Return array of jobs used as fifo registry */ async jobs() { return this.model.jobs(); } /** Return array of pendingJobs used as fifo registry */ async pendingJobs() { return this.model.pendingJobs(); } /** * Execute a command over the producer or consumer * @param message - message to be processed * @returns - response message */ async command(message) { return new Promise((resolve, reject) => { let timer = undefined; const onTimeout = () => { reject(new crash_1.Crash('Command timeout')); }; const done = (error, response) => { if (timeout) { clearTimeout(timer); timer = undefined; } if (error) { reject(error); } else { resolve(response); } }; const timeout = helpers_1.Accessors.getDelayFromCommandMessage(message); timer = setTimeout(onTimeout, timeout); this.emit('command', message, done); }); } } exports.Service = Service; //# sourceMappingURL=oc2.service.js.map