brsh 3.10.1 → 3.10.2

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/shell.min.js CHANGED
@@ -2,7 +2,7 @@
2
2
  `&&r++,r++;continue}if(i===127||i===8){n.push(`Backspace`),r++;continue}if(i===9){n.push(`Tab`),r++;continue}if(i>=1&&i<=26){n.push(`^`+String.fromCharCode(i+64)),r++;continue}n.push(e),r++}return n}var a=class extends r{constructor(e={}){super(),this.isTTY=e.isTTY!==!1,this.readable=!0,this.writable=!0,this.columns=e.columns||80,this.rows=e.rows||24,this.isRaw=!1,this._encoding=null,this._onWrite=e.onWrite||null}write(e,t,n){if(e==null)return!0;typeof this._onWrite==`function`&&this._onWrite(e),this._isPushing=!0;try{this.emit(`data`,this._formatChunk(e))}finally{this._isPushing=!1}return typeof t==`function`?t():typeof n==`function`&&n(),!0}pushData(e){if(e!=null){this._isPushing=!0;try{this.emit(`data`,this._formatChunk(e))}finally{this._isPushing=!1}}}_formatChunk(e){return this._encoding?typeof e==`string`?e:e instanceof Uint8Array||v!==void 0&&v.isBuffer(e)?new TextDecoder().decode(e):String(e):e}setRawMode(e){return this.isRaw=!!e,this}setEncoding(e){return this._encoding=e,this}getWindowSize(){return[this.columns,this.rows]}resize(e,t){this.columns=e,this.rows=t,this.emit(`resize`)}resume(){return this}pause(){return this}pipe(e){return this.on(`data`,t=>e.write(t)),e}unpipe(){return this}end(e,t,n){return e!=null&&this.write(e,typeof t==`string`?t:void 0),this.emit(`end`),typeof t==`function`?t():typeof n==`function`&&n(),this}destroy(){this.removeAllListeners()}toString(){return``}valueOf(){return``}};function o(e){if(!e||e._managesOwnStdinCapture||!e.stdinStream||typeof e.stdinStream.listenerCount!=`function`)return!1;let t=e.stdinStream.listenerCount(`data`);return t===0?!1:e._onStdinStreamData&&typeof e.stdinStream.listeners==`function`?t>+!!e.stdinStream.listeners(`data`).includes(e._onStdinStreamData):t>0}t.exports={TtyStream:a,parseVtKeys:i,hasCustomStdinListener:o}})),w=t(((e,t)=>{var n=b(),r=S(),{TtyStream:i,parseVtKeys:a}=C(),o=n.EventEmitter||n.default||n;t.exports=class extends o{constructor(e,t){super(),this.arguments=e,this.result=new r,this.context=t||null;let n=this.context&&typeof this.context.getVar==`function`&&parseInt(this.context.getVar(`COLUMNS`),10)||80,o=this.context&&typeof this.context.getVar==`function`&&parseInt(this.context.getVar(`LINES`),10)||24;this.stdinStream=new i({columns:n,rows:o}),this.stdin=this.stdinStream,this.stdout=new i({columns:n,rows:o,onWrite:e=>this.emit(`stdout`,e)}),this.stderr=new i({columns:n,rows:o,onWrite:e=>this.emit(`stderr`,e)}),this._onStdinStreamData=e=>{if(!this.captureInput)return;let t=a(e);for(let e of t){if(e===`^C`&&!(this.stdinStream&&this.stdinStream.isRaw)){this.kill(`SIGINT`);return}!this.interactive&&!this.stdinStream.isRaw&&(e===`Enter`?this.stdout.write(`\r
3
3
  `):e===`Backspace`?this.stdout.write(`\b \b`):e.length===1&&this.stdout.write(e)),this.onInput(e)}},this.stdinStream.on(`data`,this._onStdinStreamData)}get name(){return`unnamed`}run(){return Promise.reject(`Command not implemented...`)}kill(e=`SIGINT`){let t=typeof e==`number`?e:{SIGHUP:129,SIGINT:130,SIGQUIT:131,SIGKILL:137,SIGTERM:143,SIGWINCH:156}[e]||130;this.exitCode=t,this.emit(`signal`,e),(e===`SIGINT`||e===`SIGTERM`||e===`SIGKILL`||e===`SIGQUIT`)&&(this.emit(`abort`),this._abortController&&this._abortController.abort())}runCommand(){return new Promise(e=>{let t=!1,n=()=>{t||(t=!0,e(this.result))};this.once(`abort`,n),Promise.resolve().then(()=>this.run()).then(r=>{t||(t=!0,this.removeListener(`abort`,n),r&&r.split(/\r?\n/).forEach(e=>this.stdOut=e),e(this.result))}).catch(r=>{if(!t){if(t=!0,this.removeListener(`abort`,n),r&&r.__type)throw r;r&&(this.stdErr=r.message||r),this.result.getExitCode()===0&&(this.exitCode=127),e(this.result)}})})}onInput(e){}set exitCode(e){this.result.exitCode=e}set stdOut(e){this.result.addStdOutLine(e)}set stdErr(e){this.result.addStdErrLine(e)}flush(){this.emit(`flush`,this.result.flush())}}})),T=t(((e,t)=>{var n=w();t.exports=class extends n{constructor(e,t){super(e,t)}}})),E=t(((e,t)=>{var n=T(),{parseVtKeys:r,hasCustomStdinListener:i}=C();function a(e){return/^#!\/(sh\.js|bin\/(sh|brsh))/.test(e)}t.exports=class extends n{constructor(e,t,n,r=!1){super(t,n),this.script=e,this.source=r}get captureInput(){if(!this.sandbox||!this.sandbox.runningCommand)return!1;let e=this.sandbox.runningCommand;return!!(e.captureInput||e.interactive||e._inAltScreen||i(e))}get interactive(){return!!(this.sandbox&&this.sandbox.runningCommand&&this.sandbox.runningCommand.interactive)}get isRaw(){return!!(this.stdinStream&&this.stdinStream.isRaw||this.sandbox&&this.sandbox.runningCommand&&(this.sandbox.runningCommand.isRaw||this.sandbox.runningCommand.stdinStream&&this.sandbox.runningCommand.stdinStream.isRaw))}get _inAltScreen(){return!!(this.__inAltScreen||this.sandbox&&this.sandbox.runningCommand&&this.sandbox.runningCommand._inAltScreen)}set _inAltScreen(e){this.__inAltScreen=!!e}onInput(e){this.sandbox&&this.sandbox.onInput(e)}kill(e=`SIGINT`){this.sandbox&&this.sandbox.runningCommand&&typeof this.sandbox.runningCommand.kill==`function`&&this.sandbox.runningCommand.kill(e),this.sandbox&&typeof this.sandbox.destroy==`function`&&this.sandbox.destroy(130),super.kill(e)}run(){this._managesOwnStdinCapture=!0;let e=this.context&&this.context.fs||this.fs,t=e&&e[Symbol.for(`brsh.fs.target`)]||e,n=t&&t.isRealFilesystem?{useRealFilesystem:!0}:{filesystem:t&&t.getRaw()||{},permissions:t&&t.getRawPermissions()||{}};this.sandbox=new globalThis.shell({cwd:t&&t.cwd||`/`,...n,subshell:!0}),this.sandbox.context.source(this.context);let o=this.stdout&&this.stdout.columns||this.stdinStream&&this.stdinStream.columns||this.context&&typeof this.context.getVar==`function`&&parseInt(this.context.getVar(`COLUMNS`),10)||80,s=this.stdout&&this.stdout.rows||this.stdinStream&&this.stdinStream.rows||this.context&&typeof this.context.getVar==`function`&&parseInt(this.context.getVar(`LINES`),10)||24;typeof this.sandbox.setTerminalSize==`function`&&this.sandbox.setTerminalSize(o,s),Array.isArray(this.arguments)&&(this.arguments.forEach((e,t)=>this.sandbox.context.setVar(String(t+1),e)),this.sandbox.context.setVar(`#`,String(this.arguments.length)),this.sandbox.context.setVar(`@`,this.arguments.join(` `)),this.sandbox.context.setVar(`*`,this.arguments.join(` `))),this._onStdinStreamData&&this.stdinStream&&typeof this.stdinStream.off==`function`&&this.stdinStream.off(`data`,this._onStdinStreamData);let c=e=>{if(!this.sandbox||!this.sandbox.runningCommand)return;let t=this.sandbox.runningCommand,n=i(t);if(t.interactive||t._inAltScreen||n){t.stdinStream&&t.stdinStream.pushData(e);return}if(t.captureInput&&typeof t.onInput==`function`){let n=!!(t.isRaw||t.stdinStream&&t.stdinStream.isRaw);for(let i of r(e)){if(i===`^C`&&!n){this.kill(`SIGINT`);return}n||(i===`Enter`?this.stdout.write(`\r
4
4
  `):i===`Backspace`?this.stdout.write(`\b \b`):i.length===1&&this.stdout.write(i)),t.onInput(i)}}},l=()=>{this.sandbox&&typeof this.sandbox.setTerminalSize==`function`&&this.stdinStream&&this.sandbox.setTerminalSize(this.stdinStream.columns,this.stdinStream.rows)},u=()=>{this.sandbox&&typeof this.sandbox.setTerminalSize==`function`&&this.stdout&&this.sandbox.setTerminalSize(this.stdout.columns,this.stdout.rows)},d=()=>{this.stdinStream&&typeof this.stdinStream.off==`function`&&(this.stdinStream.off(`data`,c),this.stdinStream.off(`resize`,l)),this.stdout&&typeof this.stdout.off==`function`&&this.stdout.off(`resize`,u)};this.stdinStream&&typeof this.stdinStream.on==`function`&&(this.stdinStream.on(`data`,c),this.stdinStream.on(`resize`,l)),this.stdout&&this.stdout!==this.stdinStream&&typeof this.stdout.on==`function`&&this.stdout.on(`resize`,u),this.sandbox.on(`stdOut`,e=>{this.stdOut=e,this.flush()}),this.sandbox.on(`stdErr`,e=>{this.stdErr=e,this.flush()}),this.sandbox.stdout&&typeof this.sandbox.stdout.on==`function`&&this.sandbox.stdout.on(`data`,e=>{this.stdout&&typeof this.stdout.write==`function`&&this.stdout.write(e)}),this.sandbox.stderr&&typeof this.sandbox.stderr.on==`function`&&this.sandbox.stderr.on(`data`,e=>{this.stderr&&typeof this.stderr.write==`function`&&this.stderr.write(e)}),this._scriptFunctions={};let f=this.script.split(/\r?\n/);if(!a(f[0])){let e=f[0].startsWith(`#!`)?f[0].slice(2).split(/\s/)[0]:f[0];return d(),Promise.reject(`${e}: bad interpreter: No such file or directory`)}let p=this._parseLines(f.slice(1));return this._executeNodes(p).catch(e=>{if(e&&e.__type===`return`)this.sandbox.lastCode=e.code;else if(e&&e.__type===`errexit`)this.sandbox.lastCode=e.code;else if(e&&(e.__type===`break`||e.__type===`continue`))this.stdErr=`${e.__type}: only meaningful in a for or while loop`,this.sandbox.lastCode=1;else return Promise.reject(e)}).finally(()=>{d()}).then(()=>{this.exitCode=this.sandbox.lastCode,this.source&&this.context.source(this.sandbox.context)})}_accumulateLines(e){let t=[],n=null,r=!1,i=!1,a=!1;for(let o=0;o<e.length;o++){let s=e[o];n===null?n=s:a?n+=s:r||i?n+=`
