"use strict";var __importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.getFreePortSync=exports.getRandomPort=exports.getUsedPorts=exports.getFreePort=exports.isPortFree=void 0;const net_1=__importDefault(require("net")),child_process_1=require("child_process"),usedPortsCache=[];function isPortFree(e,t="127.0.0.1"){return new Promise(r=>{const o=new net_1.default.Socket;o.once("error",e=>{e&&"ECONNREFUSED"!==e.code?r(!1):r(!0)}),o.once("connect",()=>{o.destroy(),o.unref(),r(!1)}),o.connect({host:t,port:e})})}async function getFreePort(e=8081){let t=e;for(;;){if(await isPortFree(t))return t;t++}}exports.isPortFree=isPortFree,exports.getFreePort=getFreePort;const getUsedPorts=(e=[])=>{try{const t=new RegExp("\\s(0.0.0.0|127.0.0.1):(\\d+)\\s","g"),r=new RegExp("\\s(\\*|127.0.0.1)\\.(\\d+)\\s","g");let o=":";const s=(0,child_process_1.execSync)("netstat -an",{encoding:"utf-8"});let n=s.match(t);n||(n=s.match(r),o=".");const c=n?n.map(e=>{const t=e.split(o).slice(-1)[0];return parseInt(t.slice(0,-1),10)}):[];return[...new Set(c.concat(e))]}catch(t){return e}};exports.getUsedPorts=getUsedPorts;const getRandomPort=()=>{const e=Math.floor(6e4*Math.random());return e<1e3?(0,exports.getRandomPort)():e};exports.getRandomPort=getRandomPort;const getFreePortSync=()=>{const e=(0,exports.getUsedPorts)(usedPortsCache);for(;;){const t=(0,exports.getRandomPort)();if(!e.includes(t))return usedPortsCache.push(t),t}};exports.getFreePortSync=getFreePortSync;