agent-device 0.12.6 → 0.12.7
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.
package/dist/src/818.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{constants as
|
|
1
|
+
import{constants as t}from"node:fs";import{access as e,stat as r}from"node:fs/promises";import o from"node:path";import{spawn as n,spawnSync as i}from"node:child_process";import{AppError as u}from"./152.js";let s=/^[A-Za-z0-9][A-Za-z0-9._+-]*$/,d=[".com",".exe",".bat",".cmd"];async function f(t,e,r={}){let o=m(t);return new Promise((i,s)=>{let d=n(o,e,{cwd:r.cwd,env:r.env,stdio:["pipe","pipe","pipe"],detached:r.detached,shell:!1}),f="",l=r.binaryStdout?Buffer.alloc(0):void 0,a="",c=!1,m=A(r.timeoutMs),p=m?setTimeout(()=>{c=!0,d.kill("SIGKILL")},m):null;r.binaryStdout||d.stdout.setEncoding("utf8"),d.stderr.setEncoding("utf8"),void 0!==r.stdin&&d.stdin.write(r.stdin),d.stdin.end(),d.stdout.on("data",t=>{r.binaryStdout?l=Buffer.concat([l??Buffer.alloc(0),Buffer.isBuffer(t)?t:Buffer.from(t)]):f+=t}),d.stderr.on("data",t=>{a+=t}),d.on("error",r=>{(p&&clearTimeout(p),"ENOENT"===r.code)?s(new u("TOOL_MISSING",`${o} not found in PATH`,{cmd:t},r)):s(new u("COMMAND_FAILED",`Failed to run ${o}`,{cmd:t,args:e},r))}),d.on("close",n=>{p&&clearTimeout(p);let d=n??1;c&&m?s(new u("COMMAND_FAILED",`${o} timed out after ${m}ms`,{cmd:t,args:e,stdout:f,stderr:a,exitCode:d,timeoutMs:m})):0===d||r.allowFailure?i({stdout:f,stderr:a,exitCode:d,stdoutBuffer:l}):s(new u("COMMAND_FAILED",`${o} exited with code ${d}`,{cmd:t,args:e,stdout:f,stderr:a,exitCode:d,processExitError:!0}))})})}async function l(t){let e=p(t);if(!e)return!1;if(o.isAbsolute(e))return w(e);let r=process.env.PATH;if(!r)return!1;let n=function(){if("win32"!==process.platform)return[""];let t=process.env.PATHEXT;if(!t)return d;let e=t.split(";").map(t=>t.trim().toLowerCase()).filter(t=>t.length>0);return e.length>0?e:d}();for(let t of r.split(o.delimiter)){let r=t.trim();if(r){for(let t of function(t,e){if("win32"!==process.platform)return[t];let r=t.toLowerCase();return e.some(t=>r.endsWith(t))?[t]:e.map(e=>`${t}${e}`)}(e,n))if(await w(o.join(r,t)))return!0}}return!1}function a(t,e,r={}){let o=m(t),n=i(o,e,{cwd:r.cwd,env:r.env,stdio:["pipe","pipe","pipe"],encoding:r.binaryStdout?void 0:"utf8",input:r.stdin,timeout:A(r.timeoutMs),shell:!1});if(n.error){let i=n.error.code;if("ETIMEDOUT"===i)throw new u("COMMAND_FAILED",`${o} timed out after ${A(r.timeoutMs)}ms`,{cmd:t,args:e,timeoutMs:A(r.timeoutMs)},n.error);if("ENOENT"===i)throw new u("TOOL_MISSING",`${o} not found in PATH`,{cmd:t},n.error);throw new u("COMMAND_FAILED",`Failed to run ${o}`,{cmd:t,args:e},n.error)}let s=r.binaryStdout?Buffer.isBuffer(n.stdout)?n.stdout:Buffer.from(n.stdout??""):void 0,d=r.binaryStdout?"":"string"==typeof n.stdout?n.stdout:(n.stdout??"").toString(),f="string"==typeof n.stderr?n.stderr:(n.stderr??"").toString(),l=n.status??1;if(0!==l&&!r.allowFailure)throw new u("COMMAND_FAILED",`${o} exited with code ${l}`,{cmd:t,args:e,stdout:d,stderr:f,exitCode:l,processExitError:!0});return{stdout:d,stderr:f,exitCode:l,stdoutBuffer:s}}function c(t,e,r={}){let o=n(m(t),e,{cwd:r.cwd,env:r.env,stdio:r.stdio??"ignore",detached:!0,shell:!1});return o.unref(),o.pid??0}function m(t){let e=p(t);if(!e)throw new u("INVALID_ARGS",`Invalid executable command: ${JSON.stringify(t)}`,{cmd:t,hint:"Use a bare command name from PATH or an absolute executable path."});return e}function p(t){let e=t.trim();return!e||e.includes("\0")?null:o.isAbsolute(e)?e:e.includes("/")||e.includes("\\")?null:s.test(e)?e:null}async function w(r){try{if(!await h(r))return!1;return await e(r,"win32"===process.platform?t.F_OK:t.X_OK),!0}catch{return!1}}async function h(t){try{return(await r(t)).isFile()}catch{return!1}}function A(t){if(!Number.isFinite(t))return;let e=Math.floor(t);if(!(e<=0))return e}export{w as isExecutablePath,f as runCmd,c as runCmdDetached,a as runCmdSync,l as whichCmd};(new u("TOOL_MISSING",`${n} not found in PATH`,{cmd:e},r)):s(new u("COMMAND_FAILED",`Failed to run ${n}`,{cmd:e,args:t},r))}),a.on("close",o=>{m&&clearTimeout(m);let a=o??1;f&&p?s(new u("COMMAND_FAILED",`${n} timed out after ${p}ms`,{cmd:e,args:t,stdout:d,stderr:c,exitCode:a,timeoutMs:p})):0===a||r.allowFailure?i({stdout:d,stderr:c,exitCode:a,stdoutBuffer:l}):s(new u("COMMAND_FAILED",`${n} exited with code ${a}`,{cmd:e,args:t,stdout:d,stderr:c,exitCode:a,processExitError:!0}))})})}async function h(e){let t=S(e);if(!t)return!1;if(n.isAbsolute(t))return T(t);let r=process.env.PATH;if(!r)return!1;let o=function(){if("win32"!==process.platform)return[""];let e=process.env.PATHEXT;if(!e)return p;let t=e.split(";").map(e=>e.trim().toLowerCase()).filter(e=>e.length>0);return t.length>0?t:p}();for(let e of r.split(n.delimiter)){let r=e.trim();if(r){for(let e of function(e,t){if("win32"!==process.platform)return[e];let r=e.toLowerCase();return t.some(e=>r.endsWith(e))?[e]:t.map(t=>`${e}${t}`)}(t,o))if(await T(n.join(r,e)))return!0}}return!1}async function w(e,t){let r=D(e,t,"executable");if(r){if(!await T(r))throw new u("TOOL_MISSING",`${t} points to a missing or non-executable file: ${r}`,{envName:t,path:r});return r}}async function E(e,t){let r=D(e,t,"file");if(r){if(!await M(r))throw new u("TOOL_MISSING",`${t} points to a missing or non-file path: ${r}`,{envName:t,path:r});return r}}function A(e,t,r={}){let n=I(e),o=i(n,t,{cwd:r.cwd,env:r.env,stdio:["pipe","pipe","pipe"],encoding:r.binaryStdout?void 0:"utf8",input:r.stdin,timeout:y(r.timeoutMs),shell:!1});if(o.error){let i=o.error.code;if("ETIMEDOUT"===i)throw new u("COMMAND_FAILED",`${n} timed out after ${y(r.timeoutMs)}ms`,{cmd:e,args:t,timeoutMs:y(r.timeoutMs)},o.error);if("ENOENT"===i)throw new u("TOOL_MISSING",`${n} not found in PATH`,{cmd:e},o.error);throw new u("COMMAND_FAILED",`Failed to run ${n}`,{cmd:e,args:t},o.error)}let s=r.binaryStdout?Buffer.isBuffer(o.stdout)?o.stdout:Buffer.from(o.stdout??""):void 0,a=r.binaryStdout?"":"string"==typeof o.stdout?o.stdout:(o.stdout??"").toString(),d="string"==typeof o.stderr?o.stderr:(o.stderr??"").toString(),l=o.status??1;if(0!==l&&!r.allowFailure)throw new u("COMMAND_FAILED",`${n} exited with code ${l}`,{cmd:e,args:t,stdout:a,stderr:d,exitCode:l,processExitError:!0});return{stdout:a,stderr:d,exitCode:l,stdoutBuffer:s}}function g(e,t,r={}){let n=o(I(e),t,{cwd:r.cwd,env:r.env,stdio:r.stdio??"ignore",detached:!0,shell:!1});return n.unref(),n.pid??0}async function O(e,t,r={}){let n=I(e);return new Promise((i,s)=>{let a=o(n,t,{cwd:r.cwd,env:r.env,stdio:["pipe","pipe","pipe"],detached:r.detached,shell:!1});r.onSpawn?.(a);let d="",l="",c=r.binaryStdout?Buffer.alloc(0):void 0,f=!1,p=y(r.timeoutMs),m=p?setTimeout(()=>{f=!0,a.kill("SIGKILL")},p):null;r.binaryStdout||a.stdout.setEncoding("utf8"),a.stderr.setEncoding("utf8"),void 0!==r.stdin&&a.stdin.write(r.stdin),a.stdin.end(),a.stdout.on("data",e=>{if(r.binaryStdout){c=Buffer.concat([c??Buffer.alloc(0),Buffer.isBuffer(e)?e:Buffer.from(e)]);return}let t=String(e);d+=t,r.onStdoutChunk?.(t)}),a.stderr.on("data",e=>{let t=String(e);l+=t,r.onStderrChunk?.(t)}),a.on("error",r=>{(m&&clearTimeout(m),"ENOENT"===r.code)?s(new u("TOOL_MISSING",`${n} not found in PATH`,{cmd:e},r)):s(new u("COMMAND_FAILED",`Failed to run ${n}`,{cmd:e,args:t},r))}),a.on("close",o=>{m&&clearTimeout(m);let a=o??1;f&&p?s(new u("COMMAND_FAILED",`${n} timed out after ${p}ms`,{cmd:e,args:t,stdout:d,stderr:l,exitCode:a,timeoutMs:p})):0===a||r.allowFailure?i({stdout:d,stderr:l,exitCode:a,stdoutBuffer:c}):s(new u("COMMAND_FAILED",`${n} exited with code ${a}`,{cmd:e,args:t,stdout:d,stderr:l,exitCode:a,processExitError:!0}))})})}function N(e,t,r={}){let n=I(e),i=o(n,t,{cwd:r.cwd,env:r.env,stdio:["ignore","pipe","pipe"],detached:r.detached,shell:!1}),s="",a="";i.stdout.setEncoding("utf8"),i.stderr.setEncoding("utf8"),i.stdout.on("data",e=>{s+=e}),i.stderr.on("data",e=>{a+=e});let d=new Promise((o,d)=>{i.on("error",r=>{"ENOENT"===r.code?d(new u("TOOL_MISSING",`${n} not found in PATH`,{cmd:e},r)):d(new u("COMMAND_FAILED",`Failed to run ${n}`,{cmd:e,args:t},r))}),i.on("close",i=>{let l=i??1;0===l||r.allowFailure?o({stdout:s,stderr:a,exitCode:l}):d(new u("COMMAND_FAILED",`${n} exited with code ${l}`,{cmd:e,args:t,stdout:s,stderr:a,exitCode:l,processExitError:!0}))})});return{child:i,wait:d}}function I(e){let t=S(e);if(!t)throw new u("INVALID_ARGS",`Invalid executable command: ${JSON.stringify(e)}`,{cmd:e,hint:"Use a bare command name from PATH or an absolute executable path."});return t}function D(e,t,r){let o=e?.trim();if(o){if(!n.isAbsolute(o)||o.includes("\0"))throw new u("INVALID_ARGS",`${t} must be an absolute ${r} path, not ${JSON.stringify(e)}`,{envName:t,path:e});return o}}function S(e){let t=e.trim();return!t||t.includes("\0")?null:n.isAbsolute(t)?t:t.includes("/")||t.includes("\\")?null:f.test(t)?t:null}async function T(r){try{if(!await M(r))return!1;return await t(r,"win32"===process.platform?e.F_OK:e.X_OK),!0}catch{return!1}}async function M(e){try{return(await r(e)).isFile()}catch{return!1}}function y(e){if(!Number.isFinite(e))return;let t=Math.floor(e);if(!(t<=0))return t}export{u as AppError,l as asAppError,T as isExecutablePath,c as normalizeError,d as redactDiagnosticData,w as resolveExecutableOverridePath,E as resolveFileOverridePath,m as runCmd,N as runCmdBackground,g as runCmdDetached,O as runCmdStreaming,A as runCmdSync,h as whichCmd};
|