5
- `+s:n+=s;let c=0,l=!1,u=!1;for(;c<s.length;){let e=s[c];if(r)e===`'`&&(r=!1),c++;else if(i)e===`\\`&&c+1<s.length?c+=2:e===`\\`&&c+1===s.length?(l=!0,c++):(e===`"`&&(i=!1),c++);else{if(e===`#`&&(c===0||/\s/.test(s[c-1]))){u=!0;break}e===`\\`&&c+1<s.length?c+=2:e===`\\`&&c+1===s.length?(l=!0,c++):e===`'`?(r=!0,c++):(e===`"`&&(i=!0),c++)}}if(!r&&!l&&!u){let e=s.match(/(\\+)\s*$/);e&&e[1].length%2==1&&(l=!0)}a=l,l&&(n=n.replace(/\\\s*$/,``)),!r&&!i&&!l&&(t.push(n),n=null,a=!1)}return n!==null&&t.push(n),t}_parseLines(e){e=this._accumulateLines(e);let t=[],n=0;for(;n<e.length;){let r=e[n].trim();if(!r||r.startsWith(`#`)){n++;continue}if(r.startsWith(`if `)||r===`if`){let r=this._parseIf(e,n);t.push(r.node),n+=r.consumed;continue}if(r.startsWith(`while `)){let r=this._parseWhile(e,n);t.push(r.node),n+=r.consumed;continue}if(r.startsWith(`for `)){let r=this._parseFor(e,n);t.push(r.node),n+=r.consumed;continue}if(r.startsWith(`case `)||r===`case`){let r=this._parseCase(e,n);t.push(r.node),n+=r.consumed;continue}if(/^(?:function\s+[\w\d_]+|[\w\d_]+\s*\(\s*\))\s*(?:\{.*)?$/.test(r)){let r=this._parseFunction(e,n);t.push(r.node),n+=r.consumed;continue}t.push({type:`command`,line:r}),n++}return t}_parseIf(e,t){let n=e=>e.replace(/^(?:if|elif)\s+/,``).replace(/\s*;\s*then\s*$/,``).replace(/\s+then\s*$/,``).trim(),r=[{condition:n(e[t].trim()),bodyLines:[]}],i=[],a=r[0],o=!1,s=1,c=t+1;for(c<e.length&&e[c].trim()===`then`&&c++;c<e.length;){let t=e[c].trim();if((t.startsWith(`if `)||t===`if`)&&s++,t===`fi`&&(s--,s===0)){c++;break}if(s===1&&t.startsWith(`elif `)){let i={condition:n(t),bodyLines:[]};r.push(i),a=i,c++,c<e.length&&e[c].trim()===`then`&&c++;continue}if(s===1&&t===`else`){o=!0,c++;continue}o?i.push(e[c]):a.bodyLines.push(e[c]),c++}return{node:{type:`if`,clauses:r,elseLines:i},consumed:c-t}}_parseWhile(e,t){let n=e[t].trim().replace(/^while\s+/,``).replace(/\s*;\s*do\s*$/,``).replace(/\s+do\s*$/,``).trim(),r=t+1;r<e.length&&e[r].trim()===`do`&&r++;let i=[],a=1;for(;r<e.length;){let t=e[r].trim();if((t.startsWith(`while `)||t.startsWith(`for `))&&a++,t===`done`&&(a--,a===0)){r++;break}i.push(e[r]),r++}return{node:{type:`while`,condition:n,bodyLines:i},consumed:r-t}}_parseFor(e,t){let n=e[t].trim().replace(/\s*;\s*do\s*$/,``).replace(/\s+do\s*$/,``).match(/^for\s+(\w+)\s+in\s+(.+)$/),r=n?n[1]:``,i=n?n[2].trim():``,a=t+1;a<e.length&&e[a].trim()===`do`&&a++;let o=[],s=1;for(;a<e.length;){let t=e[a].trim();if((t.startsWith(`while `)||t.startsWith(`for `))&&s++,t===`done`&&(s--,s===0)){a++;break}o.push(e[a]),a++}return{node:{type:`for`,variable:r,list:i,bodyLines:o},consumed:a-t}}async _executeNodes(e){for(let t of e)await this._executeNode(t)}_executeNode(e){if(e.type===`function`){let t=e.name,n=e.bodyLines,r=this,i=T(),a=class extends i{static get name(){return t}run(){let e=this.arguments.map((e,t)=>({key:String(t+1),prev:r.sandbox.context.getVar(String(t+1))}));this.arguments.forEach((e,t)=>r.sandbox.context.setVar(String(t+1),e));let t=()=>e.forEach(({key:e,prev:t})=>t?r.sandbox.context.setVar(e,t):r.sandbox.context.unsetVar(e));return r._executeFunctionBody(n).then(()=>{t()}).catch(e=>{if(t(),e&&e.__type===`return`){this.exitCode=e.code;return}return Promise.reject(e)})}};return this.sandbox.context.setCommand(a),Promise.resolve()}if(e.type===`command`){if(/^return(\s+\d+)?\s*$/.test(e.line.trim())){let t=e.line.trim().match(/^return\s+(\d+)/),n=t?parseInt(t[1],10):0;return Promise.reject({__type:`return`,code:n})}if(/^break(\s+\d+)?\s*$/.test(e.line.trim())){let t=e.line.trim().match(/^break\s+(\d+)/),n=t?parseInt(t[1],10):1;return Promise.reject({__type:`break`,count:n})}if(/^continue(\s+\d+)?\s*$/.test(e.line.trim())){let t=e.line.trim().match(/^continue\s+(\d+)/),n=t?parseInt(t[1],10):1;return Promise.reject({__type:`continue`,count:n})}return(this.sandbox.context.getVar(`-`)||``).includes(`x`)&&(this.stdErr=`+ ${e.line.trim()}`,this.flush()),this.sandbox.onCommand(e.line,!1).then(()=>{if((this.sandbox.context.getVar(`-`)||``).includes(`e`)&&this.sandbox.lastCode!==0)return Promise.reject({__type:`errexit`,code:this.sandbox.lastCode})})}return e.type===`if`?this._executeIf(e):e.type===`while`?this._executeWhile(e):e.type===`for`?this._executeFor(e):e.type===`case`?this._executeCase(e):Promise.resolve()}async _executeIf(e){for(let t of e.clauses)if(await this.sandbox.onCommand(t.condition,!1),this.sandbox.lastCode===0)return this._executeNodes(this._parseLines(t.bodyLines));if(e.elseLines.length>0)return this._executeNodes(this._parseLines(e.elseLines))}async _executeWhile(e){for(;await this.sandbox.onCommand(e.condition,!1),this.sandbox.lastCode===0;)try{await this._executeNodes(this._parseLines(e.bodyLines))}catch(e){if(e&&e.__type===`break`){if(e.count<=1)break;throw{__type:`break`,count:e.count-1}}if(e&&e.__type===`continue`){if(e.count<=1)continue;throw{__type:`continue`,count:e.count-1}}throw e}}async _executeFor(e){let t;if(e.list.trim()===`"$@"`){let e=parseInt(this.sandbox.context.getVar(`#`)||`0`,10);t=[];for(let n=1;n<=e;n++)t.push(this.sandbox.context.getVar(String(n))||``)}else t=(await this.sandbox._replaceVariables(e.list)).split(/\s+/).filter(Boolean);for(let n of t){this.sandbox.context.setVar(e.variable,n);try{await this._executeNodes(this._parseLines(e.bodyLines))}catch(e){if(e&&e.__type===`break`){if(e.count<=1)break;throw{__type:`break`,count:e.count-1}}if(e&&e.__type===`continue`){if(e.count<=1)continue;throw{__type:`continue`,count:e.count-1}}throw e}}}_parseFunction(e,t){let n=e[t].trim(),r=n.match(/^function\s+([\w\d_]+)/)||n.match(/^([\w\d_]+)\s*\(/),i=r?r[1]:``,a=t+1;if(!n.includes(`{`)){for(;a<e.length&&!e[a].includes(`{`);)a++;a++}let o=[],s=1;for(;a<e.length;){if(e[a].trim()===`}`){a++;break}let t=(e[a].match(/\{/g)||[]).length,n=(e[a].match(/\}/g)||[]).length;if(s+=t-n,s<=0){a++;break}o.push(e[a]),a++}return{node:{type:`function`,name:i,bodyLines:o},consumed:a-t}}_parseCase(e,t){let n=e[t].trim().match(/^case\s+(.+?)\s+in\s*$/),r=n?n[1]:``,i=t+1,a=[],o=null,s=[];for(;i<e.length;){let t=e[i],n=t.trim();if(i++,!(!n||n.startsWith(`#`))){if(n===`esac`)break;if(o===null){let e=n.match(/^(.+?)\)\s*(.*)$/);if(e){o=e[1].trim();let t=e[2].trim();if(t===`;;`)a.push({pattern:o,bodyLines:[]}),o=null;else if(t.endsWith(`;;`)){let e=t.slice(0,-2).trim();a.push({pattern:o,bodyLines:e?[e]:[]}),o=null}else s=t?[t]:[]}}else n===`;;`?(a.push({pattern:o,bodyLines:s}),o=null,s=[]):n.endsWith(`;;`)?(s.push(n.slice(0,-2).trim()),a.push({pattern:o,bodyLines:s}),o=null,s=[]):s.push(t)}}return o!==null&&a.push({pattern:o,bodyLines:s}),{node:{type:`case`,word:r,clauses:a},consumed:i-t}}_executeFunctionBody(e){return this._executeNodes(this._parseLines(e))}async _executeCase(e){let t=await this.sandbox._replaceVariables(e.word),n=t.startsWith(`"`)&&t.endsWith(`"`)||t.startsWith(`'`)&&t.endsWith(`'`)?t.slice(1,-1):t;for(let t of e.clauses)if(t.pattern.replace(/^\(\s*/,``).split(`|`).map(e=>e.trim()).some(e=>this._matchPattern(e,n)))return this._executeNodes(this._parseLines(t.bodyLines))}_matchPattern(e,t){let n=``,r=0;for(;r<e.length;){let t=e[r];if(t===`[`){let t=e.indexOf(`]`,r+1);t===-1?(n+=`\\[`,r++):(n+=e.slice(r,t+1),r=t+1)}else t===`*`?(n+=`.*`,r++):t===`?`?(n+=`.`,r++):/[.+^${}()|\\]/.test(t)?(n+=`\\`+t,r++):(n+=t,r++)}try{return RegExp(`^`+n+`$`).test(t)}catch{return!1}}},t.exports.isKnownInterpreter=a})),D=t(((e,t)=>{t.exports=null})),O=t(((e,t)=>{y();var n=E(),{BinaryFile:r}=J(),i=0,a=6,o=8,s=20,c=27,l=28,u=31,d=44,f=54,p=70,m=0,h=2,g=3,_=4,b=1,x=2,S=4,C=8,T=1,O=4,k=class extends n{constructor(e,t,n){super(e,t,n),this._ensurePkgMeta()}_ensurePkgMeta(){if(this._pkgMeta===void 0&&this.script){let e=this.context&&this.context.fs||this.fs;e&&(this._pkgMeta=ne(e,this.script))}return this._pkgMeta||null}get name(){let e=this._ensurePkgMeta();return e&&e.bin?String(e.bin).toLowerCase():this.script?this.script.split(`/`).pop().replace(/\.(wasm|wat)$/i,``).toLowerCase():`wasm`}get requiresFilesystem(){return!0}get interactive(){if((this.arguments||[]).some(e=>e===`--help`||e===`-h`||e===`--version`||e===`-V`||e===`-v`))return!1;if(this._isInteractive!==void 0)return!!this._isInteractive;let e=this._ensurePkgMeta();return!!(e&&(e.interactive||e.wasm?.interactive)||this._inspectedWasm&&this._inspectedWasm.isInteractive)}get captureInput(){return this.interactive}kill(e=`SIGINT`){if(this._killed=!0,this.exitCode=130,this._exitAltScreen(),this._asyncifyReject){try{this._asyncifyReject(Error(`Interrupted by SIGINT`))}catch{}this._asyncifyReject=null}if(this._pendingReadResolve){let e=this._pendingReadResolve;this._pendingReadResolve=null;try{e()}catch{}}super.kill(e)}_enterAltScreen(){this._inAltScreen||(this._inAltScreen=!0,this.stdinStream&&typeof this.stdinStream.setRawMode==`function`&&this.stdinStream.setRawMode(!0),this.stdout&&typeof this.stdout.write==`function`&&this.stdout.write(`\x1B[?1049h\x1B[2J\x1B[H`))}_exitAltScreen(){this._inAltScreen&&(this._inAltScreen=!1,this.stdinStream&&typeof this.stdinStream.setRawMode==`function`&&this.stdinStream.setRawMode(!1),this.stdout&&typeof this.stdout.write==`function`&&this.stdout.write(`\x1B[0m\x1B[?1l\x1B[?1000l\x1B[?1002l\x1B[?1003l\x1B[?1006l\x1B[?1049l\x1B[?25h`))}_keyToBytes(e){if(!e)return null;if(typeof e!=`string`)return e instanceof Uint8Array?e:new Uint8Array([Number(e)&255]);if([`Control`,`Alt`,`Shift`,`Meta`,`CapsLock`,`NumLock`,`ScrollLock`,`Dead`,`Unidentified`].includes(e))return null;let t=e.match(/^\^([A-Za-z@\[\\\]^_?])$/);if(t){let e=t[1].toUpperCase();return e===`?`?new Uint8Array([127]):new Uint8Array([e.charCodeAt(0)-64])}let n={Enter:`\r`,Return:`\r`,Space:` `,Spacebar:` `,Backspace:``,Tab:` `,BackTab:`\x1B[Z`,Escape:`\x1B`,Esc:`\x1B`,ArrowUp:`\x1B[A`,Up:`\x1B[A`,ArrowDown:`\x1B[B`,Down:`\x1B[B`,ArrowRight:`\x1B[C`,Right:`\x1B[C`,ArrowLeft:`\x1B[D`,Left:`\x1B[D`,Home:`\x1B[H`,End:`\x1B[F`,PageUp:`\x1B[5~`,PageDown:`\x1B[6~`,Delete:`\x1B[3~`,Del:`\x1B[3~`,Insert:`\x1B[2~`,F1:`\x1BOP`,F2:`\x1BOQ`,F3:`\x1BOR`,F4:`\x1BOS`,F5:`\x1B[15~`,F6:`\x1B[17~`,F7:`\x1B[18~`,F8:`\x1B[19~`,F9:`\x1B[20~`,F10:`\x1B[21~`,F11:`\x1B[23~`,F12:`\x1B[24~`};return n[e]?new TextEncoder().encode(n[e]):new TextEncoder().encode(e)}onInput(e){if(!this._handlingStreamData&&!(this.stdinStream&&this.stdinStream._isPushing)){if(typeof this._externalKeyHandler==`function`){this._externalKeyHandler(e);return}this.stdinStream&&this.stdinStream.isRaw}}runCommand(){return super.runCommand().finally(()=>{this._inAltScreen&&this._exitAltScreen()})}run(){let e=this.script,t=(()=>{try{return D()}catch{return null}})();return this._loadBytes(e,t).then(({bytes:e,isWat:t})=>(this.interactive&&(this.emit(`interactive`),this.context&&typeof this.context.emit==`function`&&this.context.emit(`interactive`)),t?this._runWat(e):this._runWasm(e))).catch(e=>{this.stdErr=e.message||String(e),this.exitCode=1})}_loadBytes(e,t){let n=this.context&&this.context.fs||this.fs;if(!n)return Promise.reject(Error(`${e}: Filesystem is not available`));let r;if(n.isRealFilesystem&&t)try{let i=t.readFileSync(n.absolutePath(e)),a=new Uint8Array(i);r=Promise.resolve({bytes:a,isWat:this._isWat(e,a)})}catch(t){return Promise.reject(Error(`${e}: ${t.message}`))}else{let t=n.getFileByPath(e);if(t==null)return Promise.reject(Error(`${e}: No such file or directory`));let i=this._contentToBytes(t);r=Promise.resolve({bytes:i,isWat:this._isWat(e,i)})}return r.then(e=>(!e.isWat&&e.bytes&&(this._inspectedWasm=P(e.bytes),this._inspectedWasm.isInteractive&&(this._isInteractive=!0)),e))}_isWat(e,t){return e.endsWith(`.wat`)?!0:t.length>0&&t[0]===40}_contentToBytes(e){if(e instanceof Uint8Array)return e;if(v!==void 0&&v.isBuffer(e))return new Uint8Array(e.buffer,e.byteOffset,e.byteLength);if(e instanceof ArrayBuffer)return new Uint8Array(e);let t=new Uint8Array(e.length);for(let n=0;n<e.length;n++)t[n]=e.charCodeAt(n)&255;return t}async _runWat(e){let t=new TextDecoder().decode(e),n,r;try{r=await import(`wabt`)}catch{this.stdErr=`${this.script}: WAT execution requires the 'wabt' package\nInstall it with: npm install wabt`,this.exitCode=1;return}try{let e=r.default??r,{buffer:i}=(typeof e==`function`?await e():e).parseWat(this.script,t,{mutable_globals:!0}).toBinary({});n=new Uint8Array(i)}catch(e){this.stdErr=`${this.script}: WAT compilation error: ${e.message}`,this.exitCode=1;return}return this._runWasm(n)}async _runWasm(e){if(typeof WebAssembly>`u`){this.stdErr=`${this.script}: WebAssembly is not available in this environment`,this.exitCode=1;return}let t={1:``,2:``},n=new TextDecoder,y=new TextEncoder,w=null,E=null,D=!1,k=0,A=null,j=null,M=null,ee=!1,N=!1,I=!1,te=null,ne=null,L=null,R=this.stdin===void 0||this.stdin===null||this.stdin===this.stdinStream||this.stdin&&this.stdin.isTTY,z=!!(this.interactive&&R);z&&(this.emit(`interactive`),this.context&&typeof this.context.emit==`function`&&this.context.emit(`interactive`));let B=()=>{z||!R||(z=!0,this._isInteractive=!0,this.emit(`interactive`),this.context&&typeof this.context.emit==`function`&&this.context.emit(`interactive`),this._enterAltScreen(),t[1]&&=(this.stdout&&typeof this.stdout.write==`function`&&this.stdout.write(t[1]),``),t[2]&&=(this.stderr&&typeof this.stderr.write==`function`&&this.stderr.write(t[2]),``),this.stdinStream&&typeof this.stdinStream.setRawMode==`function`&&this.stdinStream.setRawMode(!0))},V=(e,n)=>{if(z&&(e===1||e===2)){if(e===1&&this.stdout){this.stdout.write(n);return}if(e===2&&this.stderr){this.stderr.write(n);return}}t[e]=(t[e]||``)+n;let r=t[e].split(`
5
+ `+s:n+=s;let c=0,l=!1,u=!1;for(;c<s.length;){let e=s[c];if(r)e===`'`&&(r=!1),c++;else if(i)e===`\\`&&c+1<s.length?c+=2:e===`\\`&&c+1===s.length?(l=!0,c++):(e===`"`&&(i=!1),c++);else{if(e===`#`&&(c===0||/\s/.test(s[c-1]))){u=!0;break}e===`\\`&&c+1<s.length?c+=2:e===`\\`&&c+1===s.length?(l=!0,c++):e===`'`?(r=!0,c++):(e===`"`&&(i=!0),c++)}}if(!r&&!l&&!u){let e=s.match(/(\\+)\s*$/);e&&e[1].length%2==1&&(l=!0)}a=l,l&&(n=n.replace(/\\\s*$/,``)),!r&&!i&&!l&&(t.push(n),n=null,a=!1)}return n!==null&&t.push(n),t}_parseLines(e){e=this._accumulateLines(e);let t=[],n=0;for(;n<e.length;){let r=e[n].trim();if(!r||r.startsWith(`#`)){n++;continue}if(r.startsWith(`if `)||r===`if`){let r=this._parseIf(e,n);t.push(r.node),n+=r.consumed;continue}if(r.startsWith(`while `)){let r=this._parseWhile(e,n);t.push(r.node),n+=r.consumed;continue}if(r.startsWith(`for `)){let r=this._parseFor(e,n);t.push(r.node),n+=r.consumed;continue}if(r.startsWith(`case `)||r===`case`){let r=this._parseCase(e,n);t.push(r.node),n+=r.consumed;continue}if(/^(?:function\s+[\w\d_]+|[\w\d_]+\s*\(\s*\))\s*(?:\{.*)?$/.test(r)){let r=this._parseFunction(e,n);t.push(r.node),n+=r.consumed;continue}t.push({type:`command`,line:r}),n++}return t}_parseIf(e,t){let n=e=>e.replace(/^(?:if|elif)\s+/,``).replace(/\s*;\s*then\s*$/,``).replace(/\s+then\s*$/,``).trim(),r=[{condition:n(e[t].trim()),bodyLines:[]}],i=[],a=r[0],o=!1,s=1,c=t+1;for(c<e.length&&e[c].trim()===`then`&&c++;c<e.length;){let t=e[c].trim();if((t.startsWith(`if `)||t===`if`)&&s++,t===`fi`&&(s--,s===0)){c++;break}if(s===1&&t.startsWith(`elif `)){let i={condition:n(t),bodyLines:[]};r.push(i),a=i,c++,c<e.length&&e[c].trim()===`then`&&c++;continue}if(s===1&&t===`else`){o=!0,c++;continue}o?i.push(e[c]):a.bodyLines.push(e[c]),c++}return{node:{type:`if`,clauses:r,elseLines:i},consumed:c-t}}_parseWhile(e,t){let n=e[t].trim().replace(/^while\s+/,``).replace(/\s*;\s*do\s*$/,``).replace(/\s+do\s*$/,``).trim(),r=t+1;r<e.length&&e[r].trim()===`do`&&r++;let i=[],a=1;for(;r<e.length;){let t=e[r].trim();if((t.startsWith(`while `)||t.startsWith(`for `))&&a++,t===`done`&&(a--,a===0)){r++;break}i.push(e[r]),r++}return{node:{type:`while`,condition:n,bodyLines:i},consumed:r-t}}_parseFor(e,t){let n=e[t].trim().replace(/\s*;\s*do\s*$/,``).replace(/\s+do\s*$/,``).match(/^for\s+(\w+)\s+in\s+(.+)$/),r=n?n[1]:``,i=n?n[2].trim():``,a=t+1;a<e.length&&e[a].trim()===`do`&&a++;let o=[],s=1;for(;a<e.length;){let t=e[a].trim();if((t.startsWith(`while `)||t.startsWith(`for `))&&s++,t===`done`&&(s--,s===0)){a++;break}o.push(e[a]),a++}return{node:{type:`for`,variable:r,list:i,bodyLines:o},consumed:a-t}}async _executeNodes(e){for(let t of e)await this._executeNode(t)}_executeNode(e){if(e.type===`function`){let t=e.name,n=e.bodyLines,r=this,i=T(),a=class extends i{static get name(){return t}run(){let e=parseInt(r.sandbox.context.getVar(`#`)||`0`,10)||0,t=Math.max(e,this.arguments.length),i=[];for(let e=1;e<=t;e++){let t=String(e);i.push({key:t,prev:r.sandbox.context.getVar(t)}),e<=this.arguments.length?r.sandbox.context.setVar(t,this.arguments[e-1]):r.sandbox.context.unsetVar(t)}for(let e of[`#`,`@`,`*`])i.push({key:e,prev:r.sandbox.context.getVar(e)});r.sandbox.context.setVar(`#`,String(this.arguments.length)),r.sandbox.context.setVar(`@`,this.arguments.join(` `)),r.sandbox.context.setVar(`*`,this.arguments.join(` `));let a=()=>i.forEach(({key:e,prev:t})=>t?r.sandbox.context.setVar(e,t):r.sandbox.context.unsetVar(e));return r._executeFunctionBody(n).then(()=>{a()}).catch(e=>{if(a(),e&&e.__type===`return`){this.exitCode=e.code;return}return Promise.reject(e)})}};return this.sandbox.context.setCommand(a),Promise.resolve()}if(e.type===`command`){if(/^return(\s+\d+)?\s*$/.test(e.line.trim())){let t=e.line.trim().match(/^return\s+(\d+)/),n=t?parseInt(t[1],10):0;return Promise.reject({__type:`return`,code:n})}if(/^break(\s+\d+)?\s*$/.test(e.line.trim())){let t=e.line.trim().match(/^break\s+(\d+)/),n=t?parseInt(t[1],10):1;return Promise.reject({__type:`break`,count:n})}if(/^continue(\s+\d+)?\s*$/.test(e.line.trim())){let t=e.line.trim().match(/^continue\s+(\d+)/),n=t?parseInt(t[1],10):1;return Promise.reject({__type:`continue`,count:n})}return(this.sandbox.context.getVar(`-`)||``).includes(`x`)&&(this.stdErr=`+ ${e.line.trim()}`,this.flush()),this.sandbox.onCommand(e.line,!1).then(()=>{if((this.sandbox.context.getVar(`-`)||``).includes(`e`)&&this.sandbox.lastCode!==0)return Promise.reject({__type:`errexit`,code:this.sandbox.lastCode})})}return e.type===`if`?this._executeIf(e):e.type===`while`?this._executeWhile(e):e.type===`for`?this._executeFor(e):e.type===`case`?this._executeCase(e):Promise.resolve()}async _executeIf(e){for(let t of e.clauses)if(await this.sandbox.onCommand(t.condition,!1),this.sandbox.lastCode===0)return this._executeNodes(this._parseLines(t.bodyLines));if(e.elseLines.length>0)return this._executeNodes(this._parseLines(e.elseLines))}async _executeWhile(e){for(;await this.sandbox.onCommand(e.condition,!1),this.sandbox.lastCode===0;)try{await this._executeNodes(this._parseLines(e.bodyLines))}catch(e){if(e&&e.__type===`break`){if(e.count<=1)break;throw{__type:`break`,count:e.count-1}}if(e&&e.__type===`continue`){if(e.count<=1)continue;throw{__type:`continue`,count:e.count-1}}throw e}}async _executeFor(e){let t;if(e.list.trim()===`"$@"`){let e=parseInt(this.sandbox.context.getVar(`#`)||`0`,10);t=[];for(let n=1;n<=e;n++)t.push(this.sandbox.context.getVar(String(n))||``)}else t=(await this.sandbox._replaceVariables(e.list)).split(/\s+/).filter(Boolean);for(let n of t){this.sandbox.context.setVar(e.variable,n);try{await this._executeNodes(this._parseLines(e.bodyLines))}catch(e){if(e&&e.__type===`break`){if(e.count<=1)break;throw{__type:`break`,count:e.count-1}}if(e&&e.__type===`continue`){if(e.count<=1)continue;throw{__type:`continue`,count:e.count-1}}throw e}}}_parseFunction(e,t){let n=e[t].trim(),r=n.match(/^function\s+([\w\d_]+)/)||n.match(/^([\w\d_]+)\s*\(/),i=r?r[1]:``,a=t+1;if(!n.includes(`{`)){for(;a<e.length&&!e[a].includes(`{`);)a++;a++}let o=[],s=1;for(;a<e.length;){if(e[a].trim()===`}`){a++;break}let t=(e[a].match(/\{/g)||[]).length,n=(e[a].match(/\}/g)||[]).length;if(s+=t-n,s<=0){a++;break}o.push(e[a]),a++}return{node:{type:`function`,name:i,bodyLines:o},consumed:a-t}}_parseCase(e,t){let n=e[t].trim().match(/^case\s+(.+?)\s+in\s*$/),r=n?n[1]:``,i=t+1,a=[],o=null,s=[];for(;i<e.length;){let t=e[i],n=t.trim();if(i++,!(!n||n.startsWith(`#`))){if(n===`esac`)break;if(o===null){let e=n.match(/^(.+?)\)\s*(.*)$/);if(e){o=e[1].trim();let t=e[2].trim();if(t===`;;`)a.push({pattern:o,bodyLines:[]}),o=null;else if(t.endsWith(`;;`)){let e=t.slice(0,-2).trim();a.push({pattern:o,bodyLines:e?[e]:[]}),o=null}else s=t?[t]:[]}}else n===`;;`?(a.push({pattern:o,bodyLines:s}),o=null,s=[]):n.endsWith(`;;`)?(s.push(n.slice(0,-2).trim()),a.push({pattern:o,bodyLines:s}),o=null,s=[]):s.push(t)}}return o!==null&&a.push({pattern:o,bodyLines:s}),{node:{type:`case`,word:r,clauses:a},consumed:i-t}}_executeFunctionBody(e){return this._executeNodes(this._parseLines(e))}async _executeCase(e){let t=await this.sandbox._replaceVariables(e.word),n=t.startsWith(`"`)&&t.endsWith(`"`)||t.startsWith(`'`)&&t.endsWith(`'`)?t.slice(1,-1):t;for(let t of e.clauses)if(t.pattern.replace(/^\(\s*/,``).split(`|`).map(e=>e.trim()).some(e=>this._matchPattern(e,n)))return this._executeNodes(this._parseLines(t.bodyLines))}_matchPattern(e,t){let n=``,r=0;for(;r<e.length;){let t=e[r];if(t===`[`){let t=e.indexOf(`]`,r+1);t===-1?(n+=`\\[`,r++):(n+=e.slice(r,t+1),r=t+1)}else t===`*`?(n+=`.*`,r++):t===`?`?(n+=`.`,r++):/[.+^${}()|\\]/.test(t)?(n+=`\\`+t,r++):(n+=t,r++)}try{return RegExp(`^`+n+`$`).test(t)}catch{return!1}}},t.exports.isKnownInterpreter=a})),D=t(((e,t)=>{t.exports=null})),O=t(((e,t)=>{y();var n=E(),{BinaryFile:r}=J(),i=0,a=6,o=8,s=20,c=27,l=28,u=31,d=44,f=54,p=70,m=0,h=2,g=3,_=4,b=1,x=2,S=4,C=8,T=1,O=4,k=class extends n{constructor(e,t,n){super(e,t,n),this._ensurePkgMeta()}_ensurePkgMeta(){if(this._pkgMeta===void 0&&this.script){let e=this.context&&this.context.fs||this.fs;e&&(this._pkgMeta=ne(e,this.script))}return this._pkgMeta||null}get name(){let e=this._ensurePkgMeta();return e&&e.bin?String(e.bin).toLowerCase():this.script?this.script.split(`/`).pop().replace(/\.(wasm|wat)$/i,``).toLowerCase():`wasm`}get requiresFilesystem(){return!0}get interactive(){if((this.arguments||[]).some(e=>e===`--help`||e===`-h`||e===`--version`||e===`-V`||e===`-v`))return!1;if(this._isInteractive!==void 0)return!!this._isInteractive;let e=this._ensurePkgMeta();return!!(e&&(e.interactive||e.wasm?.interactive)||this._inspectedWasm&&this._inspectedWasm.isInteractive)}get captureInput(){return this.interactive}kill(e=`SIGINT`){if(this._killed=!0,this.exitCode=130,this._exitAltScreen(),this._asyncifyReject){try{this._asyncifyReject(Error(`Interrupted by SIGINT`))}catch{}this._asyncifyReject=null}if(this._pendingReadResolve){let e=this._pendingReadResolve;this._pendingReadResolve=null;try{e()}catch{}}super.kill(e)}_enterAltScreen(){this._inAltScreen||(this._inAltScreen=!0,this.stdinStream&&typeof this.stdinStream.setRawMode==`function`&&this.stdinStream.setRawMode(!0),this.stdout&&typeof this.stdout.write==`function`&&this.stdout.write(`\x1B[?1049h\x1B[2J\x1B[H`))}_exitAltScreen(){this._inAltScreen&&(this._inAltScreen=!1,this.stdinStream&&typeof this.stdinStream.setRawMode==`function`&&this.stdinStream.setRawMode(!1),this.stdout&&typeof this.stdout.write==`function`&&this.stdout.write(`\x1B[0m\x1B[?1l\x1B[?1000l\x1B[?1002l\x1B[?1003l\x1B[?1006l\x1B[?1049l\x1B[?25h`))}_keyToBytes(e){if(!e)return null;if(typeof e!=`string`)return e instanceof Uint8Array?e:new Uint8Array([Number(e)&255]);if([`Control`,`Alt`,`Shift`,`Meta`,`CapsLock`,`NumLock`,`ScrollLock`,`Dead`,`Unidentified`].includes(e))return null;let t=e.match(/^\^([A-Za-z@\[\\\]^_?])$/);if(t){let e=t[1].toUpperCase();return e===`?`?new Uint8Array([127]):new Uint8Array([e.charCodeAt(0)-64])}let n={Enter:`\r`,Return:`\r`,Space:` `,Spacebar:` `,Backspace:``,Tab:` `,BackTab:`\x1B[Z`,Escape:`\x1B`,Esc:`\x1B`,ArrowUp:`\x1B[A`,Up:`\x1B[A`,ArrowDown:`\x1B[B`,Down:`\x1B[B`,ArrowRight:`\x1B[C`,Right:`\x1B[C`,ArrowLeft:`\x1B[D`,Left:`\x1B[D`,Home:`\x1B[H`,End:`\x1B[F`,PageUp:`\x1B[5~`,PageDown:`\x1B[6~`,Delete:`\x1B[3~`,Del:`\x1B[3~`,Insert:`\x1B[2~`,F1:`\x1BOP`,F2:`\x1BOQ`,F3:`\x1BOR`,F4:`\x1BOS`,F5:`\x1B[15~`,F6:`\x1B[17~`,F7:`\x1B[18~`,F8:`\x1B[19~`,F9:`\x1B[20~`,F10:`\x1B[21~`,F11:`\x1B[23~`,F12:`\x1B[24~`};return n[e]?new TextEncoder().encode(n[e]):new TextEncoder().encode(e)}onInput(e){if(!this._handlingStreamData&&!(this.stdinStream&&this.stdinStream._isPushing)){if(typeof this._externalKeyHandler==`function`){this._externalKeyHandler(e);return}this.stdinStream&&this.stdinStream.isRaw}}runCommand(){return super.runCommand().finally(()=>{this._inAltScreen&&this._exitAltScreen()})}run(){let e=this.script,t=(()=>{try{return D()}catch{return null}})();return this._loadBytes(e,t).then(({bytes:e,isWat:t})=>(this.interactive&&(this.emit(`interactive`),this.context&&typeof this.context.emit==`function`&&this.context.emit(`interactive`)),t?this._runWat(e):this._runWasm(e))).catch(e=>{this.stdErr=e.message||String(e),this.exitCode=1})}_loadBytes(e,t){let n=this.context&&this.context.fs||this.fs;if(!n)return Promise.reject(Error(`${e}: Filesystem is not available`));let r;if(n.isRealFilesystem&&t)try{let i=t.readFileSync(n.absolutePath(e)),a=new Uint8Array(i);r=Promise.resolve({bytes:a,isWat:this._isWat(e,a)})}catch(t){return Promise.reject(Error(`${e}: ${t.message}`))}else{let t=n.getFileByPath(e);if(t==null)return Promise.reject(Error(`${e}: No such file or directory`));let i=this._contentToBytes(t);r=Promise.resolve({bytes:i,isWat:this._isWat(e,i)})}return r.then(e=>(!e.isWat&&e.bytes&&(this._inspectedWasm=P(e.bytes),this._inspectedWasm.isInteractive&&(this._isInteractive=!0)),e))}_isWat(e,t){return e.endsWith(`.wat`)?!0:t.length>0&&t[0]===40}_contentToBytes(e){if(e instanceof Uint8Array)return e;if(v!==void 0&&v.isBuffer(e))return new Uint8Array(e.buffer,e.byteOffset,e.byteLength);if(e instanceof ArrayBuffer)return new Uint8Array(e);let t=new Uint8Array(e.length);for(let n=0;n<e.length;n++)t[n]=e.charCodeAt(n)&255;return t}async _runWat(e){let t=new TextDecoder().decode(e),n,r;try{r=await import(`wabt`)}catch{this.stdErr=`${this.script}: WAT execution requires the 'wabt' package\nInstall it with: npm install wabt`,this.exitCode=1;return}try{let e=r.default??r,{buffer:i}=(typeof e==`function`?await e():e).parseWat(this.script,t,{mutable_globals:!0}).toBinary({});n=new Uint8Array(i)}catch(e){this.stdErr=`${this.script}: WAT compilation error: ${e.message}`,this.exitCode=1;return}return this._runWasm(n)}async _runWasm(e){if(typeof WebAssembly>`u`){this.stdErr=`${this.script}: WebAssembly is not available in this environment`,this.exitCode=1;return}let t={1:``,2:``},n=new TextDecoder,y=new TextEncoder,w=null,E=null,D=!1,k=0,A=null,j=null,M=null,ee=!1,N=!1,I=!1,te=null,ne=null,L=null,R=this.stdin===void 0||this.stdin===null||this.stdin===this.stdinStream||this.stdin&&this.stdin.isTTY,z=!!(this.interactive&&R);z&&(this.emit(`interactive`),this.context&&typeof this.context.emit==`function`&&this.context.emit(`interactive`));let B=()=>{z||!R||(z=!0,this._isInteractive=!0,this.emit(`interactive`),this.context&&typeof this.context.emit==`function`&&this.context.emit(`interactive`),this._enterAltScreen(),t[1]&&=(this.stdout&&typeof this.stdout.write==`function`&&this.stdout.write(t[1]),``),t[2]&&=(this.stderr&&typeof this.stderr.write==`function`&&this.stderr.write(t[2]),``),this.stdinStream&&typeof this.stdinStream.setRawMode==`function`&&this.stdinStream.setRawMode(!0))},V=(e,n)=>{if(z&&(e===1||e===2)){if(e===1&&this.stdout){this.stdout.write(n);return}if(e===2&&this.stderr){this.stderr.write(n);return}}t[e]=(t[e]||``)+n;let r=t[e].split(`
6
6
  `);t[e]=r.pop();for(let t of r)e===1?this.stdOut=t:this.stdErr=t},H=()=>{t[1]&&=(z&&this.stdout?this.stdout.write(t[1]):this.stdOut=t[1],``),t[2]&&=(z&&this.stderr?this.stderr.write(t[2]):this.stdErr=t[2],``)},U=[],re=e=>{if(!(!e||e.length===0)){for(let t=0;t<e.length;t++)U.push(e[t]);if(j){let e=j;j=null,e()}}};L=e=>{this._handlingStreamData=!0;try{re(typeof e==`string`?y.encode(e):e instanceof Uint8Array?e:v!==void 0&&v.isBuffer(e)?new Uint8Array(e):y.encode(String(e??``)))}finally{this._handlingStreamData=!1}},this._externalKeyHandler=e=>{if(this._handlingStreamData)return;let t=this._keyToBytes(e);t&&re(t)};let ie=!!(this._onStdinStreamData&&this.stdinStream&&typeof this.stdinStream.listeners==`function`&&this.stdinStream.listeners(`data`).includes(this._onStdinStreamData));ie&&typeof this.stdinStream.off==`function`&&this.stdinStream.off(`data`,this._onStdinStreamData),R&&this.stdinStream&&typeof this.stdinStream.on==`function`&&this.stdinStream.on(`data`,L),R&&this.stdin&&this.stdin!==this.stdinStream&&typeof this.stdin.on==`function`&&this.stdin.on(`data`,L);let W=!R&&typeof this.stdin==`string`?y.encode(this.stdin):!R&&this.stdin instanceof Uint8Array?this.stdin:!R&&v!==void 0&&v.isBuffer(this.stdin)?new Uint8Array(this.stdin):new Uint8Array,ae=0,G=this.context&&this.context.fs?this.context.fs:this.fs,K=this.context&&this.context.fs&&this.context.fs.cwd||this.fs&&this.fs.cwd||this.context&&typeof this.context.getVar==`function`&&this.context.getVar(`PWD`)||G&&G.cwd||`/`,q=K.length>1&&K.endsWith(`/`)?K.slice(0,-1):K,J=5,Y=new Map([[3,{type:`preopen`,path:`/`,guestName:`/`}],[4,{type:`preopen`,path:q,guestName:`.`}]]),oe=new Map([[0,0],[1,0],[2,0]]),se=e=>{let t=e.split(`/`).filter(Boolean);return t.pop(),`/`+t.join(`/`)},ce=(e,t)=>{let n=Y.get(e);if(!n||n.type!==`preopen`&&n.type!==`dir`)return null;if(t.startsWith(`/`))return G.absolutePath(t);if(!t||t===`.`)return G.absolutePath(n.path);let r=n.path.endsWith(`/`)?n.path:n.path+`/`,i=G.absolutePath(r+t),a=G.absolutePath(`/`+t);if(n.path!==`/`){if(G.exists(i))return i;if(G.exists(a))return a;let e=G.exists(se(i));return G.exists(se(a))&&!e?a:i}if(G.exists(i))return i;let o=G.absolutePath(t);return G.exists(o)?o:i},le=e=>{if(!(!e||e.type!==`file`||!e.dirty||!G))try{let t=!0;for(let n=0;n<e.bytes.length;n++)if(e.bytes[n]===0){t=!1;break}if(t){let t=n.decode(e.bytes);G.setFileByPath(e.path,t,!0)}else{let t=new Uint8Array(e.bytes.length);t.set(e.bytes),G.setFileByPath(e.path,new r(t.buffer,`application/octet-stream`),!0)}e.dirty=!1}catch{}},ue=this.stdout&&this.stdout.columns||this.stdinStream&&this.stdinStream.columns||this.context&&typeof this.context.getVar==`function`&&parseInt(this.context.getVar(`COLUMNS`),10)||80,de=this.stdout&&this.stdout.rows||this.stdinStream&&this.stdinStream.rows||this.context&&typeof this.context.getVar==`function`&&parseInt(this.context.getVar(`LINES`),10)||24,fe={TERM:`xterm-256color`,COLORTERM:`truecolor`,...this.context&&this.context.vars||{},COLUMNS:String(ue),LINES:String(de)},X=Object.entries(fe).map(([e,t])=>`${e}=${t}`),pe=new Proxy({fd_write:(e,t,r,a)=>{if(!w)return o;let s=new DataView(w.buffer);if(e===1||e===2){let o=0;for(let i=0;i<r;i++){let r=s.getUint32(t+i*8,!0),a=s.getUint32(t+i*8+4,!0);a>0&&V(e,n.decode(new Uint8Array(w.buffer,r,a))),o+=a}return s.setUint32(a,o,!0),i}let c=Y.get(e);if(!c)return o;if(c.type===`dev_null`||c.type===`dev_zero`){let e=0;for(let n=0;n<r;n++)e+=s.getUint32(t+n*8+4,!0);return s.setUint32(a,e,!0),i}if(c.type===`dev_tty`){let e=0;for(let i=0;i<r;i++){let r=s.getUint32(t+i*8,!0),a=s.getUint32(t+i*8+4,!0);a>0&&V(1,n.decode(new Uint8Array(w.buffer,r,a))),e+=a}return s.setUint32(a,e,!0),i}if(c.type!==`file`)return o;let l=0;for(let e=0;e<r;e++){let n=s.getUint32(t+e*8,!0),r=s.getUint32(t+e*8+4,!0);if(r>0){let e=new Uint8Array(w.buffer,n,r),t=c.append?c.bytes.length:c.pos,i=t+r;if(i>c.bytes.length){let e=new Uint8Array(i);e.set(c.bytes),c.bytes=e}c.bytes.set(e,t),c.pos=t+r,c.dirty=!0,l+=r}}return s.setUint32(a,l,!0),i},fd_read:(e,t,n,r)=>{if(!w)return o;let s=new DataView(w.buffer),l=Y.get(e),u=!!(l&&l.type===`dev_tty`);if(e===0||z&&e===2||u){(u||e===2)&&B();let o=!!((oe.get(e)||0)&O);if(D){if((typeof E?.exports?.asyncify_get_state==`function`?E.exports.asyncify_get_state():0)===2){if(E.exports.asyncify_stop_rewind(),I&&U.length===0)return I=!1,s.setUint32(r,0,!0),c;I=!1;let e=0;for(let r=0;r<n&&U.length>0;r++){let n=s.getUint32(t+r*8,!0),i=s.getUint32(t+r*8+4,!0),a=Math.min(i,U.length),o=U.splice(0,a);new Uint8Array(w.buffer).set(o,n),e+=a}return s.setUint32(r,e,!0),i}if(U.length>0){let e=0;for(let r=0;r<n&&U.length>0;r++){let n=s.getUint32(t+r*8,!0),i=s.getUint32(t+r*8+4,!0),a=Math.min(i,U.length),o=U.splice(0,a);new Uint8Array(w.buffer).set(o,n),e+=a}return s.setUint32(r,e,!0),i}return o?(s.setUint32(r,0,!0),a):(I=!1,E.exports.asyncify_start_unwind(k),A=new Promise((e,t)=>{let n=()=>{I=!1,j=null,M=null,this._pendingReadResolve=null,this._asyncifyReject=null,e()};j=n,this._pendingReadResolve=n,M=()=>{I=!0,j=null,M=null,this._pendingReadResolve=null,this._asyncifyReject=null,e()},this._asyncifyReject=t}),s.setUint32(r,0,!0),i)}if(U.length>0){let e=0;for(let r=0;r<n&&U.length>0;r++){let n=s.getUint32(t+r*8,!0),i=s.getUint32(t+r*8+4,!0),a=Math.min(i,U.length),o=U.splice(0,a);new Uint8Array(w.buffer).set(o,n),e+=a}return s.setUint32(r,e,!0),i}let l=0;for(let e=0;e<n&&ae<W.length;e++){let n=s.getUint32(t+e*8,!0),r=s.getUint32(t+e*8+4,!0),i=Math.min(r,W.length-ae);new Uint8Array(w.buffer).set(W.slice(ae,ae+i),n),ae+=i,l+=i}return l===0&&o&&ae>=W.length?(s.setUint32(r,0,!0),a):(s.setUint32(r,l,!0),i)}if(!l)return o;if(l.type===`dev_null`)return s.setUint32(r,0,!0),i;if(l.type===`dev_zero`){let e=0;for(let r=0;r<n;r++){let n=s.getUint32(t+r*8,!0),i=s.getUint32(t+r*8+4,!0);new Uint8Array(w.buffer,n,i).fill(0),e+=i}return s.setUint32(r,e,!0),i}if(l.type===`dev_urandom`){let e=0;for(let r=0;r<n;r++){let n=s.getUint32(t+r*8,!0),i=s.getUint32(t+r*8+4,!0),a=new Uint8Array(w.buffer,n,i);if(typeof crypto<`u`&&crypto.getRandomValues)crypto.getRandomValues(a);else for(let e=0;e<i;e++)a[e]=Math.floor(Math.random()*256);e+=i}return s.setUint32(r,e,!0),i}if(l.type!==`file`)return o;let d=0;for(let e=0;e<n&&l.pos<l.bytes.length;e++){let n=s.getUint32(t+e*8,!0),r=s.getUint32(t+e*8+4,!0),i=Math.min(r,l.bytes.length-l.pos);new Uint8Array(w.buffer).set(l.bytes.subarray(l.pos,l.pos+i),n),l.pos+=i,d+=i}return s.setUint32(r,d,!0),i},fd_seek:(e,t,n,r,a)=>{if(!w)return o;if(e<=2)return p;let s=Y.get(e);if(!s)return o;if(s.type===`dev_tty`||s.type===`dev_null`||s.type===`dev_zero`||s.type===`dev_urandom`)return p;if(s.type!==`file`)return o;let c,u,d;typeof t==`bigint`?(c=Number(t),u=n,d=r):(c=t,u=r,d=a);let f=s.pos;if(u===0)f=c;else if(u===1)f=s.pos+c;else if(u===2)f=s.bytes.length+c;else return l;return f<0?l:(s.pos=f,d&&new DataView(w.buffer).setBigUint64(d,BigInt(s.pos),!0),i)},fd_tell:(e,t)=>{if(!w)return o;let n=Y.get(e);return!n||n.type!==`file`?o:(new DataView(w.buffer).setBigUint64(t,BigInt(n.pos),!0),i)},fd_close:e=>{if(e<=2)return i;let t=Y.get(e);return t?(t.type===`file`&&le(t),Y.delete(e),oe.delete(e),i):o},fd_sync:e=>{let t=Y.get(e);return t&&le(t),i},fd_datasync:e=>{let t=Y.get(e);return t&&le(t),i},fd_prestat_get:(e,t)=>{if(!w)return o;let n=Y.get(e);if(!n||n.type!==`preopen`)return o;let r=new DataView(w.buffer),a=y.encode(n.guestName);return r.setUint8(t,0),r.setUint32(t+4,a.length,!0),i},fd_prestat_dir_name:(e,t,n)=>{if(!w)return o;let r=Y.get(e);if(!r||r.type!==`preopen`)return o;let a=y.encode(r.guestName);return new Uint8Array(w.buffer,t,Math.min(n,a.length)).set(a.subarray(0,n)),i},fd_fdstat_get:(e,t)=>{if(!w)return o;let n=new DataView(w.buffer),r=m;if(e<=2)r=h;else{let t=Y.get(e);if(!t)return o;r=t.type===`dev_null`||t.type===`dev_zero`||t.type===`dev_urandom`||t.type===`dev_tty`?h:t.type===`preopen`||t.type===`dir`?g:_}for(let e=0;e<24;e++)n.setUint8(t+e,0);n.setUint8(t,r);let a=oe.get(e)||(Y.get(e)?.append?T:0);return n.setUint16(t+2,a,!0),n.setBigUint64(t+8,18446744073709551615n,!0),n.setBigUint64(t+16,18446744073709551615n,!0),i},fd_fdstat_set_flags:(e,t)=>{if(!w||e>2&&!Y.has(e))return o;oe.set(e,t);let n=Y.get(e);return n&&(n.append=!!(t&T)),(e===0||e===2||n&&n.type===`dev_tty`)&&t&O&&B(),i},fd_filestat_get:(e,t)=>{if(!w)return o;let n=new DataView(w.buffer);for(let e=0;e<64;e++)n.setUint8(t+e,0);if(e<=2)return n.setUint8(t+16,h),i;let r=Y.get(e);if(!r)return o;if(r.type===`dev_null`||r.type===`dev_zero`||r.type===`dev_urandom`||r.type===`dev_tty`)return n.setUint8(t+16,h),n.setBigUint64(t+24,1n,!0),i;let a=r.type===`preopen`||r.type===`dir`;return n.setUint8(t+16,a?g:_),n.setBigUint64(t+24,1n,!0),n.setBigUint64(t+32,BigInt(r.bytes?r.bytes.length:0),!0),i},path_open:(e,t,r,a,c,l,p,m,h)=>{if(!w||!G)return o;let g=ce(e,n.decode(new Uint8Array(w.buffer,r,a)));if(!g)return o;let _=g.replace(/\/+/g,`/`).replace(/\/$/,``)||`/`;if(_===`/dev/null`){let e=J++;return Y.set(e,{type:`dev_null`,path:`/dev/null`}),new DataView(w.buffer).setUint32(h,e,!0),i}if(_===`/dev/zero`){let e=J++;return Y.set(e,{type:`dev_zero`,path:`/dev/zero`}),new DataView(w.buffer).setUint32(h,e,!0),i}if(_===`/dev/urandom`||_===`/dev/random`){let e=J++;return Y.set(e,{type:`dev_urandom`,path:_}),new DataView(w.buffer).setUint32(h,e,!0),i}if(_===`/dev/tty`||_===`/dev/stdin`){B();let e=J++;return Y.set(e,{type:`dev_tty`,path:_}),new DataView(w.buffer).setUint32(h,e,!0),i}if(_===`/dev/stdout`)return new DataView(w.buffer).setUint32(h,1,!0),i;if(_===`/dev/stderr`)return new DataView(w.buffer).setUint32(h,2,!0),i;let v=G.stat(g),E=v!==null;if(c&S&&c&b&&E)return s;if(c&x){if(!E)return d;if(!v.isDirectory)return f;let e=J++;return Y.set(e,{type:`dir`,path:g}),new DataView(w.buffer).setUint32(h,e,!0),i}if(E&&v.isDirectory)return u;if(!E&&!(c&b))return d;let D=new Uint8Array,O=!1;if(E&&!(c&C)){let e=G.getFileByPath(g);e!=null&&(D=typeof e==`string`?y.encode(e):new Uint8Array(this._contentToBytes(e)))}else O=!0;let k=J++;return Y.set(k,{type:`file`,path:g,bytes:D,pos:m&T?D.length:0,append:!!(m&T),dirty:O}),new DataView(w.buffer).setUint32(h,k,!0),i},path_filestat_get:(e,t,r,a,s)=>{if(!w||!G)return o;let c=ce(e,n.decode(new Uint8Array(w.buffer,r,a)));if(!c)return o;let l=c.replace(/\/+/g,`/`).replace(/\/$/,``)||`/`,u=G.stat(c);if(l===`/dev/null`||l===`/dev/zero`||l===`/dev/urandom`||l===`/dev/random`||l===`/dev/tty`||l===`/dev/stdin`||l===`/dev/stdout`||l===`/dev/stderr`||u&&(u.isDevice||u.isCharacterDevice)){let e=new DataView(w.buffer);for(let t=0;t<64;t++)e.setUint8(s+t,0);return e.setUint8(s+16,h),e.setBigUint64(s+24,1n,!0),i}if(!u)return d;let f=new DataView(w.buffer);for(let e=0;e<64;e++)f.setUint8(s+e,0);return f.setUint8(s+16,u.isDirectory?g:_),f.setBigUint64(s+24,1n,!0),f.setBigUint64(s+32,BigInt(G.getSize(c)||0),!0),i},path_create_directory:(e,t,r)=>{if(!w||!G)return o;let a=ce(e,n.decode(new Uint8Array(w.buffer,t,r)));if(!a)return o;if(G.exists(a))return s;try{return G.setFileByPath(a,{},!0),i}catch{return l}},path_unlink_file:(e,t,r)=>{if(!w||!G)return o;let a=ce(e,n.decode(new Uint8Array(w.buffer,t,r)));if(!a||!G.exists(a))return d;try{return G.deleteFileByPath(a),i}catch{return l}},path_remove_directory:(e,t,r)=>{if(!w||!G)return o;let a=ce(e,n.decode(new Uint8Array(w.buffer,t,r)));if(!a||!G.exists(a))return d;try{return G.deleteFileByPath(a),i}catch{return l}},path_rename:(e,t,r,a,s,c)=>{if(!w||!G)return o;let u=n.decode(new Uint8Array(w.buffer,t,r)),f=n.decode(new Uint8Array(w.buffer,s,c)),p=ce(e,u),m=ce(a,f);if(!p||!m)return o;if(!G.exists(p))return d;for(let e of Y.values())e.path===p&&(le(e),e.path=m);try{let e=G.getFileByPath(p);return G.setFileByPath(m,e,!0),G.deleteFileByPath(p),i}catch{return l}},proc_exit:e=>{throw e!==0&&(this.exitCode=e),{_wasmExit:!0,code:e}},args_sizes_get:(e,t)=>{if(!w)return l;let n=[this.script,...this.arguments||[]],r=new DataView(w.buffer);return r.setUint32(e,n.length,!0),r.setUint32(t,n.reduce((e,t)=>e+y.encode(t).length+1,0),!0),i},args_get:(e,t)=>{if(!w)return l;let n=[this.script,...this.arguments||[]],r=new DataView(w.buffer),a=t;for(let t=0;t<n.length;t++){r.setUint32(e+t*4,a,!0);let i=y.encode(n[t]+`\0`);new Uint8Array(w.buffer).set(i,a),a+=i.length}return i},environ_sizes_get:(e,t)=>{if(!w)return l;let n=new DataView(w.buffer);n.setUint32(e,X.length,!0);let r=X.reduce((e,t)=>e+y.encode(t).length+1,0);return n.setUint32(t,r,!0),i},environ_get:(e,t)=>{if(!w)return l;let n=new DataView(w.buffer),r=t;for(let t=0;t<X.length;t++){n.setUint32(e+t*4,r,!0);let i=y.encode(X[t]+`\0`);new Uint8Array(w.buffer).set(i,r),r+=i.length}return i},clock_time_get:(e,t,n)=>{if(w){let t=e===1&&typeof performance<`u`&&typeof performance.now==`function`?BigInt(Math.round(performance.now()*1e6)):BigInt(Date.now())*1000000n;new DataView(w.buffer).setBigUint64(n,t,!0)}return i},random_get:(e,t)=>{if(w){let n=new Uint8Array(w.buffer,e,t);for(let e=0;e<t;e++)n[e]=Math.random()*256|0}return i},clock_nanosleep:(e,t,n,r,a)=>{if(!w)return o;let s=new DataView(w.buffer).getBigUint64(n,!0),c=0;if(t&1){let e=BigInt(Date.now())*1000000n;c=s>e?Number((s-e)/1000000n):0}else c=Number(s/1000000n);return D&&c>0?(typeof E?.exports?.asyncify_get_state==`function`?E.exports.asyncify_get_state():0)===2?(E.exports.asyncify_stop_rewind(),i):(E.exports.asyncify_start_unwind(k),A=new Promise(e=>setTimeout(e,Math.max(1,c))),i):i},poll_oneoff:(e,t,n,r)=>{if(!w)return o;let a=new DataView(w.buffer);if(D){if((typeof E?.exports?.asyncify_get_state==`function`?E.exports.asyncify_get_state():0)===2){if(E.exports.asyncify_stop_rewind(),N&&U.length===0)return N=!1,a.setUint32(r,0,!0),c;N=!1;let o=0;for(let r=0;r<n;r++){let n=e+r*48,s=a.getBigUint64(n,!0),c=a.getUint8(n+8),l=!1;if(c===0)l=ee;else if(c===1){let e=a.getUint32(n+16,!0),t=Y.get(e),r=!!(t&&t.type===`dev_tty`);(e===0||z&&e===2||r)&&U.length>0&&(l=!0)}else c===2&&(l=!0);if(l){let e=t+o*32;a.setBigUint64(e,s,!0),a.setUint16(e+8,i,!0),a.setUint8(e+10,c);for(let t=11;t<32;t++)a.setUint8(e+t,0);c===1&&a.setBigUint64(e+16,BigInt(U.length),!0),o++}}return a.setUint32(r,o,!0),i}let o=0,s=!1,l=1/0;for(let t=0;t<n;t++){let n=e+t*48,r=a.getUint8(n+8);if(r===0){let e=a.getBigUint64(n+24,!0),t=a.getUint16(n+40,!0),r=0;if(t&1){let t=BigInt(Date.now())*1000000n;r=e>t?Number((e-t)/1000000n):0}else r=Number(e/1000000n);r<l&&(l=r)}else if(r===1){s=!0;let e=a.getUint32(n+16,!0),t=Y.get(e),r=!!(t&&t.type===`dev_tty`);(e===0||e===2||r)&&B(),(e===0||z&&e===2||r)&&U.length>0&&o++}else r===2&&o++}if(o>0||l===0){let o=0;for(let r=0;r<n;r++){let n=e+r*48,s=a.getBigUint64(n,!0),c=a.getUint8(n+8),u=!1;if(c===0&&l===0)u=!0;else if(c===1){let e=a.getUint32(n+16,!0),t=Y.get(e),r=!!(t&&t.type===`dev_tty`);(e===0||z&&e===2||r)&&U.length>0&&(u=!0)}else c===2&&(u=!0);if(u){let e=t+o*32;a.setBigUint64(e,s,!0),a.setUint16(e+8,i,!0),a.setUint8(e+10,c);for(let t=11;t<32;t++)a.setUint8(e+t,0);c===1&&a.setBigUint64(e+16,BigInt(U.length),!0),o++}}return a.setUint32(r,o,!0),i}return ee=!1,N=!1,E.exports.asyncify_start_unwind(k),A=new Promise((e,t)=>{let n=null;this._asyncifyReject=t;let r=()=>{n&&clearTimeout(n),ee=!1,N=!1,j=null,M=null,this._pendingReadResolve=null,this._asyncifyReject=null,e()};s&&(j=r,this._pendingReadResolve=r,M=()=>{n&&clearTimeout(n),ee=!1,N=!0,j=null,M=null,this._pendingReadResolve=null,this._asyncifyReject=null,e()}),l!==1/0&&(n=setTimeout(()=>{ee=!0,N=!1,j=null,M=null,this._pendingReadResolve=null,this._asyncifyReject=null,e()},Math.max(1,l)))}),a.setUint32(r,0,!0),i}return a.setUint32(r,0,!0),i}},{get(e,t){return t in e?e[t]:()=>0}}),me=e=>{if(!w||!e)return``;try{let t=new DataView(w.buffer).getUint32(e-4,!0),n=new Uint16Array(w.buffer,e,t>>>1);return String.fromCharCode(...n)}catch{return``}},Z={wasi_snapshot_preview1:pe,wasi_unstable:pe,env:new Proxy({print:(e,t)=>{w&&V(1,n.decode(new Uint8Array(w.buffer,e,t)))},eprint:(e,t)=>{w&&V(2,n.decode(new Uint8Array(w.buffer,e,t)))},log:e=>{V(1,me(e)+`
