{"version":3,"sources":["../../src/utils/isServerActive.ts"],"sourcesContent":["import { log } from './index.js';\nimport { Connection } from '../network/index.js';\n\nconst resolve_timout_pointer = (timeout_pointer: NodeJS.Timeout | null) => {\n    if (timeout_pointer) {\n        clearTimeout(timeout_pointer);\n        timeout_pointer = null;\n    }\n}\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    if(timeout === undefined) {\n        timeout = 5000; // Default timeout of 5 seconds\n    }\n    return new Promise((resolve) => {\n        let timeout_pointer: NodeJS.Timeout | null = null;\n        const connection = new Connection({\n            host, port, id: 'connection_test' + Math.random(),\n            options: {\n                timeout: 10000, // Increased timeout (e.g. 10 second\n                onConnect: (connection: Connection) => {\n                    // clear the timeout if the connection is successful\n                    connection.close();\n                    resolve_timout_pointer(timeout_pointer);\n                    resolve(true);\n                }\n            }\n\n        });\n        connection.on('connect_error', () => {\n            //console.log(`Connection error to ${name} at ${host}:${port}`);\n        });\n\n        connection.on('connect_timeout', () => {\n            //console.log(`Connection timeout to ${name} at ${host}:${port}`);\n            resolve(false);\n            connection.close();\n            resolve_timout_pointer(timeout_pointer);\n        });\n        connection.connected();\n        // Optional: set a manual timeout fallback, in case events fail to fire\n        timeout_pointer = setTimeout(() => {\n            console.error(`Timeout waiting for ${name} at ${host}:${port}`);\n            connection.close();\n            resolve(false);\n        }, timeout);\n    });\n}\n\n\nexport default isServerActive\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,qBAA2B;AAE3B,MAAM,yBAAyB,CAAC,oBAA2C;AACvE,MAAI,iBAAiB;AACjB,iBAAa,eAAe;AAC5B,sBAAkB;AAAA,EACtB;AACJ;AASA,eAAe,eAAe,EAAE,MAAM,MAAM,MAAM,QAAQ,GAErC;AACjB,MAAG,YAAY,QAAW;AACtB,cAAU;AAAA,EACd;AACA,SAAO,IAAI,QAAQ,CAAC,YAAY;AAC5B,QAAI,kBAAyC;AAC7C,UAAM,aAAa,IAAI,0BAAW;AAAA,MAC9B;AAAA,MAAM;AAAA,MAAM,IAAI,oBAAoB,KAAK,OAAO;AAAA,MAChD,SAAS;AAAA,QACL,SAAS;AAAA;AAAA,QACT,WAAW,CAACA,gBAA2B;AAEnC,UAAAA,YAAW,MAAM;AACjB,iCAAuB,eAAe;AACtC,kBAAQ,IAAI;AAAA,QAChB;AAAA,MACJ;AAAA,IAEJ,CAAC;AACD,eAAW,GAAG,iBAAiB,MAAM;AAAA,IAErC,CAAC;AAED,eAAW,GAAG,mBAAmB,MAAM;AAEnC,cAAQ,KAAK;AACb,iBAAW,MAAM;AACjB,6BAAuB,eAAe;AAAA,IAC1C,CAAC;AACD,eAAW,UAAU;AAErB,sBAAkB,WAAW,MAAM;AAC/B,cAAQ,MAAM,uBAAuB,IAAI,OAAO,IAAI,IAAI,IAAI,EAAE;AAC9D,iBAAW,MAAM;AACjB,cAAQ,KAAK;AAAA,IACjB,GAAG,OAAO;AAAA,EACd,CAAC;AACL;AAGA,IAAO,yBAAQ;","names":["connection"]}