slavery-js
Version:
A simple clustering app that allows you to scale an application on multiple thread, containers or machines
1 lines • 2.49 kB
Source Map (JSON)
{"version":3,"sources":["../../src/utils/isServerActive.ts"],"sourcesContent":["import { log } from './index.js';\nimport { Connection } from '../network/index.js';\n\n/**\n * Checks if a Socket.IO server is running at the specified host and port.\n * @param host - The hostname or IP address of the server.\n * @param port - The port number on which the server is expected to be listening.\n * @param timeout - The maximum time to wait for a response from the server, in milliseconds.\n * @returns A promise that resolves to true if the server is running, otherwise false.\n */\nasync function isServerActive({ name, host, port, timeout }: { \n name?: string, host: string, port: number, timeout?: number\n}): Promise<boolean> {\n return new Promise((resolve) => {\n const connection = new Connection({\n host,\n port,\n id: 'connection_test' + Math.random(),\n timeout: 10000, // Increased timeout (e.g. 10 seconds)\n onConnect: (connection: Connection) => {\n resolve(true);\n connection.close();\n }\n\n });\n connection.on('connect_error', () => {\n log(`Connection error to ${name} at ${host}:${port}`);\n });\n\n connection.on('connect_timeout', () => {\n log(`Connection timeout to ${name} at ${host}:${port}`);\n resolve(false);\n connection.close();\n });\n connection.connected();\n // Optional: set a manual timeout fallback, in case events fail to fire\n setTimeout(() => {\n resolve(false);\n connection.close();\n }, 12000); // fallback timeout longer than socket timeout\n });\n}\n\n\nexport default isServerActive\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAoB;AACpB,qBAA2B;AAS3B,eAAe,eAAe,EAAE,MAAM,MAAM,MAAM,QAAQ,GAErC;AACjB,SAAO,IAAI,QAAQ,CAAC,YAAY;AAC5B,UAAM,aAAa,IAAI,0BAAW;AAAA,MAC9B;AAAA,MACA;AAAA,MACA,IAAI,oBAAoB,KAAK,OAAO;AAAA,MACpC,SAAS;AAAA;AAAA,MACT,WAAW,CAACA,gBAA2B;AACnC,gBAAQ,IAAI;AACZ,QAAAA,YAAW,MAAM;AAAA,MACrB;AAAA,IAEJ,CAAC;AACD,eAAW,GAAG,iBAAiB,MAAM;AACjC,4BAAI,uBAAuB,IAAI,OAAO,IAAI,IAAI,IAAI,EAAE;AAAA,IACxD,CAAC;AAED,eAAW,GAAG,mBAAmB,MAAM;AACnC,4BAAI,yBAAyB,IAAI,OAAO,IAAI,IAAI,IAAI,EAAE;AACtD,cAAQ,KAAK;AACb,iBAAW,MAAM;AAAA,IACrB,CAAC;AACD,eAAW,UAAU;AAErB,eAAW,MAAM;AACb,cAAQ,KAAK;AACb,iBAAW,MAAM;AAAA,IACrB,GAAG,IAAK;AAAA,EACZ,CAAC;AACL;AAGA,IAAO,yBAAQ;","names":["connection"]}