7
7
  `)},logi:e=>{V(1,String(e)+`
8
8
  `)},logf:e=>{V(1,String(e)+`
@@ -42,7 +42,7 @@
42
42
  `),this._redrawPromptLine(),n++;continue}r>=32&&(this._lineBuffer.splice(this._cursorPos,0,e),this._cursorPos++,this._redrawPromptLine()),n++}}_handleEscapeSeq(e){e===`A`?this._navigateHistory(-1):e===`B`?this._navigateHistory(1):e===`C`?this._cursorPos<this._lineBuffer.length&&(this._cursorPos++,this._redrawPromptLine()):e===`D`?this._cursorPos>0&&(this._cursorPos--,this._redrawPromptLine()):e===`1;3D`||e===`1;5D`?(this._cursorPos=this._wordLeft(),this._redrawPromptLine()):e===`1;3C`||e===`1;5C`?(this._cursorPos=this._wordRight(),this._redrawPromptLine()):e===`H`||e===`1~`||e===`7~`?(this._cursorPos=0,this._redrawPromptLine()):e===`F`||e===`4~`||e===`8~`?(this._cursorPos=this._lineBuffer.length,this._redrawPromptLine()):e===`3~`&&this._cursorPos<this._lineBuffer.length&&(this._lineBuffer.splice(this._cursorPos,1),this._redrawPromptLine())}_handleTab(){let e=this._lineBuffer.join(``).split(` `),t=e.pop(),n=this.tabCompletion(t);if(!n)return;let{options:r,path:i}=n;r.length===1?(e.push(`${i==null?``:i.concat(`/`)}${r[0]}`),this._lineBuffer=e.join(` `).split(``),this._cursorPos=this._lineBuffer.length,this._redrawPromptLine()):r.length>1&&(this.stdout.write(`\r
