Files
QR-master/.output/server/node_modules/just-bash/dist/bundle/chunks/python3-JPCH6LLN.js
2026-07-15 18:08:51 +02:00

13 lines
6.7 KiB
JavaScript

import{createRequire} from"node:module";const require=createRequire(import.meta.url);
import{a as O,b as A}from"./chunk-AX2X3SZS.js";import"./chunk-WNH3HOQA.js";import"./chunk-HBVMHTO5.js";import{a as T,b}from"./chunk-SY4HAUHP.js";import"./chunk-L2UW7DWF.js";import{a as _}from"./chunk-DLL7UR66.js";import{c as y}from"./chunk-KAPG5ONA.js";import{b as v}from"./chunk-VACKIICN.js";import{c as E}from"./chunk-XEKXHOPO.js";import{a as D,b as c}from"./chunk-FOCWZZDE.js";import{a as w}from"./chunk-W5DWRFSU.js";import{a as P,b as M}from"./chunk-QAYAQNCG.js";import"./chunk-BZUGFHVS.js";import{randomBytes as Q}from"node:crypto";import{fileURLToPath as R}from"node:url";import{Worker as U}from"node:worker_threads";var H=1e4,I=6e4,L={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 z(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'
`,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'
`,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}'
`,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 C=new WeakMap;function N(r){let e=C.get(r);return e||(e={executionQueue:[],isExecuting:!1},C.set(r,e)),e}function ue(){C=new WeakMap}var B=R(new URL("./worker.js",import.meta.url));function V(){return Q(16).toString("hex")}function j(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=v.runTrusted(()=>new U(B,{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(j(n,e.input.protocolToken)),r.isExecuting=!1,t.terminate(),a(r)}),s=y(e.requireDefenseContext,"python3","worker error callback",n=>{let i=c(w(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 Y(r,e,t,o=[]){let s=O(),p=new A(s,e.fs,e.cwd,"python3",e.fetch,e.limits?.maxOutputSize??0),f=e.limits?.maxPythonTimeoutMs??H,l=e.fetch?Math.max(f,I):f,g=N(e.fs),n={protocolToken:V(),sharedBuffer:s,pythonCode:r,cwd:e.cwd,env:_(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},F=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`})}),W=T(()=>{try{F()}catch(h){let $=h instanceof Error?h.message:String(h);u({success:!1,error:c($)})}},l);d.resolve=h=>{b(W),u(h)},g.executionQueue.push(d),a(g)}),[x,k]=await Promise.all([p.run(l).catch(u=>({stdout:"",stderr:`python3: bridge error: ${c(w(u))}
`,exitCode:1})),m.catch(u=>({success:!1,error:c(w(u))}))]);if(!k.success&&k.error){let u=c(k.error);return{stdout:x.stdout,stderr:`${x.stderr}python3: ${u}
`,exitCode:x.exitCode||1}}return{...x}}var Z={name:"python3",async execute(r,e){if(M(r))return P(L);let t=z(r);if("exitCode"in t)return t;if(t.showVersion)return{stdout:`Python 3.13.2 (Emscripten)
`,stderr:"",exitCode:0};let o,s;if(t.code!==null)o=t.code,s="-c";else if(t.module!==null){if(!/^[a-zA-Z_][a-zA-Z0-9_.]*$/.test(t.module))return{stdout:"",stderr:`python3: No module named '${t.module.slice(0,200)}'
`,exitCode:1};o=`import runpy; runpy.run_module('${t.module}', run_name='__main__')`,s=t.module}else if(t.scriptFile==="-")o=E(e.stdin),s="-";else if(t.scriptFile!==null){let p=e.fs.resolvePath(e.cwd,t.scriptFile);if(!await e.fs.exists(p))return{stdout:"",stderr:`python3: can't open file '${t.scriptFile}': [Errno 2] No such file or directory
`,exitCode:2};try{o=await e.fs.readFile(p),s=t.scriptFile}catch(f){let l=D(f.message);return{stdout:"",stderr:`python3: can't open file '${t.scriptFile}': ${l}
`,exitCode:2}}}else if(E(e.stdin).trim())o=E(e.stdin),s="<stdin>";else return{stdout:"",stderr:`python3: no input provided (use -c CODE, -m MODULE, or provide a script file)
`,exitCode:2};return Y(o,e,s,t.scriptArgs)}},le={name:"python",async execute(r,e){return Z.execute(r,e)}};export{ue as _resetExecutionQueue,Z as python3Command,le as pythonCommand};