import{t as e}from"./subsystem-kTFXkQM5.js";import{Jr as t,qr as n}from"./auth-profiles-qXC1L8hs.js";import{S as r}from"./utils-7Gdj77QB.js";import{execFileSync as i}from"node:child_process";import{createServer as a}from"node:net";const o=e(`ports`),s={SIGTERM:`TERM`,SIGKILL:`KILL`};function c(e){return typeof e==`string`?e:e instanceof Buffer?e.toString(`utf8`):``}function l(e,t,n){let r=Error(e,{cause:n instanceof Error?n:void 0});return r.code=t,r}function u(e){if(!e||typeof e!=`object`)return;let t=e.code;if(typeof t==`string`&&t.length>0)return t;let n=e.cause;if(n&&typeof n==`object`){let e=n.code;if(typeof e==`string`&&e.length>0)return e}}function d(e){let t=u(e);if(t===`ENOENT`||t===`EACCES`||t===`EPERM`)return!0;let n=e instanceof Error?e.message:String(e);return/lsof.*(permission denied|not permitted|operation not permitted|eacces|eperm)/i.test(n)}function f(e){if(!e)return[];let t=new Set;for(let n of e.split(/\r?\n/)){let e=n.trim();if(!e)continue;let r=(e.includes(`:`)?e.slice(e.indexOf(`:`)+1):e).match(/\d+/g)??[];for(let e of r){let n=Number.parseInt(e,10);Number.isFinite(n)&&n>0&&t.add(n)}}return[...t]}function p(e,t){let n=[`-k`,`-${s[t]}`,`${e}/tcp`];try{return f(i(`fuser`,n,{encoding:`utf-8`,stdio:[`ignore`,`pipe`,`pipe`]})).map(e=>({pid:e}))}catch(e){let t=e,n=t.code,r=t.status,i=f([c(t.stdout),c(t.stderr)].filter(Boolean).join(`
`));if(r===1)return i.map(e=>({pid:e}));throw n===`ENOENT`?l(`fuser not found; required for --force when lsof is unavailable`,`ENOENT`,e):n===`EACCES`||n===`EPERM`?l(`fuser permission denied while forcing gateway port`,n,e):e instanceof Error?e:Error(String(e))}}async function m(e){try{return await n({port:e,exclusive:!0}),!1}catch(e){if(e.code===`EADDRINUSE`)return!0;throw e instanceof Error?e:Error(String(e))}}function h(e){let t=e.split(/\r?\n/).filter(Boolean),n=[],r={};for(let e of t)e.startsWith(`p`)?(r.pid&&n.push(r),r={pid:Number.parseInt(e.slice(1),10)}):e.startsWith(`c`)&&(r.command=e.slice(1));return r.pid&&n.push(r),n}function g(e){if(process.platform===`win32`){o.debug(`listPortListeners: using netstat for port ${e}`);try{let t=i(`netstat`,[`-ano`,`-p`,`TCP`],{encoding:`utf-8`}).split(/\r?\n/).filter(Boolean),n=[];for(let r of t){let t=r.trim().split(/\s+/);if(t.length>=5&&t[3]===`LISTENING`&&t[1].split(`:`).pop()===String(e)){let e=Number.parseInt(t[4],10);!Number.isNaN(e)&&e>0&&(n.some(t=>t.pid===e)||n.push({pid:e}))}}return o.debug(`listPortListeners: found ${n.length} listener(s) on port ${e}`),n}catch(e){throw o.error(`listPortListeners: netstat failed: ${String(e)}`),Error(`netstat failed: ${String(e)}`,{cause:e})}}try{return h(i(t(),[`-nP`,`-iTCP:${e}`,`-sTCP:LISTEN`,`-FpFc`],{encoding:`utf-8`}))}catch(e){let t=e,n=t.status??void 0,r=t.code;if(r===`ENOENT`)throw l(`lsof not found; required for --force`,`ENOENT`,e);if(r===`EACCES`||r===`EPERM`)throw l(`lsof permission denied while inspecting gateway port`,r,e);if(n===1){let n=c(t.stderr).trim();if(n&&/permission denied|not permitted|operation not permitted|can't stat/i.test(n))throw l(`lsof permission denied while inspecting gateway port: ${n}`,`EACCES`,e);return[]}throw e instanceof Error?e:Error(String(e))}}function _(e){let t=g(e);for(let e of t)try{process.kill(e.pid,`SIGTERM`)}catch(t){throw Error(`failed to kill pid ${e.pid}${e.command?` (${e.command})`:``}: ${String(t)}`,{cause:t})}return t}function v(e,t){for(let n of e)try{process.kill(n.pid,t)}catch(e){throw Error(`failed to kill pid ${n.pid}${n.command?` (${n.command})`:``}: ${String(e)}`,{cause:e})}}async function y(e,t={}){let n=Math.max(t.timeoutMs??1500,0),i=Math.max(t.intervalMs??100,1),a=Math.min(Math.max(t.sigtermTimeoutMs??600,0),n),s=[],c=!1;o.info(`forceFreePortAndWait: starting for port ${e}, platform=${process.platform}`);try{s=_(e),o.info(`forceFreePortAndWait: forceFreePort succeeded, killed ${s.length} process(es)`)}catch(t){if(o.warn(`forceFreePortAndWait: forceFreePort failed: ${String(t)}`),!d(t))throw t;c=!0,o.info(`forceFreePortAndWait: using fuser fallback`),s=p(e,`SIGTERM`)}let l=async()=>c?m(e):g(e).length>0;if(!await l())return{killed:s,waitedMs:0,escalatedToSigkill:!1};let u=0,f=i>0?Math.ceil(a/i):0;for(let e=0;e<f;e++){if(!await l())return{killed:s,waitedMs:u,escalatedToSigkill:!1};await r(i),u+=i}if(!await l())return{killed:s,waitedMs:u,escalatedToSigkill:!1};c?p(e,`SIGKILL`):v(g(e),`SIGKILL`);let h=Math.max(n-u,0),y=i>0?Math.ceil(h/i):0;for(let e=0;e<y;e++){if(!await l())return{killed:s,waitedMs:u,escalatedToSigkill:!0};await r(i),u+=i}if(!await l())return{killed:s,waitedMs:u,escalatedToSigkill:!0};if(c)throw Error(`port ${e} still has listeners after --force (fuser fallback)`);let b=g(e);throw Error(`port ${e} still has listeners after --force: ${b.map(e=>e.pid).join(`, `)}`)}function b(e,t=`0.0.0.0`){return new Promise((n,r)=>{let i=a();i.unref(),i.once(`error`,e=>{i.close(),e.code===`EADDRINUSE`?n(!1):r(e)}),i.listen(e,t,()=>{i.close(()=>n(!0))})})}async function x(e,t={}){let n=Math.max(t.timeoutMs??3e3,0),i=Math.max(t.intervalMs??150,1),a=t.host,o=0;for(;o<n;){if(await b(e,a))return o;await r(i),o+=i}if(await b(e,a))return o;throw Error(`port ${e} still not bindable after ${o}ms (TIME_WAIT or kernel hold)`)}export{y as n,x as r,_ as t};