43
43
  `+r.join(` `)+`\r
44
44
  `),this._redrawPromptLine())}_splitInput(e){return this._parseOperators(e)}_parseOperators(e){let t=[],n=``,r=0,i=!1,a=!1;for(;r<e.length;){let o=e[r];if(o===`'`&&!a){i=!i,n+=o,r++;continue}if(o===`"`&&!i){a=!a,n+=o,r++;continue}if(!i&&!a){if(o===`#`&&(n.trim()===``||/\s/.test(e[r-1]))){let t=e.indexOf(`
45
- `,r);if(t===-1)break;r=t;continue}if(o===`&`&&e[r+1]===`&`){t.push({command:n.trim(),op:`&&`}),n=``,r+=2;continue}if(o===`|`&&e[r+1]===`|`){t.push({command:n.trim(),op:`||`}),n=``,r+=2;continue}if(o===`|`){t.push({command:n.trim(),op:`|`}),n=``,r++;continue}if(o===`;`){t.push({command:n.trim(),op:`;`}),n=``,r++;continue}}n+=o,r++}return n.trim()&&t.push({command:n.trim(),op:null}),t}_loadJsCommand(e,t){return this._jsCommandCache||=new Map,o.load(this.context.fs,e,t,this._jsCommandCache)}_findExecutablePath(e){return this.context.fs.exists(e)?e:!e.endsWith(`.wasm`)&&this.context.fs.exists(`${e}.wasm`)?`${e}.wasm`:!e.endsWith(`.js`)&&this.context.fs.exists(`${e}.js`)?`${e}.js`:null}_resolveCommandFromPath(e){let t=this.context.fs.realpath(e);if(!this.context.fs.isExecutable(e)&&!this.context.fs.isExecutable(t))throw Error(`${e}: permission denied`);let n=this.context.fs.getFileByPath(t);return o.isCommandClass(n)?n:typeof n==`string`&&n.startsWith(`#!`)?i.bind(this,n):a.isWasmFile(t,n)?a.bind(null,t):o.isJsFile(t,n)?this._loadJsCommand(t,n):null}_parseCommand(e){if(e.startsWith(`./`)||e.startsWith(`/`)||e.startsWith(`../`)||e.includes(`/`)){let t=this._findExecutablePath(this.context.fs.absolutePath(e));if(!t)throw Error(`${e}: No such file or directory`);let n=this.context.fs.stat(t);if(n&&n.isDirectory)throw Error(`${e}: is a directory`);let r=this._resolveCommandFromPath(t);if(!r)throw Error(`${e}: permission denied`);return r}let t=this.context.getCommand(e)||!e.endsWith(`.wasm`)&&this.context.getCommand(`${e}.wasm`)||!e.endsWith(`.js`)&&this.context.getCommand(`${e}.js`);if(o.isCommandClass(t))return t;if(typeof t==`string`&&t.startsWith(`#!`))return i.bind(this,t);let n=(this.context.getVar(`PATH`)||`/bin`).split(`:`).filter(Boolean);for(let t of n){let n=this._findExecutablePath(`${t}/${e}`);if(n){let e=this._resolveCommandFromPath(n);if(e)return e}}throw Error(`${e}: Command not found`)}async _parseCommandLine(t){let n=e._splitCommand(t);return(await Promise.all(n.map(({value:e,hasQuote:t})=>this._replaceVariables(e).then(e=>({value:e,hasQuote:t}))))).filter(({value:e,hasQuote:t})=>e.length>0||t)}static _sanitiseCommandPart(e){return e.startsWith(`"`)&&e.endsWith(`"`)||e.startsWith(`'`)&&e.endsWith(`'`)?e.substring(1,e.length-1):e}static _sanitiseArray(e){return e.filter(e=>e.length>0)}async _replaceVariables(t){let n=[...t.matchAll(/\$\(\(([^)]+)\)\)/g)];for(let e of n)t=t.replace(e[0],String(this._evalArithmetic(e[1])));let r=[...t.matchAll(/\$\((?!\()([^)]+)\)/g)];for(let e of r){let n=await this._runAndCapture(e[1]);t=t.replace(e[0],n)}let i=t.matchAll(x),a=i.next();for(;!a.done;){let n=a.value[1]||a.value[2],r=this.context.getVar(n);t=t.replace(new RegExp(e._escapeRegExp(a.value[0]),`g`),String(r)),a=i.next()}return t}_evalArithmetic(e){let t=e.replace(/\${?([\w\d]+)}?/g,(e,t)=>String(parseInt(this.context.getVar(t))||0));if(t=t.replace(/\b([A-Za-z_]\w*)\b/g,(e,t)=>String(parseInt(this.context.getVar(t))||0)),!/^[\d\s+\-*/%()]+$/.test(t.trim()))return 0;try{return Function(`return (`+t+`)`)()}catch{return 0}}async _runAndCapture(e){let t=new globalThis.shell({cwd:this.context.fs.cwd,filesystem:this.context.fs.getRaw(),permissions:this.context.fs.getRawPermissions(),subshell:!0});t.context.source(this.context);let n=[];return t.on(`stdOut`,e=>n.push(e)),await t.onCommand(e,!1),n.join(`
45
+ `,r);if(t===-1)break;r=t;continue}if(o===`&`&&e[r+1]===`&`){t.push({command:n.trim(),op:`&&`}),n=``,r+=2;continue}if(o===`|`&&e[r+1]===`|`){t.push({command:n.trim(),op:`||`}),n=``,r+=2;continue}if(o===`|`){t.push({command:n.trim(),op:`|`}),n=``,r++;continue}if(o===`;`){t.push({command:n.trim(),op:`;`}),n=``,r++;continue}}n+=o,r++}return n.trim()&&t.push({command:n.trim(),op:null}),t}_loadJsCommand(e,t){return this._jsCommandCache||=new Map,o.load(this.context.fs,e,t,this._jsCommandCache)}_findExecutablePath(e){return this.context.fs.exists(e)?e:!e.endsWith(`.wasm`)&&this.context.fs.exists(`${e}.wasm`)?`${e}.wasm`:!e.endsWith(`.js`)&&this.context.fs.exists(`${e}.js`)?`${e}.js`:null}_resolveCommandFromPath(e){let t=this.context.fs.realpath(e);if(!this.context.fs.isExecutable(e)&&!this.context.fs.isExecutable(t))throw Error(`${e}: permission denied`);let n=this.context.fs.getFileByPath(t);return o.isCommandClass(n)?n:typeof n==`string`&&n.startsWith(`#!`)?i.bind(this,n):a.isWasmFile(t,n)?a.bind(null,t):o.isJsFile(t,n)?this._loadJsCommand(t,n):null}_parseCommand(e){if(e.startsWith(`./`)||e.startsWith(`/`)||e.startsWith(`../`)||e.includes(`/`)){let t=this._findExecutablePath(this.context.fs.absolutePath(e));if(!t)throw Error(`${e}: No such file or directory`);let n=this.context.fs.stat(t);if(n&&n.isDirectory)throw Error(`${e}: is a directory`);let r=this._resolveCommandFromPath(t);if(!r)throw Error(`${e}: permission denied`);return r}let t=this.context.getCommand(e)||!e.endsWith(`.wasm`)&&this.context.getCommand(`${e}.wasm`)||!e.endsWith(`.js`)&&this.context.getCommand(`${e}.js`);if(o.isCommandClass(t))return t;if(typeof t==`string`&&t.startsWith(`#!`))return i.bind(this,t);let n=(this.context.getVar(`PATH`)||`/bin`).split(`:`).filter(Boolean);for(let t of n){let n=this._findExecutablePath(`${t}/${e}`);if(n){let e=this._resolveCommandFromPath(n);if(e)return e}}throw Error(`${e}: Command not found`)}async _parseCommandLine(t){let n=e._splitCommand(t),r=n.length>0?n[0].value:``,i=r===`export`||r===`alias`||r===`local`,a=[];for(let e=0;e<n.length;e++){let{value:t,hasQuote:r}=n[e];if(r&&t===`$@`){let e=parseInt(this.context.getVar(`#`)||`0`,10)||0;for(let t=1;t<=e;t++)a.push({value:String(this.context.getVar(String(t))),hasQuote:!0});continue}let o=await this._replaceVariables(t),s=n.length===1&&/^[A-Za-z_]\w*=/.test(t),c=e>0&&i&&/^[A-Za-z_]\w*=/.test(t);if(r||s||c||!t.includes(`$`)){(o.length>0||r)&&a.push({value:o,hasQuote:r});continue}let l=o.trim().split(/\s+/).filter(Boolean);for(let e of l)a.push({value:e,hasQuote:!1})}return a}static _sanitiseCommandPart(e){return e.startsWith(`"`)&&e.endsWith(`"`)||e.startsWith(`'`)&&e.endsWith(`'`)?e.substring(1,e.length-1):e}static _sanitiseArray(e){return e.filter(e=>e.length>0)}async _replaceVariables(t){let n=[...t.matchAll(/\$\(\(([^)]+)\)\)/g)];for(let e of n)t=t.replace(e[0],String(this._evalArithmetic(e[1])));let r=[...t.matchAll(/\$\((?!\()([^)]+)\)/g)];for(let e of r){let n=await this._runAndCapture(e[1]);t=t.replace(e[0],n)}let i=t.matchAll(x),a=i.next();for(;!a.done;){let n=a.value[1]||a.value[2],r=this.context.getVar(n);t=t.replace(new RegExp(e._escapeRegExp(a.value[0]),`g`),String(r)),a=i.next()}return t}_evalArithmetic(e){let t=e.replace(/\${?([\w\d]+)}?/g,(e,t)=>String(parseInt(this.context.getVar(t))||0));if(t=t.replace(/\b([A-Za-z_]\w*)\b/g,(e,t)=>String(parseInt(this.context.getVar(t))||0)),!/^[\d\s+\-*/%()]+$/.test(t.trim()))return 0;try{return Function(`return (`+t+`)`)()}catch{return 0}}async _runAndCapture(e){let t=new globalThis.shell({cwd:this.context.fs.cwd,filesystem:this.context.fs.getRaw(),permissions:this.context.fs.getRawPermissions(),subshell:!0});t.context.source(this.context);let n=[];return t.on(`stdOut`,e=>n.push(e)),await t.onCommand(e,!1),n.join(`
46
46
  `).trimEnd()}_groupPipes(e){let t=[],n=[];for(let{command:r,op:i}of e)n.push(r),i!==`|`&&(t.push({commands:n,op:i}),n=[]);return n.length&&t.push({commands:n,op:null}),t}async _runPipeChain(t){if(t.length===1)return this._runCommand(t[0]);let n=!1;try{let r=e._splitCommand(t[0]),i=t[0],a=!1;for(;r.length>0&&!r[0].hasQuote&&r[0].value===`!`;)n=!n,a=!0,r.shift(),i=i.replace(/^\s*!\s*/,``);a&&(t=[i,...t.slice(1)])}catch{}let r;for(let e=0;e<t.length;e++)if(e===t.length-1){r!==void 0&&(this._stdin=r);try{await this._runCommand(t[e])}finally{delete this._stdin}}else r=await this._captureForPipe(t[e],r);n&&(this.lastCode=+(this.lastCode===0),this.emit(`exitCode`,this.lastCode))}async _captureForPipe(e,t){let n=new globalThis.shell({cwd:this.context.fs.cwd,filesystem:this.context.fs.getRaw(),permissions:this.context.fs.getRawPermissions(),subshell:!0});n.context.source(this.context),t!==void 0&&(n.stdin=t);let r=[];return n.on(`stdOut`,e=>r.push(e)),n.on(`stdErr`,e=>this.emit(`stdErr`,e)),await n.onCommand(e,!1),this.lastCode=n.lastCode,r.join(`
