import"./chunk-R6VWJ2ZL-BVeTwIsz.mjs";import"./chunk-CWQS3NFK-BkE4vqrf.mjs";import"./chunk-DXB73IDG-F_tmy-Lz.mjs";import{t as e}from"./chunk-5QMZ5MUS-BwpNU33k.mjs";import{n as t,t as n}from"./chunk-A5O5YHGN-DApqA6Ow.mjs";import{r}from"./chunk-OJDRYQWQ-CmrD0Dg2.mjs";import{n as i,t as a}from"./chunk-3THT3N7L-B1WzJUh4.mjs";import{t as o}from"./chunk-44UOCSGV-BZ6LfyW2.mjs";import{t as s}from"./chunk-74CEPOFO-V0JXh91S.mjs";import{n as c,t as l}from"./chunk-I57KEIP3-DSoF8c_0.mjs";import{fileURLToPath as u}from"node:url";import{randomBytes as d}from"node:crypto";import{AsyncLocalStorage as f}from"node:async_hooks";import{Worker as p}from"node:worker_threads";var m=1e4,h=6e4,g=new f,_=`js-exec - Sandboxed JavaScript/TypeScript runtime with Node.js-compatible APIs

Usage: js-exec [OPTIONS] [-c CODE | FILE] [ARGS...]

Options:
  -c CODE          Execute inline code
  -m, --module     Enable ES module mode (import/export)
  --strip-types    Strip TypeScript type annotations
  --version, -V    Show version
  --help           Show this help

Examples:
  js-exec -c "console.log(1 + 2)"
  js-exec script.js
  js-exec app.ts
  echo 'console.log("hello")' | js-exec

File Extension Auto-Detection:
  .js              script mode (module mode if top-level await detected)
  .mjs             ES module mode
  .ts, .mts        ES module mode + TypeScript stripping

Node.js Compatibility:
  Code written for Node.js largely works here. Both require and import
  are supported, the node: prefix works, and standard globals like process,
  console, and fetch are available. All I/O is synchronous.

  Available modules:
    fs, path, child_process, process, console,
    os, url, assert, util, events, buffer, stream,
    string_decoder, querystring

  fs (global, require('fs'), or import from 'node:fs'):
    readFileSync, writeFileSync, appendFileSync, copyFileSync, renameSync
    readdirSync, mkdirSync, rmSync, unlinkSync, rmdirSync
    statSync, lstatSync, existsSync, realpathSync, chmodSync
    symlinkSync, readlinkSync, readFileBuffer
    fs.promises.readFile, fs.promises.writeFile, fs.promises.access, ...

  path: join, resolve, dirname, basename, extname, normalize,
    relative, isAbsolute, parse, format, sep, delimiter

  child_process:
    execSync(cmd)       throws on non-zero exit, returns stdout
    spawnSync(cmd, args) returns { stdout, stderr, status }

  process (also global): argv, cwd(), exit(), env, platform, arch,
    version, versions

  os: platform(), arch(), homedir(), tmpdir(), type(), hostname(),
    EOL, cpus(), endianness()

  url: URL, URLSearchParams, parse(), format()

  assert: ok(), equal(), strictEqual(), deepEqual(), throws(),
    doesNotThrow(), fail()

  util: format(), inspect(), promisify(), types, inherits()

  events: EventEmitter (on, once, emit, off, removeListener, ...)

  buffer: Buffer.from(), Buffer.alloc(), Buffer.concat(),
    Buffer.isBuffer(), toString(), slice(), equals()

  stream: Readable, Writable, Duplex, Transform, PassThrough, pipeline

  string_decoder: StringDecoder (write, end)

  querystring: parse(), stringify(), escape(), unescape()

Other Globals:
  console            log (stdout), error/warn (stderr)
  fetch(url, opts)   HTTP; returns Promise<Response> (Web Fetch API)
  URL, URLSearchParams, Headers, Request, Response
  Buffer             Buffer.from(), Buffer.alloc(), etc.

Not Available:
  http, https, net, tls, crypto, zlib, dns, cluster, worker_threads,
  vm, v8, readline, and other Node.js built-in modules that require
  native bindings. Use fetch() for HTTP requests.

Limits:
  Memory: 64 MB per execution
  Timeout: 10 s (60 s with network; configurable via maxJsTimeoutMs)
  Engine: QuickJS (compiled to WebAssembly)
`;function v(e){let t={code:null,scriptFile:null,showVersion:!1,scriptArgs:[],isModule:!1,stripTypes:!1};if(e.length===0)return t;for(let n=0;n<e.length;n++){let r=e[n];if(r===`-m`||r===`--module`){t.isModule=!0;continue}if(r===`--strip-types`){t.stripTypes=!0;continue}if(r===`-c`)return n+1>=e.length?{stdout:``,stderr:`js-exec: option requires an argument -- 'c'
`,exitCode:2}:(t.code=e[n+1],t.scriptArgs=e.slice(n+2),t);if(r===`--version`||r===`-V`)return t.showVersion=!0,t;if(r.startsWith(`-`)&&r!==`-`&&r!==`--`)return{stdout:``,stderr:`js-exec: unrecognized option '${r}'
`,exitCode:2};if(r===`--`)return n+1<e.length&&(t.scriptFile=e[n+1],t.scriptArgs=e.slice(n+2)),t;if(!r.startsWith(`-`))return t.scriptFile=r,t.scriptArgs=e.slice(n+1),t}return t}var y=null,b=null,x=[],S=null,C=u(new URL(`./worker.js`,import.meta.url));function w(){for(;x.length>0&&x[0].canceled;)x.shift();if(S||x.length===0)return;let e=x.shift();e&&(S=e,E().postMessage(S.input))}function T(e,t){if(!e||typeof e!=`object`)return{success:!1,error:`Malformed worker response`};let n=e;return typeof n.protocolToken!=`string`||n.protocolToken!==t?{success:!1,error:`Malformed worker response: invalid protocol token`}:typeof n.success==`boolean`?n.success?{success:!0}:{success:!1,error:typeof n.error==`string`&&n.error.length>0?n.error:`Worker execution failed`}:{success:!1,error:`Malformed worker response: missing success flag`}}function E(){if(b&&=(t(b),null),y)return y;let n=e.runTrusted(()=>new p(C));return y=n,n.on(`message`,e=>{if(y===n){if(S){let t=T(e,S.input.protocolToken);S.resolve(t),S=null}x.length>0?w():D()}}),n.on(`error`,e=>{if(y===n){if(S){let t=a(o(e));S.resolve({success:!1,error:t}),S=null}for(let e of x)e.resolve({success:!1,error:`Worker crashed`});x.length=0,y=null}}),n.on(`exit`,()=>{y===n&&(y=null,S&&=(S.resolve({success:!1,error:`Worker exited unexpectedly`}),null),x.length>0&&w())}),n}function D(){b=n(()=>{y&&!S&&x.length===0&&(y.terminate(),y=null)},5e3)}async function O(e,t,n,r=[],i,a,o){return g.getStore()?{stdout:``,stderr:`js-exec: recursive invocation is not supported
`,exitCode:1}:k(e,t,n,r,i,a,o)}async function k(e,i,s,u=[],f,p,_){let v=c(),b=i.exec,C=b?(e,t)=>g.run(!0,()=>b(e,t)):void 0,T=new l(v,i.fs,i.cwd,`js-exec`,i.fetch,i.limits?.maxOutputSize??0,C),E=i.limits?.maxJsTimeoutMs??m,D=i.fetch?Math.max(E,h):E,O={protocolToken:d(16).toString(`hex`),sharedBuffer:v,jsCode:e,cwd:i.cwd,env:r(i.env),args:u,scriptPath:s,bootstrapCode:f,isModule:p,stripTypes:_,timeoutMs:D},k,A=new Promise(e=>{k=e}),j={input:O,resolve:()=>{}},M=n(()=>{if(S===j){let e=y;e&&(y=null,e.terminate()),S=null,w()}else j.canceled=!0,S||w();j.resolve({success:!1,error:`Execution timeout: exceeded ${D}ms limit`})},D);j.resolve=e=>{t(M),k(e)},x.push(j),w();let[N,P]=await Promise.all([T.run(D),A.catch(e=>({success:!1,error:a(o(e))}))]);return!P.success&&P.error?{stdout:N.stdout,stderr:`${N.stderr}js-exec: ${a(P.error)}
`,exitCode:N.exitCode||1}:N}var A={name:`js-exec`,async execute(e,t){if(s(e))return{stdout:_,stderr:``,exitCode:0};let n=v(e);if(`exitCode`in n)return n;if(n.showVersion)return{stdout:`QuickJS (quickjs-emscripten)
`,stderr:``,exitCode:0};let r,a;if(n.code!==null)r=n.code,a=`-c`;else if(n.scriptFile!==null){let e=t.fs.resolvePath(t.cwd,n.scriptFile);if(!await t.fs.exists(e))return{stdout:``,stderr:`js-exec: can't open file '${n.scriptFile}': No such file or directory
`,exitCode:2};try{r=await t.fs.readFile(e),a=e}catch(e){return{stdout:``,stderr:`js-exec: can't open file '${n.scriptFile}': ${i(e.message)}
`,exitCode:2}}}else if(t.stdin.trim())r=t.stdin,a=`<stdin>`;else return{stdout:``,stderr:`js-exec: no input provided (use -c CODE or provide a script file)
`,exitCode:2};let o=n.isModule,c=n.stripTypes;a&&a!==`-c`&&a!==`<stdin>`&&((a.endsWith(`.mjs`)||a.endsWith(`.mts`)||a.endsWith(`.ts`))&&(o=!0),(a.endsWith(`.ts`)||a.endsWith(`.mts`))&&(c=!0)),!o&&/\bawait\s+[\w([`]/.test(r)&&(o=!0);let l=t.jsBootstrapCode;return O(r,t,a,n.scriptArgs,l,o,c)}},j={name:`node`,async execute(){return{stdout:``,stderr:`node: this sandbox uses js-exec instead of node

${_}`,exitCode:1}}};export{A as jsExecCommand,j as nodeStubCommand};
//# sourceMappingURL=js-exec-M6UR76J5-Cu6NIWHE.mjs.map