UNPKG

nope-js-node

Version:

NoPE Runtime for Nodejs. For Browser-Support please use nope-browser

153 lines (152 loc) 6.25 kB
"use strict"; /** * @author Martin Karkowski * @email m.karkowski@zema.de * @create date 2021-03-22 19:03:15 * @modify date 2022-01-03 17:34:13 * @desc [description] */ Object.defineProperty(exports, "__esModule", { value: true }); exports.ioSocketServerLayer = void 0; const io = require("socket.io"); const getLogger_1 = require("../../logger/getLogger"); const index_browser_1 = require("../../logger/index.browser"); const nope_1 = require("../../types/nope"); const EventCommunicationInterface_1 = require("./EventCommunicationInterface"); /** * Mirror Layer using IO-Sockets. * * @export * @class IoSocketMirrorServer */ class ioSocketServerLayer extends EventCommunicationInterface_1.EventCommunicationInterface { /** * Creates an instance of IoSocketMirrorServer. * @author M.Karkowski * @param {number} port Port the Server is running on. * @param {ValidLoggerDefinition} [logger="info"] * @memberof IoSocketMirrorServer */ constructor(port, logger = "info", profile = false) { var _a; super( // As event Emitter, we provide the IO-Client. io({ cors: { origin: "*", methods: ["GET", "POST"], }, }), (0, getLogger_1.defineNopeLogger)(logger, "core.mirror.io-srv")); this.port = port; this._openRequests = {}; const _this = this; // Store, whether we want to profile our data or not. this._profile = profile; // Tell the Server to listen. this._emitter.listen(port); // Now, because we arent connected we set the connected flag to false, // it will only be true, if a connection with this server has been established this.connected.getter = () => { return true; }; if ((_a = _this._logger) === null || _a === void 0 ? void 0 : _a.enabledFor(index_browser_1.INFO)) { this._logger.info("Hosting Server on Port " + port.toString()); } this._emitter.on("connection", (client) => { var _a; if ((_a = _this._logger) === null || _a === void 0 ? void 0 : _a.enabledFor(index_browser_1.INFO)) { _this._logger.info("New Connection established: " + client.id); } _this._sockets.add(client); // Now Subscribe the Events and make shure we // are forwarding the data. for (const event of nope_1.Eventnames) { client.on(event, (data) => { var _a; if (_this._profile) { if (event == "rpcRequest") { _this._profileTask("add", data.taskId); } else if (event == "rpcResponse") { _this._profileTask("remove", data.taskId); } } if (event !== "statusChanged" && ((_a = _this._logger) === null || _a === void 0 ? void 0 : _a.enabledFor(index_browser_1.DEBUG))) { _this._logger.debug("forwarding", "'" + event.toString() + "'", data); } _this._forward(client, event, data); }); } // Subscribe to Loosing connection: client.on("disconnect", () => { var _a; if ((_a = _this._logger) === null || _a === void 0 ? void 0 : _a.enabledFor(index_browser_1.INFO)) { _this._logger.info("Connection of : " + client.id + " lost."); } _this._sockets.delete(client); // Force an Update of the connect-flag _this.connected.forcePublish(); }); // Force an Update of the connect-flag _this.connected.forcePublish(); }); this._sockets = new Set(); } _profileTask(mode, data) { try { if (mode == "add") { this._openRequests[data.taskId] = Date.now(); } else { if (this._openRequests[data.taskId] !== undefined) { const start = this._openRequests[data.taskId]; const end = Date.now(); const delta = Math.round((end - start) * 100) / 100; this._logger.info(`The execution of the task took ${delta} [ms]!`); delete this._openRequests[data.taskId]; } } } catch (e) { logger.error(`Failed in 'profileTask' mode=${mode}, data=${data}`); } } /** * Helper Function, to forward events to the other connected Sockets. * * @protected * @param {io.Socket} socket The socket, which initally received the data. * @param {string} event the event which was received * @param {*} data the data, that needs to be forwarded * @memberof IoSocketMirrorServer */ _forward(socket, event, data) { var _a; // Flag, used to Debug let forwarded = false; for (const socketToForward of this._sockets) { if (socket !== socketToForward) { socketToForward.emit(event, data); // If data has been sended, our flag is set to true forwarded = true; } } // Now we log the output if (event !== "statusChanged" && ((_a = this._logger) === null || _a === void 0 ? void 0 : _a.enabledFor(index_browser_1.DEBUG))) { this._logger.debug(forwarded ? "forwarded" : "didnt forward", "'" + event.toString() + "'", data); } } dispose() { // Disposes the Emitter. return new Promise((resolve, reject) => { this._emitter.removeAllListeners(); this._emitter.close((err) => { if (err) reject(err); else resolve(); }); }); } } exports.ioSocketServerLayer = ioSocketServerLayer;