import{shutdownActiveSandboxHandles}from"#execution/sandbox/active-handles.js";import{isEveDevEnvironment}from"#internal/application/optional-package-install.js";const SHUTDOWN_SIGNALS=[`SIGINT`,`SIGTERM`];let installed=!1;function shouldInstallSandboxShutdown(e){return!(isEveDevEnvironment()||e.EVE_DEVELOPMENT_SANDBOX_RUN_ID!==void 0||e.VERCEL!==void 0)}async function runSandboxShutdown(t){let n,r=new Promise(e=>{n=setTimeout(()=>{t(`eve: sandbox shutdown timed out; continuing exit`),e()},15e3),n.unref?.()});try{await Promise.race([shutdownActiveSandboxHandles({log:t}),r])}finally{clearTimeout(n)}}function exitCodeForSignal(e){return e===`SIGINT`?130:143}function installSandboxShutdownHandlers(e){if(shouldInstallSandboxShutdown(e.process.env)){e.nitroApp?.hooks?.hook(`close`,async()=>{await runSandboxShutdown(e.log)});for(let t of SHUTDOWN_SIGNALS)e.process.once(t,()=>{runSandboxShutdown(e.log).finally(()=>{e.process.exit(exitCodeForSignal(t))})})}}function sandboxShutdownPlugin(e){installed||(installed=!0,installSandboxShutdownHandlers({log:e=>console.error(e),nitroApp:e,process}))}export{sandboxShutdownPlugin as default,installSandboxShutdownHandlers,runSandboxShutdown,shouldInstallSandboxShutdown};