UNPKG

@mdf.js/socket-server-provider

Version:

MMS - Socket.io Server Port for Javascript/Typescript

139 lines 5.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Port = void 0; const tslib_1 = require("tslib"); /** * 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 http_server_provider_1 = require("@mdf.js/http-server-provider"); const utils_1 = require("@mdf.js/utils"); const admin_ui_1 = require("@socket.io/admin-ui"); const express_1 = tslib_1.__importDefault(require("express")); const fs_1 = tslib_1.__importDefault(require("fs")); const config_1 = require("../config"); const types_1 = require("./types"); /** Default Express app for the UI */ let CONFIG_SERVER_DEFAULT_APP = undefined; /** Default path for the UI */ const DEFAULT_DIST_PATH = (0, utils_1.findNodeModule)('@socket.io'); /** Default configuration for the Socket.io server */ if (DEFAULT_DIST_PATH && fs_1.default.existsSync(DEFAULT_DIST_PATH)) { CONFIG_SERVER_DEFAULT_APP = (0, express_1.default)().use('/ui', express_1.default.static(DEFAULT_DIST_PATH)); } class Port extends core_1.Layer.Provider.Port { /** * Implementation of functionalities of an Socket.io server port instance. * @param config - Port configuration options * @param logger - Port logger, to be used internally */ constructor(config, logger) { var _a; super(config, logger, config_1.CONFIG_PROVIDER_BASE_NAME); /** Callback function for `error` event in the HTTP Provider */ this.onErrorEvent = (error) => { // Stryker disable next-line all this.logger.error(`Error event: error was wrapped to error`); this.emit('error', crash_1.Crash.from(error)); }; /** Callback function for `connection` event in socket.io server */ this.onConnectionEvent = (socket) => { // Stryker disable next-line all this.logger.debug(`New connection from ${socket.id}`); this.onEvent('connection'); }; /** Callback function for `connection_error` event in engine.io */ this.onConnectionErrorEvent = (error) => { // Stryker disable next-line all this.logger.debug(`Connection error: ${error.message}`); this.emit('error', new crash_1.Crash(`Connection error: ${error.message}`, { info: { code: error.code, context: error.context }, })); }; this.httpServer = http_server_provider_1.HTTP.Factory.create({ name: this.name, logger: this.logger, config: { port: this.config.port, host: this.config.host, app: this.config.enableUI ? CONFIG_SERVER_DEFAULT_APP : undefined, }, }); if (this.config.enableUI) { this.config.transports = ['polling', 'websocket']; } this.instance = new types_1.Server(this.httpServer.client, this.config); if (this.config.enableUI) { (0, admin_ui_1.instrument)(this.instance, (_a = this.config.ui) !== null && _a !== void 0 ? _a : { auth: false }); } // Stryker disable next-line all this.logger.debug(`New instance of Socket.io server port: ${this.uuid}`, this.uuid, this.name); this.isWrapped = false; } /** Return the underlying port instance */ get client() { return this.instance; } /** Return the port state as a boolean value, true if the port is available, false in otherwise */ get state() { return this.httpServer.client.listening; } /** Initialize the port instance */ async start() { this.socketIOServerEventsWrapping(this.instance, this.httpServer); await this.httpServer.start(); } /** Stop the port instance */ async stop() { this.socketIOServerEventsUnwrapping(this.instance, this.httpServer); this.instance.disconnectSockets(); await this.httpServer.stop(); } /** Close the port instance */ async close() { await this.stop(); } /** * Auxiliar function to log and emit events * @param event - original event name * @param args - arguments to be emitted with the event */ onEvent(event, ...args) { // Stryker disable next-line all this.logger.debug(`Event: ${event} was listened`); // Stryker enable all for (const arg of args) { // Stryker disable next-line all this.logger.silly(`Event ${event} arg: ${arg}`); } } /** * Adapts the `server` instance events to standard Port events * @param instance - Server instance over which the events should be wrapped */ socketIOServerEventsWrapping(instance, server) { if (this.isWrapped) { return; } instance.engine.on('connection_error', this.onConnectionErrorEvent); instance.on('connection', this.onConnectionEvent); server.on('error', this.onErrorEvent); this.isWrapped = true; } /** * Clean all the events handlers * @param instance - Server instance over which the events should be cleaned */ socketIOServerEventsUnwrapping(instance, server) { instance.engine.off('connection_error', this.onConnectionErrorEvent); instance.off('connection', this.onConnectionEvent); server.off('error', this.onErrorEvent); this.isWrapped = false; } } exports.Port = Port; //# sourceMappingURL=Port.js.map