47
47
  `)}static _splitCommand(e){let t=/\s/g,n=!1,r=!1,i=0,a=[],o=!1,s=[];for(let c=0;c<e.length;c++){let l=e[c];if(i>0){a.push(l),l===`(`?i++:l===`)`&&i--;continue}if(r&&l===`\\`&&c+1<e.length){let t=e[c+1];if(t===`"`||t===`\\`||t===`$`){a.push(t),c++;continue}}if(!n&&!r&&l===`\\`&&c+1<e.length){let t=e[++c];a.push(t),(t===`"`||t===`'`)&&(o=!0);continue}if(l===`'`&&!r){n=!n,o=!0;continue}if(l===`"`&&!n){r=!r,o=!0;continue}if(!n&&!r){if(l===`$`&&c+1<e.length&&e[c+1]===`(`){i=1,a.push(`$`),a.push(`(`),c++;continue}if(l.match(t)){(a.length>0||o)&&(s.push({value:a.join(``),hasQuote:o}),a=[],o=!1);continue}}a.push(l)}if((a.length>0||o)&&s.push({value:a.join(``),hasQuote:o}),n||r)throw Error(`quote expression not closed`);return s}static _escapeRegExp(e){return e.replace(/[.*+?^${}()|[\]\\]/g,`\\$&`)}static async create(t={}){let{filesystem:n,permissions:r,...i}=t;if(g(n)||n&&typeof n==`object`&&g(n.filesystem)){let e=await _(n,r);n=e.filesystem,r=e.permissions}let a=new e(Object.assign({},i,{filesystem:n,permissions:r}));return await a.ready,a}static async fromContainer(t,n={}){return e.create(Object.assign({},n,{filesystem:t}))}};D.STATUS_READY=`READY`,D.STATUS_WORKING=`WORKING`,D.STATUS_DESTROYED=`DESTROYED`,D.Command=s,D.InteractiveCommand=c,D.WasmCommand=a,D.createWasmCommand=a.createGenericWasmCommand||a.createCommand,D.ReplCommand=A(),D.JsCommand=o,D.FS=h,D.BinaryContainer=q(),D.Storage=D.BinaryContainer.FilesystemStorage,globalThis.shell=D,globalThis.Shell=D,t.exports=D}))()})();
48
48
  //# sourceMappingURL=shell.min.js.map