@crewx/sdk 0.9.0-rc.98 → 0.9.0-rc.99

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- import {spawnSync,spawn}from'child_process';var P=3e3,_=2e3,R=25,L=100;function p(e){return e instanceof Error?e:e===void 0?void 0:new Error(String(e))}function g(e){return typeof e=="object"&&e!==null&&"code"in e?String(e.code):void 0}function w(e){return Number.isSafeInteger(e)&&e>0}function b(e,t){try{let r=spawnSync(e,t,{encoding:"utf8",timeout:P,windowsHide:!0});return {status:r.status,stdout:typeof r.stdout=="string"?r.stdout:"",stderr:typeof r.stderr=="string"?r.stderr:"",error:r.error}}catch(r){return {status:null,stdout:"",stderr:"",error:p(r)}}}function O(e,t){return new Promise(r=>{let n;try{n=spawn(e,t,{windowsHide:!0});}catch(a){r({status:null,stdout:"",stderr:"",error:p(a)});return}let o="",i="",u=false,s,m=setTimeout(()=>{try{n.kill();}catch{}s({status:null,stdout:o,stderr:i,error:new Error(`Command timed out after ${P}ms`)});},P);s=a=>{u||(u=true,clearTimeout(m),r(a));},n.stdout?.setEncoding("utf8"),n.stderr?.setEncoding("utf8"),n.stdout?.on("data",a=>{o+=String(a);}),n.stderr?.on("data",a=>{i+=String(a);}),n.once("error",a=>{s({status:null,stdout:o,stderr:i,error:a});}),n.once("close",a=>{s({status:a,stdout:o,stderr:i});});})}async function h(e,t,r){return r.runCommandAsync?r.runCommandAsync(e,t):r.runCommand?r.runCommand(e,t):O(e,t)}function x(e){return new Promise(t=>{setTimeout(t,Math.max(0,e));})}function M(){return new Promise(e=>{setImmediate(e);})}function f(e,t=process.kill){try{return t(e,0),"alive"}catch(r){return g(r)==="ESRCH"?"not_found":g(r)==="EPERM"?"alive":"unknown"}}function N(e,t){if(!w(e))return {status:"unknown"};let r=t.platform??process.platform,n=t.runCommand??b,o=r==="win32"?"powershell.exe":"ps",i=r==="win32"?["-NoLogo","-NoProfile","-NonInteractive","-ExecutionPolicy","Bypass","-Command",`$p = Get-CimInstance -ClassName Win32_Process -Filter 'ProcessId = ${e}' -ErrorAction Stop; if ($null -ne $p) { [Console]::Write([string]$p.CommandLine) }`]:["-p",String(e),"-o","command="],u;try{u=n(o,i);}catch{return {status:"unknown"}}if(u.error||u.status!==0)return {status:"unknown"};let s=u.stdout.trim();return s?{status:"found",commandLine:s}:{status:"not_found"}}function A(e,t){let r=f(-e,t),n=f(e,t);return r==="alive"||n==="alive"?"alive":r==="unknown"||n==="unknown"?"unknown":"not_found"}async function k(e,t,r,n){let o=Math.max(0,r),i=Math.max(1,n.pollIntervalMs??R),u=Date.now()+o,s,m=o===0,a=n.sleep?Promise.resolve().then(()=>n.sleep(o)).then(()=>{},()=>{}):new Promise(c=>{s=setTimeout(c,o);});a.then(()=>{m=true;});try{for(await M();;){let c=A(e,t);if(c==="not_found")return c;let l=u-Date.now();if(m||l<=0)return c;await Promise.race([a,x(Math.min(i,l))]);}}finally{s!==void 0&&clearTimeout(s);}}async function y(e,t,r,n,o){let i=await k(e,t,o.hardConfirmMs??L,{...o,sleep:void 0});return i==="not_found"?r?{status:"not_found",pid:e,method:n}:{status:"terminated",pid:e,method:n}:i==="alive"?{status:"failed",pid:e,method:n,error:"Process is still running after termination attempt"}:{status:"failed",pid:e,method:n,error:"Unable to verify process termination"}}function G(e,t,r={}){let n=N(e,r);if(n.status!=="found")return n.status;let o=r.platform??process.platform,i=o==="win32"?n.commandLine.toLowerCase():n.commandLine;return t.filter(s=>s.trim().length>0).some(s=>{let m=o==="win32"?s.toLowerCase():s;return i.includes(m)})?"owned":"not_owned"}async function $(e,t,r={}){if(!w(e))return "unknown";let n=r.platform??process.platform,o=n==="win32"?"powershell.exe":"ps",i=n==="win32"?["-NoLogo","-NoProfile","-NonInteractive","-ExecutionPolicy","Bypass","-Command",`$p = Get-CimInstance -ClassName Win32_Process -Filter 'ProcessId = ${e}' -ErrorAction Stop; if ($null -ne $p) { [Console]::Write([string]$p.CommandLine) }`]:["-p",String(e),"-o","command="],u;try{u=await h(o,i,r);}catch{return "unknown"}if(u.error||u.status!==0)return "unknown";let s=u.stdout.trim();if(!s)return "not_found";let m=n==="win32"?s.toLowerCase():s;return t.filter(c=>c.trim().length>0).some(c=>{let l=n==="win32"?c.toLowerCase():c;return m.includes(l)})?"owned":"not_owned"}async function U(e,t={}){let r=t.platform??process.platform,n=r==="win32"?"taskkill":"process-group",o=d=>({status:"failed",pid:e,method:n,error:d});if(!w(e))return o("Invalid process ID");let i=t.kill??process.kill,u=f(e,i);if(u==="not_found")return {status:"not_found",pid:e,method:n};if(u!=="alive")return o("Unable to determine whether the process is alive");if(r==="win32"){let d;try{d=await h("taskkill",["/PID",String(e),"/T","/F"],t);}catch(S){d={status:null,stdout:"",stderr:"",error:p(S)};}let C=f(e,i);return C==="not_found"?d.status===0&&!d.error?{status:"terminated",pid:e,method:n}:{status:"not_found",pid:e,method:n}:o(C==="alive"?"Process is still running after termination attempt":"Unable to verify process termination")}let s,m=t.signal??"SIGKILL";try{i(-e,m);}catch(d){s=d;}if(m==="SIGKILL")return y(e,i,s,n,t);if(await k(e,i,t.graceMs??_,t)==="not_found")return s?{status:"not_found",pid:e,method:n}:{status:"terminated",pid:e,method:n};let c;try{i(-e,"SIGKILL");}catch(d){c=d;}let l=await y(e,i,c,n,t);return l.status==="terminated"?l:l.status==="not_found"?s?l:{status:"terminated",pid:e,method:n}:o(l.error??"Process termination failed")}export{R as UNIX_PROCESS_POLL_INTERVAL_MS,L as UNIX_SIGKILL_CONFIRM_MS,_ as UNIX_SIGTERM_GRACE_MS,G as checkProcessOwnership,$ as checkProcessOwnershipAsync,f as probeProcessLiveness,U as terminateProcessTree};
1
+ import {spawnSync,spawn}from'child_process';var P=3e3;var _=2e3,S=25,R=100;function p(e){return e instanceof Error?e:e===void 0?void 0:new Error(String(e))}function g(e){return typeof e=="object"&&e!==null&&"code"in e?String(e.code):void 0}function w(e){return Number.isSafeInteger(e)&&e>0}function O(e,t){try{let r=spawnSync(e,t,{encoding:"utf8",timeout:P,windowsHide:!0});return {status:r.status,stdout:typeof r.stdout=="string"?r.stdout:"",stderr:typeof r.stderr=="string"?r.stderr:"",error:r.error}}catch(r){return {status:null,stdout:"",stderr:"",error:p(r)}}}function b(e,t){return new Promise(r=>{let n;try{n=spawn(e,t,{windowsHide:!0});}catch(a){r({status:null,stdout:"",stderr:"",error:p(a)});return}let o="",i="",u=false,s,m=setTimeout(()=>{try{n.kill();}catch{}s({status:null,stdout:o,stderr:i,error:new Error(`Command timed out after ${P}ms`)});},P);s=a=>{u||(u=true,clearTimeout(m),r(a));},n.stdout?.setEncoding("utf8"),n.stderr?.setEncoding("utf8"),n.stdout?.on("data",a=>{o+=String(a);}),n.stderr?.on("data",a=>{i+=String(a);}),n.once("error",a=>{s({status:null,stdout:o,stderr:i,error:a});}),n.once("close",a=>{s({status:a,stdout:o,stderr:i});});})}async function L(e,t,r){return r.runCommandAsync?r.runCommandAsync(e,t):r.runCommand?r.runCommand(e,t):b(e,t)}function x(e){return new Promise(t=>{setTimeout(t,Math.max(0,e));})}function M(){return new Promise(e=>{setImmediate(e);})}function f(e,t=process.kill){try{return t(e,0),"alive"}catch(r){return g(r)==="ESRCH"?"not_found":g(r)==="EPERM"?"alive":"unknown"}}function N(e,t){if(!w(e))return {status:"unknown"};let r=t.platform??process.platform,n=t.runCommand??O,o=r==="win32"?"powershell.exe":"ps",i=r==="win32"?["-NoLogo","-NoProfile","-NonInteractive","-ExecutionPolicy","Bypass","-Command",`$p = Get-CimInstance -ClassName Win32_Process -Filter 'ProcessId = ${e}' -ErrorAction Stop; if ($null -ne $p) { [Console]::Write([string]$p.CommandLine) }`]:["-p",String(e),"-o","command="],u;try{u=n(o,i);}catch{return {status:"unknown"}}if(u.error||u.status!==0)return {status:"unknown"};let s=u.stdout.trim();return s?{status:"found",commandLine:s}:{status:"not_found"}}function A(e,t){let r=f(-e,t),n=f(e,t);return r==="alive"||n==="alive"?"alive":r==="unknown"||n==="unknown"?"unknown":"not_found"}async function h(e,t,r,n){let o=Math.max(0,r),i=Math.max(1,n.pollIntervalMs??S),u=Date.now()+o,s,m=o===0,a=n.sleep?Promise.resolve().then(()=>n.sleep(o)).then(()=>{},()=>{}):new Promise(c=>{s=setTimeout(c,o);});a.then(()=>{m=true;});try{for(await M();;){let c=A(e,t);if(c==="not_found")return c;let l=u-Date.now();if(m||l<=0)return c;await Promise.race([a,x(Math.min(i,l))]);}}finally{s!==void 0&&clearTimeout(s);}}async function y(e,t,r,n,o){let i=await h(e,t,o.hardConfirmMs??R,{...o,sleep:void 0});return i==="not_found"?r?{status:"not_found",pid:e,method:n}:{status:"terminated",pid:e,method:n}:i==="alive"?{status:"failed",pid:e,method:n,error:"Process is still running after termination attempt"}:{status:"failed",pid:e,method:n,error:"Unable to verify process termination"}}function G(e,t,r={}){let n=N(e,r);if(n.status!=="found")return n.status;let o=r.platform??process.platform,i=o==="win32"?n.commandLine.toLowerCase():n.commandLine;return t.filter(s=>s.trim().length>0).some(s=>{let m=o==="win32"?s.toLowerCase():s;return i.includes(m)})?"owned":"not_owned"}async function $(e,t,r={}){if(!w(e))return "unknown";let n=r.platform??process.platform,o=n==="win32"?"powershell.exe":"ps",i=n==="win32"?["-NoLogo","-NoProfile","-NonInteractive","-ExecutionPolicy","Bypass","-Command",`$p = Get-CimInstance -ClassName Win32_Process -Filter 'ProcessId = ${e}' -ErrorAction Stop; if ($null -ne $p) { [Console]::Write([string]$p.CommandLine) }`]:["-p",String(e),"-o","command="],u;try{u=await L(o,i,r);}catch{return "unknown"}if(u.error||u.status!==0)return "unknown";let s=u.stdout.trim();if(!s)return "not_found";let m=n==="win32"?s.toLowerCase():s;return t.filter(c=>c.trim().length>0).some(c=>{let l=n==="win32"?c.toLowerCase():c;return m.includes(l)})?"owned":"not_owned"}async function U(e,t={}){let r=t.platform??process.platform,n=r==="win32"?"taskkill":"process-group",o=d=>({status:"failed",pid:e,method:n,error:d});if(!w(e))return o("Invalid process ID");let i=t.kill??process.kill,u=f(e,i);if(u==="not_found")return {status:"not_found",pid:e,method:n};if(u!=="alive")return o("Unable to determine whether the process is alive");if(r==="win32"){let d;try{d=await L("taskkill",["/PID",String(e),"/T","/F"],t);}catch(k){d={status:null,stdout:"",stderr:"",error:p(k)};}let C=f(e,i);return C==="not_found"?d.status===0&&!d.error?{status:"terminated",pid:e,method:n}:{status:"not_found",pid:e,method:n}:o(C==="alive"?"Process is still running after termination attempt":"Unable to verify process termination")}let s,m=t.signal??"SIGKILL";try{i(-e,m);}catch(d){s=d;}if(m==="SIGKILL")return y(e,i,s,n,t);if(await h(e,i,t.graceMs??_,t)==="not_found")return s?{status:"not_found",pid:e,method:n}:{status:"terminated",pid:e,method:n};let c;try{i(-e,"SIGKILL");}catch(d){c=d;}let l=await y(e,i,c,n,t);return l.status==="terminated"?l:l.status==="not_found"?s?l:{status:"terminated",pid:e,method:n}:o(l.error??"Process termination failed")}export{S as UNIX_PROCESS_POLL_INTERVAL_MS,R as UNIX_SIGKILL_CONFIRM_MS,_ as UNIX_SIGTERM_GRACE_MS,G as checkProcessOwnership,$ as checkProcessOwnershipAsync,f as probeProcessLiveness,U as terminateProcessTree};