UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 3.55 kB
import{l as isRecord,u as isStringArray}from"../record-coerce-BTbqdJ3-.mjs";import{r as signalProcessTree}from"../kill-tree-CIjHhVfN.mjs";import{t as createDeferredCore}from"../deferred-QsZCoVH-.mjs";import{t as TAILSCALE_ROUTE_OWNER_ARG}from"../tailscale-route-owner-protocol-CZSdi0qK.mjs";import process from"node:process";import{execFile,spawn}from"node:child_process";import{promisify}from"node:util"; //!src/infra/tailscale-route-owner.worker.ts const READY_MARKER=`Press Ctrl+C to exit.`;const OUTPUT_LIMIT=2e5;const STOP_GRACE_MS=2e3;const execFileAsync=promisify(execFile);function appendBounded(e,t){const n=e+t.toString();return n.length<=OUTPUT_LIMIT?n:n.slice(n.length-OUTPUT_LIMIT)}function parseStart(e){const t=JSON.parse(e??`null`);const n=isRecord(t)?t.argv:void 0;if(!isStringArray(n)||n.length===0)throw new Error(`invalid Tailscale route-owner start payload`);return{argv:n}}const pendingReports=/* @__PURE__ */ new Set;function send(e){const t=process.send?.bind(process);if(!process.connected||!t)return;const n=new Promise(n=>{try{t(e,()=>n())}catch{n()}});pendingReports.add(n);n.then(()=>pendingReports.delete(n))}async function signalChild(e,t,n,r){if(typeof e.pid!==`number`||e.pid<=0)return;if(process.platform!==`win32`){if(n){try{await execFileAsync(`sudo`,[`-n`,`/bin/kill`,`-${t.slice(3)}`,`--`,`-${e.pid}`],{timeout:5e3,maxBuffer:16384})}catch{r(`Could not stop the owned Tailscale process group ${e.pid} through sudo. Run \`sudo /bin/kill -TERM -- -${e.pid}\` to stop it, then \`sudo tailscale set --operator=$USER\` to avoid privileged claims.`)}return}signalProcessTree(e.pid,t,{detached:true});return}e.kill(t===`SIGKILL`?`SIGTERM`:t)}function runTailscaleRouteOwner(e,t=send){const n=e.argv[0];if(!n)throw new Error(`Tailscale route-owner command is empty`);const r=e.argv.slice(1);const i={stdout:``,stderr:``};let a=false;let o=false;let s=false;let c;const l=[];const u=createDeferredCore();const d=spawn(n,r,{detached:process.platform!==`win32`,stdio:[`ignore`,`pipe`,`pipe`],windowsHide:true});const stop=()=>{if(o||s)return;o=true;const signal=e=>{l.push(signalChild(d,e,n===`sudo`&&r[0]===`-n`,e=>{if(!s)t({type:`stop-failed`,message:e})}))};signal(`SIGTERM`);c=setTimeout(()=>signal(`SIGKILL`),STOP_GRACE_MS);c.unref?.()};d.once(`spawn`,()=>{if(typeof d.pid===`number`)t({type:`spawned`,pid:d.pid})});for(const e of[`stdout`,`stderr`])d[e]?.on(`data`,n=>{i[e]=appendBounded(i[e],n);if(!a&&i[e].includes(READY_MARKER)){a=true;t({type:`ready`})}});d.once(`error`,e=>{i.stderr=appendBounded(i.stderr,e instanceof Error?e.message:String(e))});d.once(`close`,(e,n)=>{s=true;if(c)clearTimeout(c);Promise.all(l).then(()=>{if(!o||!a)t({type:`failed`,code:e,signal:n,...i});u.resolve({code:e,signal:n,stopping:o})})});return{exited:u.promise,stop}}if(process.argv[2]===`--openclaw-tailscale-route-owner`){let e=0;try{const t=runTailscaleRouteOwner(parseStart(process.argv[3]));const n=[`SIGINT`,`SIGTERM`,`SIGHUP`];const onMessage=e=>{if(isRecord(e)&&e.type===`stop`)t.stop()};for(const e of n)process.on(e,t.stop);process.once(`disconnect`,t.stop);process.once(`message`,onMessage);try{if(!process.connected)t.stop();e=(await t.exited).stopping?0:1}finally{for(const e of n)process.off(e,t.stop);process.off(`disconnect`,t.stop);process.off(`message`,onMessage)}}catch(t){send({type:`failed`,code:null,signal:null,stdout:``,stderr:t instanceof Error?t.message:String(t)});e=1}finally{await Promise.all(pendingReports);if(process.connected)process.disconnect?.();process.exitCode=e}}export{runTailscaleRouteOwner};