just-bash 2.14.0 → 2.15.0-executor.0
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/Bash.d.ts +86 -0
- package/dist/bin/chunks/chunk-MNMRGJJM.js +11 -0
- package/dist/bin/chunks/{js-exec-BDQGEAU6.js → js-exec-6UJKEL4G.js} +9 -9
- package/dist/bin/chunks/js-exec-worker.js +233 -1
- package/dist/bin/chunks/{python3-VCIXXAXF.js → python3-QOJU2POC.js} +1 -1
- package/dist/bin/chunks/worker.js +15 -1
- package/dist/bin/just-bash.js +236 -236
- package/dist/bin/shell/chunks/chunk-MNMRGJJM.js +11 -0
- package/dist/bin/shell/chunks/{js-exec-HPXZV7UJ.js → js-exec-H26D5H6V.js} +9 -9
- package/dist/bin/shell/chunks/{python3-KFZH67GD.js → python3-O4VTP6TD.js} +1 -1
- package/dist/bin/shell/shell.js +101 -101
- package/dist/bundle/browser.js +6 -6
- package/dist/bundle/chunks/chunk-B6O2PIY4.js +10 -0
- package/dist/bundle/chunks/{js-exec-4CW5N6RM.js → js-exec-OOPTBRNB.js} +9 -9
- package/dist/bundle/chunks/js-exec-worker.js +233 -1
- package/dist/bundle/chunks/{python3-SG3DOKBZ.js → python3-5F2XPEW4.js} +1 -1
- package/dist/bundle/chunks/worker.js +15 -1
- package/dist/bundle/index.cjs +663 -663
- package/dist/bundle/index.js +7 -7
- package/dist/commands/js-exec/executor-adapter.d.ts +66 -0
- package/dist/commands/js-exec/js-exec.d.ts +19 -1
- package/dist/commands/js-exec/worker.d.ts +8 -0
- package/dist/commands/worker-bridge/bridge-handler.d.ts +3 -1
- package/dist/commands/worker-bridge/protocol.d.ts +1 -0
- package/dist/commands/worker-bridge/sync-backend.d.ts +5 -0
- package/dist/executor-init.d.ts +10 -0
- package/dist/interpreter/interpreter.d.ts +2 -0
- package/dist/interpreter/types.d.ts +5 -0
- package/dist/types.d.ts +6 -0
- package/package.json +37 -34
- package/vendor/cpython-emscripten/python.wasm +0 -0
- package/vendor/executor/executor-sdk-bundle.d.mts +18 -0
- package/vendor/executor/executor-sdk-bundle.mjs +972 -0
- package/dist/bin/chunks/chunk-3KAVXQP4.js +0 -11
- package/dist/bin/shell/chunks/chunk-3KAVXQP4.js +0 -11
- package/dist/bundle/chunks/chunk-S4EYC6T6.js +0 -10
package/dist/bundle/browser.js
CHANGED
|
@@ -1411,7 +1411,7 @@ ${e} ()
|
|
|
1411
1411
|
`,127):z(`bash: ${t}: command not found
|
|
1412
1412
|
`,127);if("error"in c)return c.error==="permission_denied"?z(`bash: ${t}: Permission denied
|
|
1413
1413
|
`,126):z(`bash: ${t}: No such file or directory
|
|
1414
|
-
`,127);if("script"in c)return t.includes("/")||(i.state.hashTable||(i.state.hashTable=new Map),i.state.hashTable.set(t,c.path)),await a(c.path,n,r);let{cmd:u,path:f}=c;t.includes("/")||(i.state.hashTable||(i.state.hashTable=new Map),i.state.hashTable.set(t,f));let p=r||i.state.groupStdin||"",h=o(),d={fs:i.fs,cwd:i.state.cwd,env:i.state.env,exportedEnv:h,stdin:p,limits:i.limits,exec:i.execFn,fetch:i.fetch,getRegisteredCommands:()=>Array.from(i.commands.keys()),sleep:i.sleep,trace:i.trace,fileDescriptors:i.state.fileDescriptors,xpgEcho:i.state.shoptOptions.xpg_echo,coverage:i.coverage,signal:i.state.signal,requireDefenseContext:i.requireDefenseContext,jsBootstrapCode:i.jsBootstrapCode},m=i7(d,t);try{let g=()=>Rt(i.requireDefenseContext,"command",`${t} execution`,()=>u.execute(n,m));return u.trusted?await yt.runTrustedAsync(()=>g()):await g()}catch(g){if(g instanceof K||g instanceof fe)throw g;return z(`${t}: ${Me(Ve(g))}
|
|
1414
|
+
`,127);if("script"in c)return t.includes("/")||(i.state.hashTable||(i.state.hashTable=new Map),i.state.hashTable.set(t,c.path)),await a(c.path,n,r);let{cmd:u,path:f}=c;t.includes("/")||(i.state.hashTable||(i.state.hashTable=new Map),i.state.hashTable.set(t,f));let p=r||i.state.groupStdin||"",h=o(),d={fs:i.fs,cwd:i.state.cwd,env:i.state.env,exportedEnv:h,stdin:p,limits:i.limits,exec:i.execFn,fetch:i.fetch,getRegisteredCommands:()=>Array.from(i.commands.keys()),sleep:i.sleep,trace:i.trace,fileDescriptors:i.state.fileDescriptors,xpgEcho:i.state.shoptOptions.xpg_echo,coverage:i.coverage,signal:i.state.signal,requireDefenseContext:i.requireDefenseContext,jsBootstrapCode:i.jsBootstrapCode,executorInvokeTool:i.executorInvokeTool},m=i7(d,t);try{let g=()=>Rt(i.requireDefenseContext,"command",`${t} execution`,()=>u.execute(n,m));return u.trusted?await yt.runTrustedAsync(()=>g()):await g()}catch(g){if(g instanceof K||g instanceof fe)throw g;return z(`${t}: ${Me(Ve(g))}
|
|
1415
1415
|
`)}}Dt();we();_t();async function qu(e,t){let n=e.state.inCondition;e.state.inCondition=!0;let r="",s="",i=0;try{for(let o of t){let a=await e.executeStatement(o);r+=a.stdout,s+=a.stderr,i=a.exitCode}}finally{e.state.inCondition=n}return{stdout:r,stderr:s,exitCode:i}}we();Yt();function mi(e,t,n,r){if(e instanceof Ct)return t+=e.stdout,n+=e.stderr,e.levels>1&&r>1?(e.levels--,e.stdout=t,e.stderr=n,{action:"rethrow",stdout:t,stderr:n,error:e}):{action:"break",stdout:t,stderr:n};if(e instanceof vt)return t+=e.stdout,n+=e.stderr,e.levels>1&&r>1?(e.levels--,e.stdout=t,e.stderr=n,{action:"rethrow",stdout:t,stderr:n,error:e}):{action:"continue",stdout:t,stderr:n};if(e instanceof wt||e instanceof Mt||e instanceof de||e instanceof K)return e.prependOutput(t,n),{action:"rethrow",stdout:t,stderr:n,error:e};let s=Ve(e);return{action:"error",stdout:t,stderr:`${n}${s}
|
|
1416
1416
|
`,exitCode:1}}we();Yt();async function da(e,t,n="",r=""){let s=n,i=r,o=0;try{for(let a of t){let l=await e.executeStatement(a);s+=l.stdout,i+=l.stderr,o=l.exitCode}}catch(a){if(xi(a)||a instanceof Mt||a instanceof de||a instanceof K||a instanceof In)throw a.prependOutput(s,i),a;return{stdout:s,stderr:`${i}${Ve(a)}
|
|
1417
1417
|
`,exitCode:1}}return{stdout:s,stderr:i,exitCode:o}}async function d7(e,t){let n="",r="";for(let s of t.clauses){let i=await qu(e,s.condition);if(n+=i.stdout,r+=i.stderr,i.exitCode===0)return da(e,s.body,n,r)}return t.elseBody?da(e,t.elseBody,n,r):H(n,r,0)}async function m7(e,t){let n=await Wn(e,t.redirections);if(n)return n;let r="",s="",i=0,o=0;if(!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(t.variable))return z(`bash: \`${t.variable}': not a valid identifier
|
|
@@ -1451,7 +1451,7 @@ sys 0m0.000s
|
|
|
1451
1451
|
`,1)}e.state.groupStdin=c;let u=H(s,i,o);return Ze(e,u,t.redirections)}async function O7(e,t,n,r,s){let i;try{i=await e.fs.readFile(t)}catch{return z(`bash: ${t}: No such file or directory
|
|
1452
1452
|
`,127)}if(i.startsWith("#!")){let g=i.indexOf(`
|
|
1453
1453
|
`);g!==-1&&(i=i.slice(g+1))}let o=new Map(e.state.env),a=e.state.cwd,l={...e.state.options},c=e.state.loopDepth,u=e.state.parentHasLoopContext,f=e.state.lastArg,p=e.state.bashPid,h=e.state.groupStdin,d=e.state.currentSource;e.state.parentHasLoopContext=c>0,e.state.loopDepth=0,e.state.bashPid=e.state.nextVirtualPid++,r&&(e.state.groupStdin=r),e.state.currentSource=t,e.state.env.set("0",t),e.state.env.set("#",String(n.length)),e.state.env.set("@",n.join(" ")),e.state.env.set("*",n.join(" "));for(let g=0;g<n.length&&g<9;g++)e.state.env.set(String(g+1),n[g]);for(let g=n.length+1;g<=9;g++)e.state.env.delete(String(g));let m=()=>{e.state.env=o,e.state.cwd=a,e.state.options=l,e.state.loopDepth=c,e.state.parentHasLoopContext=u,e.state.lastArg=f,e.state.bashPid=p,e.state.groupStdin=h,e.state.currentSource=d};try{let y=new le().parse(i),b=await s(y);return m(),b}catch(g){if(m(),g instanceof de||g instanceof K)throw g;if(g.name==="ParseException")return z(`bash: ${t}: ${g.message}
|
|
1454
|
-
`);throw g}}var gi=class{ctx;constructor(t,n){this.ctx={state:n,fs:t.fs,commands:t.commands,limits:t.limits,execFn:t.exec,executeScript:this.executeScript.bind(this),executeStatement:this.executeStatement.bind(this),executeCommand:this.executeCommand.bind(this),fetch:t.fetch,sleep:t.sleep,trace:t.trace,coverage:t.coverage,requireDefenseContext:t.requireDefenseContext??!1,jsBootstrapCode:t.jsBootstrapCode}}assertDefenseContext(t){if(!this.ctx.requireDefenseContext||yt.isInSandboxedContext())return;let n=`interpreter ${t} attempted outside defense context`;throw new fe(n,{timestamp:Date.now(),type:"missing_defense_context",message:n,path:"DefenseInDepthBox.context",stack:new Error().stack,executionId:yt.getCurrentExecutionId()})}buildExportedEnv(){let t=this.ctx.state.exportedVars,n=this.ctx.state.tempExportedVars,r=new Set;if(t)for(let i of t)r.add(i);if(n)for(let i of n)r.add(i);if(r.size===0)return Object.create(null);let s=Object.create(null);for(let i of r){let o=this.ctx.state.env.get(i);o!==void 0&&(s[i]=o)}return s}async executeScript(t){this.assertDefenseContext("execution");let n="",r="",s=0,i=this.ctx.limits.maxOutputSize,o=(a,l)=>{n.length+r.length+a.length+l.length>i&&Un(`total output size exceeded (>${i} bytes), increase executionLimits.maxOutputSize`,"output_size"),n+=a,r+=l};for(let a of t.statements)try{let l=await this.executeStatement(a);o(l.stdout,l.stderr),s=l.exitCode,this.ctx.state.lastExitCode=s,this.ctx.state.env.set("?",String(s))}catch(l){if(l instanceof de)throw l.prependOutput(n,r),l;if(l instanceof Xt)return o(l.stdout,l.stderr),s=l.exitCode,this.ctx.state.lastExitCode=s,this.ctx.state.env.set("?",String(s)),{stdout:n,stderr:r,exitCode:s,env:xt(this.ctx.state.env)};if(l instanceof K)throw l;if(l instanceof Mt)return o(l.stdout,l.stderr),s=l.exitCode,this.ctx.state.lastExitCode=s,this.ctx.state.env.set("?",String(s)),{stdout:n,stderr:r,exitCode:s,env:xt(this.ctx.state.env)};if(l instanceof It)return o(l.stdout,l.stderr),s=1,this.ctx.state.lastExitCode=s,this.ctx.state.env.set("?",String(s)),{stdout:n,stderr:r,exitCode:s,env:xt(this.ctx.state.env)};if(l instanceof $t)return o(l.stdout,l.stderr),s=1,this.ctx.state.lastExitCode=s,this.ctx.state.env.set("?",String(s)),{stdout:n,stderr:r,exitCode:s,env:xt(this.ctx.state.env)};if(l instanceof Pe){o(l.stdout,l.stderr),s=1,this.ctx.state.lastExitCode=s,this.ctx.state.env.set("?",String(s));continue}if(l instanceof zr){o(l.stdout,l.stderr),s=1,this.ctx.state.lastExitCode=s,this.ctx.state.env.set("?",String(s));continue}if(l instanceof Ct||l instanceof vt){if(this.ctx.state.loopDepth>0)throw l.prependOutput(n,r),l;o(l.stdout,l.stderr);continue}throw l instanceof wt&&l.prependOutput(n,r),l}return{stdout:n,stderr:r,exitCode:s,env:xt(this.ctx.state.env)}}async executeUserScript(t,n,r=""){return O7(this.ctx,t,n,r,s=>this.executeScript(s))}async executeStatement(t){if(this.assertDefenseContext("statement"),this.ctx.state.signal?.aborted)throw new Hr;if(this.ctx.state.commandCount++,this.ctx.state.commandCount>this.ctx.limits.maxCommandCount&&Un(`too many commands executed (>${this.ctx.limits.maxCommandCount}), increase executionLimits.maxCommandCount`,"commands"),t.deferredError)throw new bt(t.deferredError.message,t.line??1,1);if(this.ctx.state.options.noexec)return ge;this.ctx.state.errexitSafe=!1;let n="",r="";this.ctx.state.options.verbose&&!this.ctx.state.suppressVerbose&&t.sourceText&&(r+=`${t.sourceText}
|
|
1454
|
+
`);throw g}}var gi=class{ctx;constructor(t,n){this.ctx={state:n,fs:t.fs,commands:t.commands,limits:t.limits,execFn:t.exec,executeScript:this.executeScript.bind(this),executeStatement:this.executeStatement.bind(this),executeCommand:this.executeCommand.bind(this),fetch:t.fetch,sleep:t.sleep,trace:t.trace,coverage:t.coverage,requireDefenseContext:t.requireDefenseContext??!1,jsBootstrapCode:t.jsBootstrapCode,executorInvokeTool:t.executorInvokeTool}}assertDefenseContext(t){if(!this.ctx.requireDefenseContext||yt.isInSandboxedContext())return;let n=`interpreter ${t} attempted outside defense context`;throw new fe(n,{timestamp:Date.now(),type:"missing_defense_context",message:n,path:"DefenseInDepthBox.context",stack:new Error().stack,executionId:yt.getCurrentExecutionId()})}buildExportedEnv(){let t=this.ctx.state.exportedVars,n=this.ctx.state.tempExportedVars,r=new Set;if(t)for(let i of t)r.add(i);if(n)for(let i of n)r.add(i);if(r.size===0)return Object.create(null);let s=Object.create(null);for(let i of r){let o=this.ctx.state.env.get(i);o!==void 0&&(s[i]=o)}return s}async executeScript(t){this.assertDefenseContext("execution");let n="",r="",s=0,i=this.ctx.limits.maxOutputSize,o=(a,l)=>{n.length+r.length+a.length+l.length>i&&Un(`total output size exceeded (>${i} bytes), increase executionLimits.maxOutputSize`,"output_size"),n+=a,r+=l};for(let a of t.statements)try{let l=await this.executeStatement(a);o(l.stdout,l.stderr),s=l.exitCode,this.ctx.state.lastExitCode=s,this.ctx.state.env.set("?",String(s))}catch(l){if(l instanceof de)throw l.prependOutput(n,r),l;if(l instanceof Xt)return o(l.stdout,l.stderr),s=l.exitCode,this.ctx.state.lastExitCode=s,this.ctx.state.env.set("?",String(s)),{stdout:n,stderr:r,exitCode:s,env:xt(this.ctx.state.env)};if(l instanceof K)throw l;if(l instanceof Mt)return o(l.stdout,l.stderr),s=l.exitCode,this.ctx.state.lastExitCode=s,this.ctx.state.env.set("?",String(s)),{stdout:n,stderr:r,exitCode:s,env:xt(this.ctx.state.env)};if(l instanceof It)return o(l.stdout,l.stderr),s=1,this.ctx.state.lastExitCode=s,this.ctx.state.env.set("?",String(s)),{stdout:n,stderr:r,exitCode:s,env:xt(this.ctx.state.env)};if(l instanceof $t)return o(l.stdout,l.stderr),s=1,this.ctx.state.lastExitCode=s,this.ctx.state.env.set("?",String(s)),{stdout:n,stderr:r,exitCode:s,env:xt(this.ctx.state.env)};if(l instanceof Pe){o(l.stdout,l.stderr),s=1,this.ctx.state.lastExitCode=s,this.ctx.state.env.set("?",String(s));continue}if(l instanceof zr){o(l.stdout,l.stderr),s=1,this.ctx.state.lastExitCode=s,this.ctx.state.env.set("?",String(s));continue}if(l instanceof Ct||l instanceof vt){if(this.ctx.state.loopDepth>0)throw l.prependOutput(n,r),l;o(l.stdout,l.stderr);continue}throw l instanceof wt&&l.prependOutput(n,r),l}return{stdout:n,stderr:r,exitCode:s,env:xt(this.ctx.state.env)}}async executeUserScript(t,n,r=""){return O7(this.ctx,t,n,r,s=>this.executeScript(s))}async executeStatement(t){if(this.assertDefenseContext("statement"),this.ctx.state.signal?.aborted)throw new Hr;if(this.ctx.state.commandCount++,this.ctx.state.commandCount>this.ctx.limits.maxCommandCount&&Un(`too many commands executed (>${this.ctx.limits.maxCommandCount}), increase executionLimits.maxCommandCount`,"commands"),t.deferredError)throw new bt(t.deferredError.message,t.line??1,1);if(this.ctx.state.options.noexec)return ge;this.ctx.state.errexitSafe=!1;let n="",r="";this.ctx.state.options.verbose&&!this.ctx.state.suppressVerbose&&t.sourceText&&(r+=`${t.sourceText}
|
|
1455
1455
|
`);let s=0,i=-1,o=!1;for(let c=0;c<t.pipelines.length;c++){let u=t.pipelines[c],f=c>0?t.operators[c-1]:null;if(f==="&&"&&s!==0||f==="||"&&s===0)continue;let p=await this.executePipeline(u);n+=p.stdout,r+=p.stderr,s=p.exitCode,i=c,o=u.negated,this.ctx.state.lastExitCode=s,this.ctx.state.env.set("?",String(s))}let a=i<t.pipelines.length-1,l=this.ctx.state.errexitSafe;if(this.ctx.state.errexitSafe=a||o||l,this.ctx.state.options.errexit&&s!==0&&i===t.pipelines.length-1&&!o&&!this.ctx.state.inCondition&&!l)throw new Mt(s,n,r);return H(n,r,s)}async executePipeline(t){return v7(this.ctx,t,(n,r)=>this.executeCommand(n,r))}async executeCommand(t,n){switch(this.assertDefenseContext("command"),this.ctx.coverage?.hit(`bash:cmd:${t.type}`),t.type){case"SimpleCommand":return this.executeSimpleCommand(t,n);case"If":return d7(this.ctx,t);case"For":return m7(this.ctx,t);case"CStyleFor":return g7(this.ctx,t);case"While":return y7(this.ctx,t,n);case"Until":return b7(this.ctx,t);case"Case":return w7(this.ctx,t);case"Subshell":return this.executeSubshell(t,n);case"Group":return this.executeGroup(t,n);case"FunctionDef":return _6(this.ctx,t);case"ArithmeticCommand":return this.executeArithmeticCommand(t);case"ConditionalCommand":return this.executeConditionalCommand(t);default:return ge}}async executeSimpleCommand(t,n){try{return await this.executeSimpleCommandInner(t,n)}catch(r){if(r instanceof Nn)return z(r.stderr);throw r}}async executeSimpleCommandInner(t,n){if(t.line!==void 0&&(this.ctx.state.currentLine=t.line),this.ctx.state.shoptOptions.expand_aliases&&t.name){let x=t,A=100;for(;A>0;){let $=this.expandAlias(x);if($===x)break;x=$,A--}this.aliasExpansionStack.clear(),x!==t&&(t=x)}this.ctx.state.expansionStderr="";let r=await k7(this.ctx,t);if(r.error)return r.error;let s=r.tempAssignments,i=r.xtraceOutput;if(!t.name){if(t.redirections.length>0){let A=await Wn(this.ctx,t.redirections);if(A)return A;let $=H("",i,0);return Ze(this.ctx,$,t.redirections)}this.ctx.state.lastArg="";let x=(this.ctx.state.expansionStderr||"")+i;return this.ctx.state.expansionStderr="",H("",x,this.ctx.state.lastExitCode)}let o=t.name&&Zu(t.name,["local","declare","typeset","export","readonly"]),a=Array.from(s.keys());if(a.length>0&&!o){this.ctx.state.tempExportedVars=this.ctx.state.tempExportedVars||new Set;for(let x of a)this.ctx.state.tempExportedVars.add(x)}let l=await oa(this.ctx,t.redirections);if(l){for(let[x,A]of s)A===void 0?this.ctx.state.env.delete(x):this.ctx.state.env.set(x,A);return l}let c=-1;for(let x of t.redirections){if((x.operator==="<<"||x.operator==="<<-")&&x.target.type==="HereDoc"){let A=x.target,$=await se(this.ctx,A.content);A.stripTabs&&($=$.split(`
|
|
1456
1456
|
`).map(F=>F.replace(/^\t+/,"")).join(`
|
|
1457
1457
|
`));let O=x.fd??0;O!==0?(this.ctx.state.fileDescriptors||(this.ctx.state.fileDescriptors=new Map),Nt(this.ctx),this.ctx.state.fileDescriptors.set(O,$)):n=$;continue}if(x.operator==="<<<"&&x.target.type==="Word"){n=`${await se(this.ctx,x.target)}
|
|
@@ -1483,11 +1483,11 @@ ${t}
|
|
|
1483
1483
|
esac${kn(e.redirections)}`}function YS(e){let t=e.patterns.map(Ft).join(" | "),n=pn(e.body);return n?`${t})
|
|
1484
1484
|
${n}
|
|
1485
1485
|
${e.terminator}`:`${t})
|
|
1486
|
-
${e.terminator}`}function JS(e){return`(${pn(e.body)})${kn(e.redirections)}`}function eC(e){return`{ ${pn(e.body)}; }${kn(e.redirections)}`}function tC(e){return`((${ze(e.expression.expression)}))${kn(e.redirections)}`}function nC(e){return`[[ ${Fr(e.expression)} ]]${kn(e.redirections)}`}function rC(e){let t=W7(e.body);return`${e.name}() ${t}${kn(e.redirections)}`}function ze(e){switch(e.type){case"ArithNumber":return String(e.value);case"ArithVariable":return e.hasDollarPrefix?`$${e.name}`:e.name;case"ArithSpecialVar":return`$${e.name}`;case"ArithBinary":return`${ze(e.left)} ${e.operator} ${ze(e.right)}`;case"ArithUnary":return e.prefix?`${e.operator}${ze(e.operand)}`:`${ze(e.operand)}${e.operator}`;case"ArithTernary":return`${ze(e.condition)} ? ${ze(e.consequent)} : ${ze(e.alternate)}`;case"ArithAssignment":return`${e.subscript?`${e.variable}[${ze(e.subscript)}]`:e.stringKey!==void 0?`${e.variable}[${e.stringKey}]`:e.variable} ${e.operator} ${ze(e.value)}`;case"ArithDynamicAssignment":return`${e.subscript?`${ze(e.target)}[${ze(e.subscript)}]`:ze(e.target)} ${e.operator} ${ze(e.value)}`;case"ArithDynamicElement":return`${ze(e.nameExpr)}[${ze(e.subscript)}]`;case"ArithGroup":return`(${ze(e.expression)})`;case"ArithNested":return`$((${ze(e.expression)}))`;case"ArithCommandSubst":return`$(${e.command})`;case"ArithBracedExpansion":return`\${${e.content}}`;case"ArithArrayElement":return e.stringKey!==void 0?`${e.array}[${e.stringKey}]`:e.index?`${e.array}[${ze(e.index)}]`:e.array;case"ArithDynamicBase":return`\${${e.baseExpr}}#${e.value}`;case"ArithDynamicNumber":return`\${${e.prefix}}${e.suffix}`;case"ArithConcat":return e.parts.map(ze).join("");case"ArithDoubleSubscript":return`${e.array}[${ze(e.index)}]`;case"ArithNumberSubscript":return`${e.number}[${e.errorToken}]`;case"ArithSyntaxError":return e.errorToken;case"ArithSingleQuote":return`'${e.content}'`;default:{let t=e;throw new Error(`Unsupported arithmetic expression type: ${t.type}`)}}}function Fr(e){switch(e.type){case"CondBinary":return`${Ft(e.left)} ${e.operator} ${Ft(e.right)}`;case"CondUnary":return`${e.operator} ${Ft(e.operand)}`;case"CondNot":return`! ${Fr(e.operand)}`;case"CondAnd":return`${Fr(e.left)} && ${Fr(e.right)}`;case"CondOr":return`${Fr(e.left)} || ${Fr(e.right)}`;case"CondGroup":return`( ${Fr(e.expression)} )`;case"CondWord":return Ft(e.word);default:{let t=e;throw new Error(`Unsupported conditional expression type: ${t.type}`)}}}var Ju=class{fs;commands=new Map;useDefaultLayout=!1;limits;secureFetch;sleepFn;traceFn;logger;defenseInDepthConfig;coverageWriter;jsBootstrapCode;transformPlugins=[];state;constructor(t={}){let n=t.fs??new Ir(t.files);this.fs=n,this.useDefaultLayout=!t.cwd&&!t.files;let r=t.cwd||(this.useDefaultLayout?"/home/user":"/"),s=new Map([["HOME",this.useDefaultLayout?"/home/user":"/"],["PATH","/usr/bin:/bin"],["IFS",`
|
|
1487
|
-
`],["OSTYPE","linux-gnu"],["MACHTYPE","x86_64-pc-linux-gnu"],["HOSTTYPE","x86_64"],["HOSTNAME","localhost"],["PWD",r],["OLDPWD",r],["OPTIND","1"],...Object.entries(t.env??{})]);if(this.limits=R7({...t.executionLimits,...t.maxCallDepth!==void 0&&{maxCallDepth:t.maxCallDepth},...t.maxCommandCount!==void 0&&{maxCommandCount:t.maxCommandCount},...t.maxLoopIterations!==void 0&&{maxLoopIterations:t.maxLoopIterations}}),t.fetch?this.secureFetch=t.fetch:t.network&&(this.secureFetch=Yu(t.network)),this.sleepFn=t.sleep,this.traceFn=t.trace,this.logger=t.logger,this.defenseInDepthConfig=t.defenseInDepth??!0,this.coverageWriter=t.coverage,this.state={env:s,cwd:r,previousDir:"/home/user",functions:new Map,localScopes:[],callDepth:0,sourceDepth:0,commandCount:0,lastExitCode:0,lastArg:"",startTime:Date.now(),lastBackgroundPid:0,virtualPid:t.processInfo?.pid??1,virtualPpid:t.processInfo?.ppid??0,virtualUid:t.processInfo?.uid??1e3,virtualGid:t.processInfo?.gid??1e3,bashPid:t.processInfo?.pid??1,nextVirtualPid:(t.processInfo?.pid??1)+1,currentLine:1,options:{errexit:!1,pipefail:!1,nounset:!1,xtrace:!1,verbose:!1,posix:!1,allexport:!1,noclobber:!1,noglob:!1,noexec:!1,vi:!1,emacs:!1},shoptOptions:{extglob:!1,dotglob:!1,nullglob:!1,failglob:!1,globstar:!1,globskipdots:!0,nocaseglob:!1,nocasematch:!1,expand_aliases:!1,lastpipe:!1,xpg_echo:!1},inCondition:!1,loopDepth:0,exportedVars:new Set(["HOME","PATH","PWD","OLDPWD",...Object.keys(t.env||{})]),readonlyVars:new Set(["SHELLOPTS","BASHOPTS"]),hashTable:new Map},this.state.env.set("SHELLOPTS",fc(this.state.options)),this.state.env.set("BASHOPTS",pc(this.state.shoptOptions)),Gm(n,this.useDefaultLayout,{pid:this.state.virtualPid,ppid:this.state.virtualPpid,uid:this.state.virtualUid,gid:this.state.virtualGid}),r!=="/"&&n instanceof Ir)try{n.mkdirSync(r,{recursive:!0})}catch{}for(let i of Dm(t.commands))this.registerCommand(i);if(t.fetch||t.network)for(let i of _m())this.registerCommand(i);if(t.python)for(let i of Fm())this.registerCommand(i);if(t.javascript){for(let o of Lm())this.registerCommand(o);let i=typeof t.javascript=="object"?t.javascript:Object.create(null);i.bootstrap&&(this.jsBootstrapCode=i.bootstrap)}if(t.customCommands)for(let i of t.customCommands)Mm(i)?this.registerCommand(Um(i)):this.registerCommand({...i,trusted:i.trusted??!0})}registerCommand(t){this.commands.set(t.name,t);let n=this.fs;if(typeof n.writeFileSync=="function"){let r=`#!/bin/bash
|
|
1486
|
+
${e.terminator}`}function JS(e){return`(${pn(e.body)})${kn(e.redirections)}`}function eC(e){return`{ ${pn(e.body)}; }${kn(e.redirections)}`}function tC(e){return`((${ze(e.expression.expression)}))${kn(e.redirections)}`}function nC(e){return`[[ ${Fr(e.expression)} ]]${kn(e.redirections)}`}function rC(e){let t=W7(e.body);return`${e.name}() ${t}${kn(e.redirections)}`}function ze(e){switch(e.type){case"ArithNumber":return String(e.value);case"ArithVariable":return e.hasDollarPrefix?`$${e.name}`:e.name;case"ArithSpecialVar":return`$${e.name}`;case"ArithBinary":return`${ze(e.left)} ${e.operator} ${ze(e.right)}`;case"ArithUnary":return e.prefix?`${e.operator}${ze(e.operand)}`:`${ze(e.operand)}${e.operator}`;case"ArithTernary":return`${ze(e.condition)} ? ${ze(e.consequent)} : ${ze(e.alternate)}`;case"ArithAssignment":return`${e.subscript?`${e.variable}[${ze(e.subscript)}]`:e.stringKey!==void 0?`${e.variable}[${e.stringKey}]`:e.variable} ${e.operator} ${ze(e.value)}`;case"ArithDynamicAssignment":return`${e.subscript?`${ze(e.target)}[${ze(e.subscript)}]`:ze(e.target)} ${e.operator} ${ze(e.value)}`;case"ArithDynamicElement":return`${ze(e.nameExpr)}[${ze(e.subscript)}]`;case"ArithGroup":return`(${ze(e.expression)})`;case"ArithNested":return`$((${ze(e.expression)}))`;case"ArithCommandSubst":return`$(${e.command})`;case"ArithBracedExpansion":return`\${${e.content}}`;case"ArithArrayElement":return e.stringKey!==void 0?`${e.array}[${e.stringKey}]`:e.index?`${e.array}[${ze(e.index)}]`:e.array;case"ArithDynamicBase":return`\${${e.baseExpr}}#${e.value}`;case"ArithDynamicNumber":return`\${${e.prefix}}${e.suffix}`;case"ArithConcat":return e.parts.map(ze).join("");case"ArithDoubleSubscript":return`${e.array}[${ze(e.index)}]`;case"ArithNumberSubscript":return`${e.number}[${e.errorToken}]`;case"ArithSyntaxError":return e.errorToken;case"ArithSingleQuote":return`'${e.content}'`;default:{let t=e;throw new Error(`Unsupported arithmetic expression type: ${t.type}`)}}}function Fr(e){switch(e.type){case"CondBinary":return`${Ft(e.left)} ${e.operator} ${Ft(e.right)}`;case"CondUnary":return`${e.operator} ${Ft(e.operand)}`;case"CondNot":return`! ${Fr(e.operand)}`;case"CondAnd":return`${Fr(e.left)} && ${Fr(e.right)}`;case"CondOr":return`${Fr(e.left)} || ${Fr(e.right)}`;case"CondGroup":return`( ${Fr(e.expression)} )`;case"CondWord":return Ft(e.word);default:{let t=e;throw new Error(`Unsupported conditional expression type: ${t.type}`)}}}var Ju=class{fs;commands=new Map;useDefaultLayout=!1;limits;secureFetch;sleepFn;traceFn;logger;defenseInDepthConfig;coverageWriter;jsBootstrapCode;executorInvokeTool;executorSetup;executorApproval;executorSDK;executorInitPromise;transformPlugins=[];state;constructor(t={}){let n=t.fs??new Ir(t.files);this.fs=n,this.useDefaultLayout=!t.cwd&&!t.files;let r=t.cwd||(this.useDefaultLayout?"/home/user":"/"),s=new Map([["HOME",this.useDefaultLayout?"/home/user":"/"],["PATH","/usr/bin:/bin"],["IFS",`
|
|
1487
|
+
`],["OSTYPE","linux-gnu"],["MACHTYPE","x86_64-pc-linux-gnu"],["HOSTTYPE","x86_64"],["HOSTNAME","localhost"],["PWD",r],["OLDPWD",r],["OPTIND","1"],...Object.entries(t.env??{})]);if(this.limits=R7({...t.executionLimits,...t.maxCallDepth!==void 0&&{maxCallDepth:t.maxCallDepth},...t.maxCommandCount!==void 0&&{maxCommandCount:t.maxCommandCount},...t.maxLoopIterations!==void 0&&{maxLoopIterations:t.maxLoopIterations}}),t.fetch?this.secureFetch=t.fetch:t.network&&(this.secureFetch=Yu(t.network)),this.sleepFn=t.sleep,this.traceFn=t.trace,this.logger=t.logger,this.defenseInDepthConfig=t.defenseInDepth??!0,this.coverageWriter=t.coverage,this.state={env:s,cwd:r,previousDir:"/home/user",functions:new Map,localScopes:[],callDepth:0,sourceDepth:0,commandCount:0,lastExitCode:0,lastArg:"",startTime:Date.now(),lastBackgroundPid:0,virtualPid:t.processInfo?.pid??1,virtualPpid:t.processInfo?.ppid??0,virtualUid:t.processInfo?.uid??1e3,virtualGid:t.processInfo?.gid??1e3,bashPid:t.processInfo?.pid??1,nextVirtualPid:(t.processInfo?.pid??1)+1,currentLine:1,options:{errexit:!1,pipefail:!1,nounset:!1,xtrace:!1,verbose:!1,posix:!1,allexport:!1,noclobber:!1,noglob:!1,noexec:!1,vi:!1,emacs:!1},shoptOptions:{extglob:!1,dotglob:!1,nullglob:!1,failglob:!1,globstar:!1,globskipdots:!0,nocaseglob:!1,nocasematch:!1,expand_aliases:!1,lastpipe:!1,xpg_echo:!1},inCondition:!1,loopDepth:0,exportedVars:new Set(["HOME","PATH","PWD","OLDPWD",...Object.keys(t.env||{})]),readonlyVars:new Set(["SHELLOPTS","BASHOPTS"]),hashTable:new Map},this.state.env.set("SHELLOPTS",fc(this.state.options)),this.state.env.set("BASHOPTS",pc(this.state.shoptOptions)),Gm(n,this.useDefaultLayout,{pid:this.state.virtualPid,ppid:this.state.virtualPpid,uid:this.state.virtualUid,gid:this.state.virtualGid}),r!=="/"&&n instanceof Ir)try{n.mkdirSync(r,{recursive:!0})}catch{}for(let i of Dm(t.commands))this.registerCommand(i);if(t.fetch||t.network)for(let i of _m())this.registerCommand(i);if(t.python)for(let i of Fm())this.registerCommand(i);if(t.javascript||t.executor){for(let o of Lm())this.registerCommand(o);let i=typeof t.javascript=="object"?t.javascript:Object.create(null);i.bootstrap&&(this.jsBootstrapCode=i.bootstrap)}if(t.executor?.tools){let i=t.executor.tools;this.executorInvokeTool=async(o,a)=>{if(!Object.hasOwn(i,o))throw new Error(`Unknown tool: ${o}`);let l=i[o],c;try{c=a?JSON.parse(a):void 0}catch{c=void 0}let u=await l.execute(c);return u!==void 0?JSON.stringify(u):""}}if(t.executor?.setup&&(this.executorSetup=t.executor.setup),t.executor?.onToolApproval&&(this.executorApproval=t.executor.onToolApproval),t.customCommands)for(let i of t.customCommands)Mm(i)?this.registerCommand(Um(i)):this.registerCommand({...i,trusted:i.trusted??!0})}registerCommand(t){this.commands.set(t.name,t);let n=this.fs;if(typeof n.writeFileSync=="function"){let r=`#!/bin/bash
|
|
1488
1488
|
# Built-in command: ${t.name}
|
|
1489
|
-
`;try{n.writeFileSync(`/bin/${t.name}`,r)}catch{}try{n.writeFileSync(`/usr/bin/${t.name}`,r)}catch{}}}logResult(t){return this.logger&&(t.stdout&&this.logger.debug("stdout",{output:t.stdout}),t.stderr&&this.logger.info("stderr",{output:t.stderr}),this.logger.info("exit",{exitCode:t.exitCode})),t.stdout=G7(t.stdout),t.stderr=G7(t.stderr),t}async exec(t,n){if(this.state.callDepth===0&&(this.state.commandCount=0),this.state.commandCount++,this.state.commandCount>this.limits.maxCommandCount)return{stdout:"",stderr:`bash: maximum command count (${this.limits.maxCommandCount}) exceeded (possible infinite loop). Increase with executionLimits.maxCommandCount option.
|
|
1490
|
-
`,exitCode:1,env:an(this.state.env,n?.env)};if(!t.trim())return{stdout:"",stderr:"",exitCode:0,env:an(this.state.env,n?.env)};this.logger?.info("exec",{command:t});let r=n?.cwd??this.state.cwd,s,i=r;if(n?.cwd)if(n.env&&"PWD"in n.env)s=n.env.PWD;else if(n?.env&&!("PWD"in n.env))try{s=await this.fs.realpath(r),i=s}catch{s=r}else s=r;let o=n?.replaceEnv?new Map:new Map(this.state.env);if(n?.env)for(let[f,p]of Object.entries(n.env))o.set(f,p);s!==void 0&&o.set("PWD",s);let a={...this.state,env:o,cwd:i,functions:new Map(this.state.functions),localScopes:[...this.state.localScopes],options:{...this.state.options},hashTable:this.state.hashTable,groupStdin:n?.stdin,signal:n?.signal,extraArgs:n?.args},l=t;n?.rawScript||(l=V7(t));let c=this.defenseInDepthConfig?yt.getInstance(this.defenseInDepthConfig):null,u=c?.activate();try{let f=async()=>{let p=wn(l,{maxHeredocSize:this.limits.maxHeredocSize}),h;if(this.transformPlugins.length>0){let b=Object.create(null);for(let w of this.transformPlugins){let x=w.transform({ast:p,metadata:b});p=x.ast,x.metadata&&(b=wr(b,x.metadata))}h=b}let d={fs:this.fs,commands:this.commands,limits:this.limits,exec:this.exec.bind(this),fetch:this.secureFetch,sleep:this.sleepFn,trace:this.traceFn,coverage:this.coverageWriter,requireDefenseContext:c?.isEnabled()===!0,jsBootstrapCode:this.jsBootstrapCode},y=await new gi(d,a).executeScript(p);return h&&(y.metadata=h),this.logResult(y)};return u?await u.run(f):await f()}catch(f){if(f instanceof de)return this.logResult({stdout:f.stdout,stderr:f.stderr,exitCode:f.exitCode,env:an(this.state.env,n?.env)});if(f instanceof Xt)return this.logResult({stdout:f.stdout,stderr:f.stderr,exitCode:f.exitCode,env:an(this.state.env,n?.env)});if(f instanceof Pe)return this.logResult({stdout:f.stdout,stderr:f.stderr,exitCode:1,env:an(this.state.env,n?.env)});if(f instanceof Hr)return this.logResult({stdout:f.stdout,stderr:f.stderr,exitCode:124,env:an(this.state.env,n?.env)});if(f instanceof K)return this.logResult({stdout:f.stdout,stderr:Me(f.stderr),exitCode:K.EXIT_CODE,env:an(this.state.env,n?.env)});if(f instanceof fe)return this.logResult({stdout:"",stderr:`bash: security violation: ${Me(f.message)}
|
|
1489
|
+
`;try{n.writeFileSync(`/bin/${t.name}`,r)}catch{}try{n.writeFileSync(`/usr/bin/${t.name}`,r)}catch{}}}logResult(t){return this.logger&&(t.stdout&&this.logger.debug("stdout",{output:t.stdout}),t.stderr&&this.logger.info("stderr",{output:t.stderr}),this.logger.info("exit",{exitCode:t.exitCode})),t.stdout=G7(t.stdout),t.stderr=G7(t.stderr),t}async ensureExecutorReady(){if(!(!this.executorSetup||this.executorSDK)){if(this.executorInitPromise){await this.executorInitPromise;return}this.executorInitPromise=(async()=>{let t="./executor-init.js",{initExecutorSDK:n}=await import(t),r=this.executorSetup;if(!r)return;let{sdk:s,invokeTool:i}=await n(r,this.executorApproval,this.fs,()=>this.state.cwd,()=>this.state.env,()=>this.limits);this.executorSDK=s,this.executorInvokeTool=i})(),await this.executorInitPromise}}async exec(t,n){if(await this.ensureExecutorReady(),this.state.callDepth===0&&(this.state.commandCount=0),this.state.commandCount++,this.state.commandCount>this.limits.maxCommandCount)return{stdout:"",stderr:`bash: maximum command count (${this.limits.maxCommandCount}) exceeded (possible infinite loop). Increase with executionLimits.maxCommandCount option.
|
|
1490
|
+
`,exitCode:1,env:an(this.state.env,n?.env)};if(!t.trim())return{stdout:"",stderr:"",exitCode:0,env:an(this.state.env,n?.env)};this.logger?.info("exec",{command:t});let r=n?.cwd??this.state.cwd,s,i=r;if(n?.cwd)if(n.env&&"PWD"in n.env)s=n.env.PWD;else if(n?.env&&!("PWD"in n.env))try{s=await this.fs.realpath(r),i=s}catch{s=r}else s=r;let o=n?.replaceEnv?new Map:new Map(this.state.env);if(n?.env)for(let[f,p]of Object.entries(n.env))o.set(f,p);s!==void 0&&o.set("PWD",s);let a={...this.state,env:o,cwd:i,functions:new Map(this.state.functions),localScopes:[...this.state.localScopes],options:{...this.state.options},hashTable:this.state.hashTable,groupStdin:n?.stdin,signal:n?.signal,extraArgs:n?.args},l=t;n?.rawScript||(l=V7(t));let c=this.defenseInDepthConfig?yt.getInstance(this.defenseInDepthConfig):null,u=c?.activate();try{let f=async()=>{let p=wn(l,{maxHeredocSize:this.limits.maxHeredocSize}),h;if(this.transformPlugins.length>0){let b=Object.create(null);for(let w of this.transformPlugins){let x=w.transform({ast:p,metadata:b});p=x.ast,x.metadata&&(b=wr(b,x.metadata))}h=b}let d={fs:this.fs,commands:this.commands,limits:this.limits,exec:this.exec.bind(this),fetch:this.secureFetch,sleep:this.sleepFn,trace:this.traceFn,coverage:this.coverageWriter,requireDefenseContext:c?.isEnabled()===!0,jsBootstrapCode:this.jsBootstrapCode,executorInvokeTool:this.executorInvokeTool},y=await new gi(d,a).executeScript(p);return h&&(y.metadata=h),this.logResult(y)};return u?await u.run(f):await f()}catch(f){if(f instanceof de)return this.logResult({stdout:f.stdout,stderr:f.stderr,exitCode:f.exitCode,env:an(this.state.env,n?.env)});if(f instanceof Xt)return this.logResult({stdout:f.stdout,stderr:f.stderr,exitCode:f.exitCode,env:an(this.state.env,n?.env)});if(f instanceof Pe)return this.logResult({stdout:f.stdout,stderr:f.stderr,exitCode:1,env:an(this.state.env,n?.env)});if(f instanceof Hr)return this.logResult({stdout:f.stdout,stderr:f.stderr,exitCode:124,env:an(this.state.env,n?.env)});if(f instanceof K)return this.logResult({stdout:f.stdout,stderr:Me(f.stderr),exitCode:K.EXIT_CODE,env:an(this.state.env,n?.env)});if(f instanceof fe)return this.logResult({stdout:"",stderr:`bash: security violation: ${Me(f.message)}
|
|
1491
1491
|
`,exitCode:1,env:an(this.state.env,n?.env)});if(f.name==="ParseException")return this.logResult({stdout:"",stderr:`bash: syntax error: ${Me(f.message)}
|
|
1492
1492
|
`,exitCode:2,env:an(this.state.env,n?.env)});if(f instanceof tr)return this.logResult({stdout:"",stderr:`bash: ${Me(f.message)}
|
|
1493
1493
|
`,exitCode:2,env:an(this.state.env,n?.env)});if(f instanceof RangeError)return this.logResult({stdout:"",stderr:`bash: ${Me(f.message)}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import{a as w,b as h}from"./chunk-CWQS3NFK.js";import{a as T,b as g}from"./chunk-A5O5YHGN.js";import{a as F}from"./chunk-IPJHKYVM.js";import{a as m}from"./chunk-3THT3N7L.js";var n={NOOP:0,READ_FILE:1,WRITE_FILE:2,STAT:3,READDIR:4,MKDIR:5,RM:6,EXISTS:7,APPEND_FILE:8,SYMLINK:9,READLINK:10,LSTAT:11,CHMOD:12,REALPATH:13,RENAME:14,COPY_FILE:15,WRITE_STDOUT:100,WRITE_STDERR:101,EXIT:102,HTTP_REQUEST:200,EXEC_COMMAND:300,INVOKE_TOOL:400},r={PENDING:0,READY:1,SUCCESS:2,ERROR:3},l={NONE:0,NOT_FOUND:1,IS_DIRECTORY:2,NOT_DIRECTORY:3,EXISTS:4,PERMISSION_DENIED:5,INVALID_PATH:6,IO_ERROR:7,TIMEOUT:8,NETWORK_ERROR:9,NETWORK_NOT_CONFIGURED:10},o={OP_CODE:0,STATUS:4,PATH_LENGTH:8,DATA_LENGTH:12,RESULT_LENGTH:16,ERROR_CODE:20,FLAGS:24,MODE:28,PATH_BUFFER:32,DATA_BUFFER:4128},p={CONTROL_REGION:32,PATH_BUFFER:4096,DATA_BUFFER:1048576,TOTAL:1052704},S={NONE:0,RECURSIVE:1,FORCE:2,MKDIR_RECURSIVE:1},u={IS_FILE:0,IS_DIRECTORY:1,IS_SYMLINK:2,MODE:4,SIZE:8,MTIME:16,TOTAL:24};function _(){return new w(p.TOTAL)}var R=class{int32View;uint8View;dataView;constructor(t){this.int32View=new Int32Array(t),this.uint8View=new Uint8Array(t),this.dataView=new DataView(t)}getOpCode(){return h.load(this.int32View,o.OP_CODE/4)}setOpCode(t){h.store(this.int32View,o.OP_CODE/4,t)}getStatus(){return h.load(this.int32View,o.STATUS/4)}setStatus(t){h.store(this.int32View,o.STATUS/4,t)}getPathLength(){return h.load(this.int32View,o.PATH_LENGTH/4)}setPathLength(t){h.store(this.int32View,o.PATH_LENGTH/4,t)}getDataLength(){return h.load(this.int32View,o.DATA_LENGTH/4)}setDataLength(t){h.store(this.int32View,o.DATA_LENGTH/4,t)}getResultLength(){return h.load(this.int32View,o.RESULT_LENGTH/4)}setResultLength(t){h.store(this.int32View,o.RESULT_LENGTH/4,t)}getErrorCode(){return h.load(this.int32View,o.ERROR_CODE/4)}setErrorCode(t){h.store(this.int32View,o.ERROR_CODE/4,t)}getFlags(){return h.load(this.int32View,o.FLAGS/4)}setFlags(t){h.store(this.int32View,o.FLAGS/4,t)}getMode(){return h.load(this.int32View,o.MODE/4)}setMode(t){h.store(this.int32View,o.MODE/4,t)}getPath(){let t=this.getPathLength(),e=this.uint8View.slice(o.PATH_BUFFER,o.PATH_BUFFER+t);return new TextDecoder().decode(e)}setPath(t){let e=new TextEncoder().encode(t);if(e.length>p.PATH_BUFFER)throw new Error(`Path too long: ${e.length} > ${p.PATH_BUFFER}`);this.uint8View.set(e,o.PATH_BUFFER),this.setPathLength(e.length)}getData(){let t=this.getDataLength();return this.uint8View.slice(o.DATA_BUFFER,o.DATA_BUFFER+t)}setData(t){if(t.length>p.DATA_BUFFER)throw new Error(`Data too large: ${t.length} > ${p.DATA_BUFFER}`);this.uint8View.set(t,o.DATA_BUFFER),this.setDataLength(t.length)}getDataAsString(){let t=this.getData();return new TextDecoder().decode(t)}setDataFromString(t){let e=new TextEncoder().encode(t);this.setData(e)}getResult(){let t=this.getResultLength();return this.uint8View.slice(o.DATA_BUFFER,o.DATA_BUFFER+t)}setResult(t){if(t.length>p.DATA_BUFFER)throw new Error(`Result too large: ${t.length} > ${p.DATA_BUFFER}`);this.uint8View.set(t,o.DATA_BUFFER),this.setResultLength(t.length)}getResultAsString(){let t=this.getResult();return new TextDecoder().decode(t)}setResultFromString(t){let e=new TextEncoder().encode(t);this.setResult(e)}encodeStat(t){this.uint8View[o.DATA_BUFFER+u.IS_FILE]=t.isFile?1:0,this.uint8View[o.DATA_BUFFER+u.IS_DIRECTORY]=t.isDirectory?1:0,this.uint8View[o.DATA_BUFFER+u.IS_SYMLINK]=t.isSymbolicLink?1:0,this.dataView.setInt32(o.DATA_BUFFER+u.MODE,t.mode,!0);let e=Math.min(t.size,Number.MAX_SAFE_INTEGER);this.dataView.setFloat64(o.DATA_BUFFER+u.SIZE,e,!0),this.dataView.setFloat64(o.DATA_BUFFER+u.MTIME,t.mtime.getTime(),!0),this.setResultLength(u.TOTAL)}decodeStat(){return{isFile:this.uint8View[o.DATA_BUFFER+u.IS_FILE]===1,isDirectory:this.uint8View[o.DATA_BUFFER+u.IS_DIRECTORY]===1,isSymbolicLink:this.uint8View[o.DATA_BUFFER+u.IS_SYMLINK]===1,mode:this.dataView.getInt32(o.DATA_BUFFER+u.MODE,!0),size:this.dataView.getFloat64(o.DATA_BUFFER+u.SIZE,!0),mtime:new Date(this.dataView.getFloat64(o.DATA_BUFFER+u.MTIME,!0))}}waitForReady(t){return h.wait(this.int32View,o.STATUS/4,r.PENDING,t)}waitForReadyAsync(t){return h.waitAsync(this.int32View,o.STATUS/4,r.PENDING,t)}async waitUntilReady(t){let e=Date.now();for(;;){let a=this.getStatus();if(a===r.READY)return!0;let s=Date.now()-e;if(s>=t)return!1;let i=t-s,c=h.waitAsync(this.int32View,o.STATUS/4,a,i);if(c.async&&await c.value==="timed-out")return!1}}waitForResult(t){return h.wait(this.int32View,o.STATUS/4,r.READY,t)}notify(){return h.notify(this.int32View,o.STATUS/4)}reset(){this.setOpCode(n.NOOP),this.setStatus(r.PENDING),this.setPathLength(0),this.setDataLength(0),this.setResultLength(0),this.setErrorCode(l.NONE),this.setFlags(S.NONE),this.setMode(0)}};var O=class{fs;cwd;commandName;secureFetch;maxOutputSize;exec;invokeTool;protocol;running=!1;output={stdout:"",stderr:"",exitCode:0};outputLimitExceeded=!1;startTime=0;timeoutMs=0;constructor(t,e,a,s,i=void 0,c=0,d=void 0,E=void 0){this.fs=e,this.cwd=a,this.commandName=s,this.secureFetch=i,this.maxOutputSize=c,this.exec=d,this.invokeTool=E,this.protocol=new R(t)}remainingMs(){return Math.max(0,this.timeoutMs-(Date.now()-this.startTime))}raceDeadline(t){let e=this.remainingMs();if(e<=0)return this.running=!1,this.output.exitCode=124,this.output.stderr+=`
|
|
2
|
+
${this.commandName}: execution timeout exceeded
|
|
3
|
+
`,Promise.reject(new Error("Operation timed out"));let a=t();return new Promise((s,i)=>{let c=T(()=>{this.running=!1,this.output.exitCode=124,this.output.stderr+=`
|
|
4
|
+
${this.commandName}: execution timeout exceeded
|
|
5
|
+
`,i(new Error("Operation timed out"))},e);a.then(d=>{g(c),s(d)},d=>{g(c),i(d)})})}async run(t){for(this.running=!0,this.startTime=Date.now(),this.timeoutMs=t;this.running;){if(Date.now()-this.startTime>=t){this.output.stderr+=`
|
|
6
|
+
${this.commandName}: execution timeout exceeded
|
|
7
|
+
`,this.output.exitCode=124;break}let a=this.remainingMs();if(!await this.protocol.waitUntilReady(a)){this.output.stderr+=`
|
|
8
|
+
${this.commandName}: execution timeout exceeded
|
|
9
|
+
`,this.output.exitCode=124;break}let i=this.protocol.getOpCode();await this.handleOperation(i),this.protocol.notify()}return this.output}stop(){this.running=!1}async handleOperation(t){try{switch(t){case n.READ_FILE:await this.handleReadFile();break;case n.WRITE_FILE:await this.handleWriteFile();break;case n.STAT:await this.handleStat();break;case n.LSTAT:await this.handleLstat();break;case n.READDIR:await this.handleReaddir();break;case n.MKDIR:await this.handleMkdir();break;case n.RM:await this.handleRm();break;case n.EXISTS:await this.handleExists();break;case n.APPEND_FILE:await this.handleAppendFile();break;case n.SYMLINK:await this.handleSymlink();break;case n.READLINK:await this.handleReadlink();break;case n.CHMOD:await this.handleChmod();break;case n.REALPATH:await this.handleRealpath();break;case n.RENAME:await this.handleRename();break;case n.COPY_FILE:await this.handleCopyFile();break;case n.WRITE_STDOUT:this.handleWriteStdout();break;case n.WRITE_STDERR:this.handleWriteStderr();break;case n.EXIT:this.handleExit();break;case n.HTTP_REQUEST:await this.handleHttpRequest();break;case n.EXEC_COMMAND:await this.handleExecCommand();break;case n.INVOKE_TOOL:await this.handleInvokeTool();break;default:this.protocol.setErrorCode(l.IO_ERROR),this.protocol.setStatus(r.ERROR)}}catch(e){this.setErrorFromException(e)}}resolvePath(t){return this.fs.resolvePath(this.cwd,t)}async handleReadFile(){let t=this.resolvePath(this.protocol.getPath());try{let e=await this.fs.readFileBuffer(t);this.protocol.setResult(e),this.protocol.setStatus(r.SUCCESS)}catch(e){this.setErrorFromException(e)}}async handleWriteFile(){let t=this.resolvePath(this.protocol.getPath()),e=this.protocol.getData();try{await this.fs.writeFile(t,e),this.protocol.setStatus(r.SUCCESS)}catch(a){this.setErrorFromException(a)}}async handleStat(){let t=this.resolvePath(this.protocol.getPath());try{let e=await this.fs.stat(t);this.protocol.encodeStat(e),this.protocol.setStatus(r.SUCCESS)}catch(e){this.setErrorFromException(e)}}async handleLstat(){let t=this.resolvePath(this.protocol.getPath());try{let e=await this.fs.lstat(t);this.protocol.encodeStat(e),this.protocol.setStatus(r.SUCCESS)}catch(e){this.setErrorFromException(e)}}async handleReaddir(){let t=this.resolvePath(this.protocol.getPath());try{let e=await this.fs.readdir(t);this.protocol.setResultFromString(JSON.stringify(e)),this.protocol.setStatus(r.SUCCESS)}catch(e){this.setErrorFromException(e)}}async handleMkdir(){let t=this.resolvePath(this.protocol.getPath()),a=(this.protocol.getFlags()&S.MKDIR_RECURSIVE)!==0;try{await this.fs.mkdir(t,{recursive:a}),this.protocol.setStatus(r.SUCCESS)}catch(s){this.setErrorFromException(s)}}async handleRm(){let t=this.resolvePath(this.protocol.getPath()),e=this.protocol.getFlags(),a=(e&S.RECURSIVE)!==0,s=(e&S.FORCE)!==0;try{await this.fs.rm(t,{recursive:a,force:s}),this.protocol.setStatus(r.SUCCESS)}catch(i){this.setErrorFromException(i)}}async handleExists(){let t=this.resolvePath(this.protocol.getPath());try{let e=await this.fs.exists(t);this.protocol.setResult(new Uint8Array([e?1:0])),this.protocol.setStatus(r.SUCCESS)}catch(e){this.setErrorFromException(e)}}async handleAppendFile(){let t=this.resolvePath(this.protocol.getPath()),e=this.protocol.getData();try{await this.fs.appendFile(t,e),this.protocol.setStatus(r.SUCCESS)}catch(a){this.setErrorFromException(a)}}async handleSymlink(){let t=this.protocol.getPath(),e=this.protocol.getDataAsString(),a=this.resolvePath(t);try{await this.fs.symlink(e,a),this.protocol.setStatus(r.SUCCESS)}catch(s){this.setErrorFromException(s)}}async handleReadlink(){let t=this.resolvePath(this.protocol.getPath());try{let e=await this.fs.readlink(t);this.protocol.setResultFromString(e),this.protocol.setStatus(r.SUCCESS)}catch(e){this.setErrorFromException(e)}}async handleChmod(){let t=this.resolvePath(this.protocol.getPath()),e=this.protocol.getMode();try{await this.fs.chmod(t,e),this.protocol.setStatus(r.SUCCESS)}catch(a){this.setErrorFromException(a)}}async handleRealpath(){let t=this.resolvePath(this.protocol.getPath());try{let e=await this.fs.realpath(t);this.protocol.setResultFromString(e),this.protocol.setStatus(r.SUCCESS)}catch(e){this.setErrorFromException(e)}}async handleRename(){let t=this.resolvePath(this.protocol.getPath()),e=this.resolvePath(this.protocol.getDataAsString());try{await this.fs.mv(t,e),this.protocol.setStatus(r.SUCCESS)}catch(a){this.setErrorFromException(a)}}async handleCopyFile(){let t=this.resolvePath(this.protocol.getPath()),e=this.resolvePath(this.protocol.getDataAsString());try{await this.fs.cp(t,e),this.protocol.setStatus(r.SUCCESS)}catch(a){this.setErrorFromException(a)}}handleWriteStdout(){let t=this.protocol.getDataAsString();if(!this.tryAppendOutput("stdout",t)){this.outputLimitExceeded=!0,this.output.exitCode=1,this.appendOutputLimitError(),this.protocol.setErrorCode(l.IO_ERROR),this.protocol.setResultFromString("Output size limit exceeded"),this.protocol.setStatus(r.ERROR);return}this.protocol.setStatus(r.SUCCESS)}handleWriteStderr(){let t=this.protocol.getDataAsString();if(!this.tryAppendOutput("stderr",t)){this.outputLimitExceeded=!0,this.output.exitCode=1,this.appendOutputLimitError(),this.protocol.setErrorCode(l.IO_ERROR),this.protocol.setResultFromString("Output size limit exceeded"),this.protocol.setStatus(r.ERROR);return}this.protocol.setStatus(r.SUCCESS)}handleExit(){let t=this.protocol.getFlags();this.outputLimitExceeded?this.output.exitCode===0&&(this.output.exitCode=1):this.output.exitCode=t,this.protocol.setStatus(r.SUCCESS),this.running=!1}tryAppendOutput(t,e){return this.outputLimitExceeded?!1:this.maxOutputSize<=0?(t==="stdout"?this.output.stdout+=e:this.output.stderr+=e,!0):this.output.stdout.length+this.output.stderr.length+e.length>this.maxOutputSize?!1:(t==="stdout"?this.output.stdout+=e:this.output.stderr+=e,!0)}appendOutputLimitError(){if(this.maxOutputSize<=0)return;let t=`${this.commandName}: total output size exceeded (>${this.maxOutputSize} bytes), increase executionLimits.maxOutputSize
|
|
10
|
+
`,e=t.length>this.maxOutputSize?t.slice(0,this.maxOutputSize):t;if(this.output.stderr.includes("total output size exceeded"))return;let s=this.output.stdout.length+this.output.stderr.length+e.length-this.maxOutputSize;if(s>0)if(this.output.stdout.length>=s)this.output.stdout=this.output.stdout.slice(0,this.output.stdout.length-s);else{let i=s-this.output.stdout.length;this.output.stdout="",i>=this.output.stderr.length?this.output.stderr="":this.output.stderr=this.output.stderr.slice(0,this.output.stderr.length-i)}this.output.stderr+=e}async handleHttpRequest(){let t=this.secureFetch;if(!t){this.protocol.setErrorCode(l.NETWORK_NOT_CONFIGURED),this.protocol.setResultFromString("Network access not configured. Enable network in Bash options."),this.protocol.setStatus(r.ERROR);return}let e=this.protocol.getPath(),a=this.protocol.getDataAsString();try{let s=a?JSON.parse(a):{},i=this.remainingMs(),c=await this.raceDeadline(()=>t(e,{method:s.method,headers:s.headers,body:s.body,timeoutMs:i})),d=JSON.stringify({status:c.status,statusText:c.statusText,headers:c.headers,body:c.body,url:c.url});this.protocol.setResultFromString(d),this.protocol.setStatus(r.SUCCESS)}catch(s){let i=m(s instanceof Error?s.message:String(s));this.protocol.setErrorCode(l.NETWORK_ERROR),this.protocol.setResultFromString(i),this.protocol.setStatus(r.ERROR)}}async handleExecCommand(){let t=this.exec;if(!t){this.protocol.setErrorCode(l.IO_ERROR),this.protocol.setResultFromString("Command execution not available in this context."),this.protocol.setStatus(r.ERROR);return}let e=this.protocol.getPath(),a=this.protocol.getDataAsString(),s=new AbortController;try{let i={cwd:this.cwd,signal:s.signal};if(a){let E=JSON.parse(a);E.stdin&&(i.stdin=E.stdin),E.args&&Array.isArray(E.args)&&(i.args=E.args.map(D=>String(D)),e=F([e]))}let c=await this.raceDeadline(()=>t(e,i)),d=JSON.stringify({stdout:c.stdout,stderr:c.stderr,exitCode:c.exitCode});this.protocol.setResultFromString(d),this.protocol.setStatus(r.SUCCESS)}catch(i){s.abort();let c=i instanceof Error?i.message:String(i);this.protocol.setErrorCode(l.IO_ERROR),this.protocol.setResultFromString(c),this.protocol.setStatus(r.ERROR)}}async handleInvokeTool(){let t=this.invokeTool;if(!t){this.protocol.setErrorCode(l.IO_ERROR),this.protocol.setResultFromString("Tool invocation not available in this context."),this.protocol.setStatus(r.ERROR);return}let e=this.protocol.getPath(),a=this.protocol.getDataAsString();try{let s=await this.raceDeadline(()=>t(e,a));this.protocol.setResultFromString(s),this.protocol.setStatus(r.SUCCESS)}catch(s){let i=s instanceof Error?s.message:String(s);this.protocol.setErrorCode(l.IO_ERROR),this.protocol.setResultFromString(i),this.protocol.setStatus(r.ERROR)}}setErrorFromException(t){let e=t instanceof Error?t.message:String(t),a=m(e),s=l.IO_ERROR,i=e.toLowerCase();i.includes("no such file")||i.includes("not found")||i.includes("enoent")?s=l.NOT_FOUND:i.includes("is a directory")||i.includes("eisdir")?s=l.IS_DIRECTORY:i.includes("not a directory")||i.includes("enotdir")?s=l.NOT_DIRECTORY:i.includes("already exists")||i.includes("eexist")?s=l.EXISTS:(i.includes("permission")||i.includes("eperm")||i.includes("eacces"))&&(s=l.PERMISSION_DENIED),this.protocol.setErrorCode(s),this.protocol.setResultFromString(a),this.protocol.setStatus(r.ERROR)}};export{_ as a,O as b};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import{a as
|
|
1
|
+
import{a as F,b as P}from"./chunk-B6O2PIY4.js";import"./chunk-XHM67O4N.js";import"./chunk-CWQS3NFK.js";import{a as v,b as T}from"./chunk-A5O5YHGN.js";import"./chunk-IPJHKYVM.js";import{a as M}from"./chunk-OJDRYQWQ.js";import{b as B}from"./chunk-5QMZ5MUS.js";import{a as O,b as S}from"./chunk-3THT3N7L.js";import{a as E}from"./chunk-44UOCSGV.js";import{b as L}from"./chunk-74CEPOFO.js";import"./chunk-DXB73IDG.js";import{AsyncLocalStorage as N}from"node:async_hooks";import{randomBytes as q}from"node:crypto";import{fileURLToPath as D}from"node:url";import{Worker as z}from"node:worker_threads";var _=1e4,I=6e4,W=new N,J=`js-exec - Sandboxed JavaScript/TypeScript runtime with Node.js-compatible APIs
|
|
2
2
|
|
|
3
3
|
Usage: js-exec [OPTIONS] [-c CODE | FILE] [ARGS...]
|
|
4
4
|
|
|
@@ -83,14 +83,14 @@ Limits:
|
|
|
83
83
|
Memory: 64 MB per execution
|
|
84
84
|
Timeout: 10 s (60 s with network; configurable via maxJsTimeoutMs)
|
|
85
85
|
Engine: QuickJS (compiled to WebAssembly)
|
|
86
|
-
`;function
|
|
86
|
+
`;function $(t){let e={code:null,scriptFile:null,showVersion:!1,scriptArgs:[],isModule:!1,stripTypes:!1};if(t.length===0)return e;for(let r=0;r<t.length;r++){let s=t[r];if(s==="-m"||s==="--module"){e.isModule=!0;continue}if(s==="--strip-types"){e.stripTypes=!0;continue}if(s==="-c")return r+1>=t.length?{stdout:"",stderr:`js-exec: option requires an argument -- 'c'
|
|
87
87
|
`,exitCode:2}:(e.code=t[r+1],e.scriptArgs=t.slice(r+2),e);if(s==="--version"||s==="-V")return e.showVersion=!0,e;if(s.startsWith("-")&&s!=="-"&&s!=="--")return{stdout:"",stderr:`js-exec: unrecognized option '${s}'
|
|
88
|
-
`,exitCode:2};if(s==="--")return r+1<t.length&&(e.scriptFile=t[r+1],e.scriptArgs=t.slice(r+2)),e;if(!s.startsWith("-"))return e.scriptFile=s,e.scriptArgs=t.slice(r+1),e}return e}var i=null,
|
|
89
|
-
`,exitCode:1}:
|
|
90
|
-
`,exitCode:
|
|
91
|
-
`,stderr:"",exitCode:0};let s,n;if(r.code!==null)s=r.code,n="-c";else if(r.scriptFile!==null){let
|
|
92
|
-
`,exitCode:2};try{s=await e.fs.readFile(
|
|
88
|
+
`,exitCode:2};if(s==="--")return r+1<t.length&&(e.scriptFile=t[r+1],e.scriptArgs=t.slice(r+2)),e;if(!s.startsWith("-"))return e.scriptFile=s,e.scriptArgs=t.slice(r+1),e}return e}var i=null,j=null,l=[],o=null,V=D(new URL("./worker.js",import.meta.url));function h(){for(;l.length>0&&l[0].canceled;)l.shift();if(o||l.length===0)return;let t=l.shift();if(!t)return;o=t,G().postMessage(o.input)}function Q(t,e){if(!t||typeof t!="object")return{success:!1,error:"Malformed worker response"};let r=t;return typeof r.protocolToken!="string"||r.protocolToken!==e?{success:!1,error:"Malformed worker response: invalid protocol token"}:typeof r.success!="boolean"?{success:!1,error:"Malformed worker response: missing success flag"}:r.success?{success:!0}:{success:!1,error:typeof r.error=="string"&&r.error.length>0?r.error:"Worker execution failed"}}function G(){if(j&&(T(j),j=null),i)return i;let t=B.runTrusted(()=>new z(V));return i=t,t.on("message",e=>{if(i===t){if(o){let r=Q(e,o.input.protocolToken);o.resolve(r),o=null}l.length>0?h():K()}}),t.on("error",e=>{if(i===t){if(o){let r=S(E(e));o.resolve({success:!1,error:r}),o=null}for(let r of l)r.resolve({success:!1,error:"Worker crashed"});l.length=0,i=null}}),t.on("exit",()=>{i===t&&(i=null,o&&(o.resolve({success:!1,error:"Worker exited unexpectedly"}),o=null),l.length>0&&h())}),t}function K(){j=v(()=>{i&&!o&&l.length===0&&(i.terminate(),i=null)},5e3)}async function X(t,e,r,s=[],n,u,a){return W.getStore()?{stdout:"",stderr:`js-exec: recursive invocation is not supported
|
|
89
|
+
`,exitCode:1}:Y(t,e,r,s,n,u,a)}async function Y(t,e,r,s=[],n,u,a){let y=F(),p=e.exec,x=p?(d,R)=>W.run(!0,()=>p(d,R)):void 0,b=new P(y,e.fs,e.cwd,"js-exec",e.fetch,e.limits?.maxOutputSize??0,x,e.executorInvokeTool),m=e.limits?.maxJsTimeoutMs??_,w=e.fetch?Math.max(m,I):m,c={protocolToken:q(16).toString("hex"),sharedBuffer:y,jsCode:t,cwd:e.cwd,env:M(e.env),args:s,scriptPath:r,bootstrapCode:n,isModule:u,stripTypes:a,timeoutMs:w,hasExecutorTools:e.executorInvokeTool!==void 0},A,U=new Promise(d=>{A=d}),g={input:c,resolve:()=>{}},H=v(()=>{if(o===g){let d=i;d&&(i=null,d.terminate()),o=null,h()}else g.canceled=!0,o||h();g.resolve({success:!1,error:`Execution timeout: exceeded ${w}ms limit`})},w);g.resolve=d=>{T(H),A(d)},l.push(g),h();let[k,C]=await Promise.all([b.run(w),U.catch(d=>({success:!1,error:S(E(d))}))]);return!C.success&&C.error?{stdout:k.stdout,stderr:`${k.stderr}js-exec: ${S(C.error)}
|
|
90
|
+
`,exitCode:k.exitCode||1}:k}async function de(t,e,r){if(W.getStore())return{result:null,error:"js-exec: recursive invocation is not supported"};let s=F(),n=new P(s,e.fs,e.cwd,"js-exec",e.fetch,e.limits?.maxOutputSize??0,void 0,r),u=e.limits?.maxJsTimeoutMs??_,a=e.fetch?Math.max(u,I):u,p={protocolToken:q(16).toString("hex"),sharedBuffer:s,jsCode:t,cwd:e.cwd,env:M(e.env),args:[],executorMode:!0,timeoutMs:a},x,b=new Promise(c=>{x=c}),m={input:p,resolve:()=>{}},w=v(()=>{if(o===m){let c=i;c&&(i=null,c.terminate()),o=null,h()}else m.canceled=!0,o||h();m.resolve({success:!1,error:`Execution timeout: exceeded ${a}ms limit`})},a);m.resolve=c=>{T(w),x(c)},l.push(m),h();let[,f]=await Promise.all([n.run(a),b.catch(c=>({success:!1,error:S(E(c))}))]);if("executorResult"in f&&f.executorResult!==void 0){let c;try{c=JSON.parse(f.executorResult)}catch{c=f.executorResult}return{result:c,logs:f.executorLogs}}return{result:null,error:f.error||"Unknown execution error",logs:"executorLogs"in f?f.executorLogs:void 0}}var fe={name:"js-exec",async execute(t,e){if(L(t))return{stdout:J,stderr:"",exitCode:0};let r=$(t);if("exitCode"in r)return r;if(r.showVersion)return{stdout:`QuickJS (quickjs-emscripten)
|
|
91
|
+
`,stderr:"",exitCode:0};let s,n;if(r.code!==null)s=r.code,n="-c";else if(r.scriptFile!==null){let p=e.fs.resolvePath(e.cwd,r.scriptFile);if(!await e.fs.exists(p))return{stdout:"",stderr:`js-exec: can't open file '${r.scriptFile}': No such file or directory
|
|
92
|
+
`,exitCode:2};try{s=await e.fs.readFile(p),n=p}catch(x){return{stdout:"",stderr:`js-exec: can't open file '${r.scriptFile}': ${O(x.message)}
|
|
93
93
|
`,exitCode:2}}}else if(e.stdin.trim())s=e.stdin,n="<stdin>";else return{stdout:"",stderr:`js-exec: no input provided (use -c CODE or provide a script file)
|
|
94
|
-
`,exitCode:2};let u=r.isModule,a=r.stripTypes;n&&n!=="-c"&&n!=="<stdin>"&&((n.endsWith(".mjs")||n.endsWith(".mts")||n.endsWith(".ts"))&&(u=!0),(n.endsWith(".ts")||n.endsWith(".mts"))&&(a=!0)),!u&&/\bawait\s+[\w([`]/.test(s)&&(u=!0);let
|
|
94
|
+
`,exitCode:2};let u=r.isModule,a=r.stripTypes;n&&n!=="-c"&&n!=="<stdin>"&&((n.endsWith(".mjs")||n.endsWith(".mts")||n.endsWith(".ts"))&&(u=!0),(n.endsWith(".ts")||n.endsWith(".mts"))&&(a=!0)),!u&&/\bawait\s+[\w([`]/.test(s)&&(u=!0);let y=e.jsBootstrapCode;return X(s,e,n,r.scriptArgs,y,u,a)}},pe={name:"node",async execute(){return{stdout:"",stderr:`node: this sandbox uses js-exec instead of node
|
|
95
95
|
|
|
96
|
-
${
|
|
96
|
+
${J}`,exitCode:1}}};export{de as executeForExecutor,fe as jsExecCommand,pe as nodeStubCommand};
|
|
@@ -1530,7 +1530,9 @@ var OpCode = {
|
|
|
1530
1530
|
// HTTP operations
|
|
1531
1531
|
HTTP_REQUEST: 200,
|
|
1532
1532
|
// Sub-shell execution
|
|
1533
|
-
EXEC_COMMAND: 300
|
|
1533
|
+
EXEC_COMMAND: 300,
|
|
1534
|
+
// Tool invocation (executor mode)
|
|
1535
|
+
INVOKE_TOOL: 400
|
|
1534
1536
|
};
|
|
1535
1537
|
var Status = {
|
|
1536
1538
|
PENDING: 0,
|
|
@@ -2003,6 +2005,18 @@ var SyncBackend = class {
|
|
|
2003
2005
|
const responseJson = new TextDecoder().decode(result.result);
|
|
2004
2006
|
return JSON.parse(responseJson);
|
|
2005
2007
|
}
|
|
2008
|
+
/**
|
|
2009
|
+
* Invoke a tool through the main thread's tool invoker (executor mode).
|
|
2010
|
+
* Returns the JSON-serialized result.
|
|
2011
|
+
*/
|
|
2012
|
+
invokeTool(path, argsJson) {
|
|
2013
|
+
const requestData = argsJson ? new TextEncoder().encode(argsJson) : void 0;
|
|
2014
|
+
const result = this.execSync(OpCode.INVOKE_TOOL, path, requestData);
|
|
2015
|
+
if (!result.success) {
|
|
2016
|
+
throw new Error(result.error || "Tool invocation failed");
|
|
2017
|
+
}
|
|
2018
|
+
return new TextDecoder().decode(result.result);
|
|
2019
|
+
}
|
|
2006
2020
|
};
|
|
2007
2021
|
|
|
2008
2022
|
// src/commands/js-exec/fetch-polyfill.ts
|
|
@@ -3813,6 +3827,26 @@ function setupContext(context, backend, input) {
|
|
|
3813
3827
|
);
|
|
3814
3828
|
context.setProp(context.global, "__execArgs", execArgsFn);
|
|
3815
3829
|
execArgsFn.dispose();
|
|
3830
|
+
if (input.hasExecutorTools || input.executorMode) {
|
|
3831
|
+
const invokeToolFn = context.newFunction(
|
|
3832
|
+
"__invokeTool",
|
|
3833
|
+
(pathHandle, argsHandle) => {
|
|
3834
|
+
const path = context.getString(pathHandle);
|
|
3835
|
+
const argsJson = context.getString(argsHandle);
|
|
3836
|
+
try {
|
|
3837
|
+
const resultJson = backend.invokeTool(path, argsJson);
|
|
3838
|
+
return context.newString(resultJson);
|
|
3839
|
+
} catch (e) {
|
|
3840
|
+
return throwError(
|
|
3841
|
+
context,
|
|
3842
|
+
e.message || "tool invocation failed"
|
|
3843
|
+
);
|
|
3844
|
+
}
|
|
3845
|
+
}
|
|
3846
|
+
);
|
|
3847
|
+
context.setProp(context.global, "__invokeTool", invokeToolFn);
|
|
3848
|
+
invokeToolFn.dispose();
|
|
3849
|
+
}
|
|
3816
3850
|
const envObj = jsToHandle(context, input.env);
|
|
3817
3851
|
context.setProp(context.global, "env", envObj);
|
|
3818
3852
|
envObj.dispose();
|
|
@@ -4070,6 +4104,78 @@ async function initializeWithDefense() {
|
|
|
4070
4104
|
]
|
|
4071
4105
|
});
|
|
4072
4106
|
}
|
|
4107
|
+
var TOOLS_ONLY_SETUP_SOURCE = `(function() {
|
|
4108
|
+
globalThis.tools = (function makeProxy(path) {
|
|
4109
|
+
return new Proxy(function(){}, {
|
|
4110
|
+
get: function(_t, prop) {
|
|
4111
|
+
if (prop === 'then' || typeof prop === 'symbol') return undefined;
|
|
4112
|
+
return makeProxy(path.concat([String(prop)]));
|
|
4113
|
+
},
|
|
4114
|
+
apply: function(_t, _this, args) {
|
|
4115
|
+
var toolPath = path.join('.');
|
|
4116
|
+
if (!toolPath) throw new Error('Tool path missing in invocation');
|
|
4117
|
+
var argsJson = args[0] !== undefined ? JSON.stringify(args[0]) : '{}';
|
|
4118
|
+
var resultJson = globalThis.__invokeTool(toolPath, argsJson);
|
|
4119
|
+
return resultJson !== undefined && resultJson !== '' ? JSON.parse(resultJson) : undefined;
|
|
4120
|
+
}
|
|
4121
|
+
});
|
|
4122
|
+
})([]);
|
|
4123
|
+
})();`;
|
|
4124
|
+
var EXECUTOR_SETUP_SOURCE = `(function() {
|
|
4125
|
+
var __logs = [];
|
|
4126
|
+
globalThis[Symbol.for('jb:executorLogs')] = __logs;
|
|
4127
|
+
|
|
4128
|
+
var _fmt = function(v) {
|
|
4129
|
+
if (typeof v === 'string') return v;
|
|
4130
|
+
try { return JSON.stringify(v); }
|
|
4131
|
+
catch(e) { return String(v); }
|
|
4132
|
+
};
|
|
4133
|
+
|
|
4134
|
+
globalThis.console = {
|
|
4135
|
+
log: function() { var a = []; for(var i=0;i<arguments.length;i++) a.push(_fmt(arguments[i])); __logs.push('[log] ' + a.join(' ')); },
|
|
4136
|
+
error: function() { var a = []; for(var i=0;i<arguments.length;i++) a.push(_fmt(arguments[i])); __logs.push('[error] ' + a.join(' ')); },
|
|
4137
|
+
warn: function() { var a = []; for(var i=0;i<arguments.length;i++) a.push(_fmt(arguments[i])); __logs.push('[warn] ' + a.join(' ')); },
|
|
4138
|
+
info: function() { var a = []; for(var i=0;i<arguments.length;i++) a.push(_fmt(arguments[i])); __logs.push('[info] ' + a.join(' ')); },
|
|
4139
|
+
debug: function() { var a = []; for(var i=0;i<arguments.length;i++) a.push(_fmt(arguments[i])); __logs.push('[debug] ' + a.join(' ')); },
|
|
4140
|
+
};
|
|
4141
|
+
|
|
4142
|
+
globalThis.tools = (function makeProxy(path) {
|
|
4143
|
+
return new Proxy(function(){}, {
|
|
4144
|
+
get: function(_t, prop) {
|
|
4145
|
+
if (prop === 'then' || typeof prop === 'symbol') return undefined;
|
|
4146
|
+
return makeProxy(path.concat([String(prop)]));
|
|
4147
|
+
},
|
|
4148
|
+
apply: function(_t, _this, args) {
|
|
4149
|
+
var toolPath = path.join('.');
|
|
4150
|
+
if (!toolPath) throw new Error('Tool path missing in invocation');
|
|
4151
|
+
var argsJson = args[0] !== undefined ? JSON.stringify(args[0]) : '{}';
|
|
4152
|
+
var resultJson = globalThis.__invokeTool(toolPath, argsJson);
|
|
4153
|
+
return resultJson !== undefined && resultJson !== '' ? JSON.parse(resultJson) : undefined;
|
|
4154
|
+
}
|
|
4155
|
+
});
|
|
4156
|
+
})([]);
|
|
4157
|
+
})();`;
|
|
4158
|
+
function readExecutorLogs(context) {
|
|
4159
|
+
const logsResult = context.evalCode(
|
|
4160
|
+
`globalThis[Symbol.for('jb:executorLogs')]`,
|
|
4161
|
+
"<read-logs>"
|
|
4162
|
+
);
|
|
4163
|
+
if (logsResult.error) {
|
|
4164
|
+
logsResult.error.dispose();
|
|
4165
|
+
return [];
|
|
4166
|
+
}
|
|
4167
|
+
const logs = context.dump(logsResult.value);
|
|
4168
|
+
logsResult.value.dispose();
|
|
4169
|
+
return Array.isArray(logs) ? logs : [];
|
|
4170
|
+
}
|
|
4171
|
+
function readPropDump(context, handle, key) {
|
|
4172
|
+
const prop = context.getProp(handle, key);
|
|
4173
|
+
try {
|
|
4174
|
+
return context.dump(prop);
|
|
4175
|
+
} finally {
|
|
4176
|
+
prop.dispose();
|
|
4177
|
+
}
|
|
4178
|
+
}
|
|
4073
4179
|
async function executeCode(input) {
|
|
4074
4180
|
const qjs = await getQuickJSModule();
|
|
4075
4181
|
const backend = new SyncBackend(input.sharedBuffer, input.timeoutMs);
|
|
@@ -4277,6 +4383,132 @@ async function executeCode(input) {
|
|
|
4277
4383
|
}
|
|
4278
4384
|
bootstrapResult.value.dispose();
|
|
4279
4385
|
}
|
|
4386
|
+
if (input.hasExecutorTools && !input.executorMode) {
|
|
4387
|
+
const toolsSetupResult = context.evalCode(
|
|
4388
|
+
TOOLS_ONLY_SETUP_SOURCE,
|
|
4389
|
+
"<tools-setup>"
|
|
4390
|
+
);
|
|
4391
|
+
if (toolsSetupResult.error) {
|
|
4392
|
+
toolsSetupResult.error.dispose();
|
|
4393
|
+
} else {
|
|
4394
|
+
toolsSetupResult.value.dispose();
|
|
4395
|
+
}
|
|
4396
|
+
}
|
|
4397
|
+
if (input.executorMode) {
|
|
4398
|
+
const executorSetupResult = context.evalCode(
|
|
4399
|
+
EXECUTOR_SETUP_SOURCE,
|
|
4400
|
+
"<executor-setup>"
|
|
4401
|
+
);
|
|
4402
|
+
if (executorSetupResult.error) {
|
|
4403
|
+
const errVal = context.dump(executorSetupResult.error);
|
|
4404
|
+
executorSetupResult.error.dispose();
|
|
4405
|
+
backend.exit(1);
|
|
4406
|
+
return {
|
|
4407
|
+
success: true,
|
|
4408
|
+
error: formatError(errVal),
|
|
4409
|
+
executorLogs: []
|
|
4410
|
+
};
|
|
4411
|
+
}
|
|
4412
|
+
executorSetupResult.value.dispose();
|
|
4413
|
+
const wrappedCode = `(async () => {
|
|
4414
|
+
${input.jsCode}
|
|
4415
|
+
})()`;
|
|
4416
|
+
const evalResult = context.evalCode(wrappedCode, "<executor>");
|
|
4417
|
+
if (evalResult.error) {
|
|
4418
|
+
const errorVal = context.dump(evalResult.error);
|
|
4419
|
+
evalResult.error.dispose();
|
|
4420
|
+
const errorMsg = formatError(errorVal);
|
|
4421
|
+
backend.exit(1);
|
|
4422
|
+
return {
|
|
4423
|
+
success: true,
|
|
4424
|
+
executorLogs: readExecutorLogs(context),
|
|
4425
|
+
error: errorMsg
|
|
4426
|
+
};
|
|
4427
|
+
}
|
|
4428
|
+
context.setProp(context.global, "__executorPromise", evalResult.value);
|
|
4429
|
+
evalResult.value.dispose();
|
|
4430
|
+
const stateResult = context.evalCode(
|
|
4431
|
+
[
|
|
4432
|
+
"(function(p){",
|
|
4433
|
+
" var s = { v: void 0, e: void 0, settled: false };",
|
|
4434
|
+
" var fmtErr = function(e) {",
|
|
4435
|
+
" if (e && typeof e === 'object') {",
|
|
4436
|
+
" var m = typeof e.message === 'string' ? e.message : '';",
|
|
4437
|
+
" var st = typeof e.stack === 'string' ? e.stack : '';",
|
|
4438
|
+
" if (m && st) return st.indexOf(m) === -1 ? m + '\\n' + st : st;",
|
|
4439
|
+
" if (m) return m; if (st) return st;",
|
|
4440
|
+
" }",
|
|
4441
|
+
" return String(e);",
|
|
4442
|
+
" };",
|
|
4443
|
+
" p.then(",
|
|
4444
|
+
" function(v){ s.v = v; s.settled = true; },",
|
|
4445
|
+
" function(e){ s.e = fmtErr(e); s.settled = true; }",
|
|
4446
|
+
" );",
|
|
4447
|
+
" return s;",
|
|
4448
|
+
"})(__executorPromise)"
|
|
4449
|
+
].join("\n"),
|
|
4450
|
+
"<state-tracker>"
|
|
4451
|
+
);
|
|
4452
|
+
if (stateResult.error) {
|
|
4453
|
+
const errorVal = context.dump(stateResult.error);
|
|
4454
|
+
stateResult.error.dispose();
|
|
4455
|
+
backend.exit(1);
|
|
4456
|
+
return {
|
|
4457
|
+
success: true,
|
|
4458
|
+
executorLogs: readExecutorLogs(context),
|
|
4459
|
+
error: formatError(errorVal)
|
|
4460
|
+
};
|
|
4461
|
+
}
|
|
4462
|
+
const stateHandle = stateResult.value;
|
|
4463
|
+
const pendingResult = runtime.executePendingJobs();
|
|
4464
|
+
if ("error" in pendingResult && pendingResult.error) {
|
|
4465
|
+
const errorVal = context.dump(pendingResult.error);
|
|
4466
|
+
pendingResult.error.dispose();
|
|
4467
|
+
const rawMsg = typeof errorVal === "object" && errorVal !== null && "message" in errorVal ? errorVal.message : String(errorVal);
|
|
4468
|
+
if (rawMsg !== "__EXIT__") {
|
|
4469
|
+
stateHandle.dispose();
|
|
4470
|
+
backend.exit(1);
|
|
4471
|
+
return {
|
|
4472
|
+
success: true,
|
|
4473
|
+
executorLogs: readExecutorLogs(context),
|
|
4474
|
+
error: formatError(errorVal)
|
|
4475
|
+
};
|
|
4476
|
+
}
|
|
4477
|
+
}
|
|
4478
|
+
const settled = readPropDump(context, stateHandle, "settled");
|
|
4479
|
+
const value = readPropDump(context, stateHandle, "v");
|
|
4480
|
+
const stateError = readPropDump(context, stateHandle, "e");
|
|
4481
|
+
stateHandle.dispose();
|
|
4482
|
+
const logs = readExecutorLogs(context);
|
|
4483
|
+
if (!settled) {
|
|
4484
|
+
backend.exit(0);
|
|
4485
|
+
return {
|
|
4486
|
+
success: true,
|
|
4487
|
+
executorLogs: logs,
|
|
4488
|
+
error: "Execution did not settle"
|
|
4489
|
+
};
|
|
4490
|
+
}
|
|
4491
|
+
if (typeof stateError !== "undefined") {
|
|
4492
|
+
backend.exit(0);
|
|
4493
|
+
return {
|
|
4494
|
+
success: true,
|
|
4495
|
+
executorLogs: logs,
|
|
4496
|
+
error: String(stateError)
|
|
4497
|
+
};
|
|
4498
|
+
}
|
|
4499
|
+
let resultJson;
|
|
4500
|
+
try {
|
|
4501
|
+
resultJson = value !== void 0 ? JSON.stringify(value) : void 0;
|
|
4502
|
+
} catch {
|
|
4503
|
+
resultJson = void 0;
|
|
4504
|
+
}
|
|
4505
|
+
backend.exit(0);
|
|
4506
|
+
return {
|
|
4507
|
+
success: true,
|
|
4508
|
+
executorResult: resultJson,
|
|
4509
|
+
executorLogs: logs
|
|
4510
|
+
};
|
|
4511
|
+
}
|
|
4280
4512
|
const filename = input.scriptPath || "<eval>";
|
|
4281
4513
|
let jsCode = input.jsCode;
|
|
4282
4514
|
if (input.stripTypes) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import{a as _,b as O}from"./chunk-
|
|
1
|
+
import{a as _,b as O}from"./chunk-B6O2PIY4.js";import"./chunk-XHM67O4N.js";import"./chunk-CWQS3NFK.js";import{a as v,b as T}from"./chunk-A5O5YHGN.js";import"./chunk-IPJHKYVM.js";import{a as D}from"./chunk-OJDRYQWQ.js";import{c as y}from"./chunk-24IMIIXA.js";import{b as C}from"./chunk-5QMZ5MUS.js";import{a as M,b as c}from"./chunk-3THT3N7L.js";import{a as E}from"./chunk-44UOCSGV.js";import{a as b,b as P}from"./chunk-74CEPOFO.js";import"./chunk-DXB73IDG.js";import{randomBytes as $}from"node:crypto";import{fileURLToPath as Q}from"node:url";import{Worker as R}from"node:worker_threads";var U=1e4,H=6e4,I={name:"python3",summary:"Execute Python code via CPython Emscripten",usage:"python3 [OPTIONS] [-c CODE | -m MODULE | FILE] [ARGS...]",description:["Execute Python code using CPython compiled to WebAssembly via Emscripten.","","This command runs Python in an isolated environment with access to","the virtual filesystem. Standard library modules are available."],options:["-c CODE Execute CODE as Python script","-m MODULE Run library module as a script","--version Show Python version","--help Show this help"],examples:['python3 -c "print(1 + 2)"','python3 -c "import sys; print(sys.version)"',"python3 script.py","python3 script.py arg1 arg2",`echo 'print("hello")' | python3`],notes:["CPython runs in WebAssembly, so execution may be slower than native Python.","Standard library modules are available (no pip install).","Maximum execution time is 30 seconds by default."]};function L(r){let e={code:null,module:null,scriptFile:null,showVersion:!1,scriptArgs:[]};if(r.length===0)return e;let t=r.findIndex(o=>!o.startsWith("-")||o==="-"||o==="--");for(let o=0;o<(t===-1?r.length:t);o++){let s=r[o];if(s==="-c")return o+1>=r.length?{stdout:"",stderr:`python3: option requires an argument -- 'c'
|
|
2
2
|
`,exitCode:2}:(e.code=r[o+1],e.scriptArgs=r.slice(o+2),e);if(s==="-m")return o+1>=r.length?{stdout:"",stderr:`python3: option requires an argument -- 'm'
|
|
3
3
|
`,exitCode:2}:(e.module=r[o+1],e.scriptArgs=r.slice(o+2),e);if(s==="--version"||s==="-V")return e.showVersion=!0,e;if(s.startsWith("-")&&s!=="-")return{stdout:"",stderr:`python3: unrecognized option '${s}'
|
|
4
4
|
`,exitCode:2}}if(t!==-1){let o=r[t];o==="--"?t+1<r.length&&(e.scriptFile=r[t+1],e.scriptArgs=r.slice(t+2)):(e.scriptFile=o,e.scriptArgs=r.slice(t+1))}return e}var k=new WeakMap;function z(r){let e=k.get(r);return e||(e={executionQueue:[],isExecuting:!1},k.set(r,e)),e}function ie(){k=new WeakMap}var N=Q(new URL("./worker.js",import.meta.url));function B(){return $(16).toString("hex")}function V(r,e){if(!r||typeof r!="object")return{success:!1,error:"Malformed worker response"};let t=r;return typeof t.protocolToken!="string"||t.protocolToken!==e?{success:!1,error:"Malformed worker response: invalid protocol token"}:t.type==="security-violation"?{success:!1,error:`Security violation: ${typeof t.violation?.type=="string"?t.violation.type:"unknown"}`}:typeof t.success!="boolean"?{success:!1,error:"Malformed worker response: missing success flag"}:t.success?{success:!0}:{success:!1,error:typeof t.error=="string"&&t.error.length>0?t.error:"Worker execution failed"}}function a(r){if(r.isExecuting||r.executionQueue.length===0)return;for(;r.executionQueue.length>0&&r.executionQueue[0].canceled;)r.executionQueue.shift();if(r.executionQueue.length===0)return;let e=r.executionQueue.shift();if(!e)return;r.isExecuting=!0;let t;try{t=C.runTrusted(()=>new R(N,{workerData:e.input}))}catch(n){let i=n instanceof Error?n.message:String(n);e.resolve({success:!1,error:c(i)}),r.isExecuting=!1,a(r);return}e.workerRef&&(e.workerRef.current=t);let o=y(e.requireDefenseContext,"python3","worker message callback",n=>{e.resolve(V(n,e.input.protocolToken)),r.isExecuting=!1,t.terminate(),a(r)}),s=y(e.requireDefenseContext,"python3","worker error callback",n=>{let i=c(E(n));e.resolve({success:!1,error:i}),r.isExecuting=!1,a(r)}),p=y(e.requireDefenseContext,"python3","worker exit callback",()=>{r.isExecuting&&(e.resolve({success:!1,error:"Worker exited unexpectedly"}),r.isExecuting=!1,a(r))}),f=n=>{try{o(n)}catch(i){let m=i instanceof Error?i.message:String(i);e.resolve({success:!1,error:c(m)}),r.isExecuting=!1,t.terminate(),a(r)}},l=n=>{try{s(n)}catch(i){let m=i instanceof Error?i.message:String(i);e.resolve({success:!1,error:c(m)}),r.isExecuting=!1,a(r)}},g=()=>{try{p()}catch(n){let i=n instanceof Error?n.message:String(n);e.resolve({success:!1,error:c(i)}),r.isExecuting=!1,a(r)}};t.on("message",f),t.on("error",l),t.on("exit",g)}async function j(r,e,t,o=[]){let s=_(),p=new O(s,e.fs,e.cwd,"python3",e.fetch,e.limits?.maxOutputSize??0),f=e.limits?.maxPythonTimeoutMs??U,l=e.fetch?Math.max(f,H):f,g=z(e.fs),n={protocolToken:B(),sharedBuffer:s,pythonCode:r,cwd:e.cwd,env:D(e.env),args:o,scriptPath:t,timeoutMs:l},i={current:null},m=new Promise(u=>{let d={input:n,resolve:()=>{},workerRef:i,requireDefenseContext:e.requireDefenseContext},A=y(e.requireDefenseContext,"python3","worker timeout callback",()=>{i.current?i.current.terminate():d.canceled=!0,u({success:!1,error:`Execution timeout: exceeded ${l}ms limit`})}),F=v(()=>{try{A()}catch(h){let W=h instanceof Error?h.message:String(h);u({success:!1,error:c(W)})}},l);d.resolve=h=>{T(F),u(h)},g.executionQueue.push(d),a(g)}),[x,w]=await Promise.all([p.run(l).catch(u=>({stdout:"",stderr:`python3: bridge error: ${c(E(u))}
|
|
@@ -1569,7 +1569,9 @@ var OpCode = {
|
|
|
1569
1569
|
// HTTP operations
|
|
1570
1570
|
HTTP_REQUEST: 200,
|
|
1571
1571
|
// Sub-shell execution
|
|
1572
|
-
EXEC_COMMAND: 300
|
|
1572
|
+
EXEC_COMMAND: 300,
|
|
1573
|
+
// Tool invocation (executor mode)
|
|
1574
|
+
INVOKE_TOOL: 400
|
|
1573
1575
|
};
|
|
1574
1576
|
var Status = {
|
|
1575
1577
|
PENDING: 0,
|
|
@@ -2042,6 +2044,18 @@ var SyncBackend = class {
|
|
|
2042
2044
|
const responseJson = new TextDecoder().decode(result.result);
|
|
2043
2045
|
return JSON.parse(responseJson);
|
|
2044
2046
|
}
|
|
2047
|
+
/**
|
|
2048
|
+
* Invoke a tool through the main thread's tool invoker (executor mode).
|
|
2049
|
+
* Returns the JSON-serialized result.
|
|
2050
|
+
*/
|
|
2051
|
+
invokeTool(path, argsJson) {
|
|
2052
|
+
const requestData = argsJson ? new TextEncoder().encode(argsJson) : void 0;
|
|
2053
|
+
const result = this.execSync(OpCode.INVOKE_TOOL, path, requestData);
|
|
2054
|
+
if (!result.success) {
|
|
2055
|
+
throw new Error(result.error || "Tool invocation failed");
|
|
2056
|
+
}
|
|
2057
|
+
return new TextDecoder().decode(result.result);
|
|
2058
|
+
}
|
|
2045
2059
|
};
|
|
2046
2060
|
|
|
2047
2061
|
// src/commands/python3/worker.ts
|