just-bash 2.11.3 → 2.11.4

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.
@@ -703,7 +703,7 @@ ${e.terminator}`}function ha(e){return`(${ge(e.body)})${Ae(e.redirections)}`}fun
703
703
  `,exitCode:2,env:be(this.state.env,s?.env)});if(f instanceof RangeError)return this.logResult({stdout:"",stderr:`bash: ${f.message}
704
704
  `,exitCode:1,env:be(this.state.env,s?.env)});throw f}finally{u?.deactivate()}}async readFile(t){return this.fs.readFile(this.fs.resolvePath(this.state.cwd,t))}async writeFile(t,s){return this.fs.writeFile(this.fs.resolvePath(this.state.cwd,t),s)}getCwd(){return this.state.cwd}getEnv(){return Te(this.state.env)}registerTransformPlugin(t){this.transformPlugins.push(t)}transform(t){let s=ai(t),r=$e(s),n=Object.create(null);for(let i of this.transformPlugins){let o=i.transform({ast:r,metadata:n});r=o.ast,o.metadata&&(n={...n,...o.metadata})}return{script:ti(r),ast:r,metadata:n}}};function ai(e){let t=e.split(`
705
705
  `),s=[],r=[];for(let n=0;n<t.length;n++){let i=t[n];if(r.length>0){let c=r[r.length-1];if((c.stripTabs?i.replace(/^\t+/,""):i)===c.delimiter){s.push(i.trimStart()),r.pop();continue}s.push(i);continue}let o=i.trimStart();s.push(o);let a=/<<(-?)\s*(['"]?)([\w-]+)\2/g;for(let c of o.matchAll(a)){let l=c[1]==="-",u=c[3];r.push({delimiter:u,stripTabs:l})}}return s.join(`
706
- `)}import*as G from"node:fs";import*as pe from"node:path";import*as we from"node:fs";import*as oe from"node:path";function j(e){if(!e||e==="/")return"/";let t=e.endsWith("/")&&e!=="/"?e.slice(0,-1):e;t.startsWith("/")||(t=`/${t}`);let s=t.split("/").filter(n=>n&&n!=="."),r=[];for(let n of s)n===".."?r.pop():r.push(n);return`/${r.join("/")}`||"/"}function ts(e,t){return e===t||e.startsWith(`${t}/`)}function wt(e,t){try{let s=we.realpathSync(e);return ts(s,t)?s:null}catch(s){if(s.code==="ENOENT"){let r=oe.dirname(e);if(r===e)return null;let n=wt(r,t);if(n===null)return null;try{if(we.lstatSync(e).isSymbolicLink()){let o=we.readlinkSync(e),a=oe.isAbsolute(o)?o:oe.resolve(oe.dirname(e),o);if(wt(a,t)===null)return null}}catch{}return oe.join(n,oe.basename(e))}return null}}function li(e,t,s){let r=wt(e,s);if(r===null)return null;let n=oe.resolve(e),i=n.slice(t.length),o=r.slice(s.length);if(i!==o)return null;try{if(we.lstatSync(n).isSymbolicLink())return null}catch{}return r}function ci(e,t){if(!we.existsSync(e))throw new Error(`${t} root does not exist: ${e}`);if(!we.statSync(e).isDirectory())throw new Error(`${t} root is not a directory: ${e}`)}function J(e,t){if(e.includes("\0"))throw new Error(`ENOENT: path contains null byte, ${t} '${e}'`)}function ui(e,t){if(!oe.isAbsolute(e))return{withinRoot:!0,relativePath:e};let s;try{s=we.realpathSync(e)}catch{s=oe.resolve(e)}return ts(s,t)?{withinRoot:!0,relativePath:s.slice(t.length)||"/"}:{withinRoot:!1,safeName:oe.basename(e)}}var wa="/home/user/project",st=class{root;canonicalRoot;mountPoint;readOnly;maxFileReadSize;allowSymlinks;memory=new Map;deleted=new Set;constructor(t){this.root=pe.resolve(t.root);let s=t.mountPoint??wa;if(this.mountPoint=s==="/"?"/":s.replace(/\/+$/,""),!this.mountPoint.startsWith("/"))throw new Error(`Mount point must be an absolute path: ${s}`);this.readOnly=t.readOnly??!1,this.maxFileReadSize=t.maxFileReadSize??10485760,this.allowSymlinks=t.allowSymlinks??!1,ci(this.root,"OverlayFs"),this.canonicalRoot=G.realpathSync(this.root),this.createMountPointDirs()}assertWritable(t){if(this.readOnly)throw new Error(`EROFS: read-only file system, ${t}`)}createMountPointDirs(){let t=this.mountPoint.split("/").filter(Boolean),s="";for(let r of t)s+=`/${r}`,this.memory.has(s)||this.memory.set(s,{type:"directory",mode:493,mtime:new Date});this.memory.has("/")||this.memory.set("/",{type:"directory",mode:493,mtime:new Date})}getMountPoint(){return this.mountPoint}mkdirSync(t,s){let n=j(t).split("/").filter(Boolean),i="";for(let o of n)i+=`/${o}`,this.memory.has(i)||this.memory.set(i,{type:"directory",mode:493,mtime:new Date})}writeFileSync(t,s){let r=j(t),n=this.getDirname(r);n!=="/"&&this.mkdirSync(n);let i=s instanceof Uint8Array?s:new TextEncoder().encode(s);this.memory.set(r,{type:"file",content:i,mode:420,mtime:new Date})}getDirname(t){let s=t.lastIndexOf("/");return s===0?"/":t.slice(0,s)}getRelativeToMount(t){return this.mountPoint==="/"?t:t===this.mountPoint?"/":t.startsWith(`${this.mountPoint}/`)?t.slice(this.mountPoint.length):null}toRealPath(t){let s=j(t),r=this.getRelativeToMount(s);if(r===null)return null;let n=pe.join(this.root,r),i=pe.resolve(n);return ts(i,this.root)?i:null}dirname(t){let s=j(t);if(s==="/")return"/";let r=s.lastIndexOf("/");return r===0?"/":s.slice(0,r)}resolveRealPath_(t){return t?this.allowSymlinks?wt(t,this.canonicalRoot):li(t,this.root,this.canonicalRoot):null}resolveRealPathParent_(t){if(!t)return null;let s=pe.dirname(t),r=this.resolveRealPath_(s);return r===null?null:pe.join(r,pe.basename(t))}ensureParentDirs(t){let s=this.dirname(t);s!=="/"&&(this.memory.has(s)||(this.ensureParentDirs(s),this.memory.set(s,{type:"directory",mode:493,mtime:new Date})),this.deleted.delete(s))}async existsInOverlay(t){let s=j(t);if(this.deleted.has(s))return!1;if(this.memory.has(s))return!0;let r=this.resolveRealPathParent_(this.toRealPath(s));if(!r)return!1;try{return await G.promises.lstat(r),!0}catch{return!1}}async readFile(t,s){let r=await this.readFileBuffer(t),n=Le(s);return Pt(r,n)}async readFileBuffer(t,s=new Set){J(t,"open");let r=j(t);if(s.has(r))throw new Error(`ELOOP: too many levels of symbolic links, open '${t}'`);if(s.add(r),this.deleted.has(r))throw new Error(`ENOENT: no such file or directory, open '${t}'`);let n=this.memory.get(r);if(n){if(n.type==="symlink"){let o=this.resolveSymlink(r,n.target);return this.readFileBuffer(o,s)}if(n.type!=="file")throw new Error(`EISDIR: illegal operation on a directory, read '${t}'`);return n.content}let i=this.resolveRealPath_(this.toRealPath(r));if(!i)throw new Error(`ENOENT: no such file or directory, open '${t}'`);try{let o=await G.promises.lstat(i);if(o.isSymbolicLink()){if(!this.allowSymlinks)throw new Error(`ENOENT: no such file or directory, open '${t}'`);let c=await G.promises.readlink(i),l=this.realTargetToVirtual(r,c),u=this.resolveSymlink(r,l);return this.readFileBuffer(u,s)}if(o.isDirectory())throw new Error(`EISDIR: illegal operation on a directory, read '${t}'`);if(this.maxFileReadSize>0&&o.size>this.maxFileReadSize)throw new Error(`EFBIG: file too large, read '${t}' (${o.size} bytes, max ${this.maxFileReadSize})`);let a=await G.promises.readFile(i);return new Uint8Array(a)}catch(o){throw o.code==="ENOENT"?new Error(`ENOENT: no such file or directory, open '${t}'`):o}}async writeFile(t,s,r){J(t,"write"),this.assertWritable(`write '${t}'`);let n=j(t);this.ensureParentDirs(n);let i=Le(r),o=Ge(s,i);this.memory.set(n,{type:"file",content:o,mode:420,mtime:new Date}),this.deleted.delete(n)}async appendFile(t,s,r){J(t,"append"),this.assertWritable(`append '${t}'`);let n=j(t),i=Le(r),o=Ge(s,i),a;try{a=await this.readFileBuffer(n)}catch{a=new Uint8Array(0)}let c=new Uint8Array(a.length+o.length);c.set(a),c.set(o,a.length),this.ensureParentDirs(n),this.memory.set(n,{type:"file",content:c,mode:420,mtime:new Date}),this.deleted.delete(n)}async exists(t){return t.includes("\0")?!1:this.existsInOverlay(t)}async stat(t,s=new Set){J(t,"stat");let r=j(t);if(s.has(r))throw new Error(`ELOOP: too many levels of symbolic links, stat '${t}'`);if(s.add(r),this.deleted.has(r))throw new Error(`ENOENT: no such file or directory, stat '${t}'`);let n=this.memory.get(r);if(n){if(n.type==="symlink"){let a=this.resolveSymlink(r,n.target);return this.stat(a,s)}let o=0;return n.type==="file"&&(o=n.content.length),{isFile:n.type==="file",isDirectory:n.type==="directory",isSymbolicLink:!1,mode:n.mode,size:o,mtime:n.mtime}}let i=this.resolveRealPath_(this.toRealPath(r));if(!i)throw new Error(`ENOENT: no such file or directory, stat '${t}'`);try{let o=await G.promises.lstat(i);if(o.isSymbolicLink()){if(!this.allowSymlinks)throw new Error(`ENOENT: no such file or directory, stat '${t}'`);let a=await G.promises.readlink(i),c=this.realTargetToVirtual(r,a),l=this.resolveSymlink(r,c);return this.stat(l,s)}return{isFile:o.isFile(),isDirectory:o.isDirectory(),isSymbolicLink:!1,mode:o.mode,size:o.size,mtime:o.mtime}}catch(o){throw o.code==="ENOENT"?new Error(`ENOENT: no such file or directory, stat '${t}'`):o}}async lstat(t){J(t,"lstat");let s=j(t);if(this.deleted.has(s))throw new Error(`ENOENT: no such file or directory, lstat '${t}'`);let r=this.memory.get(s);if(r){if(r.type==="symlink")return{isFile:!1,isDirectory:!1,isSymbolicLink:!0,mode:r.mode,size:r.target.length,mtime:r.mtime};let i=0;return r.type==="file"&&(i=r.content.length),{isFile:r.type==="file",isDirectory:r.type==="directory",isSymbolicLink:!1,mode:r.mode,size:i,mtime:r.mtime}}let n=this.resolveRealPathParent_(this.toRealPath(s));if(!n)throw new Error(`ENOENT: no such file or directory, lstat '${t}'`);try{let i=await G.promises.lstat(n);return{isFile:i.isFile(),isDirectory:i.isDirectory(),isSymbolicLink:i.isSymbolicLink(),mode:i.mode,size:i.size,mtime:i.mtime}}catch(i){throw i.code==="ENOENT"?new Error(`ENOENT: no such file or directory, lstat '${t}'`):i}}resolveSymlink(t,s){if(s.startsWith("/"))return j(s);let r=this.dirname(t);return j(r==="/"?`/${s}`:`${r}/${s}`)}realTargetToVirtual(t,s){let r=ui(s,this.canonicalRoot);if(r.withinRoot){if(!pe.isAbsolute(s))return s;let n=r.relativePath;return this.mountPoint==="/"?n:`${this.mountPoint}${n}`}return r.safeName}async mkdir(t,s){J(t,"mkdir"),this.assertWritable(`mkdir '${t}'`);let r=j(t);if(await this.existsInOverlay(r)){if(!s?.recursive)throw new Error(`EEXIST: file already exists, mkdir '${t}'`);return}let i=this.dirname(r);if(i!=="/"&&!await this.existsInOverlay(i))if(s?.recursive)await this.mkdir(i,{recursive:!0});else throw new Error(`ENOENT: no such file or directory, mkdir '${t}'`);this.memory.set(r,{type:"directory",mode:493,mtime:new Date}),this.deleted.delete(r)}async readdirCore(t,s){if(this.deleted.has(s))throw new Error(`ENOENT: no such file or directory, scandir '${t}'`);let r=new Map,n=new Set,i=s==="/"?"/":`${s}/`;for(let a of this.deleted)if(a.startsWith(i)){let c=a.slice(i.length),l=c.split("/")[0];l&&!c.includes("/",l.length)&&n.add(l)}for(let[a,c]of this.memory)if(a!==s&&a.startsWith(i)){let l=a.slice(i.length),u=l.split("/")[0];u&&!n.has(u)&&!l.includes("/",1)&&r.set(u,{name:u,isFile:c.type==="file",isDirectory:c.type==="directory",isSymbolicLink:c.type==="symlink"})}let o=this.resolveRealPath_(this.toRealPath(s));if(o)try{let a=await G.promises.readdir(o,{withFileTypes:!0});for(let c of a)!n.has(c.name)&&!r.has(c.name)&&r.set(c.name,{name:c.name,isFile:c.isFile(),isDirectory:c.isDirectory(),isSymbolicLink:c.isSymbolicLink()})}catch(a){if(a.code==="ENOENT"){if(!this.memory.has(s))throw new Error(`ENOENT: no such file or directory, scandir '${t}'`)}else if(a.code!=="ENOTDIR")throw a}return r}async resolveForReaddir(t,s=!1){let r=j(t),n=new Set,i=s,o=this.memory.get(r);for(;o&&o.type==="symlink";){if(n.has(r))throw new Error(`ELOOP: too many levels of symbolic links, scandir '${t}'`);n.add(r),i=!0,r=this.resolveSymlink(r,o.target),o=this.memory.get(r)}if(o)return{normalized:r,outsideOverlay:!1};if(this.getRelativeToMount(r)===null)return{normalized:r,outsideOverlay:!0};let c=this.resolveRealPath_(this.toRealPath(r));if(!c)return{normalized:r,outsideOverlay:!0};try{if((await G.promises.lstat(c)).isSymbolicLink()){if(!this.allowSymlinks)return{normalized:r,outsideOverlay:!0};let u=await G.promises.readlink(c),f=this.realTargetToVirtual(r,u),d=this.resolveSymlink(r,f);return this.resolveForReaddir(d,!0)}return{normalized:r,outsideOverlay:!1}}catch{return i?{normalized:r,outsideOverlay:!0}:{normalized:r,outsideOverlay:!1}}}async readdir(t){J(t,"scandir");let{normalized:s,outsideOverlay:r}=await this.resolveForReaddir(t);if(r)return[];let n=await this.readdirCore(t,s);return Array.from(n.keys()).sort((i,o)=>i<o?-1:i>o?1:0)}async readdirWithFileTypes(t){J(t,"scandir");let{normalized:s,outsideOverlay:r}=await this.resolveForReaddir(t);if(r)return[];let n=await this.readdirCore(t,s);return Array.from(n.values()).sort((i,o)=>i.name<o.name?-1:i.name>o.name?1:0)}async rm(t,s){J(t,"rm"),this.assertWritable(`rm '${t}'`);let r=j(t);if(!await this.existsInOverlay(r)){if(s?.force)return;throw new Error(`ENOENT: no such file or directory, rm '${t}'`)}try{if((await this.stat(r)).isDirectory){let o=await this.readdir(r);if(o.length>0){if(!s?.recursive)throw new Error(`ENOTEMPTY: directory not empty, rm '${t}'`);for(let a of o){let c=r==="/"?`/${a}`:`${r}/${a}`;await this.rm(c,s)}}}}catch(i){if(i instanceof Error&&(i.message.includes("ENOTEMPTY")||i.message.includes("EISDIR")))throw i}this.deleted.add(r),this.memory.delete(r)}async cp(t,s,r){J(t,"cp"),J(s,"cp"),this.assertWritable(`cp '${s}'`);let n=j(t),i=j(s);if(!await this.existsInOverlay(n))throw new Error(`ENOENT: no such file or directory, cp '${t}'`);let a=await this.stat(n);if(a.isFile){let c=await this.readFileBuffer(n);await this.writeFile(i,c)}else if(a.isDirectory){if(!r?.recursive)throw new Error(`EISDIR: is a directory, cp '${t}'`);await this.mkdir(i,{recursive:!0});let c=await this.readdir(n);for(let l of c){let u=n==="/"?`/${l}`:`${n}/${l}`,f=i==="/"?`/${l}`:`${i}/${l}`;await this.cp(u,f,r)}}}async mv(t,s){this.assertWritable(`mv '${s}'`),await this.cp(t,s,{recursive:!0}),await this.rm(t,{recursive:!0})}resolvePath(t,s){if(s.startsWith("/"))return j(s);let r=t==="/"?`/${s}`:`${t}/${s}`;return j(r)}getAllPaths(){let t=new Set(this.memory.keys());for(let s of this.deleted)t.delete(s);return this.scanRealFs("/",t),Array.from(t)}scanRealFs(t,s){if(this.deleted.has(t))return;let r=this.resolveRealPath_(this.toRealPath(t));if(r)try{let n=G.readdirSync(r);for(let i of n){let o=t==="/"?`/${i}`:`${t}/${i}`;if(this.deleted.has(o))continue;s.add(o);let a=pe.join(r,i);G.lstatSync(a).isDirectory()&&this.scanRealFs(o,s)}}catch{}}async chmod(t,s){J(t,"chmod"),this.assertWritable(`chmod '${t}'`);let r=j(t);if(!await this.existsInOverlay(r))throw new Error(`ENOENT: no such file or directory, chmod '${t}'`);let i=this.memory.get(r);if(i){i.mode=s;return}let o=await this.stat(r);if(o.isFile){let a=await this.readFileBuffer(r);this.memory.set(r,{type:"file",content:a,mode:s,mtime:new Date})}else o.isDirectory&&this.memory.set(r,{type:"directory",mode:s,mtime:new Date})}async symlink(t,s){if(!this.allowSymlinks)throw new Error(`EPERM: operation not permitted, symlink '${s}'`);J(s,"symlink"),this.assertWritable(`symlink '${s}'`);let r=j(s);if(await this.existsInOverlay(r))throw new Error(`EEXIST: file already exists, symlink '${s}'`);this.ensureParentDirs(r),this.memory.set(r,{type:"symlink",target:t,mode:511,mtime:new Date}),this.deleted.delete(r)}async link(t,s){J(t,"link"),J(s,"link"),this.assertWritable(`link '${s}'`);let r=j(t),n=j(s);if(!await this.existsInOverlay(r))throw new Error(`ENOENT: no such file or directory, link '${t}'`);let o=await this.stat(r);if(!o.isFile)throw new Error(`EPERM: operation not permitted, link '${t}'`);if(await this.existsInOverlay(n))throw new Error(`EEXIST: file already exists, link '${s}'`);let c=await this.readFileBuffer(r);this.ensureParentDirs(n),this.memory.set(n,{type:"file",content:c,mode:o.mode,mtime:new Date}),this.deleted.delete(n)}async readlink(t){J(t,"readlink");let s=j(t);if(this.deleted.has(s))throw new Error(`ENOENT: no such file or directory, readlink '${t}'`);let r=this.memory.get(s);if(r){if(r.type!=="symlink")throw new Error(`EINVAL: invalid argument, readlink '${t}'`);return r.target}let n=this.resolveRealPathParent_(this.toRealPath(s));if(!n)throw new Error(`ENOENT: no such file or directory, readlink '${t}'`);try{let i=await G.promises.readlink(n);return this.realTargetToVirtual(s,i)}catch(i){throw i.code==="ENOENT"?new Error(`ENOENT: no such file or directory, readlink '${t}'`):i.code==="EINVAL"?new Error(`EINVAL: invalid argument, readlink '${t}'`):i}}async realpath(t){J(t,"realpath");let s=j(t),r=new Set,n=async a=>{let c=a==="/"?[]:a.slice(1).split("/"),l="";for(let u of c){if(l=`${l}/${u}`,r.has(l))throw new Error(`ELOOP: too many levels of symbolic links, realpath '${t}'`);if(this.deleted.has(l))throw new Error(`ENOENT: no such file or directory, realpath '${t}'`);let f=this.memory.get(l),d=0,h=40;for(;f&&f.type==="symlink"&&d<h;){if(r.add(l),l=this.resolveSymlink(l,f.target),d++,r.has(l))throw new Error(`ELOOP: too many levels of symbolic links, realpath '${t}'`);if(this.deleted.has(l))throw new Error(`ENOENT: no such file or directory, realpath '${t}'`);f=this.memory.get(l)}if(d>=h)throw new Error(`ELOOP: too many levels of symbolic links, realpath '${t}'`);if(!f){let m=this.toRealPath(l),p=this.resolveRealPath_(m);if(p)try{if((await G.promises.lstat(p)).isSymbolicLink()){if(!this.allowSymlinks)throw new Error(`ENOENT: no such file or directory, realpath '${t}'`);let S=await G.promises.readlink(p),v=this.realTargetToVirtual(l,S);return r.add(l),l=this.resolveSymlink(l,v),n(l)}}catch(g){throw g.code==="ENOENT"?new Error(`ENOENT: no such file or directory, realpath '${t}'`):g}else if(!this.allowSymlinks){let g=this.resolveRealPathParent_(m);if(g)try{if((await G.promises.lstat(g)).isSymbolicLink())throw new Error(`ENOENT: no such file or directory, realpath '${t}'`)}catch(S){throw S.message?.includes("ENOENT")||S.message?.includes("ELOOP")?new Error(`ENOENT: no such file or directory, realpath '${t}'`):S}}}}return l||"/"},i=await n(s);if(!await this.existsInOverlay(i))throw new Error(`ENOENT: no such file or directory, realpath '${t}'`);return i}async utimes(t,s,r){J(t,"utimes"),this.assertWritable(`utimes '${t}'`);let n=j(t);if(!await this.existsInOverlay(n))throw new Error(`ENOENT: no such file or directory, utimes '${t}'`);let o=this.memory.get(n);if(o){o.mtime=r;return}let a=await this.stat(n);if(a.isFile){let c=await this.readFileBuffer(n);this.memory.set(n,{type:"file",content:c,mode:a.mode,mtime:r})}else a.isDirectory&&this.memory.set(n,{type:"directory",mode:a.mode,mtime:r})}};function di(){console.log(`just-bash - A secure bash environment for AI agents
706
+ `)}import*as G from"node:fs";import*as pe from"node:path";import*as we from"node:fs";import*as oe from"node:path";function j(e){if(!e||e==="/")return"/";let t=e.endsWith("/")&&e!=="/"?e.slice(0,-1):e;t.startsWith("/")||(t=`/${t}`);let s=t.split("/").filter(n=>n&&n!=="."),r=[];for(let n of s)n===".."?r.pop():r.push(n);return`/${r.join("/")}`||"/"}function ts(e,t){return e===t||e.startsWith(`${t}/`)}function wt(e,t){try{let s=we.realpathSync(e);return ts(s,t)?s:null}catch(s){if(s.code==="ENOENT"){let r=oe.dirname(e);if(r===e)return null;let n=wt(r,t);if(n===null)return null;try{if(we.lstatSync(e).isSymbolicLink()){let o=we.readlinkSync(e),a=oe.isAbsolute(o)?o:oe.resolve(oe.dirname(e),o);if(wt(a,t)===null)return null}}catch{}return oe.join(n,oe.basename(e))}return null}}function li(e,t,s){let r=wt(e,s);if(r===null)return null;let n=oe.resolve(e),i=n.slice(t.length),o=r.slice(s.length);if(i!==o)return null;try{if(we.lstatSync(n).isSymbolicLink())return null}catch{}return r}function ci(e,t){if(!we.existsSync(e))throw new Error(`${t} root does not exist: ${e}`);if(!we.statSync(e).isDirectory())throw new Error(`${t} root is not a directory: ${e}`)}function J(e,t){if(e.includes("\0"))throw new Error(`ENOENT: path contains null byte, ${t} '${e}'`)}function ui(e,t){if(!oe.isAbsolute(e))return{withinRoot:!0,relativePath:e};let s;try{s=we.realpathSync(e)}catch{s=oe.resolve(e)}return ts(s,t)?{withinRoot:!0,relativePath:s.slice(t.length)||"/"}:{withinRoot:!1,safeName:oe.basename(e)}}var wa="/home/user/project",st=class{root;canonicalRoot;mountPoint;readOnly;maxFileReadSize;allowSymlinks;memory=new Map;deleted=new Set;constructor(t){this.root=pe.resolve(t.root);let s=t.mountPoint??wa;if(this.mountPoint=s==="/"?"/":s.replace(/\/+$/,""),!this.mountPoint.startsWith("/"))throw new Error(`Mount point must be an absolute path: ${s}`);this.readOnly=t.readOnly??!1,this.maxFileReadSize=t.maxFileReadSize??10485760,this.allowSymlinks=t.allowSymlinks??!1,ci(this.root,"OverlayFs"),this.canonicalRoot=G.realpathSync(this.root),this.createMountPointDirs()}assertWritable(t){if(this.readOnly)throw new Error(`EROFS: read-only file system, ${t}`)}createMountPointDirs(){let t=this.mountPoint.split("/").filter(Boolean),s="";for(let r of t)s+=`/${r}`,this.memory.has(s)||this.memory.set(s,{type:"directory",mode:493,mtime:new Date});this.memory.has("/")||this.memory.set("/",{type:"directory",mode:493,mtime:new Date})}getMountPoint(){return this.mountPoint}mkdirSync(t,s){let n=j(t).split("/").filter(Boolean),i="";for(let o of n)i+=`/${o}`,this.memory.has(i)||this.memory.set(i,{type:"directory",mode:493,mtime:new Date})}writeFileSync(t,s){let r=j(t),n=this.getDirname(r);n!=="/"&&this.mkdirSync(n);let i=s instanceof Uint8Array?s:new TextEncoder().encode(s);this.memory.set(r,{type:"file",content:i,mode:420,mtime:new Date})}getDirname(t){let s=t.lastIndexOf("/");return s===0?"/":t.slice(0,s)}getRelativeToMount(t){return this.mountPoint==="/"?t:t===this.mountPoint?"/":t.startsWith(`${this.mountPoint}/`)?t.slice(this.mountPoint.length):null}toRealPath(t){let s=j(t),r=this.getRelativeToMount(s);if(r===null)return null;let n=pe.join(this.root,r),i=pe.resolve(n);return ts(i,this.root)?i:null}dirname(t){let s=j(t);if(s==="/")return"/";let r=s.lastIndexOf("/");return r===0?"/":s.slice(0,r)}resolveRealPath_(t){return t?this.allowSymlinks?wt(t,this.canonicalRoot):li(t,this.root,this.canonicalRoot):null}resolveRealPathParent_(t){if(!t)return null;let s=pe.dirname(t),r=this.resolveRealPath_(s);return r===null?null:pe.join(r,pe.basename(t))}sanitizeError(t,s,r){let n=t;if(n.message?.includes("ELOOP")||n.message?.includes("EFBIG")||n.message?.includes("EPERM"))throw t;let i=n.code||"EIO";throw new Error(`${i}: ${r} '${s}'`)}ensureParentDirs(t){let s=this.dirname(t);s!=="/"&&(this.memory.has(s)||(this.ensureParentDirs(s),this.memory.set(s,{type:"directory",mode:493,mtime:new Date})),this.deleted.delete(s))}async existsInOverlay(t){let s=j(t);if(this.deleted.has(s))return!1;if(this.memory.has(s))return!0;let r=this.resolveRealPathParent_(this.toRealPath(s));if(!r)return!1;try{return await G.promises.lstat(r),!0}catch{return!1}}async readFile(t,s){let r=await this.readFileBuffer(t),n=Le(s);return Pt(r,n)}async readFileBuffer(t,s=new Set){J(t,"open");let r=j(t);if(s.has(r))throw new Error(`ELOOP: too many levels of symbolic links, open '${t}'`);if(s.add(r),this.deleted.has(r))throw new Error(`ENOENT: no such file or directory, open '${t}'`);let n=this.memory.get(r);if(n){if(n.type==="symlink"){let o=this.resolveSymlink(r,n.target);return this.readFileBuffer(o,s)}if(n.type!=="file")throw new Error(`EISDIR: illegal operation on a directory, read '${t}'`);return n.content}let i=this.resolveRealPath_(this.toRealPath(r));if(!i)throw new Error(`ENOENT: no such file or directory, open '${t}'`);try{let o=await G.promises.lstat(i);if(o.isSymbolicLink()){if(!this.allowSymlinks)throw new Error(`ENOENT: no such file or directory, open '${t}'`);let c=await G.promises.readlink(i),l=this.realTargetToVirtual(r,c),u=this.resolveSymlink(r,l);return this.readFileBuffer(u,s)}if(o.isDirectory())throw new Error(`EISDIR: illegal operation on a directory, read '${t}'`);if(this.maxFileReadSize>0&&o.size>this.maxFileReadSize)throw new Error(`EFBIG: file too large, read '${t}' (${o.size} bytes, max ${this.maxFileReadSize})`);let a=await G.promises.readFile(i);return new Uint8Array(a)}catch(o){if(o.code==="ENOENT")throw new Error(`ENOENT: no such file or directory, open '${t}'`);this.sanitizeError(o,t,"open")}}async writeFile(t,s,r){J(t,"write"),this.assertWritable(`write '${t}'`);let n=j(t);this.ensureParentDirs(n);let i=Le(r),o=Ge(s,i);this.memory.set(n,{type:"file",content:o,mode:420,mtime:new Date}),this.deleted.delete(n)}async appendFile(t,s,r){J(t,"append"),this.assertWritable(`append '${t}'`);let n=j(t),i=Le(r),o=Ge(s,i),a;try{a=await this.readFileBuffer(n)}catch{a=new Uint8Array(0)}let c=new Uint8Array(a.length+o.length);c.set(a),c.set(o,a.length),this.ensureParentDirs(n),this.memory.set(n,{type:"file",content:c,mode:420,mtime:new Date}),this.deleted.delete(n)}async exists(t){return t.includes("\0")?!1:this.existsInOverlay(t)}async stat(t,s=new Set){J(t,"stat");let r=j(t);if(s.has(r))throw new Error(`ELOOP: too many levels of symbolic links, stat '${t}'`);if(s.add(r),this.deleted.has(r))throw new Error(`ENOENT: no such file or directory, stat '${t}'`);let n=this.memory.get(r);if(n){if(n.type==="symlink"){let a=this.resolveSymlink(r,n.target);return this.stat(a,s)}let o=0;return n.type==="file"&&(o=n.content.length),{isFile:n.type==="file",isDirectory:n.type==="directory",isSymbolicLink:!1,mode:n.mode,size:o,mtime:n.mtime}}let i=this.resolveRealPath_(this.toRealPath(r));if(!i)throw new Error(`ENOENT: no such file or directory, stat '${t}'`);try{let o=await G.promises.lstat(i);if(o.isSymbolicLink()){if(!this.allowSymlinks)throw new Error(`ENOENT: no such file or directory, stat '${t}'`);let a=await G.promises.readlink(i),c=this.realTargetToVirtual(r,a),l=this.resolveSymlink(r,c);return this.stat(l,s)}return{isFile:o.isFile(),isDirectory:o.isDirectory(),isSymbolicLink:!1,mode:o.mode,size:o.size,mtime:o.mtime}}catch(o){if(o.code==="ENOENT")throw new Error(`ENOENT: no such file or directory, stat '${t}'`);this.sanitizeError(o,t,"stat")}}async lstat(t){J(t,"lstat");let s=j(t);if(this.deleted.has(s))throw new Error(`ENOENT: no such file or directory, lstat '${t}'`);let r=this.memory.get(s);if(r){if(r.type==="symlink")return{isFile:!1,isDirectory:!1,isSymbolicLink:!0,mode:r.mode,size:r.target.length,mtime:r.mtime};let i=0;return r.type==="file"&&(i=r.content.length),{isFile:r.type==="file",isDirectory:r.type==="directory",isSymbolicLink:!1,mode:r.mode,size:i,mtime:r.mtime}}let n=this.resolveRealPathParent_(this.toRealPath(s));if(!n)throw new Error(`ENOENT: no such file or directory, lstat '${t}'`);try{let i=await G.promises.lstat(n);return{isFile:i.isFile(),isDirectory:i.isDirectory(),isSymbolicLink:i.isSymbolicLink(),mode:i.mode,size:i.size,mtime:i.mtime}}catch(i){if(i.code==="ENOENT")throw new Error(`ENOENT: no such file or directory, lstat '${t}'`);this.sanitizeError(i,t,"lstat")}}resolveSymlink(t,s){if(s.startsWith("/"))return j(s);let r=this.dirname(t);return j(r==="/"?`/${s}`:`${r}/${s}`)}realTargetToVirtual(t,s){let r=ui(s,this.canonicalRoot);if(r.withinRoot){if(!pe.isAbsolute(s))return s;let n=r.relativePath;return this.mountPoint==="/"?n:`${this.mountPoint}${n}`}return r.safeName}async mkdir(t,s){J(t,"mkdir"),this.assertWritable(`mkdir '${t}'`);let r=j(t);if(await this.existsInOverlay(r)){if(!s?.recursive)throw new Error(`EEXIST: file already exists, mkdir '${t}'`);return}let i=this.dirname(r);if(i!=="/"&&!await this.existsInOverlay(i))if(s?.recursive)await this.mkdir(i,{recursive:!0});else throw new Error(`ENOENT: no such file or directory, mkdir '${t}'`);this.memory.set(r,{type:"directory",mode:493,mtime:new Date}),this.deleted.delete(r)}async readdirCore(t,s){if(this.deleted.has(s))throw new Error(`ENOENT: no such file or directory, scandir '${t}'`);let r=new Map,n=new Set,i=s==="/"?"/":`${s}/`;for(let a of this.deleted)if(a.startsWith(i)){let c=a.slice(i.length),l=c.split("/")[0];l&&!c.includes("/",l.length)&&n.add(l)}for(let[a,c]of this.memory)if(a!==s&&a.startsWith(i)){let l=a.slice(i.length),u=l.split("/")[0];u&&!n.has(u)&&!l.includes("/",1)&&r.set(u,{name:u,isFile:c.type==="file",isDirectory:c.type==="directory",isSymbolicLink:c.type==="symlink"})}let o=this.resolveRealPath_(this.toRealPath(s));if(o)try{let a=await G.promises.readdir(o,{withFileTypes:!0});for(let c of a)!n.has(c.name)&&!r.has(c.name)&&r.set(c.name,{name:c.name,isFile:c.isFile(),isDirectory:c.isDirectory(),isSymbolicLink:c.isSymbolicLink()})}catch(a){if(a.code==="ENOENT"){if(!this.memory.has(s))throw new Error(`ENOENT: no such file or directory, scandir '${t}'`)}else a.code!=="ENOTDIR"&&this.sanitizeError(a,t,"scandir")}return r}async resolveForReaddir(t,s=!1){let r=j(t),n=new Set,i=s,o=this.memory.get(r);for(;o&&o.type==="symlink";){if(n.has(r))throw new Error(`ELOOP: too many levels of symbolic links, scandir '${t}'`);n.add(r),i=!0,r=this.resolveSymlink(r,o.target),o=this.memory.get(r)}if(o)return{normalized:r,outsideOverlay:!1};if(this.getRelativeToMount(r)===null)return{normalized:r,outsideOverlay:!0};let c=this.resolveRealPath_(this.toRealPath(r));if(!c)return{normalized:r,outsideOverlay:!0};try{if((await G.promises.lstat(c)).isSymbolicLink()){if(!this.allowSymlinks)return{normalized:r,outsideOverlay:!0};let u=await G.promises.readlink(c),f=this.realTargetToVirtual(r,u),d=this.resolveSymlink(r,f);return this.resolveForReaddir(d,!0)}return{normalized:r,outsideOverlay:!1}}catch{return i?{normalized:r,outsideOverlay:!0}:{normalized:r,outsideOverlay:!1}}}async readdir(t){J(t,"scandir");let{normalized:s,outsideOverlay:r}=await this.resolveForReaddir(t);if(r)return[];let n=await this.readdirCore(t,s);return Array.from(n.keys()).sort((i,o)=>i<o?-1:i>o?1:0)}async readdirWithFileTypes(t){J(t,"scandir");let{normalized:s,outsideOverlay:r}=await this.resolveForReaddir(t);if(r)return[];let n=await this.readdirCore(t,s);return Array.from(n.values()).sort((i,o)=>i.name<o.name?-1:i.name>o.name?1:0)}async rm(t,s){J(t,"rm"),this.assertWritable(`rm '${t}'`);let r=j(t);if(!await this.existsInOverlay(r)){if(s?.force)return;throw new Error(`ENOENT: no such file or directory, rm '${t}'`)}try{if((await this.stat(r)).isDirectory){let o=await this.readdir(r);if(o.length>0){if(!s?.recursive)throw new Error(`ENOTEMPTY: directory not empty, rm '${t}'`);for(let a of o){let c=r==="/"?`/${a}`:`${r}/${a}`;await this.rm(c,s)}}}}catch(i){if(i instanceof Error&&(i.message.includes("ENOTEMPTY")||i.message.includes("EISDIR")))throw i}this.deleted.add(r),this.memory.delete(r)}async cp(t,s,r){J(t,"cp"),J(s,"cp"),this.assertWritable(`cp '${s}'`);let n=j(t),i=j(s);if(!await this.existsInOverlay(n))throw new Error(`ENOENT: no such file or directory, cp '${t}'`);let a=await this.stat(n);if(a.isFile){let c=await this.readFileBuffer(n);await this.writeFile(i,c)}else if(a.isDirectory){if(!r?.recursive)throw new Error(`EISDIR: is a directory, cp '${t}'`);await this.mkdir(i,{recursive:!0});let c=await this.readdir(n);for(let l of c){let u=n==="/"?`/${l}`:`${n}/${l}`,f=i==="/"?`/${l}`:`${i}/${l}`;await this.cp(u,f,r)}}}async mv(t,s){this.assertWritable(`mv '${s}'`),await this.cp(t,s,{recursive:!0}),await this.rm(t,{recursive:!0})}resolvePath(t,s){if(s.startsWith("/"))return j(s);let r=t==="/"?`/${s}`:`${t}/${s}`;return j(r)}getAllPaths(){let t=new Set(this.memory.keys());for(let s of this.deleted)t.delete(s);return this.scanRealFs("/",t),Array.from(t)}scanRealFs(t,s){if(this.deleted.has(t))return;let r=this.resolveRealPath_(this.toRealPath(t));if(r)try{let n=G.readdirSync(r);for(let i of n){let o=t==="/"?`/${i}`:`${t}/${i}`;if(this.deleted.has(o))continue;s.add(o);let a=pe.join(r,i);G.lstatSync(a).isDirectory()&&this.scanRealFs(o,s)}}catch{}}async chmod(t,s){J(t,"chmod"),this.assertWritable(`chmod '${t}'`);let r=j(t);if(!await this.existsInOverlay(r))throw new Error(`ENOENT: no such file or directory, chmod '${t}'`);let i=this.memory.get(r);if(i){i.mode=s;return}let o=await this.stat(r);if(o.isFile){let a=await this.readFileBuffer(r);this.memory.set(r,{type:"file",content:a,mode:s,mtime:new Date})}else o.isDirectory&&this.memory.set(r,{type:"directory",mode:s,mtime:new Date})}async symlink(t,s){if(!this.allowSymlinks)throw new Error(`EPERM: operation not permitted, symlink '${s}'`);J(s,"symlink"),this.assertWritable(`symlink '${s}'`);let r=j(s);if(await this.existsInOverlay(r))throw new Error(`EEXIST: file already exists, symlink '${s}'`);this.ensureParentDirs(r),this.memory.set(r,{type:"symlink",target:t,mode:511,mtime:new Date}),this.deleted.delete(r)}async link(t,s){J(t,"link"),J(s,"link"),this.assertWritable(`link '${s}'`);let r=j(t),n=j(s);if(!await this.existsInOverlay(r))throw new Error(`ENOENT: no such file or directory, link '${t}'`);let o=await this.stat(r);if(!o.isFile)throw new Error(`EPERM: operation not permitted, link '${t}'`);if(await this.existsInOverlay(n))throw new Error(`EEXIST: file already exists, link '${s}'`);let c=await this.readFileBuffer(r);this.ensureParentDirs(n),this.memory.set(n,{type:"file",content:c,mode:o.mode,mtime:new Date}),this.deleted.delete(n)}async readlink(t){J(t,"readlink");let s=j(t);if(this.deleted.has(s))throw new Error(`ENOENT: no such file or directory, readlink '${t}'`);let r=this.memory.get(s);if(r){if(r.type!=="symlink")throw new Error(`EINVAL: invalid argument, readlink '${t}'`);return r.target}let n=this.resolveRealPathParent_(this.toRealPath(s));if(!n)throw new Error(`ENOENT: no such file or directory, readlink '${t}'`);try{let i=await G.promises.readlink(n);return this.realTargetToVirtual(s,i)}catch(i){if(i.code==="ENOENT")throw new Error(`ENOENT: no such file or directory, readlink '${t}'`);if(i.code==="EINVAL")throw new Error(`EINVAL: invalid argument, readlink '${t}'`);this.sanitizeError(i,t,"readlink")}}async realpath(t){J(t,"realpath");let s=j(t),r=new Set,n=async a=>{let c=a==="/"?[]:a.slice(1).split("/"),l="";for(let u of c){if(l=`${l}/${u}`,r.has(l))throw new Error(`ELOOP: too many levels of symbolic links, realpath '${t}'`);if(this.deleted.has(l))throw new Error(`ENOENT: no such file or directory, realpath '${t}'`);let f=this.memory.get(l),d=0,h=40;for(;f&&f.type==="symlink"&&d<h;){if(r.add(l),l=this.resolveSymlink(l,f.target),d++,r.has(l))throw new Error(`ELOOP: too many levels of symbolic links, realpath '${t}'`);if(this.deleted.has(l))throw new Error(`ENOENT: no such file or directory, realpath '${t}'`);f=this.memory.get(l)}if(d>=h)throw new Error(`ELOOP: too many levels of symbolic links, realpath '${t}'`);if(!f){let m=this.toRealPath(l),p=this.resolveRealPath_(m);if(p)try{if((await G.promises.lstat(p)).isSymbolicLink()){if(!this.allowSymlinks)throw new Error(`ENOENT: no such file or directory, realpath '${t}'`);let S=await G.promises.readlink(p),v=this.realTargetToVirtual(l,S);return r.add(l),l=this.resolveSymlink(l,v),n(l)}}catch(g){if(g.code==="ENOENT")throw new Error(`ENOENT: no such file or directory, realpath '${t}'`);this.sanitizeError(g,t,"realpath")}else if(!this.allowSymlinks){let g=this.resolveRealPathParent_(m);if(g)try{if((await G.promises.lstat(g)).isSymbolicLink())throw new Error(`ENOENT: no such file or directory, realpath '${t}'`)}catch(S){if(S.message?.includes("ENOENT")||S.message?.includes("ELOOP"))throw new Error(`ENOENT: no such file or directory, realpath '${t}'`);this.sanitizeError(S,t,"realpath")}}}}return l||"/"},i=await n(s);if(!await this.existsInOverlay(i))throw new Error(`ENOENT: no such file or directory, realpath '${t}'`);return i}async utimes(t,s,r){J(t,"utimes"),this.assertWritable(`utimes '${t}'`);let n=j(t);if(!await this.existsInOverlay(n))throw new Error(`ENOENT: no such file or directory, utimes '${t}'`);let o=this.memory.get(n);if(o){o.mtime=r;return}let a=await this.stat(n);if(a.isFile){let c=await this.readFileBuffer(n);this.memory.set(n,{type:"file",content:c,mode:a.mode,mtime:r})}else a.isDirectory&&this.memory.set(n,{type:"directory",mode:a.mode,mtime:r})}};function di(){console.log(`just-bash - A secure bash environment for AI agents
707
707
 
708
708
  Usage:
709
709
  just-bash [options] [script-file]
@@ -703,7 +703,7 @@ ${e.terminator}`}function ma(e){return`(${we(e.body)})${Ce(e.redirections)}`}fun
703
703
  `,exitCode:2,env:Ee(this.state.env,s?.env)});if(f instanceof RangeError)return this.logResult({stdout:"",stderr:`bash: ${f.message}
704
704
  `,exitCode:1,env:Ee(this.state.env,s?.env)});throw f}finally{u?.deactivate()}}async readFile(t){return this.fs.readFile(this.fs.resolvePath(this.state.cwd,t))}async writeFile(t,s){return this.fs.writeFile(this.fs.resolvePath(this.state.cwd,t),s)}getCwd(){return this.state.cwd}getEnv(){return Ie(this.state.env)}registerTransformPlugin(t){this.transformPlugins.push(t)}transform(t){let s=ci(t),n=Se(s),r=Object.create(null);for(let i of this.transformPlugins){let o=i.transform({ast:n,metadata:r});n=o.ast,o.metadata&&(r={...r,...o.metadata})}return{script:ni(n),ast:n,metadata:r}}};function ci(e){let t=e.split(`
705
705
  `),s=[],n=[];for(let r=0;r<t.length;r++){let i=t[r];if(n.length>0){let l=n[n.length-1];if((l.stripTabs?i.replace(/^\t+/,""):i)===l.delimiter){s.push(i.trimStart()),n.pop();continue}s.push(i);continue}let o=i.trimStart();s.push(o);let a=/<<(-?)\s*(['"]?)([\w-]+)\2/g;for(let l of o.matchAll(a)){let c=l[1]==="-",u=l[3];n.push({delimiter:u,stripTabs:c})}}return s.join(`
706
- `)}import*as G from"node:fs";import*as me from"node:path";import*as ve from"node:fs";import*as ae from"node:path";function j(e){if(!e||e==="/")return"/";let t=e.endsWith("/")&&e!=="/"?e.slice(0,-1):e;t.startsWith("/")||(t=`/${t}`);let s=t.split("/").filter(r=>r&&r!=="."),n=[];for(let r of s)r===".."?n.pop():n.push(r);return`/${n.join("/")}`||"/"}function ts(e,t){return e===t||e.startsWith(`${t}/`)}function wt(e,t){try{let s=ve.realpathSync(e);return ts(s,t)?s:null}catch(s){if(s.code==="ENOENT"){let n=ae.dirname(e);if(n===e)return null;let r=wt(n,t);if(r===null)return null;try{if(ve.lstatSync(e).isSymbolicLink()){let o=ve.readlinkSync(e),a=ae.isAbsolute(o)?o:ae.resolve(ae.dirname(e),o);if(wt(a,t)===null)return null}}catch{}return ae.join(r,ae.basename(e))}return null}}function ui(e,t,s){let n=wt(e,s);if(n===null)return null;let r=ae.resolve(e),i=r.slice(t.length),o=n.slice(s.length);if(i!==o)return null;try{if(ve.lstatSync(r).isSymbolicLink())return null}catch{}return n}function fi(e,t){if(!ve.existsSync(e))throw new Error(`${t} root does not exist: ${e}`);if(!ve.statSync(e).isDirectory())throw new Error(`${t} root is not a directory: ${e}`)}function J(e,t){if(e.includes("\0"))throw new Error(`ENOENT: path contains null byte, ${t} '${e}'`)}function di(e,t){if(!ae.isAbsolute(e))return{withinRoot:!0,relativePath:e};let s;try{s=ve.realpathSync(e)}catch{s=ae.resolve(e)}return ts(s,t)?{withinRoot:!0,relativePath:s.slice(t.length)||"/"}:{withinRoot:!1,safeName:ae.basename(e)}}var ba="/home/user/project",ss=class{root;canonicalRoot;mountPoint;readOnly;maxFileReadSize;allowSymlinks;memory=new Map;deleted=new Set;constructor(t){this.root=me.resolve(t.root);let s=t.mountPoint??ba;if(this.mountPoint=s==="/"?"/":s.replace(/\/+$/,""),!this.mountPoint.startsWith("/"))throw new Error(`Mount point must be an absolute path: ${s}`);this.readOnly=t.readOnly??!1,this.maxFileReadSize=t.maxFileReadSize??10485760,this.allowSymlinks=t.allowSymlinks??!1,fi(this.root,"OverlayFs"),this.canonicalRoot=G.realpathSync(this.root),this.createMountPointDirs()}assertWritable(t){if(this.readOnly)throw new Error(`EROFS: read-only file system, ${t}`)}createMountPointDirs(){let t=this.mountPoint.split("/").filter(Boolean),s="";for(let n of t)s+=`/${n}`,this.memory.has(s)||this.memory.set(s,{type:"directory",mode:493,mtime:new Date});this.memory.has("/")||this.memory.set("/",{type:"directory",mode:493,mtime:new Date})}getMountPoint(){return this.mountPoint}mkdirSync(t,s){let r=j(t).split("/").filter(Boolean),i="";for(let o of r)i+=`/${o}`,this.memory.has(i)||this.memory.set(i,{type:"directory",mode:493,mtime:new Date})}writeFileSync(t,s){let n=j(t),r=this.getDirname(n);r!=="/"&&this.mkdirSync(r);let i=s instanceof Uint8Array?s:new TextEncoder().encode(s);this.memory.set(n,{type:"file",content:i,mode:420,mtime:new Date})}getDirname(t){let s=t.lastIndexOf("/");return s===0?"/":t.slice(0,s)}getRelativeToMount(t){return this.mountPoint==="/"?t:t===this.mountPoint?"/":t.startsWith(`${this.mountPoint}/`)?t.slice(this.mountPoint.length):null}toRealPath(t){let s=j(t),n=this.getRelativeToMount(s);if(n===null)return null;let r=me.join(this.root,n),i=me.resolve(r);return ts(i,this.root)?i:null}dirname(t){let s=j(t);if(s==="/")return"/";let n=s.lastIndexOf("/");return n===0?"/":s.slice(0,n)}resolveRealPath_(t){return t?this.allowSymlinks?wt(t,this.canonicalRoot):ui(t,this.root,this.canonicalRoot):null}resolveRealPathParent_(t){if(!t)return null;let s=me.dirname(t),n=this.resolveRealPath_(s);return n===null?null:me.join(n,me.basename(t))}ensureParentDirs(t){let s=this.dirname(t);s!=="/"&&(this.memory.has(s)||(this.ensureParentDirs(s),this.memory.set(s,{type:"directory",mode:493,mtime:new Date})),this.deleted.delete(s))}async existsInOverlay(t){let s=j(t);if(this.deleted.has(s))return!1;if(this.memory.has(s))return!0;let n=this.resolveRealPathParent_(this.toRealPath(s));if(!n)return!1;try{return await G.promises.lstat(n),!0}catch{return!1}}async readFile(t,s){let n=await this.readFileBuffer(t),r=Fe(s);return Pt(n,r)}async readFileBuffer(t,s=new Set){J(t,"open");let n=j(t);if(s.has(n))throw new Error(`ELOOP: too many levels of symbolic links, open '${t}'`);if(s.add(n),this.deleted.has(n))throw new Error(`ENOENT: no such file or directory, open '${t}'`);let r=this.memory.get(n);if(r){if(r.type==="symlink"){let o=this.resolveSymlink(n,r.target);return this.readFileBuffer(o,s)}if(r.type!=="file")throw new Error(`EISDIR: illegal operation on a directory, read '${t}'`);return r.content}let i=this.resolveRealPath_(this.toRealPath(n));if(!i)throw new Error(`ENOENT: no such file or directory, open '${t}'`);try{let o=await G.promises.lstat(i);if(o.isSymbolicLink()){if(!this.allowSymlinks)throw new Error(`ENOENT: no such file or directory, open '${t}'`);let l=await G.promises.readlink(i),c=this.realTargetToVirtual(n,l),u=this.resolveSymlink(n,c);return this.readFileBuffer(u,s)}if(o.isDirectory())throw new Error(`EISDIR: illegal operation on a directory, read '${t}'`);if(this.maxFileReadSize>0&&o.size>this.maxFileReadSize)throw new Error(`EFBIG: file too large, read '${t}' (${o.size} bytes, max ${this.maxFileReadSize})`);let a=await G.promises.readFile(i);return new Uint8Array(a)}catch(o){throw o.code==="ENOENT"?new Error(`ENOENT: no such file or directory, open '${t}'`):o}}async writeFile(t,s,n){J(t,"write"),this.assertWritable(`write '${t}'`);let r=j(t);this.ensureParentDirs(r);let i=Fe(n),o=Ke(s,i);this.memory.set(r,{type:"file",content:o,mode:420,mtime:new Date}),this.deleted.delete(r)}async appendFile(t,s,n){J(t,"append"),this.assertWritable(`append '${t}'`);let r=j(t),i=Fe(n),o=Ke(s,i),a;try{a=await this.readFileBuffer(r)}catch{a=new Uint8Array(0)}let l=new Uint8Array(a.length+o.length);l.set(a),l.set(o,a.length),this.ensureParentDirs(r),this.memory.set(r,{type:"file",content:l,mode:420,mtime:new Date}),this.deleted.delete(r)}async exists(t){return t.includes("\0")?!1:this.existsInOverlay(t)}async stat(t,s=new Set){J(t,"stat");let n=j(t);if(s.has(n))throw new Error(`ELOOP: too many levels of symbolic links, stat '${t}'`);if(s.add(n),this.deleted.has(n))throw new Error(`ENOENT: no such file or directory, stat '${t}'`);let r=this.memory.get(n);if(r){if(r.type==="symlink"){let a=this.resolveSymlink(n,r.target);return this.stat(a,s)}let o=0;return r.type==="file"&&(o=r.content.length),{isFile:r.type==="file",isDirectory:r.type==="directory",isSymbolicLink:!1,mode:r.mode,size:o,mtime:r.mtime}}let i=this.resolveRealPath_(this.toRealPath(n));if(!i)throw new Error(`ENOENT: no such file or directory, stat '${t}'`);try{let o=await G.promises.lstat(i);if(o.isSymbolicLink()){if(!this.allowSymlinks)throw new Error(`ENOENT: no such file or directory, stat '${t}'`);let a=await G.promises.readlink(i),l=this.realTargetToVirtual(n,a),c=this.resolveSymlink(n,l);return this.stat(c,s)}return{isFile:o.isFile(),isDirectory:o.isDirectory(),isSymbolicLink:!1,mode:o.mode,size:o.size,mtime:o.mtime}}catch(o){throw o.code==="ENOENT"?new Error(`ENOENT: no such file or directory, stat '${t}'`):o}}async lstat(t){J(t,"lstat");let s=j(t);if(this.deleted.has(s))throw new Error(`ENOENT: no such file or directory, lstat '${t}'`);let n=this.memory.get(s);if(n){if(n.type==="symlink")return{isFile:!1,isDirectory:!1,isSymbolicLink:!0,mode:n.mode,size:n.target.length,mtime:n.mtime};let i=0;return n.type==="file"&&(i=n.content.length),{isFile:n.type==="file",isDirectory:n.type==="directory",isSymbolicLink:!1,mode:n.mode,size:i,mtime:n.mtime}}let r=this.resolveRealPathParent_(this.toRealPath(s));if(!r)throw new Error(`ENOENT: no such file or directory, lstat '${t}'`);try{let i=await G.promises.lstat(r);return{isFile:i.isFile(),isDirectory:i.isDirectory(),isSymbolicLink:i.isSymbolicLink(),mode:i.mode,size:i.size,mtime:i.mtime}}catch(i){throw i.code==="ENOENT"?new Error(`ENOENT: no such file or directory, lstat '${t}'`):i}}resolveSymlink(t,s){if(s.startsWith("/"))return j(s);let n=this.dirname(t);return j(n==="/"?`/${s}`:`${n}/${s}`)}realTargetToVirtual(t,s){let n=di(s,this.canonicalRoot);if(n.withinRoot){if(!me.isAbsolute(s))return s;let r=n.relativePath;return this.mountPoint==="/"?r:`${this.mountPoint}${r}`}return n.safeName}async mkdir(t,s){J(t,"mkdir"),this.assertWritable(`mkdir '${t}'`);let n=j(t);if(await this.existsInOverlay(n)){if(!s?.recursive)throw new Error(`EEXIST: file already exists, mkdir '${t}'`);return}let i=this.dirname(n);if(i!=="/"&&!await this.existsInOverlay(i))if(s?.recursive)await this.mkdir(i,{recursive:!0});else throw new Error(`ENOENT: no such file or directory, mkdir '${t}'`);this.memory.set(n,{type:"directory",mode:493,mtime:new Date}),this.deleted.delete(n)}async readdirCore(t,s){if(this.deleted.has(s))throw new Error(`ENOENT: no such file or directory, scandir '${t}'`);let n=new Map,r=new Set,i=s==="/"?"/":`${s}/`;for(let a of this.deleted)if(a.startsWith(i)){let l=a.slice(i.length),c=l.split("/")[0];c&&!l.includes("/",c.length)&&r.add(c)}for(let[a,l]of this.memory)if(a!==s&&a.startsWith(i)){let c=a.slice(i.length),u=c.split("/")[0];u&&!r.has(u)&&!c.includes("/",1)&&n.set(u,{name:u,isFile:l.type==="file",isDirectory:l.type==="directory",isSymbolicLink:l.type==="symlink"})}let o=this.resolveRealPath_(this.toRealPath(s));if(o)try{let a=await G.promises.readdir(o,{withFileTypes:!0});for(let l of a)!r.has(l.name)&&!n.has(l.name)&&n.set(l.name,{name:l.name,isFile:l.isFile(),isDirectory:l.isDirectory(),isSymbolicLink:l.isSymbolicLink()})}catch(a){if(a.code==="ENOENT"){if(!this.memory.has(s))throw new Error(`ENOENT: no such file or directory, scandir '${t}'`)}else if(a.code!=="ENOTDIR")throw a}return n}async resolveForReaddir(t,s=!1){let n=j(t),r=new Set,i=s,o=this.memory.get(n);for(;o&&o.type==="symlink";){if(r.has(n))throw new Error(`ELOOP: too many levels of symbolic links, scandir '${t}'`);r.add(n),i=!0,n=this.resolveSymlink(n,o.target),o=this.memory.get(n)}if(o)return{normalized:n,outsideOverlay:!1};if(this.getRelativeToMount(n)===null)return{normalized:n,outsideOverlay:!0};let l=this.resolveRealPath_(this.toRealPath(n));if(!l)return{normalized:n,outsideOverlay:!0};try{if((await G.promises.lstat(l)).isSymbolicLink()){if(!this.allowSymlinks)return{normalized:n,outsideOverlay:!0};let u=await G.promises.readlink(l),f=this.realTargetToVirtual(n,u),d=this.resolveSymlink(n,f);return this.resolveForReaddir(d,!0)}return{normalized:n,outsideOverlay:!1}}catch{return i?{normalized:n,outsideOverlay:!0}:{normalized:n,outsideOverlay:!1}}}async readdir(t){J(t,"scandir");let{normalized:s,outsideOverlay:n}=await this.resolveForReaddir(t);if(n)return[];let r=await this.readdirCore(t,s);return Array.from(r.keys()).sort((i,o)=>i<o?-1:i>o?1:0)}async readdirWithFileTypes(t){J(t,"scandir");let{normalized:s,outsideOverlay:n}=await this.resolveForReaddir(t);if(n)return[];let r=await this.readdirCore(t,s);return Array.from(r.values()).sort((i,o)=>i.name<o.name?-1:i.name>o.name?1:0)}async rm(t,s){J(t,"rm"),this.assertWritable(`rm '${t}'`);let n=j(t);if(!await this.existsInOverlay(n)){if(s?.force)return;throw new Error(`ENOENT: no such file or directory, rm '${t}'`)}try{if((await this.stat(n)).isDirectory){let o=await this.readdir(n);if(o.length>0){if(!s?.recursive)throw new Error(`ENOTEMPTY: directory not empty, rm '${t}'`);for(let a of o){let l=n==="/"?`/${a}`:`${n}/${a}`;await this.rm(l,s)}}}}catch(i){if(i instanceof Error&&(i.message.includes("ENOTEMPTY")||i.message.includes("EISDIR")))throw i}this.deleted.add(n),this.memory.delete(n)}async cp(t,s,n){J(t,"cp"),J(s,"cp"),this.assertWritable(`cp '${s}'`);let r=j(t),i=j(s);if(!await this.existsInOverlay(r))throw new Error(`ENOENT: no such file or directory, cp '${t}'`);let a=await this.stat(r);if(a.isFile){let l=await this.readFileBuffer(r);await this.writeFile(i,l)}else if(a.isDirectory){if(!n?.recursive)throw new Error(`EISDIR: is a directory, cp '${t}'`);await this.mkdir(i,{recursive:!0});let l=await this.readdir(r);for(let c of l){let u=r==="/"?`/${c}`:`${r}/${c}`,f=i==="/"?`/${c}`:`${i}/${c}`;await this.cp(u,f,n)}}}async mv(t,s){this.assertWritable(`mv '${s}'`),await this.cp(t,s,{recursive:!0}),await this.rm(t,{recursive:!0})}resolvePath(t,s){if(s.startsWith("/"))return j(s);let n=t==="/"?`/${s}`:`${t}/${s}`;return j(n)}getAllPaths(){let t=new Set(this.memory.keys());for(let s of this.deleted)t.delete(s);return this.scanRealFs("/",t),Array.from(t)}scanRealFs(t,s){if(this.deleted.has(t))return;let n=this.resolveRealPath_(this.toRealPath(t));if(n)try{let r=G.readdirSync(n);for(let i of r){let o=t==="/"?`/${i}`:`${t}/${i}`;if(this.deleted.has(o))continue;s.add(o);let a=me.join(n,i);G.lstatSync(a).isDirectory()&&this.scanRealFs(o,s)}}catch{}}async chmod(t,s){J(t,"chmod"),this.assertWritable(`chmod '${t}'`);let n=j(t);if(!await this.existsInOverlay(n))throw new Error(`ENOENT: no such file or directory, chmod '${t}'`);let i=this.memory.get(n);if(i){i.mode=s;return}let o=await this.stat(n);if(o.isFile){let a=await this.readFileBuffer(n);this.memory.set(n,{type:"file",content:a,mode:s,mtime:new Date})}else o.isDirectory&&this.memory.set(n,{type:"directory",mode:s,mtime:new Date})}async symlink(t,s){if(!this.allowSymlinks)throw new Error(`EPERM: operation not permitted, symlink '${s}'`);J(s,"symlink"),this.assertWritable(`symlink '${s}'`);let n=j(s);if(await this.existsInOverlay(n))throw new Error(`EEXIST: file already exists, symlink '${s}'`);this.ensureParentDirs(n),this.memory.set(n,{type:"symlink",target:t,mode:511,mtime:new Date}),this.deleted.delete(n)}async link(t,s){J(t,"link"),J(s,"link"),this.assertWritable(`link '${s}'`);let n=j(t),r=j(s);if(!await this.existsInOverlay(n))throw new Error(`ENOENT: no such file or directory, link '${t}'`);let o=await this.stat(n);if(!o.isFile)throw new Error(`EPERM: operation not permitted, link '${t}'`);if(await this.existsInOverlay(r))throw new Error(`EEXIST: file already exists, link '${s}'`);let l=await this.readFileBuffer(n);this.ensureParentDirs(r),this.memory.set(r,{type:"file",content:l,mode:o.mode,mtime:new Date}),this.deleted.delete(r)}async readlink(t){J(t,"readlink");let s=j(t);if(this.deleted.has(s))throw new Error(`ENOENT: no such file or directory, readlink '${t}'`);let n=this.memory.get(s);if(n){if(n.type!=="symlink")throw new Error(`EINVAL: invalid argument, readlink '${t}'`);return n.target}let r=this.resolveRealPathParent_(this.toRealPath(s));if(!r)throw new Error(`ENOENT: no such file or directory, readlink '${t}'`);try{let i=await G.promises.readlink(r);return this.realTargetToVirtual(s,i)}catch(i){throw i.code==="ENOENT"?new Error(`ENOENT: no such file or directory, readlink '${t}'`):i.code==="EINVAL"?new Error(`EINVAL: invalid argument, readlink '${t}'`):i}}async realpath(t){J(t,"realpath");let s=j(t),n=new Set,r=async a=>{let l=a==="/"?[]:a.slice(1).split("/"),c="";for(let u of l){if(c=`${c}/${u}`,n.has(c))throw new Error(`ELOOP: too many levels of symbolic links, realpath '${t}'`);if(this.deleted.has(c))throw new Error(`ENOENT: no such file or directory, realpath '${t}'`);let f=this.memory.get(c),d=0,h=40;for(;f&&f.type==="symlink"&&d<h;){if(n.add(c),c=this.resolveSymlink(c,f.target),d++,n.has(c))throw new Error(`ELOOP: too many levels of symbolic links, realpath '${t}'`);if(this.deleted.has(c))throw new Error(`ENOENT: no such file or directory, realpath '${t}'`);f=this.memory.get(c)}if(d>=h)throw new Error(`ELOOP: too many levels of symbolic links, realpath '${t}'`);if(!f){let m=this.toRealPath(c),p=this.resolveRealPath_(m);if(p)try{if((await G.promises.lstat(p)).isSymbolicLink()){if(!this.allowSymlinks)throw new Error(`ENOENT: no such file or directory, realpath '${t}'`);let S=await G.promises.readlink(p),v=this.realTargetToVirtual(c,S);return n.add(c),c=this.resolveSymlink(c,v),r(c)}}catch(g){throw g.code==="ENOENT"?new Error(`ENOENT: no such file or directory, realpath '${t}'`):g}else if(!this.allowSymlinks){let g=this.resolveRealPathParent_(m);if(g)try{if((await G.promises.lstat(g)).isSymbolicLink())throw new Error(`ENOENT: no such file or directory, realpath '${t}'`)}catch(S){throw S.message?.includes("ENOENT")||S.message?.includes("ELOOP")?new Error(`ENOENT: no such file or directory, realpath '${t}'`):S}}}}return c||"/"},i=await r(s);if(!await this.existsInOverlay(i))throw new Error(`ENOENT: no such file or directory, realpath '${t}'`);return i}async utimes(t,s,n){J(t,"utimes"),this.assertWritable(`utimes '${t}'`);let r=j(t);if(!await this.existsInOverlay(r))throw new Error(`ENOENT: no such file or directory, utimes '${t}'`);let o=this.memory.get(r);if(o){o.mtime=n;return}let a=await this.stat(r);if(a.isFile){let l=await this.readFileBuffer(r);this.memory.set(r,{type:"file",content:l,mode:a.mode,mtime:n})}else a.isDirectory&&this.memory.set(r,{type:"directory",mode:a.mode,mtime:n})}};var ee={reset:"\x1B[0m",bold:"\x1B[1m",dim:"\x1B[2m",red:"\x1B[31m",green:"\x1B[32m",yellow:"\x1B[33m",blue:"\x1B[34m",cyan:"\x1B[36m"},dn=class{env;rl;running=!0;history=[];isInteractive;constructor(t={}){let s=process.cwd(),n=new ss({root:s,mountPoint:"/"});this.env=new es({fs:n,cwd:t.cwd||"/",env:{HOME:"/",USER:"user",SHELL:"/bin/bash",TERM:"xterm-256color",...t.env},network:t.network!==!1?{dangerouslyAllowFullInternetAccess:!0}:void 0}),this.isInteractive=process.stdin.isTTY===!0,this.rl=pi.createInterface({input:process.stdin,output:process.stdout,terminal:this.isInteractive}),this.rl.on("SIGINT",()=>{process.stdout.write(`^C
706
+ `)}import*as G from"node:fs";import*as me from"node:path";import*as ve from"node:fs";import*as ae from"node:path";function j(e){if(!e||e==="/")return"/";let t=e.endsWith("/")&&e!=="/"?e.slice(0,-1):e;t.startsWith("/")||(t=`/${t}`);let s=t.split("/").filter(r=>r&&r!=="."),n=[];for(let r of s)r===".."?n.pop():n.push(r);return`/${n.join("/")}`||"/"}function ts(e,t){return e===t||e.startsWith(`${t}/`)}function wt(e,t){try{let s=ve.realpathSync(e);return ts(s,t)?s:null}catch(s){if(s.code==="ENOENT"){let n=ae.dirname(e);if(n===e)return null;let r=wt(n,t);if(r===null)return null;try{if(ve.lstatSync(e).isSymbolicLink()){let o=ve.readlinkSync(e),a=ae.isAbsolute(o)?o:ae.resolve(ae.dirname(e),o);if(wt(a,t)===null)return null}}catch{}return ae.join(r,ae.basename(e))}return null}}function ui(e,t,s){let n=wt(e,s);if(n===null)return null;let r=ae.resolve(e),i=r.slice(t.length),o=n.slice(s.length);if(i!==o)return null;try{if(ve.lstatSync(r).isSymbolicLink())return null}catch{}return n}function fi(e,t){if(!ve.existsSync(e))throw new Error(`${t} root does not exist: ${e}`);if(!ve.statSync(e).isDirectory())throw new Error(`${t} root is not a directory: ${e}`)}function J(e,t){if(e.includes("\0"))throw new Error(`ENOENT: path contains null byte, ${t} '${e}'`)}function di(e,t){if(!ae.isAbsolute(e))return{withinRoot:!0,relativePath:e};let s;try{s=ve.realpathSync(e)}catch{s=ae.resolve(e)}return ts(s,t)?{withinRoot:!0,relativePath:s.slice(t.length)||"/"}:{withinRoot:!1,safeName:ae.basename(e)}}var ba="/home/user/project",ss=class{root;canonicalRoot;mountPoint;readOnly;maxFileReadSize;allowSymlinks;memory=new Map;deleted=new Set;constructor(t){this.root=me.resolve(t.root);let s=t.mountPoint??ba;if(this.mountPoint=s==="/"?"/":s.replace(/\/+$/,""),!this.mountPoint.startsWith("/"))throw new Error(`Mount point must be an absolute path: ${s}`);this.readOnly=t.readOnly??!1,this.maxFileReadSize=t.maxFileReadSize??10485760,this.allowSymlinks=t.allowSymlinks??!1,fi(this.root,"OverlayFs"),this.canonicalRoot=G.realpathSync(this.root),this.createMountPointDirs()}assertWritable(t){if(this.readOnly)throw new Error(`EROFS: read-only file system, ${t}`)}createMountPointDirs(){let t=this.mountPoint.split("/").filter(Boolean),s="";for(let n of t)s+=`/${n}`,this.memory.has(s)||this.memory.set(s,{type:"directory",mode:493,mtime:new Date});this.memory.has("/")||this.memory.set("/",{type:"directory",mode:493,mtime:new Date})}getMountPoint(){return this.mountPoint}mkdirSync(t,s){let r=j(t).split("/").filter(Boolean),i="";for(let o of r)i+=`/${o}`,this.memory.has(i)||this.memory.set(i,{type:"directory",mode:493,mtime:new Date})}writeFileSync(t,s){let n=j(t),r=this.getDirname(n);r!=="/"&&this.mkdirSync(r);let i=s instanceof Uint8Array?s:new TextEncoder().encode(s);this.memory.set(n,{type:"file",content:i,mode:420,mtime:new Date})}getDirname(t){let s=t.lastIndexOf("/");return s===0?"/":t.slice(0,s)}getRelativeToMount(t){return this.mountPoint==="/"?t:t===this.mountPoint?"/":t.startsWith(`${this.mountPoint}/`)?t.slice(this.mountPoint.length):null}toRealPath(t){let s=j(t),n=this.getRelativeToMount(s);if(n===null)return null;let r=me.join(this.root,n),i=me.resolve(r);return ts(i,this.root)?i:null}dirname(t){let s=j(t);if(s==="/")return"/";let n=s.lastIndexOf("/");return n===0?"/":s.slice(0,n)}resolveRealPath_(t){return t?this.allowSymlinks?wt(t,this.canonicalRoot):ui(t,this.root,this.canonicalRoot):null}resolveRealPathParent_(t){if(!t)return null;let s=me.dirname(t),n=this.resolveRealPath_(s);return n===null?null:me.join(n,me.basename(t))}sanitizeError(t,s,n){let r=t;if(r.message?.includes("ELOOP")||r.message?.includes("EFBIG")||r.message?.includes("EPERM"))throw t;let i=r.code||"EIO";throw new Error(`${i}: ${n} '${s}'`)}ensureParentDirs(t){let s=this.dirname(t);s!=="/"&&(this.memory.has(s)||(this.ensureParentDirs(s),this.memory.set(s,{type:"directory",mode:493,mtime:new Date})),this.deleted.delete(s))}async existsInOverlay(t){let s=j(t);if(this.deleted.has(s))return!1;if(this.memory.has(s))return!0;let n=this.resolveRealPathParent_(this.toRealPath(s));if(!n)return!1;try{return await G.promises.lstat(n),!0}catch{return!1}}async readFile(t,s){let n=await this.readFileBuffer(t),r=Fe(s);return Pt(n,r)}async readFileBuffer(t,s=new Set){J(t,"open");let n=j(t);if(s.has(n))throw new Error(`ELOOP: too many levels of symbolic links, open '${t}'`);if(s.add(n),this.deleted.has(n))throw new Error(`ENOENT: no such file or directory, open '${t}'`);let r=this.memory.get(n);if(r){if(r.type==="symlink"){let o=this.resolveSymlink(n,r.target);return this.readFileBuffer(o,s)}if(r.type!=="file")throw new Error(`EISDIR: illegal operation on a directory, read '${t}'`);return r.content}let i=this.resolveRealPath_(this.toRealPath(n));if(!i)throw new Error(`ENOENT: no such file or directory, open '${t}'`);try{let o=await G.promises.lstat(i);if(o.isSymbolicLink()){if(!this.allowSymlinks)throw new Error(`ENOENT: no such file or directory, open '${t}'`);let l=await G.promises.readlink(i),c=this.realTargetToVirtual(n,l),u=this.resolveSymlink(n,c);return this.readFileBuffer(u,s)}if(o.isDirectory())throw new Error(`EISDIR: illegal operation on a directory, read '${t}'`);if(this.maxFileReadSize>0&&o.size>this.maxFileReadSize)throw new Error(`EFBIG: file too large, read '${t}' (${o.size} bytes, max ${this.maxFileReadSize})`);let a=await G.promises.readFile(i);return new Uint8Array(a)}catch(o){if(o.code==="ENOENT")throw new Error(`ENOENT: no such file or directory, open '${t}'`);this.sanitizeError(o,t,"open")}}async writeFile(t,s,n){J(t,"write"),this.assertWritable(`write '${t}'`);let r=j(t);this.ensureParentDirs(r);let i=Fe(n),o=Ke(s,i);this.memory.set(r,{type:"file",content:o,mode:420,mtime:new Date}),this.deleted.delete(r)}async appendFile(t,s,n){J(t,"append"),this.assertWritable(`append '${t}'`);let r=j(t),i=Fe(n),o=Ke(s,i),a;try{a=await this.readFileBuffer(r)}catch{a=new Uint8Array(0)}let l=new Uint8Array(a.length+o.length);l.set(a),l.set(o,a.length),this.ensureParentDirs(r),this.memory.set(r,{type:"file",content:l,mode:420,mtime:new Date}),this.deleted.delete(r)}async exists(t){return t.includes("\0")?!1:this.existsInOverlay(t)}async stat(t,s=new Set){J(t,"stat");let n=j(t);if(s.has(n))throw new Error(`ELOOP: too many levels of symbolic links, stat '${t}'`);if(s.add(n),this.deleted.has(n))throw new Error(`ENOENT: no such file or directory, stat '${t}'`);let r=this.memory.get(n);if(r){if(r.type==="symlink"){let a=this.resolveSymlink(n,r.target);return this.stat(a,s)}let o=0;return r.type==="file"&&(o=r.content.length),{isFile:r.type==="file",isDirectory:r.type==="directory",isSymbolicLink:!1,mode:r.mode,size:o,mtime:r.mtime}}let i=this.resolveRealPath_(this.toRealPath(n));if(!i)throw new Error(`ENOENT: no such file or directory, stat '${t}'`);try{let o=await G.promises.lstat(i);if(o.isSymbolicLink()){if(!this.allowSymlinks)throw new Error(`ENOENT: no such file or directory, stat '${t}'`);let a=await G.promises.readlink(i),l=this.realTargetToVirtual(n,a),c=this.resolveSymlink(n,l);return this.stat(c,s)}return{isFile:o.isFile(),isDirectory:o.isDirectory(),isSymbolicLink:!1,mode:o.mode,size:o.size,mtime:o.mtime}}catch(o){if(o.code==="ENOENT")throw new Error(`ENOENT: no such file or directory, stat '${t}'`);this.sanitizeError(o,t,"stat")}}async lstat(t){J(t,"lstat");let s=j(t);if(this.deleted.has(s))throw new Error(`ENOENT: no such file or directory, lstat '${t}'`);let n=this.memory.get(s);if(n){if(n.type==="symlink")return{isFile:!1,isDirectory:!1,isSymbolicLink:!0,mode:n.mode,size:n.target.length,mtime:n.mtime};let i=0;return n.type==="file"&&(i=n.content.length),{isFile:n.type==="file",isDirectory:n.type==="directory",isSymbolicLink:!1,mode:n.mode,size:i,mtime:n.mtime}}let r=this.resolveRealPathParent_(this.toRealPath(s));if(!r)throw new Error(`ENOENT: no such file or directory, lstat '${t}'`);try{let i=await G.promises.lstat(r);return{isFile:i.isFile(),isDirectory:i.isDirectory(),isSymbolicLink:i.isSymbolicLink(),mode:i.mode,size:i.size,mtime:i.mtime}}catch(i){if(i.code==="ENOENT")throw new Error(`ENOENT: no such file or directory, lstat '${t}'`);this.sanitizeError(i,t,"lstat")}}resolveSymlink(t,s){if(s.startsWith("/"))return j(s);let n=this.dirname(t);return j(n==="/"?`/${s}`:`${n}/${s}`)}realTargetToVirtual(t,s){let n=di(s,this.canonicalRoot);if(n.withinRoot){if(!me.isAbsolute(s))return s;let r=n.relativePath;return this.mountPoint==="/"?r:`${this.mountPoint}${r}`}return n.safeName}async mkdir(t,s){J(t,"mkdir"),this.assertWritable(`mkdir '${t}'`);let n=j(t);if(await this.existsInOverlay(n)){if(!s?.recursive)throw new Error(`EEXIST: file already exists, mkdir '${t}'`);return}let i=this.dirname(n);if(i!=="/"&&!await this.existsInOverlay(i))if(s?.recursive)await this.mkdir(i,{recursive:!0});else throw new Error(`ENOENT: no such file or directory, mkdir '${t}'`);this.memory.set(n,{type:"directory",mode:493,mtime:new Date}),this.deleted.delete(n)}async readdirCore(t,s){if(this.deleted.has(s))throw new Error(`ENOENT: no such file or directory, scandir '${t}'`);let n=new Map,r=new Set,i=s==="/"?"/":`${s}/`;for(let a of this.deleted)if(a.startsWith(i)){let l=a.slice(i.length),c=l.split("/")[0];c&&!l.includes("/",c.length)&&r.add(c)}for(let[a,l]of this.memory)if(a!==s&&a.startsWith(i)){let c=a.slice(i.length),u=c.split("/")[0];u&&!r.has(u)&&!c.includes("/",1)&&n.set(u,{name:u,isFile:l.type==="file",isDirectory:l.type==="directory",isSymbolicLink:l.type==="symlink"})}let o=this.resolveRealPath_(this.toRealPath(s));if(o)try{let a=await G.promises.readdir(o,{withFileTypes:!0});for(let l of a)!r.has(l.name)&&!n.has(l.name)&&n.set(l.name,{name:l.name,isFile:l.isFile(),isDirectory:l.isDirectory(),isSymbolicLink:l.isSymbolicLink()})}catch(a){if(a.code==="ENOENT"){if(!this.memory.has(s))throw new Error(`ENOENT: no such file or directory, scandir '${t}'`)}else a.code!=="ENOTDIR"&&this.sanitizeError(a,t,"scandir")}return n}async resolveForReaddir(t,s=!1){let n=j(t),r=new Set,i=s,o=this.memory.get(n);for(;o&&o.type==="symlink";){if(r.has(n))throw new Error(`ELOOP: too many levels of symbolic links, scandir '${t}'`);r.add(n),i=!0,n=this.resolveSymlink(n,o.target),o=this.memory.get(n)}if(o)return{normalized:n,outsideOverlay:!1};if(this.getRelativeToMount(n)===null)return{normalized:n,outsideOverlay:!0};let l=this.resolveRealPath_(this.toRealPath(n));if(!l)return{normalized:n,outsideOverlay:!0};try{if((await G.promises.lstat(l)).isSymbolicLink()){if(!this.allowSymlinks)return{normalized:n,outsideOverlay:!0};let u=await G.promises.readlink(l),f=this.realTargetToVirtual(n,u),d=this.resolveSymlink(n,f);return this.resolveForReaddir(d,!0)}return{normalized:n,outsideOverlay:!1}}catch{return i?{normalized:n,outsideOverlay:!0}:{normalized:n,outsideOverlay:!1}}}async readdir(t){J(t,"scandir");let{normalized:s,outsideOverlay:n}=await this.resolveForReaddir(t);if(n)return[];let r=await this.readdirCore(t,s);return Array.from(r.keys()).sort((i,o)=>i<o?-1:i>o?1:0)}async readdirWithFileTypes(t){J(t,"scandir");let{normalized:s,outsideOverlay:n}=await this.resolveForReaddir(t);if(n)return[];let r=await this.readdirCore(t,s);return Array.from(r.values()).sort((i,o)=>i.name<o.name?-1:i.name>o.name?1:0)}async rm(t,s){J(t,"rm"),this.assertWritable(`rm '${t}'`);let n=j(t);if(!await this.existsInOverlay(n)){if(s?.force)return;throw new Error(`ENOENT: no such file or directory, rm '${t}'`)}try{if((await this.stat(n)).isDirectory){let o=await this.readdir(n);if(o.length>0){if(!s?.recursive)throw new Error(`ENOTEMPTY: directory not empty, rm '${t}'`);for(let a of o){let l=n==="/"?`/${a}`:`${n}/${a}`;await this.rm(l,s)}}}}catch(i){if(i instanceof Error&&(i.message.includes("ENOTEMPTY")||i.message.includes("EISDIR")))throw i}this.deleted.add(n),this.memory.delete(n)}async cp(t,s,n){J(t,"cp"),J(s,"cp"),this.assertWritable(`cp '${s}'`);let r=j(t),i=j(s);if(!await this.existsInOverlay(r))throw new Error(`ENOENT: no such file or directory, cp '${t}'`);let a=await this.stat(r);if(a.isFile){let l=await this.readFileBuffer(r);await this.writeFile(i,l)}else if(a.isDirectory){if(!n?.recursive)throw new Error(`EISDIR: is a directory, cp '${t}'`);await this.mkdir(i,{recursive:!0});let l=await this.readdir(r);for(let c of l){let u=r==="/"?`/${c}`:`${r}/${c}`,f=i==="/"?`/${c}`:`${i}/${c}`;await this.cp(u,f,n)}}}async mv(t,s){this.assertWritable(`mv '${s}'`),await this.cp(t,s,{recursive:!0}),await this.rm(t,{recursive:!0})}resolvePath(t,s){if(s.startsWith("/"))return j(s);let n=t==="/"?`/${s}`:`${t}/${s}`;return j(n)}getAllPaths(){let t=new Set(this.memory.keys());for(let s of this.deleted)t.delete(s);return this.scanRealFs("/",t),Array.from(t)}scanRealFs(t,s){if(this.deleted.has(t))return;let n=this.resolveRealPath_(this.toRealPath(t));if(n)try{let r=G.readdirSync(n);for(let i of r){let o=t==="/"?`/${i}`:`${t}/${i}`;if(this.deleted.has(o))continue;s.add(o);let a=me.join(n,i);G.lstatSync(a).isDirectory()&&this.scanRealFs(o,s)}}catch{}}async chmod(t,s){J(t,"chmod"),this.assertWritable(`chmod '${t}'`);let n=j(t);if(!await this.existsInOverlay(n))throw new Error(`ENOENT: no such file or directory, chmod '${t}'`);let i=this.memory.get(n);if(i){i.mode=s;return}let o=await this.stat(n);if(o.isFile){let a=await this.readFileBuffer(n);this.memory.set(n,{type:"file",content:a,mode:s,mtime:new Date})}else o.isDirectory&&this.memory.set(n,{type:"directory",mode:s,mtime:new Date})}async symlink(t,s){if(!this.allowSymlinks)throw new Error(`EPERM: operation not permitted, symlink '${s}'`);J(s,"symlink"),this.assertWritable(`symlink '${s}'`);let n=j(s);if(await this.existsInOverlay(n))throw new Error(`EEXIST: file already exists, symlink '${s}'`);this.ensureParentDirs(n),this.memory.set(n,{type:"symlink",target:t,mode:511,mtime:new Date}),this.deleted.delete(n)}async link(t,s){J(t,"link"),J(s,"link"),this.assertWritable(`link '${s}'`);let n=j(t),r=j(s);if(!await this.existsInOverlay(n))throw new Error(`ENOENT: no such file or directory, link '${t}'`);let o=await this.stat(n);if(!o.isFile)throw new Error(`EPERM: operation not permitted, link '${t}'`);if(await this.existsInOverlay(r))throw new Error(`EEXIST: file already exists, link '${s}'`);let l=await this.readFileBuffer(n);this.ensureParentDirs(r),this.memory.set(r,{type:"file",content:l,mode:o.mode,mtime:new Date}),this.deleted.delete(r)}async readlink(t){J(t,"readlink");let s=j(t);if(this.deleted.has(s))throw new Error(`ENOENT: no such file or directory, readlink '${t}'`);let n=this.memory.get(s);if(n){if(n.type!=="symlink")throw new Error(`EINVAL: invalid argument, readlink '${t}'`);return n.target}let r=this.resolveRealPathParent_(this.toRealPath(s));if(!r)throw new Error(`ENOENT: no such file or directory, readlink '${t}'`);try{let i=await G.promises.readlink(r);return this.realTargetToVirtual(s,i)}catch(i){if(i.code==="ENOENT")throw new Error(`ENOENT: no such file or directory, readlink '${t}'`);if(i.code==="EINVAL")throw new Error(`EINVAL: invalid argument, readlink '${t}'`);this.sanitizeError(i,t,"readlink")}}async realpath(t){J(t,"realpath");let s=j(t),n=new Set,r=async a=>{let l=a==="/"?[]:a.slice(1).split("/"),c="";for(let u of l){if(c=`${c}/${u}`,n.has(c))throw new Error(`ELOOP: too many levels of symbolic links, realpath '${t}'`);if(this.deleted.has(c))throw new Error(`ENOENT: no such file or directory, realpath '${t}'`);let f=this.memory.get(c),d=0,h=40;for(;f&&f.type==="symlink"&&d<h;){if(n.add(c),c=this.resolveSymlink(c,f.target),d++,n.has(c))throw new Error(`ELOOP: too many levels of symbolic links, realpath '${t}'`);if(this.deleted.has(c))throw new Error(`ENOENT: no such file or directory, realpath '${t}'`);f=this.memory.get(c)}if(d>=h)throw new Error(`ELOOP: too many levels of symbolic links, realpath '${t}'`);if(!f){let m=this.toRealPath(c),p=this.resolveRealPath_(m);if(p)try{if((await G.promises.lstat(p)).isSymbolicLink()){if(!this.allowSymlinks)throw new Error(`ENOENT: no such file or directory, realpath '${t}'`);let S=await G.promises.readlink(p),v=this.realTargetToVirtual(c,S);return n.add(c),c=this.resolveSymlink(c,v),r(c)}}catch(g){if(g.code==="ENOENT")throw new Error(`ENOENT: no such file or directory, realpath '${t}'`);this.sanitizeError(g,t,"realpath")}else if(!this.allowSymlinks){let g=this.resolveRealPathParent_(m);if(g)try{if((await G.promises.lstat(g)).isSymbolicLink())throw new Error(`ENOENT: no such file or directory, realpath '${t}'`)}catch(S){if(S.message?.includes("ENOENT")||S.message?.includes("ELOOP"))throw new Error(`ENOENT: no such file or directory, realpath '${t}'`);this.sanitizeError(S,t,"realpath")}}}}return c||"/"},i=await r(s);if(!await this.existsInOverlay(i))throw new Error(`ENOENT: no such file or directory, realpath '${t}'`);return i}async utimes(t,s,n){J(t,"utimes"),this.assertWritable(`utimes '${t}'`);let r=j(t);if(!await this.existsInOverlay(r))throw new Error(`ENOENT: no such file or directory, utimes '${t}'`);let o=this.memory.get(r);if(o){o.mtime=n;return}let a=await this.stat(r);if(a.isFile){let l=await this.readFileBuffer(r);this.memory.set(r,{type:"file",content:l,mode:a.mode,mtime:n})}else a.isDirectory&&this.memory.set(r,{type:"directory",mode:a.mode,mtime:n})}};var ee={reset:"\x1B[0m",bold:"\x1B[1m",dim:"\x1B[2m",red:"\x1B[31m",green:"\x1B[32m",yellow:"\x1B[33m",blue:"\x1B[34m",cyan:"\x1B[36m"},dn=class{env;rl;running=!0;history=[];isInteractive;constructor(t={}){let s=process.cwd(),n=new ss({root:s,mountPoint:"/"});this.env=new es({fs:n,cwd:t.cwd||"/",env:{HOME:"/",USER:"user",SHELL:"/bin/bash",TERM:"xterm-256color",...t.env},network:t.network!==!1?{dangerouslyAllowFullInternetAccess:!0}:void 0}),this.isInteractive=process.stdin.isTTY===!0,this.rl=pi.createInterface({input:process.stdin,output:process.stdout,terminal:this.isInteractive}),this.rl.on("SIGINT",()=>{process.stdout.write(`^C
707
707
  `),this.prompt()}),process.stdin.isTTY&&this.rl.on("close",()=>{this.running=!1,console.log(`
708
708
  Goodbye!`),process.exit(0)})}syncHistory(){let t=this.env.getEnv();t.BASH_HISTORY=JSON.stringify(this.history)}getPrompt(){let t=this.env.getCwd(),s=this.env.getEnv().HOME||"/home/user",n=t;return t===s?n="~":t.startsWith(`${s}/`)&&(n=`~${t.slice(s.length)}`),`${ee.green}${ee.bold}user@virtual${ee.reset}:${ee.blue}${ee.bold}${n}${ee.reset}$ `}async executeCommand(t){let s=t.trim();if(s){if(this.history.push(s),s==="exit"||s.startsWith("exit ")){let n=s.split(/\s+/),r=n[1]?parseInt(n[1],10):0;console.log("exit"),process.exit(r)}this.syncHistory();try{let n=await this.env.exec(s);n.stdout&&process.stdout.write(n.stdout),n.stderr&&process.stderr.write(`${ee.red}${n.stderr}${ee.reset}`)}catch(n){console.error(`${ee.red}Error: ${$e(n)}${ee.reset}`)}}}printWelcome(){console.log(`
709
709
  ${ee.cyan}${ee.bold}\u2554\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2557
@@ -702,7 +702,7 @@ ${e.terminator}`}function ka(e){return`(${Ee(e.body)})${_e(e.redirections)}`}fun
702
702
  `,exitCode:2,env:Pe(this.state.env,s?.env)});if(f instanceof RangeError)return this.logResult({stdout:"",stderr:`bash: ${f.message}
703
703
  `,exitCode:1,env:Pe(this.state.env,s?.env)});throw f}finally{u?.deactivate()}}async readFile(t){return this.fs.readFile(this.fs.resolvePath(this.state.cwd,t))}async writeFile(t,s){return this.fs.writeFile(this.fs.resolvePath(this.state.cwd,t),s)}getCwd(){return this.state.cwd}getEnv(){return ze(this.state.env)}registerTransformPlugin(t){this.transformPlugins.push(t)}transform(t){let s=bi(t),r=ue(s),n=Object.create(null);for(let i of this.transformPlugins){let o=i.transform({ast:r,metadata:n});r=o.ast,o.metadata&&(n={...n,...o.metadata})}return{script:kt(r),ast:r,metadata:n}}};function bi(e){let t=e.split(`
704
704
  `),s=[],r=[];for(let n=0;n<t.length;n++){let i=t[n];if(r.length>0){let l=r[r.length-1];if((l.stripTabs?i.replace(/^\t+/,""):i)===l.delimiter){s.push(i.trimStart()),r.pop();continue}s.push(i);continue}let o=i.trimStart();s.push(o);let a=/<<(-?)\s*(['"]?)([\w-]+)\2/g;for(let l of o.matchAll(a)){let c=l[1]==="-",u=l[3];r.push({delimiter:u,stripTabs:c})}}return s.join(`
705
- `)}var as=class{baseFs;mounts=new Map;constructor(t){if(this.baseFs=t?.base??new Le,t?.mounts)for(let{mountPoint:s,filesystem:r}of t.mounts)this.mount(s,r)}mount(t,s){this.validateMountPath(t);let r=this.normalizePath(t);this.validateMount(r),this.mounts.set(r,{mountPoint:r,filesystem:s})}unmount(t){let s=this.normalizePath(t);if(!this.mounts.has(s))throw new Error(`No filesystem mounted at '${t}'`);this.mounts.delete(s)}getMounts(){return Array.from(this.mounts.values()).map(t=>({mountPoint:t.mountPoint,filesystem:t.filesystem}))}isMountPoint(t){let s=this.normalizePath(t);return this.mounts.has(s)}validateMountPath(t){let s=t.split("/");for(let r of s)if(r==="."||r==="..")throw new Error(`Invalid mount point '${t}': contains '.' or '..' segments`)}validateMount(t){if(t==="/")throw new Error("Cannot mount at root '/'");for(let s of this.mounts.keys())if(s!==t){if(t.startsWith(`${s}/`))throw new Error(`Cannot mount at '${t}': inside existing mount '${s}'`);if(s.startsWith(`${t}/`))throw new Error(`Cannot mount at '${t}': would contain existing mount '${s}'`)}}normalizePath(t){if(!t||t==="/")return"/";let s=t.endsWith("/")&&t!=="/"?t.slice(0,-1):t;s.startsWith("/")||(s=`/${s}`);let r=s.split("/").filter(i=>i&&i!=="."),n=[];for(let i of r)i===".."?n.pop():n.push(i);return`/${n.join("/")}`}routePath(t){let s=this.normalizePath(t),r=null,n=0;for(let i of this.mounts.values()){let o=i.mountPoint;if(s===o)return{fs:i.filesystem,relativePath:"/"};s.startsWith(`${o}/`)&&o.length>n&&(r=i,n=o.length)}if(r){let i=s.slice(n);return{fs:r.filesystem,relativePath:i||"/"}}return{fs:this.baseFs,relativePath:s}}getChildMountPoints(t){let s=this.normalizePath(t),r=s==="/"?"/":`${s}/`,n=[];for(let i of this.mounts.keys())if(i.startsWith(r)){let a=i.slice(r.length).split("/")[0];a&&!n.includes(a)&&n.push(a)}return n}async readFile(t,s){let{fs:r,relativePath:n}=this.routePath(t);return r.readFile(n,s)}async readFileBuffer(t){let{fs:s,relativePath:r}=this.routePath(t);return s.readFileBuffer(r)}async writeFile(t,s,r){let{fs:n,relativePath:i}=this.routePath(t);return n.writeFile(i,s,r)}async appendFile(t,s,r){let{fs:n,relativePath:i}=this.routePath(t);return n.appendFile(i,s,r)}async exists(t){let s=this.normalizePath(t);if(this.mounts.has(s)||this.getChildMountPoints(s).length>0)return!0;let{fs:n,relativePath:i}=this.routePath(t);return n.exists(i)}async stat(t){let s=this.normalizePath(t),r=this.mounts.get(s);if(r)try{return await r.filesystem.stat("/")}catch{return{isFile:!1,isDirectory:!0,isSymbolicLink:!1,mode:493,size:0,mtime:new Date}}if(this.getChildMountPoints(s).length>0)try{return await this.baseFs.stat(s)}catch{return{isFile:!1,isDirectory:!0,isSymbolicLink:!1,mode:493,size:0,mtime:new Date}}let{fs:i,relativePath:o}=this.routePath(t);return i.stat(o)}async lstat(t){let s=this.normalizePath(t),r=this.mounts.get(s);if(r)try{return await r.filesystem.lstat("/")}catch{return{isFile:!1,isDirectory:!0,isSymbolicLink:!1,mode:493,size:0,mtime:new Date}}if(this.getChildMountPoints(s).length>0)try{return await this.baseFs.lstat(s)}catch{return{isFile:!1,isDirectory:!0,isSymbolicLink:!1,mode:493,size:0,mtime:new Date}}let{fs:i,relativePath:o}=this.routePath(t);return i.lstat(o)}async mkdir(t,s){let r=this.normalizePath(t);if(this.mounts.has(r)){if(s?.recursive)return;throw new Error(`EEXIST: directory already exists, mkdir '${t}'`)}if(this.getChildMountPoints(r).length>0&&s?.recursive)return;let{fs:i,relativePath:o}=this.routePath(t);return i.mkdir(o,s)}async readdir(t){let s=this.normalizePath(t),r=new Set,n=null,{fs:i,relativePath:o}=this.routePath(t);try{let l=await i.readdir(o);for(let c of l)r.add(c)}catch(l){let c=l.code,u=l.message||"";if(c!=="ENOENT"&&!u.includes("ENOENT"))throw l;n=l}let a=this.getChildMountPoints(s);for(let l of a)r.add(l);if(r.size===0&&n&&!this.mounts.has(s))throw n;return Array.from(r).sort()}async rm(t,s){let r=this.normalizePath(t);if(this.mounts.has(r))throw new Error(`EBUSY: mount point, cannot remove '${t}'`);if(this.getChildMountPoints(r).length>0)throw new Error(`EBUSY: contains mount points, cannot remove '${t}'`);let{fs:i,relativePath:o}=this.routePath(t);return i.rm(o,s)}async cp(t,s,r){let n=this.routePath(t),i=this.routePath(s);return n.fs===i.fs?n.fs.cp(n.relativePath,i.relativePath,r):this.crossMountCopy(t,s,r)}async mv(t,s){let r=this.normalizePath(t);if(this.mounts.has(r))throw new Error(`EBUSY: mount point, cannot move '${t}'`);let n=this.routePath(t),i=this.routePath(s);if(n.fs===i.fs)return n.fs.mv(n.relativePath,i.relativePath);await this.cp(t,s,{recursive:!0}),await this.rm(t,{recursive:!0})}resolvePath(t,s){if(s.startsWith("/"))return this.normalizePath(s);let r=t==="/"?`/${s}`:`${t}/${s}`;return this.normalizePath(r)}getAllPaths(){let t=new Set;for(let s of this.baseFs.getAllPaths())t.add(s);for(let s of this.mounts.keys()){let r=s.split("/").filter(Boolean),n="";for(let o of r)n=`${n}/${o}`,t.add(n);let i=this.mounts.get(s);if(i)for(let o of i.filesystem.getAllPaths())o==="/"?t.add(s):t.add(`${s}${o}`)}return Array.from(t).sort()}async chmod(t,s){let r=this.normalizePath(t),n=this.mounts.get(r);if(n)return n.filesystem.chmod("/",s);let{fs:i,relativePath:o}=this.routePath(t);return i.chmod(o,s)}async symlink(t,s){let{fs:r,relativePath:n}=this.routePath(s);return r.symlink(t,n)}async link(t,s){let r=this.routePath(t),n=this.routePath(s);if(r.fs!==n.fs)throw new Error(`EXDEV: cross-device link not permitted, link '${t}' -> '${s}'`);return r.fs.link(r.relativePath,n.relativePath)}async readlink(t){let{fs:s,relativePath:r}=this.routePath(t);return s.readlink(r)}async realpath(t){let s=this.normalizePath(t);if(this.mounts.get(s))return s;let{fs:n,relativePath:i}=this.routePath(t),o=await n.realpath(i);for(let[a,l]of this.mounts)if(s===a||s.startsWith(`${a}/`))return o==="/"?a:`${a}${o}`;return o}async crossMountCopy(t,s,r){let n=await this.lstat(t);if(n.isFile){let i=await this.readFileBuffer(t);await this.writeFile(s,i),await this.chmod(s,n.mode)}else if(n.isDirectory){if(!r?.recursive)throw new Error(`cp: ${t} is a directory (not copied)`);await this.mkdir(s,{recursive:!0});let i=await this.readdir(t);for(let o of i){let a=t==="/"?`/${o}`:`${t}/${o}`,l=s==="/"?`/${o}`:`${s}/${o}`;await this.crossMountCopy(a,l,r)}}else if(n.isSymbolicLink){let i=await this.readlink(t);await this.symlink(i,s)}}async utimes(t,s,r){let{fs:n,relativePath:i}=this.routePath(t);return n.utimes(i,s,r)}};import*as Y from"node:fs";import*as ge from"node:path";import*as $e from"node:fs";import*as le from"node:path";function W(e){if(!e||e==="/")return"/";let t=e.endsWith("/")&&e!=="/"?e.slice(0,-1):e;t.startsWith("/")||(t=`/${t}`);let s=t.split("/").filter(n=>n&&n!=="."),r=[];for(let n of s)n===".."?r.pop():r.push(n);return`/${r.join("/")}`||"/"}function Te(e,t){return e===t||e.startsWith(`${t}/`)}function Qe(e,t){try{let s=$e.realpathSync(e);return Te(s,t)?s:null}catch(s){if(s.code==="ENOENT"){let r=le.dirname(e);if(r===e)return null;let n=Qe(r,t);if(n===null)return null;try{if($e.lstatSync(e).isSymbolicLink()){let o=$e.readlinkSync(e),a=le.isAbsolute(o)?o:le.resolve(le.dirname(e),o);if(Qe(a,t)===null)return null}}catch{}return le.join(n,le.basename(e))}return null}}function ls(e,t,s){let r=Qe(e,s);if(r===null)return null;let n=le.resolve(e),i=n.slice(t.length),o=r.slice(s.length);if(i!==o)return null;try{if($e.lstatSync(n).isSymbolicLink())return null}catch{}return r}function cs(e,t){if(!$e.existsSync(e))throw new Error(`${t} root does not exist: ${e}`);if(!$e.statSync(e).isDirectory())throw new Error(`${t} root is not a directory: ${e}`)}function z(e,t){if(e.includes("\0"))throw new Error(`ENOENT: path contains null byte, ${t} '${e}'`)}function Ei(e,t){if(!le.isAbsolute(e))return{withinRoot:!0,relativePath:e};let s;try{s=$e.realpathSync(e)}catch{s=le.resolve(e)}return Te(s,t)?{withinRoot:!0,relativePath:s.slice(t.length)||"/"}:{withinRoot:!1,safeName:le.basename(e)}}var Oa="/home/user/project",dt=class{root;canonicalRoot;mountPoint;readOnly;maxFileReadSize;allowSymlinks;memory=new Map;deleted=new Set;constructor(t){this.root=ge.resolve(t.root);let s=t.mountPoint??Oa;if(this.mountPoint=s==="/"?"/":s.replace(/\/+$/,""),!this.mountPoint.startsWith("/"))throw new Error(`Mount point must be an absolute path: ${s}`);this.readOnly=t.readOnly??!1,this.maxFileReadSize=t.maxFileReadSize??10485760,this.allowSymlinks=t.allowSymlinks??!1,cs(this.root,"OverlayFs"),this.canonicalRoot=Y.realpathSync(this.root),this.createMountPointDirs()}assertWritable(t){if(this.readOnly)throw new Error(`EROFS: read-only file system, ${t}`)}createMountPointDirs(){let t=this.mountPoint.split("/").filter(Boolean),s="";for(let r of t)s+=`/${r}`,this.memory.has(s)||this.memory.set(s,{type:"directory",mode:493,mtime:new Date});this.memory.has("/")||this.memory.set("/",{type:"directory",mode:493,mtime:new Date})}getMountPoint(){return this.mountPoint}mkdirSync(t,s){let n=W(t).split("/").filter(Boolean),i="";for(let o of n)i+=`/${o}`,this.memory.has(i)||this.memory.set(i,{type:"directory",mode:493,mtime:new Date})}writeFileSync(t,s){let r=W(t),n=this.getDirname(r);n!=="/"&&this.mkdirSync(n);let i=s instanceof Uint8Array?s:new TextEncoder().encode(s);this.memory.set(r,{type:"file",content:i,mode:420,mtime:new Date})}getDirname(t){let s=t.lastIndexOf("/");return s===0?"/":t.slice(0,s)}getRelativeToMount(t){return this.mountPoint==="/"?t:t===this.mountPoint?"/":t.startsWith(`${this.mountPoint}/`)?t.slice(this.mountPoint.length):null}toRealPath(t){let s=W(t),r=this.getRelativeToMount(s);if(r===null)return null;let n=ge.join(this.root,r),i=ge.resolve(n);return Te(i,this.root)?i:null}dirname(t){let s=W(t);if(s==="/")return"/";let r=s.lastIndexOf("/");return r===0?"/":s.slice(0,r)}resolveRealPath_(t){return t?this.allowSymlinks?Qe(t,this.canonicalRoot):ls(t,this.root,this.canonicalRoot):null}resolveRealPathParent_(t){if(!t)return null;let s=ge.dirname(t),r=this.resolveRealPath_(s);return r===null?null:ge.join(r,ge.basename(t))}ensureParentDirs(t){let s=this.dirname(t);s!=="/"&&(this.memory.has(s)||(this.ensureParentDirs(s),this.memory.set(s,{type:"directory",mode:493,mtime:new Date})),this.deleted.delete(s))}async existsInOverlay(t){let s=W(t);if(this.deleted.has(s))return!1;if(this.memory.has(s))return!0;let r=this.resolveRealPathParent_(this.toRealPath(s));if(!r)return!1;try{return await Y.promises.lstat(r),!0}catch{return!1}}async readFile(t,s){let r=await this.readFileBuffer(t),n=be(s);return et(r,n)}async readFileBuffer(t,s=new Set){z(t,"open");let r=W(t);if(s.has(r))throw new Error(`ELOOP: too many levels of symbolic links, open '${t}'`);if(s.add(r),this.deleted.has(r))throw new Error(`ENOENT: no such file or directory, open '${t}'`);let n=this.memory.get(r);if(n){if(n.type==="symlink"){let o=this.resolveSymlink(r,n.target);return this.readFileBuffer(o,s)}if(n.type!=="file")throw new Error(`EISDIR: illegal operation on a directory, read '${t}'`);return n.content}let i=this.resolveRealPath_(this.toRealPath(r));if(!i)throw new Error(`ENOENT: no such file or directory, open '${t}'`);try{let o=await Y.promises.lstat(i);if(o.isSymbolicLink()){if(!this.allowSymlinks)throw new Error(`ENOENT: no such file or directory, open '${t}'`);let l=await Y.promises.readlink(i),c=this.realTargetToVirtual(r,l),u=this.resolveSymlink(r,c);return this.readFileBuffer(u,s)}if(o.isDirectory())throw new Error(`EISDIR: illegal operation on a directory, read '${t}'`);if(this.maxFileReadSize>0&&o.size>this.maxFileReadSize)throw new Error(`EFBIG: file too large, read '${t}' (${o.size} bytes, max ${this.maxFileReadSize})`);let a=await Y.promises.readFile(i);return new Uint8Array(a)}catch(o){throw o.code==="ENOENT"?new Error(`ENOENT: no such file or directory, open '${t}'`):o}}async writeFile(t,s,r){z(t,"write"),this.assertWritable(`write '${t}'`);let n=W(t);this.ensureParentDirs(n);let i=be(r),o=Re(s,i);this.memory.set(n,{type:"file",content:o,mode:420,mtime:new Date}),this.deleted.delete(n)}async appendFile(t,s,r){z(t,"append"),this.assertWritable(`append '${t}'`);let n=W(t),i=be(r),o=Re(s,i),a;try{a=await this.readFileBuffer(n)}catch{a=new Uint8Array(0)}let l=new Uint8Array(a.length+o.length);l.set(a),l.set(o,a.length),this.ensureParentDirs(n),this.memory.set(n,{type:"file",content:l,mode:420,mtime:new Date}),this.deleted.delete(n)}async exists(t){return t.includes("\0")?!1:this.existsInOverlay(t)}async stat(t,s=new Set){z(t,"stat");let r=W(t);if(s.has(r))throw new Error(`ELOOP: too many levels of symbolic links, stat '${t}'`);if(s.add(r),this.deleted.has(r))throw new Error(`ENOENT: no such file or directory, stat '${t}'`);let n=this.memory.get(r);if(n){if(n.type==="symlink"){let a=this.resolveSymlink(r,n.target);return this.stat(a,s)}let o=0;return n.type==="file"&&(o=n.content.length),{isFile:n.type==="file",isDirectory:n.type==="directory",isSymbolicLink:!1,mode:n.mode,size:o,mtime:n.mtime}}let i=this.resolveRealPath_(this.toRealPath(r));if(!i)throw new Error(`ENOENT: no such file or directory, stat '${t}'`);try{let o=await Y.promises.lstat(i);if(o.isSymbolicLink()){if(!this.allowSymlinks)throw new Error(`ENOENT: no such file or directory, stat '${t}'`);let a=await Y.promises.readlink(i),l=this.realTargetToVirtual(r,a),c=this.resolveSymlink(r,l);return this.stat(c,s)}return{isFile:o.isFile(),isDirectory:o.isDirectory(),isSymbolicLink:!1,mode:o.mode,size:o.size,mtime:o.mtime}}catch(o){throw o.code==="ENOENT"?new Error(`ENOENT: no such file or directory, stat '${t}'`):o}}async lstat(t){z(t,"lstat");let s=W(t);if(this.deleted.has(s))throw new Error(`ENOENT: no such file or directory, lstat '${t}'`);let r=this.memory.get(s);if(r){if(r.type==="symlink")return{isFile:!1,isDirectory:!1,isSymbolicLink:!0,mode:r.mode,size:r.target.length,mtime:r.mtime};let i=0;return r.type==="file"&&(i=r.content.length),{isFile:r.type==="file",isDirectory:r.type==="directory",isSymbolicLink:!1,mode:r.mode,size:i,mtime:r.mtime}}let n=this.resolveRealPathParent_(this.toRealPath(s));if(!n)throw new Error(`ENOENT: no such file or directory, lstat '${t}'`);try{let i=await Y.promises.lstat(n);return{isFile:i.isFile(),isDirectory:i.isDirectory(),isSymbolicLink:i.isSymbolicLink(),mode:i.mode,size:i.size,mtime:i.mtime}}catch(i){throw i.code==="ENOENT"?new Error(`ENOENT: no such file or directory, lstat '${t}'`):i}}resolveSymlink(t,s){if(s.startsWith("/"))return W(s);let r=this.dirname(t);return W(r==="/"?`/${s}`:`${r}/${s}`)}realTargetToVirtual(t,s){let r=Ei(s,this.canonicalRoot);if(r.withinRoot){if(!ge.isAbsolute(s))return s;let n=r.relativePath;return this.mountPoint==="/"?n:`${this.mountPoint}${n}`}return r.safeName}async mkdir(t,s){z(t,"mkdir"),this.assertWritable(`mkdir '${t}'`);let r=W(t);if(await this.existsInOverlay(r)){if(!s?.recursive)throw new Error(`EEXIST: file already exists, mkdir '${t}'`);return}let i=this.dirname(r);if(i!=="/"&&!await this.existsInOverlay(i))if(s?.recursive)await this.mkdir(i,{recursive:!0});else throw new Error(`ENOENT: no such file or directory, mkdir '${t}'`);this.memory.set(r,{type:"directory",mode:493,mtime:new Date}),this.deleted.delete(r)}async readdirCore(t,s){if(this.deleted.has(s))throw new Error(`ENOENT: no such file or directory, scandir '${t}'`);let r=new Map,n=new Set,i=s==="/"?"/":`${s}/`;for(let a of this.deleted)if(a.startsWith(i)){let l=a.slice(i.length),c=l.split("/")[0];c&&!l.includes("/",c.length)&&n.add(c)}for(let[a,l]of this.memory)if(a!==s&&a.startsWith(i)){let c=a.slice(i.length),u=c.split("/")[0];u&&!n.has(u)&&!c.includes("/",1)&&r.set(u,{name:u,isFile:l.type==="file",isDirectory:l.type==="directory",isSymbolicLink:l.type==="symlink"})}let o=this.resolveRealPath_(this.toRealPath(s));if(o)try{let a=await Y.promises.readdir(o,{withFileTypes:!0});for(let l of a)!n.has(l.name)&&!r.has(l.name)&&r.set(l.name,{name:l.name,isFile:l.isFile(),isDirectory:l.isDirectory(),isSymbolicLink:l.isSymbolicLink()})}catch(a){if(a.code==="ENOENT"){if(!this.memory.has(s))throw new Error(`ENOENT: no such file or directory, scandir '${t}'`)}else if(a.code!=="ENOTDIR")throw a}return r}async resolveForReaddir(t,s=!1){let r=W(t),n=new Set,i=s,o=this.memory.get(r);for(;o&&o.type==="symlink";){if(n.has(r))throw new Error(`ELOOP: too many levels of symbolic links, scandir '${t}'`);n.add(r),i=!0,r=this.resolveSymlink(r,o.target),o=this.memory.get(r)}if(o)return{normalized:r,outsideOverlay:!1};if(this.getRelativeToMount(r)===null)return{normalized:r,outsideOverlay:!0};let l=this.resolveRealPath_(this.toRealPath(r));if(!l)return{normalized:r,outsideOverlay:!0};try{if((await Y.promises.lstat(l)).isSymbolicLink()){if(!this.allowSymlinks)return{normalized:r,outsideOverlay:!0};let u=await Y.promises.readlink(l),f=this.realTargetToVirtual(r,u),d=this.resolveSymlink(r,f);return this.resolveForReaddir(d,!0)}return{normalized:r,outsideOverlay:!1}}catch{return i?{normalized:r,outsideOverlay:!0}:{normalized:r,outsideOverlay:!1}}}async readdir(t){z(t,"scandir");let{normalized:s,outsideOverlay:r}=await this.resolveForReaddir(t);if(r)return[];let n=await this.readdirCore(t,s);return Array.from(n.keys()).sort((i,o)=>i<o?-1:i>o?1:0)}async readdirWithFileTypes(t){z(t,"scandir");let{normalized:s,outsideOverlay:r}=await this.resolveForReaddir(t);if(r)return[];let n=await this.readdirCore(t,s);return Array.from(n.values()).sort((i,o)=>i.name<o.name?-1:i.name>o.name?1:0)}async rm(t,s){z(t,"rm"),this.assertWritable(`rm '${t}'`);let r=W(t);if(!await this.existsInOverlay(r)){if(s?.force)return;throw new Error(`ENOENT: no such file or directory, rm '${t}'`)}try{if((await this.stat(r)).isDirectory){let o=await this.readdir(r);if(o.length>0){if(!s?.recursive)throw new Error(`ENOTEMPTY: directory not empty, rm '${t}'`);for(let a of o){let l=r==="/"?`/${a}`:`${r}/${a}`;await this.rm(l,s)}}}}catch(i){if(i instanceof Error&&(i.message.includes("ENOTEMPTY")||i.message.includes("EISDIR")))throw i}this.deleted.add(r),this.memory.delete(r)}async cp(t,s,r){z(t,"cp"),z(s,"cp"),this.assertWritable(`cp '${s}'`);let n=W(t),i=W(s);if(!await this.existsInOverlay(n))throw new Error(`ENOENT: no such file or directory, cp '${t}'`);let a=await this.stat(n);if(a.isFile){let l=await this.readFileBuffer(n);await this.writeFile(i,l)}else if(a.isDirectory){if(!r?.recursive)throw new Error(`EISDIR: is a directory, cp '${t}'`);await this.mkdir(i,{recursive:!0});let l=await this.readdir(n);for(let c of l){let u=n==="/"?`/${c}`:`${n}/${c}`,f=i==="/"?`/${c}`:`${i}/${c}`;await this.cp(u,f,r)}}}async mv(t,s){this.assertWritable(`mv '${s}'`),await this.cp(t,s,{recursive:!0}),await this.rm(t,{recursive:!0})}resolvePath(t,s){if(s.startsWith("/"))return W(s);let r=t==="/"?`/${s}`:`${t}/${s}`;return W(r)}getAllPaths(){let t=new Set(this.memory.keys());for(let s of this.deleted)t.delete(s);return this.scanRealFs("/",t),Array.from(t)}scanRealFs(t,s){if(this.deleted.has(t))return;let r=this.resolveRealPath_(this.toRealPath(t));if(r)try{let n=Y.readdirSync(r);for(let i of n){let o=t==="/"?`/${i}`:`${t}/${i}`;if(this.deleted.has(o))continue;s.add(o);let a=ge.join(r,i);Y.lstatSync(a).isDirectory()&&this.scanRealFs(o,s)}}catch{}}async chmod(t,s){z(t,"chmod"),this.assertWritable(`chmod '${t}'`);let r=W(t);if(!await this.existsInOverlay(r))throw new Error(`ENOENT: no such file or directory, chmod '${t}'`);let i=this.memory.get(r);if(i){i.mode=s;return}let o=await this.stat(r);if(o.isFile){let a=await this.readFileBuffer(r);this.memory.set(r,{type:"file",content:a,mode:s,mtime:new Date})}else o.isDirectory&&this.memory.set(r,{type:"directory",mode:s,mtime:new Date})}async symlink(t,s){if(!this.allowSymlinks)throw new Error(`EPERM: operation not permitted, symlink '${s}'`);z(s,"symlink"),this.assertWritable(`symlink '${s}'`);let r=W(s);if(await this.existsInOverlay(r))throw new Error(`EEXIST: file already exists, symlink '${s}'`);this.ensureParentDirs(r),this.memory.set(r,{type:"symlink",target:t,mode:511,mtime:new Date}),this.deleted.delete(r)}async link(t,s){z(t,"link"),z(s,"link"),this.assertWritable(`link '${s}'`);let r=W(t),n=W(s);if(!await this.existsInOverlay(r))throw new Error(`ENOENT: no such file or directory, link '${t}'`);let o=await this.stat(r);if(!o.isFile)throw new Error(`EPERM: operation not permitted, link '${t}'`);if(await this.existsInOverlay(n))throw new Error(`EEXIST: file already exists, link '${s}'`);let l=await this.readFileBuffer(r);this.ensureParentDirs(n),this.memory.set(n,{type:"file",content:l,mode:o.mode,mtime:new Date}),this.deleted.delete(n)}async readlink(t){z(t,"readlink");let s=W(t);if(this.deleted.has(s))throw new Error(`ENOENT: no such file or directory, readlink '${t}'`);let r=this.memory.get(s);if(r){if(r.type!=="symlink")throw new Error(`EINVAL: invalid argument, readlink '${t}'`);return r.target}let n=this.resolveRealPathParent_(this.toRealPath(s));if(!n)throw new Error(`ENOENT: no such file or directory, readlink '${t}'`);try{let i=await Y.promises.readlink(n);return this.realTargetToVirtual(s,i)}catch(i){throw i.code==="ENOENT"?new Error(`ENOENT: no such file or directory, readlink '${t}'`):i.code==="EINVAL"?new Error(`EINVAL: invalid argument, readlink '${t}'`):i}}async realpath(t){z(t,"realpath");let s=W(t),r=new Set,n=async a=>{let l=a==="/"?[]:a.slice(1).split("/"),c="";for(let u of l){if(c=`${c}/${u}`,r.has(c))throw new Error(`ELOOP: too many levels of symbolic links, realpath '${t}'`);if(this.deleted.has(c))throw new Error(`ENOENT: no such file or directory, realpath '${t}'`);let f=this.memory.get(c),d=0,h=40;for(;f&&f.type==="symlink"&&d<h;){if(r.add(c),c=this.resolveSymlink(c,f.target),d++,r.has(c))throw new Error(`ELOOP: too many levels of symbolic links, realpath '${t}'`);if(this.deleted.has(c))throw new Error(`ENOENT: no such file or directory, realpath '${t}'`);f=this.memory.get(c)}if(d>=h)throw new Error(`ELOOP: too many levels of symbolic links, realpath '${t}'`);if(!f){let m=this.toRealPath(c),p=this.resolveRealPath_(m);if(p)try{if((await Y.promises.lstat(p)).isSymbolicLink()){if(!this.allowSymlinks)throw new Error(`ENOENT: no such file or directory, realpath '${t}'`);let S=await Y.promises.readlink(p),v=this.realTargetToVirtual(c,S);return r.add(c),c=this.resolveSymlink(c,v),n(c)}}catch(g){throw g.code==="ENOENT"?new Error(`ENOENT: no such file or directory, realpath '${t}'`):g}else if(!this.allowSymlinks){let g=this.resolveRealPathParent_(m);if(g)try{if((await Y.promises.lstat(g)).isSymbolicLink())throw new Error(`ENOENT: no such file or directory, realpath '${t}'`)}catch(S){throw S.message?.includes("ENOENT")||S.message?.includes("ELOOP")?new Error(`ENOENT: no such file or directory, realpath '${t}'`):S}}}}return c||"/"},i=await n(s);if(!await this.existsInOverlay(i))throw new Error(`ENOENT: no such file or directory, realpath '${t}'`);return i}async utimes(t,s,r){z(t,"utimes"),this.assertWritable(`utimes '${t}'`);let n=W(t);if(!await this.existsInOverlay(n))throw new Error(`ENOENT: no such file or directory, utimes '${t}'`);let o=this.memory.get(n);if(o){o.mtime=r;return}let a=await this.stat(n);if(a.isFile){let l=await this.readFileBuffer(n);this.memory.set(n,{type:"file",content:l,mode:a.mode,mtime:r})}else a.isDirectory&&this.memory.set(n,{type:"directory",mode:a.mode,mtime:r})}};import*as M from"node:fs";import*as U from"node:path";var us=class{root;canonicalRoot;maxFileReadSize;allowSymlinks;constructor(t){this.root=U.resolve(t.root),this.maxFileReadSize=t.maxFileReadSize??10485760,this.allowSymlinks=t.allowSymlinks??!1,cs(this.root,"ReadWriteFs"),this.canonicalRoot=M.realpathSync(this.root)}resolveAndValidate(t,s){let r=this.allowSymlinks?Qe(t,this.canonicalRoot):ls(t,this.root,this.canonicalRoot);if(r===null)throw new Error(`EACCES: permission denied, '${s}' resolves outside sandbox`);return r}validateParent(t,s){let r=U.dirname(t),n=this.resolveAndValidate(r,s);return U.join(n,U.basename(t))}toRealPath(t){let s=W(t),r=U.join(this.root,s);return U.resolve(r)}async readFile(t,s){let r=await this.readFileBuffer(t),n=be(s);return et(r,n)}async readFileBuffer(t){z(t,"open");let s=this.toRealPath(t),r=this.resolveAndValidate(s,t);try{if(this.maxFileReadSize>0){let i=await M.promises.stat(r);if(i.size>this.maxFileReadSize)throw new Error(`EFBIG: file too large, read '${t}' (${i.size} bytes, max ${this.maxFileReadSize})`)}let n=await M.promises.readFile(r);return new Uint8Array(n)}catch(n){let i=n;if(i.code==="ENOENT")throw new Error(`ENOENT: no such file or directory, open '${t}'`);if(i.code==="EISDIR")throw new Error(`EISDIR: illegal operation on a directory, read '${t}'`);this.sanitizeError(n,t,"open")}}async writeFile(t,s,r){z(t,"write");let n=this.toRealPath(t),i=this.resolveAndValidate(n,t),o=be(r),a=Re(s,o),l=U.dirname(i);await M.promises.mkdir(l,{recursive:!0}),await M.promises.writeFile(i,a)}async appendFile(t,s,r){z(t,"append");let n=this.toRealPath(t),i=this.resolveAndValidate(n,t),o=be(r),a=Re(s,o),l=U.dirname(i);await M.promises.mkdir(l,{recursive:!0}),await M.promises.appendFile(i,a)}async exists(t){if(t.includes("\0"))return!1;let s=this.toRealPath(t);try{let r=this.resolveAndValidate(s,t);return await M.promises.access(r),!0}catch{return!1}}async stat(t){z(t,"stat");let s=this.toRealPath(t),r=this.resolveAndValidate(s,t);try{let n=await M.promises.stat(r);return{isFile:n.isFile(),isDirectory:n.isDirectory(),isSymbolicLink:!1,mode:n.mode,size:n.size,mtime:n.mtime}}catch(n){if(n.code==="ENOENT")throw new Error(`ENOENT: no such file or directory, stat '${t}'`);this.sanitizeError(n,t,"stat")}}async lstat(t){z(t,"lstat");let s=this.toRealPath(t),r=this.validateParent(s,t);try{let n=await M.promises.lstat(r);return{isFile:n.isFile(),isDirectory:n.isDirectory(),isSymbolicLink:n.isSymbolicLink(),mode:n.mode,size:n.size,mtime:n.mtime}}catch(n){if(n.code==="ENOENT")throw new Error(`ENOENT: no such file or directory, lstat '${t}'`);this.sanitizeError(n,t,"lstat")}}async mkdir(t,s){z(t,"mkdir");let r=this.toRealPath(t),n=this.resolveAndValidate(r,t);try{await M.promises.mkdir(n,{recursive:s?.recursive})}catch(i){let o=i;if(o.code==="EEXIST")throw new Error(`EEXIST: file already exists, mkdir '${t}'`);if(o.code==="ENOENT")throw new Error(`ENOENT: no such file or directory, mkdir '${t}'`);this.sanitizeError(i,t,"mkdir")}}async readdir(t){return(await this.readdirWithFileTypes(t)).map(r=>r.name)}async readdirWithFileTypes(t){z(t,"scandir");let s=this.toRealPath(t),r=this.resolveAndValidate(s,t);try{return(await M.promises.readdir(r,{withFileTypes:!0})).map(i=>({name:i.name,isFile:i.isFile(),isDirectory:i.isDirectory(),isSymbolicLink:i.isSymbolicLink()})).sort((i,o)=>i.name<o.name?-1:i.name>o.name?1:0)}catch(n){let i=n;if(i.code==="ENOENT")throw new Error(`ENOENT: no such file or directory, scandir '${t}'`);if(i.code==="ENOTDIR")throw new Error(`ENOTDIR: not a directory, scandir '${t}'`);this.sanitizeError(n,t,"scandir")}}async rm(t,s){z(t,"rm");let r=this.toRealPath(t),n=this.resolveAndValidate(r,t);try{await M.promises.rm(n,{recursive:s?.recursive??!1,force:s?.force??!1})}catch(i){let o=i;if(o.code==="ENOENT"&&!s?.force)throw new Error(`ENOENT: no such file or directory, rm '${t}'`);if(o.code==="ENOTEMPTY")throw new Error(`ENOTEMPTY: directory not empty, rm '${t}'`);this.sanitizeError(i,t,"rm")}}async cp(t,s,r){z(t,"cp"),z(s,"cp");let n=this.toRealPath(t),i=this.toRealPath(s),o=this.resolveAndValidate(n,t),a=this.resolveAndValidate(i,s);try{await M.promises.cp(o,a,{recursive:r?.recursive??!1,filter:async l=>{try{if(M.lstatSync(l).isSymbolicLink()){let u=await M.promises.realpath(l).catch(()=>null);return u===null?!1:Te(u,this.canonicalRoot)}return!0}catch{return!0}}})}catch(l){let c=l;if(c.code==="ENOENT")throw new Error(`ENOENT: no such file or directory, cp '${t}'`);if(c.code==="EISDIR")throw new Error(`EISDIR: is a directory, cp '${t}'`);this.sanitizeError(l,t,"cp")}}async mv(t,s){z(t,"mv"),z(s,"mv");let r=this.toRealPath(t),n=this.toRealPath(s),i=this.validateParent(r,t),o=this.validateParent(n,s);try{if((await M.promises.lstat(i)).isSymbolicLink()){let c=await M.promises.readlink(i),u=U.resolve(U.dirname(o),c),f=await M.promises.realpath(u).catch(()=>u);if(!Te(f,this.canonicalRoot))throw new Error(`EACCES: permission denied, mv '${t}' -> '${s}' would create symlink escaping sandbox`)}}catch(l){let c=l;if(c.code==="ENOENT")throw new Error(`ENOENT: no such file or directory, mv '${t}'`);if(c.message?.includes("EACCES")||c.message?.includes("escaping sandbox"))throw l}let a=U.dirname(o);await M.promises.mkdir(a,{recursive:!0});try{await M.promises.rename(i,o)}catch(l){let c=l;if(c.code==="ENOENT")throw new Error(`ENOENT: no such file or directory, mv '${t}'`);if(c.code==="EXDEV"){await this.cp(t,s,{recursive:!0}),await this.rm(t,{recursive:!0});return}this.sanitizeError(l,t,"mv")}try{if(M.lstatSync(o).isDirectory()&&this.findEscapingSymlinks(o).length>0)throw await M.promises.rename(o,i),new Error(`EACCES: permission denied, mv '${t}' -> '${s}' would create symlinks escaping sandbox`)}catch(l){if(l.message?.includes("EACCES")||l.message?.includes("escaping sandbox"))throw l}}resolvePath(t,s){if(s.startsWith("/"))return W(s);let r=t==="/"?`/${s}`:`${t}/${s}`;return W(r)}getAllPaths(){let t=[];return this.scanDir("/",t),t}sanitizeError(t,s,r){let n=t;if(n.message?.includes("EACCES")||n.message?.includes("escaping sandbox")||n.message?.includes("EFBIG"))throw t;let i=n.code||"EIO";throw new Error(`${i}: ${r} '${s}'`)}findEscapingSymlinks(t){let s=[];try{let r=M.readdirSync(t);for(let n of r){let i=U.join(t,n);try{let o=M.lstatSync(i);if(o.isSymbolicLink()){let a=M.readlinkSync(i),l=U.resolve(t,a),c;try{c=M.realpathSync(l)}catch{c=l}Te(c,this.canonicalRoot)||s.push(i)}else o.isDirectory()&&s.push(...this.findEscapingSymlinks(i))}catch{}}}catch{}return s}scanDir(t,s){let r=this.toRealPath(t),n;try{n=this.resolveAndValidate(r,t)}catch{return}try{let i=M.readdirSync(n);for(let o of i){let a=t==="/"?`/${o}`:`${t}/${o}`;s.push(a);let l=U.join(n,o);M.lstatSync(l).isDirectory()&&this.scanDir(a,s)}}catch{}}async chmod(t,s){z(t,"chmod");let r=this.toRealPath(t),n=this.resolveAndValidate(r,t);try{await M.promises.chmod(n,s)}catch(i){if(i.code==="ENOENT")throw new Error(`ENOENT: no such file or directory, chmod '${t}'`);this.sanitizeError(i,t,"chmod")}}async symlink(t,s){if(!this.allowSymlinks)throw new Error(`EPERM: operation not permitted, symlink '${s}'`);z(s,"symlink");let r=this.toRealPath(s),n=this.validateParent(r,s),i=W(s),o=W(U.dirname(i)),a=t.startsWith("/")?W(t):W(o==="/"?`/${t}`:`${o}/${t}`),l=U.join(this.canonicalRoot,a),c=U.dirname(n),u=t.startsWith("/")?l:U.relative(c,l);try{await M.promises.symlink(u,n)}catch(f){if(f.code==="EEXIST")throw new Error(`EEXIST: file already exists, symlink '${s}'`);this.sanitizeError(f,s,"symlink")}}async link(t,s){z(t,"link"),z(s,"link");let r=this.toRealPath(t),n=this.toRealPath(s),i=this.resolveAndValidate(r,t),o=this.resolveAndValidate(n,s);try{await M.promises.link(i,o)}catch(a){let l=a;if(l.code==="ENOENT")throw new Error(`ENOENT: no such file or directory, link '${t}'`);if(l.code==="EEXIST")throw new Error(`EEXIST: file already exists, link '${s}'`);if(l.code==="EPERM")throw new Error(`EPERM: operation not permitted, link '${t}'`);this.sanitizeError(a,t,"link")}}async readlink(t){z(t,"readlink");let s=this.toRealPath(t),r=this.validateParent(s,t);try{let n=await M.promises.readlink(r),i=W(t),o=U.dirname(i),a=U.isAbsolute(n)?n:U.resolve(U.dirname(r),n),l=await M.promises.realpath(a).catch(()=>a);if(Te(l,this.canonicalRoot)){let c=l.slice(this.canonicalRoot.length)||"/";return o==="/"?c.startsWith("/")?c.slice(1)||".":c:U.relative(o,c)}return U.isAbsolute(n)?U.basename(n):n}catch(n){let i=n;if(i.code==="ENOENT")throw new Error(`ENOENT: no such file or directory, readlink '${t}'`);if(i.code==="EINVAL")throw new Error(`EINVAL: invalid argument, readlink '${t}'`);this.sanitizeError(n,t,"readlink")}}async realpath(t){z(t,"realpath");let s=this.toRealPath(t);try{this.resolveAndValidate(s,t)}catch{throw new Error(`ENOENT: no such file or directory, realpath '${t}'`)}let r;try{r=await M.promises.realpath(s)}catch(n){let i=n;if(i.code==="ENOENT")throw new Error(`ENOENT: no such file or directory, realpath '${t}'`);if(i.code==="ELOOP")throw new Error(`ELOOP: too many levels of symbolic links, realpath '${t}'`);this.sanitizeError(n,t,"realpath")}if(Te(r,this.canonicalRoot))return r.slice(this.canonicalRoot.length)||"/";throw new Error(`ENOENT: no such file or directory, realpath '${t}'`)}async utimes(t,s,r){z(t,"utimes");let n=this.toRealPath(t),i=this.resolveAndValidate(n,t);try{await M.promises.utimes(i,s,r)}catch(o){if(o.code==="ENOENT")throw new Error(`ENOENT: no such file or directory, utimes '${t}'`);this.sanitizeError(o,t,"utimes")}}};var ht=class{cmdId;cwd;startedAt;exitCode;bashEnv;cmdLine;env;explicitCwd;resultPromise;constructor(t,s,r,n,i=!1){this.cmdId=crypto.randomUUID(),this.cwd=r,this.startedAt=new Date,this.bashEnv=t,this.cmdLine=s,this.env=n,this.explicitCwd=i,this.resultPromise=this.execute()}async execute(){let t=this.env||this.explicitCwd?{cwd:this.explicitCwd?this.cwd:void 0,env:this.env}:void 0,s=await this.bashEnv.exec(this.cmdLine,t);return this.exitCode=s.exitCode,s}async*logs(){let t=await this.resultPromise;t.stdout&&(yield{type:"stdout",data:t.stdout,timestamp:new Date}),t.stderr&&(yield{type:"stderr",data:t.stderr,timestamp:new Date})}async wait(){return await this.resultPromise,this}async output(){let t=await this.resultPromise;return t.stdout+t.stderr}async stdout(){return(await this.resultPromise).stdout}async stderr(){return(await this.resultPromise).stderr}async kill(){}};var fs=class e{bashEnv;constructor(t){this.bashEnv=t}static async create(t){let s=t?.fs;if(t?.overlayRoot){if(t?.fs)throw new Error("Cannot specify both 'fs' and 'overlayRoot' options");s=new dt({root:t.overlayRoot})}let r=new _t({env:t?.env,cwd:t?.cwd,fs:s,maxCallDepth:t?.maxCallDepth,maxCommandCount:t?.maxCommandCount,maxLoopIterations:t?.maxLoopIterations,network:t?.network});return new e(r)}async runCommand(t,s){let r=s?.cwd??this.bashEnv.getCwd(),n=s?.cwd!==void 0;return new ht(this.bashEnv,t,r,s?.env,n)}async writeFiles(t){for(let[s,r]of Object.entries(t)){let n;typeof r=="string"?n=r:r.encoding==="base64"?n=Buffer.from(r.content,"base64").toString("utf-8"):n=r.content;let i=s.substring(0,s.lastIndexOf("/"))||"/";i!=="/"&&await this.bashEnv.exec(`mkdir -p ${i}`),await this.bashEnv.writeFile(s,n)}}async readFile(t,s){let r=await this.bashEnv.readFile(t);return s==="base64"?Buffer.from(r).toString("base64"):r}async mkDir(t,s){let r=s?.recursive?"-p":"";await this.bashEnv.exec(`mkdir ${r} ${t}`)}async stop(){}async extendTimeout(t){}get domain(){}get bashEnvInstance(){return this.bashEnv}};var ds=class{violations=[];violationsByType=new Map;options;constructor(t={}){this.options={maxViolationsPerType:t.maxViolationsPerType??100,includeStackTraces:t.includeStackTraces??!0,onViolation:t.onViolation??(()=>{}),logToConsole:t.logToConsole??!1}}record(t){let s=this.options.includeStackTraces?t:{...t,stack:void 0};this.violations.unshift(s);let r=this.violationsByType.get(t.type);r||(r=[],this.violationsByType.set(t.type,r)),r.length<this.options.maxViolationsPerType&&r.push(s),this.options.logToConsole&&console.warn(`[SecurityViolation] ${t.type}: ${t.message}`,t.path),this.options.onViolation(s)}getViolations(){return[...this.violations]}getViolationsByType(t){return[...this.violationsByType.get(t)??[]]}getSummary(){let t=[];for(let[s,r]of this.violationsByType){if(r.length===0)continue;let n=new Set,i=Number.POSITIVE_INFINITY,o=0;for(let a of r)n.add(a.path),i=Math.min(i,a.timestamp),o=Math.max(o,a.timestamp);t.push({type:s,count:r.length,firstSeen:i,lastSeen:o,paths:Array.from(n)})}return t.sort((s,r)=>r.count-s.count),t}getTotalCount(){return this.violations.length}hasViolations(){return this.violations.length>0}clear(){this.violations=[],this.violationsByType.clear()}createCallback(){return t=>this.record(t)}};function $i(){return e=>{console.warn("[DefenseInDepth] Security violation detected:",`
705
+ `)}var as=class{baseFs;mounts=new Map;constructor(t){if(this.baseFs=t?.base??new Le,t?.mounts)for(let{mountPoint:s,filesystem:r}of t.mounts)this.mount(s,r)}mount(t,s){this.validateMountPath(t);let r=this.normalizePath(t);this.validateMount(r),this.mounts.set(r,{mountPoint:r,filesystem:s})}unmount(t){let s=this.normalizePath(t);if(!this.mounts.has(s))throw new Error(`No filesystem mounted at '${t}'`);this.mounts.delete(s)}getMounts(){return Array.from(this.mounts.values()).map(t=>({mountPoint:t.mountPoint,filesystem:t.filesystem}))}isMountPoint(t){let s=this.normalizePath(t);return this.mounts.has(s)}validateMountPath(t){let s=t.split("/");for(let r of s)if(r==="."||r==="..")throw new Error(`Invalid mount point '${t}': contains '.' or '..' segments`)}validateMount(t){if(t==="/")throw new Error("Cannot mount at root '/'");for(let s of this.mounts.keys())if(s!==t){if(t.startsWith(`${s}/`))throw new Error(`Cannot mount at '${t}': inside existing mount '${s}'`);if(s.startsWith(`${t}/`))throw new Error(`Cannot mount at '${t}': would contain existing mount '${s}'`)}}normalizePath(t){if(!t||t==="/")return"/";let s=t.endsWith("/")&&t!=="/"?t.slice(0,-1):t;s.startsWith("/")||(s=`/${s}`);let r=s.split("/").filter(i=>i&&i!=="."),n=[];for(let i of r)i===".."?n.pop():n.push(i);return`/${n.join("/")}`}routePath(t){let s=this.normalizePath(t),r=null,n=0;for(let i of this.mounts.values()){let o=i.mountPoint;if(s===o)return{fs:i.filesystem,relativePath:"/"};s.startsWith(`${o}/`)&&o.length>n&&(r=i,n=o.length)}if(r){let i=s.slice(n);return{fs:r.filesystem,relativePath:i||"/"}}return{fs:this.baseFs,relativePath:s}}getChildMountPoints(t){let s=this.normalizePath(t),r=s==="/"?"/":`${s}/`,n=[];for(let i of this.mounts.keys())if(i.startsWith(r)){let a=i.slice(r.length).split("/")[0];a&&!n.includes(a)&&n.push(a)}return n}async readFile(t,s){let{fs:r,relativePath:n}=this.routePath(t);return r.readFile(n,s)}async readFileBuffer(t){let{fs:s,relativePath:r}=this.routePath(t);return s.readFileBuffer(r)}async writeFile(t,s,r){let{fs:n,relativePath:i}=this.routePath(t);return n.writeFile(i,s,r)}async appendFile(t,s,r){let{fs:n,relativePath:i}=this.routePath(t);return n.appendFile(i,s,r)}async exists(t){let s=this.normalizePath(t);if(this.mounts.has(s)||this.getChildMountPoints(s).length>0)return!0;let{fs:n,relativePath:i}=this.routePath(t);return n.exists(i)}async stat(t){let s=this.normalizePath(t),r=this.mounts.get(s);if(r)try{return await r.filesystem.stat("/")}catch{return{isFile:!1,isDirectory:!0,isSymbolicLink:!1,mode:493,size:0,mtime:new Date}}if(this.getChildMountPoints(s).length>0)try{return await this.baseFs.stat(s)}catch{return{isFile:!1,isDirectory:!0,isSymbolicLink:!1,mode:493,size:0,mtime:new Date}}let{fs:i,relativePath:o}=this.routePath(t);return i.stat(o)}async lstat(t){let s=this.normalizePath(t),r=this.mounts.get(s);if(r)try{return await r.filesystem.lstat("/")}catch{return{isFile:!1,isDirectory:!0,isSymbolicLink:!1,mode:493,size:0,mtime:new Date}}if(this.getChildMountPoints(s).length>0)try{return await this.baseFs.lstat(s)}catch{return{isFile:!1,isDirectory:!0,isSymbolicLink:!1,mode:493,size:0,mtime:new Date}}let{fs:i,relativePath:o}=this.routePath(t);return i.lstat(o)}async mkdir(t,s){let r=this.normalizePath(t);if(this.mounts.has(r)){if(s?.recursive)return;throw new Error(`EEXIST: directory already exists, mkdir '${t}'`)}if(this.getChildMountPoints(r).length>0&&s?.recursive)return;let{fs:i,relativePath:o}=this.routePath(t);return i.mkdir(o,s)}async readdir(t){let s=this.normalizePath(t),r=new Set,n=null,{fs:i,relativePath:o}=this.routePath(t);try{let l=await i.readdir(o);for(let c of l)r.add(c)}catch(l){let c=l.code,u=l.message||"";if(c!=="ENOENT"&&!u.includes("ENOENT"))throw l;n=l}let a=this.getChildMountPoints(s);for(let l of a)r.add(l);if(r.size===0&&n&&!this.mounts.has(s))throw n;return Array.from(r).sort()}async rm(t,s){let r=this.normalizePath(t);if(this.mounts.has(r))throw new Error(`EBUSY: mount point, cannot remove '${t}'`);if(this.getChildMountPoints(r).length>0)throw new Error(`EBUSY: contains mount points, cannot remove '${t}'`);let{fs:i,relativePath:o}=this.routePath(t);return i.rm(o,s)}async cp(t,s,r){let n=this.routePath(t),i=this.routePath(s);return n.fs===i.fs?n.fs.cp(n.relativePath,i.relativePath,r):this.crossMountCopy(t,s,r)}async mv(t,s){let r=this.normalizePath(t);if(this.mounts.has(r))throw new Error(`EBUSY: mount point, cannot move '${t}'`);let n=this.routePath(t),i=this.routePath(s);if(n.fs===i.fs)return n.fs.mv(n.relativePath,i.relativePath);await this.cp(t,s,{recursive:!0}),await this.rm(t,{recursive:!0})}resolvePath(t,s){if(s.startsWith("/"))return this.normalizePath(s);let r=t==="/"?`/${s}`:`${t}/${s}`;return this.normalizePath(r)}getAllPaths(){let t=new Set;for(let s of this.baseFs.getAllPaths())t.add(s);for(let s of this.mounts.keys()){let r=s.split("/").filter(Boolean),n="";for(let o of r)n=`${n}/${o}`,t.add(n);let i=this.mounts.get(s);if(i)for(let o of i.filesystem.getAllPaths())o==="/"?t.add(s):t.add(`${s}${o}`)}return Array.from(t).sort()}async chmod(t,s){let r=this.normalizePath(t),n=this.mounts.get(r);if(n)return n.filesystem.chmod("/",s);let{fs:i,relativePath:o}=this.routePath(t);return i.chmod(o,s)}async symlink(t,s){let{fs:r,relativePath:n}=this.routePath(s);return r.symlink(t,n)}async link(t,s){let r=this.routePath(t),n=this.routePath(s);if(r.fs!==n.fs)throw new Error(`EXDEV: cross-device link not permitted, link '${t}' -> '${s}'`);return r.fs.link(r.relativePath,n.relativePath)}async readlink(t){let{fs:s,relativePath:r}=this.routePath(t);return s.readlink(r)}async realpath(t){let s=this.normalizePath(t);if(this.mounts.get(s))return s;let{fs:n,relativePath:i}=this.routePath(t),o=await n.realpath(i);for(let[a,l]of this.mounts)if(s===a||s.startsWith(`${a}/`))return o==="/"?a:`${a}${o}`;return o}async crossMountCopy(t,s,r){let n=await this.lstat(t);if(n.isFile){let i=await this.readFileBuffer(t);await this.writeFile(s,i),await this.chmod(s,n.mode)}else if(n.isDirectory){if(!r?.recursive)throw new Error(`cp: ${t} is a directory (not copied)`);await this.mkdir(s,{recursive:!0});let i=await this.readdir(t);for(let o of i){let a=t==="/"?`/${o}`:`${t}/${o}`,l=s==="/"?`/${o}`:`${s}/${o}`;await this.crossMountCopy(a,l,r)}}else if(n.isSymbolicLink){let i=await this.readlink(t);await this.symlink(i,s)}}async utimes(t,s,r){let{fs:n,relativePath:i}=this.routePath(t);return n.utimes(i,s,r)}};import*as Y from"node:fs";import*as ge from"node:path";import*as $e from"node:fs";import*as le from"node:path";function W(e){if(!e||e==="/")return"/";let t=e.endsWith("/")&&e!=="/"?e.slice(0,-1):e;t.startsWith("/")||(t=`/${t}`);let s=t.split("/").filter(n=>n&&n!=="."),r=[];for(let n of s)n===".."?r.pop():r.push(n);return`/${r.join("/")}`||"/"}function Te(e,t){return e===t||e.startsWith(`${t}/`)}function Qe(e,t){try{let s=$e.realpathSync(e);return Te(s,t)?s:null}catch(s){if(s.code==="ENOENT"){let r=le.dirname(e);if(r===e)return null;let n=Qe(r,t);if(n===null)return null;try{if($e.lstatSync(e).isSymbolicLink()){let o=$e.readlinkSync(e),a=le.isAbsolute(o)?o:le.resolve(le.dirname(e),o);if(Qe(a,t)===null)return null}}catch{}return le.join(n,le.basename(e))}return null}}function ls(e,t,s){let r=Qe(e,s);if(r===null)return null;let n=le.resolve(e),i=n.slice(t.length),o=r.slice(s.length);if(i!==o)return null;try{if($e.lstatSync(n).isSymbolicLink())return null}catch{}return r}function cs(e,t){if(!$e.existsSync(e))throw new Error(`${t} root does not exist: ${e}`);if(!$e.statSync(e).isDirectory())throw new Error(`${t} root is not a directory: ${e}`)}function z(e,t){if(e.includes("\0"))throw new Error(`ENOENT: path contains null byte, ${t} '${e}'`)}function Ei(e,t){if(!le.isAbsolute(e))return{withinRoot:!0,relativePath:e};let s;try{s=$e.realpathSync(e)}catch{s=le.resolve(e)}return Te(s,t)?{withinRoot:!0,relativePath:s.slice(t.length)||"/"}:{withinRoot:!1,safeName:le.basename(e)}}var Oa="/home/user/project",dt=class{root;canonicalRoot;mountPoint;readOnly;maxFileReadSize;allowSymlinks;memory=new Map;deleted=new Set;constructor(t){this.root=ge.resolve(t.root);let s=t.mountPoint??Oa;if(this.mountPoint=s==="/"?"/":s.replace(/\/+$/,""),!this.mountPoint.startsWith("/"))throw new Error(`Mount point must be an absolute path: ${s}`);this.readOnly=t.readOnly??!1,this.maxFileReadSize=t.maxFileReadSize??10485760,this.allowSymlinks=t.allowSymlinks??!1,cs(this.root,"OverlayFs"),this.canonicalRoot=Y.realpathSync(this.root),this.createMountPointDirs()}assertWritable(t){if(this.readOnly)throw new Error(`EROFS: read-only file system, ${t}`)}createMountPointDirs(){let t=this.mountPoint.split("/").filter(Boolean),s="";for(let r of t)s+=`/${r}`,this.memory.has(s)||this.memory.set(s,{type:"directory",mode:493,mtime:new Date});this.memory.has("/")||this.memory.set("/",{type:"directory",mode:493,mtime:new Date})}getMountPoint(){return this.mountPoint}mkdirSync(t,s){let n=W(t).split("/").filter(Boolean),i="";for(let o of n)i+=`/${o}`,this.memory.has(i)||this.memory.set(i,{type:"directory",mode:493,mtime:new Date})}writeFileSync(t,s){let r=W(t),n=this.getDirname(r);n!=="/"&&this.mkdirSync(n);let i=s instanceof Uint8Array?s:new TextEncoder().encode(s);this.memory.set(r,{type:"file",content:i,mode:420,mtime:new Date})}getDirname(t){let s=t.lastIndexOf("/");return s===0?"/":t.slice(0,s)}getRelativeToMount(t){return this.mountPoint==="/"?t:t===this.mountPoint?"/":t.startsWith(`${this.mountPoint}/`)?t.slice(this.mountPoint.length):null}toRealPath(t){let s=W(t),r=this.getRelativeToMount(s);if(r===null)return null;let n=ge.join(this.root,r),i=ge.resolve(n);return Te(i,this.root)?i:null}dirname(t){let s=W(t);if(s==="/")return"/";let r=s.lastIndexOf("/");return r===0?"/":s.slice(0,r)}resolveRealPath_(t){return t?this.allowSymlinks?Qe(t,this.canonicalRoot):ls(t,this.root,this.canonicalRoot):null}resolveRealPathParent_(t){if(!t)return null;let s=ge.dirname(t),r=this.resolveRealPath_(s);return r===null?null:ge.join(r,ge.basename(t))}sanitizeError(t,s,r){let n=t;if(n.message?.includes("ELOOP")||n.message?.includes("EFBIG")||n.message?.includes("EPERM"))throw t;let i=n.code||"EIO";throw new Error(`${i}: ${r} '${s}'`)}ensureParentDirs(t){let s=this.dirname(t);s!=="/"&&(this.memory.has(s)||(this.ensureParentDirs(s),this.memory.set(s,{type:"directory",mode:493,mtime:new Date})),this.deleted.delete(s))}async existsInOverlay(t){let s=W(t);if(this.deleted.has(s))return!1;if(this.memory.has(s))return!0;let r=this.resolveRealPathParent_(this.toRealPath(s));if(!r)return!1;try{return await Y.promises.lstat(r),!0}catch{return!1}}async readFile(t,s){let r=await this.readFileBuffer(t),n=be(s);return et(r,n)}async readFileBuffer(t,s=new Set){z(t,"open");let r=W(t);if(s.has(r))throw new Error(`ELOOP: too many levels of symbolic links, open '${t}'`);if(s.add(r),this.deleted.has(r))throw new Error(`ENOENT: no such file or directory, open '${t}'`);let n=this.memory.get(r);if(n){if(n.type==="symlink"){let o=this.resolveSymlink(r,n.target);return this.readFileBuffer(o,s)}if(n.type!=="file")throw new Error(`EISDIR: illegal operation on a directory, read '${t}'`);return n.content}let i=this.resolveRealPath_(this.toRealPath(r));if(!i)throw new Error(`ENOENT: no such file or directory, open '${t}'`);try{let o=await Y.promises.lstat(i);if(o.isSymbolicLink()){if(!this.allowSymlinks)throw new Error(`ENOENT: no such file or directory, open '${t}'`);let l=await Y.promises.readlink(i),c=this.realTargetToVirtual(r,l),u=this.resolveSymlink(r,c);return this.readFileBuffer(u,s)}if(o.isDirectory())throw new Error(`EISDIR: illegal operation on a directory, read '${t}'`);if(this.maxFileReadSize>0&&o.size>this.maxFileReadSize)throw new Error(`EFBIG: file too large, read '${t}' (${o.size} bytes, max ${this.maxFileReadSize})`);let a=await Y.promises.readFile(i);return new Uint8Array(a)}catch(o){if(o.code==="ENOENT")throw new Error(`ENOENT: no such file or directory, open '${t}'`);this.sanitizeError(o,t,"open")}}async writeFile(t,s,r){z(t,"write"),this.assertWritable(`write '${t}'`);let n=W(t);this.ensureParentDirs(n);let i=be(r),o=Re(s,i);this.memory.set(n,{type:"file",content:o,mode:420,mtime:new Date}),this.deleted.delete(n)}async appendFile(t,s,r){z(t,"append"),this.assertWritable(`append '${t}'`);let n=W(t),i=be(r),o=Re(s,i),a;try{a=await this.readFileBuffer(n)}catch{a=new Uint8Array(0)}let l=new Uint8Array(a.length+o.length);l.set(a),l.set(o,a.length),this.ensureParentDirs(n),this.memory.set(n,{type:"file",content:l,mode:420,mtime:new Date}),this.deleted.delete(n)}async exists(t){return t.includes("\0")?!1:this.existsInOverlay(t)}async stat(t,s=new Set){z(t,"stat");let r=W(t);if(s.has(r))throw new Error(`ELOOP: too many levels of symbolic links, stat '${t}'`);if(s.add(r),this.deleted.has(r))throw new Error(`ENOENT: no such file or directory, stat '${t}'`);let n=this.memory.get(r);if(n){if(n.type==="symlink"){let a=this.resolveSymlink(r,n.target);return this.stat(a,s)}let o=0;return n.type==="file"&&(o=n.content.length),{isFile:n.type==="file",isDirectory:n.type==="directory",isSymbolicLink:!1,mode:n.mode,size:o,mtime:n.mtime}}let i=this.resolveRealPath_(this.toRealPath(r));if(!i)throw new Error(`ENOENT: no such file or directory, stat '${t}'`);try{let o=await Y.promises.lstat(i);if(o.isSymbolicLink()){if(!this.allowSymlinks)throw new Error(`ENOENT: no such file or directory, stat '${t}'`);let a=await Y.promises.readlink(i),l=this.realTargetToVirtual(r,a),c=this.resolveSymlink(r,l);return this.stat(c,s)}return{isFile:o.isFile(),isDirectory:o.isDirectory(),isSymbolicLink:!1,mode:o.mode,size:o.size,mtime:o.mtime}}catch(o){if(o.code==="ENOENT")throw new Error(`ENOENT: no such file or directory, stat '${t}'`);this.sanitizeError(o,t,"stat")}}async lstat(t){z(t,"lstat");let s=W(t);if(this.deleted.has(s))throw new Error(`ENOENT: no such file or directory, lstat '${t}'`);let r=this.memory.get(s);if(r){if(r.type==="symlink")return{isFile:!1,isDirectory:!1,isSymbolicLink:!0,mode:r.mode,size:r.target.length,mtime:r.mtime};let i=0;return r.type==="file"&&(i=r.content.length),{isFile:r.type==="file",isDirectory:r.type==="directory",isSymbolicLink:!1,mode:r.mode,size:i,mtime:r.mtime}}let n=this.resolveRealPathParent_(this.toRealPath(s));if(!n)throw new Error(`ENOENT: no such file or directory, lstat '${t}'`);try{let i=await Y.promises.lstat(n);return{isFile:i.isFile(),isDirectory:i.isDirectory(),isSymbolicLink:i.isSymbolicLink(),mode:i.mode,size:i.size,mtime:i.mtime}}catch(i){if(i.code==="ENOENT")throw new Error(`ENOENT: no such file or directory, lstat '${t}'`);this.sanitizeError(i,t,"lstat")}}resolveSymlink(t,s){if(s.startsWith("/"))return W(s);let r=this.dirname(t);return W(r==="/"?`/${s}`:`${r}/${s}`)}realTargetToVirtual(t,s){let r=Ei(s,this.canonicalRoot);if(r.withinRoot){if(!ge.isAbsolute(s))return s;let n=r.relativePath;return this.mountPoint==="/"?n:`${this.mountPoint}${n}`}return r.safeName}async mkdir(t,s){z(t,"mkdir"),this.assertWritable(`mkdir '${t}'`);let r=W(t);if(await this.existsInOverlay(r)){if(!s?.recursive)throw new Error(`EEXIST: file already exists, mkdir '${t}'`);return}let i=this.dirname(r);if(i!=="/"&&!await this.existsInOverlay(i))if(s?.recursive)await this.mkdir(i,{recursive:!0});else throw new Error(`ENOENT: no such file or directory, mkdir '${t}'`);this.memory.set(r,{type:"directory",mode:493,mtime:new Date}),this.deleted.delete(r)}async readdirCore(t,s){if(this.deleted.has(s))throw new Error(`ENOENT: no such file or directory, scandir '${t}'`);let r=new Map,n=new Set,i=s==="/"?"/":`${s}/`;for(let a of this.deleted)if(a.startsWith(i)){let l=a.slice(i.length),c=l.split("/")[0];c&&!l.includes("/",c.length)&&n.add(c)}for(let[a,l]of this.memory)if(a!==s&&a.startsWith(i)){let c=a.slice(i.length),u=c.split("/")[0];u&&!n.has(u)&&!c.includes("/",1)&&r.set(u,{name:u,isFile:l.type==="file",isDirectory:l.type==="directory",isSymbolicLink:l.type==="symlink"})}let o=this.resolveRealPath_(this.toRealPath(s));if(o)try{let a=await Y.promises.readdir(o,{withFileTypes:!0});for(let l of a)!n.has(l.name)&&!r.has(l.name)&&r.set(l.name,{name:l.name,isFile:l.isFile(),isDirectory:l.isDirectory(),isSymbolicLink:l.isSymbolicLink()})}catch(a){if(a.code==="ENOENT"){if(!this.memory.has(s))throw new Error(`ENOENT: no such file or directory, scandir '${t}'`)}else a.code!=="ENOTDIR"&&this.sanitizeError(a,t,"scandir")}return r}async resolveForReaddir(t,s=!1){let r=W(t),n=new Set,i=s,o=this.memory.get(r);for(;o&&o.type==="symlink";){if(n.has(r))throw new Error(`ELOOP: too many levels of symbolic links, scandir '${t}'`);n.add(r),i=!0,r=this.resolveSymlink(r,o.target),o=this.memory.get(r)}if(o)return{normalized:r,outsideOverlay:!1};if(this.getRelativeToMount(r)===null)return{normalized:r,outsideOverlay:!0};let l=this.resolveRealPath_(this.toRealPath(r));if(!l)return{normalized:r,outsideOverlay:!0};try{if((await Y.promises.lstat(l)).isSymbolicLink()){if(!this.allowSymlinks)return{normalized:r,outsideOverlay:!0};let u=await Y.promises.readlink(l),f=this.realTargetToVirtual(r,u),d=this.resolveSymlink(r,f);return this.resolveForReaddir(d,!0)}return{normalized:r,outsideOverlay:!1}}catch{return i?{normalized:r,outsideOverlay:!0}:{normalized:r,outsideOverlay:!1}}}async readdir(t){z(t,"scandir");let{normalized:s,outsideOverlay:r}=await this.resolveForReaddir(t);if(r)return[];let n=await this.readdirCore(t,s);return Array.from(n.keys()).sort((i,o)=>i<o?-1:i>o?1:0)}async readdirWithFileTypes(t){z(t,"scandir");let{normalized:s,outsideOverlay:r}=await this.resolveForReaddir(t);if(r)return[];let n=await this.readdirCore(t,s);return Array.from(n.values()).sort((i,o)=>i.name<o.name?-1:i.name>o.name?1:0)}async rm(t,s){z(t,"rm"),this.assertWritable(`rm '${t}'`);let r=W(t);if(!await this.existsInOverlay(r)){if(s?.force)return;throw new Error(`ENOENT: no such file or directory, rm '${t}'`)}try{if((await this.stat(r)).isDirectory){let o=await this.readdir(r);if(o.length>0){if(!s?.recursive)throw new Error(`ENOTEMPTY: directory not empty, rm '${t}'`);for(let a of o){let l=r==="/"?`/${a}`:`${r}/${a}`;await this.rm(l,s)}}}}catch(i){if(i instanceof Error&&(i.message.includes("ENOTEMPTY")||i.message.includes("EISDIR")))throw i}this.deleted.add(r),this.memory.delete(r)}async cp(t,s,r){z(t,"cp"),z(s,"cp"),this.assertWritable(`cp '${s}'`);let n=W(t),i=W(s);if(!await this.existsInOverlay(n))throw new Error(`ENOENT: no such file or directory, cp '${t}'`);let a=await this.stat(n);if(a.isFile){let l=await this.readFileBuffer(n);await this.writeFile(i,l)}else if(a.isDirectory){if(!r?.recursive)throw new Error(`EISDIR: is a directory, cp '${t}'`);await this.mkdir(i,{recursive:!0});let l=await this.readdir(n);for(let c of l){let u=n==="/"?`/${c}`:`${n}/${c}`,f=i==="/"?`/${c}`:`${i}/${c}`;await this.cp(u,f,r)}}}async mv(t,s){this.assertWritable(`mv '${s}'`),await this.cp(t,s,{recursive:!0}),await this.rm(t,{recursive:!0})}resolvePath(t,s){if(s.startsWith("/"))return W(s);let r=t==="/"?`/${s}`:`${t}/${s}`;return W(r)}getAllPaths(){let t=new Set(this.memory.keys());for(let s of this.deleted)t.delete(s);return this.scanRealFs("/",t),Array.from(t)}scanRealFs(t,s){if(this.deleted.has(t))return;let r=this.resolveRealPath_(this.toRealPath(t));if(r)try{let n=Y.readdirSync(r);for(let i of n){let o=t==="/"?`/${i}`:`${t}/${i}`;if(this.deleted.has(o))continue;s.add(o);let a=ge.join(r,i);Y.lstatSync(a).isDirectory()&&this.scanRealFs(o,s)}}catch{}}async chmod(t,s){z(t,"chmod"),this.assertWritable(`chmod '${t}'`);let r=W(t);if(!await this.existsInOverlay(r))throw new Error(`ENOENT: no such file or directory, chmod '${t}'`);let i=this.memory.get(r);if(i){i.mode=s;return}let o=await this.stat(r);if(o.isFile){let a=await this.readFileBuffer(r);this.memory.set(r,{type:"file",content:a,mode:s,mtime:new Date})}else o.isDirectory&&this.memory.set(r,{type:"directory",mode:s,mtime:new Date})}async symlink(t,s){if(!this.allowSymlinks)throw new Error(`EPERM: operation not permitted, symlink '${s}'`);z(s,"symlink"),this.assertWritable(`symlink '${s}'`);let r=W(s);if(await this.existsInOverlay(r))throw new Error(`EEXIST: file already exists, symlink '${s}'`);this.ensureParentDirs(r),this.memory.set(r,{type:"symlink",target:t,mode:511,mtime:new Date}),this.deleted.delete(r)}async link(t,s){z(t,"link"),z(s,"link"),this.assertWritable(`link '${s}'`);let r=W(t),n=W(s);if(!await this.existsInOverlay(r))throw new Error(`ENOENT: no such file or directory, link '${t}'`);let o=await this.stat(r);if(!o.isFile)throw new Error(`EPERM: operation not permitted, link '${t}'`);if(await this.existsInOverlay(n))throw new Error(`EEXIST: file already exists, link '${s}'`);let l=await this.readFileBuffer(r);this.ensureParentDirs(n),this.memory.set(n,{type:"file",content:l,mode:o.mode,mtime:new Date}),this.deleted.delete(n)}async readlink(t){z(t,"readlink");let s=W(t);if(this.deleted.has(s))throw new Error(`ENOENT: no such file or directory, readlink '${t}'`);let r=this.memory.get(s);if(r){if(r.type!=="symlink")throw new Error(`EINVAL: invalid argument, readlink '${t}'`);return r.target}let n=this.resolveRealPathParent_(this.toRealPath(s));if(!n)throw new Error(`ENOENT: no such file or directory, readlink '${t}'`);try{let i=await Y.promises.readlink(n);return this.realTargetToVirtual(s,i)}catch(i){if(i.code==="ENOENT")throw new Error(`ENOENT: no such file or directory, readlink '${t}'`);if(i.code==="EINVAL")throw new Error(`EINVAL: invalid argument, readlink '${t}'`);this.sanitizeError(i,t,"readlink")}}async realpath(t){z(t,"realpath");let s=W(t),r=new Set,n=async a=>{let l=a==="/"?[]:a.slice(1).split("/"),c="";for(let u of l){if(c=`${c}/${u}`,r.has(c))throw new Error(`ELOOP: too many levels of symbolic links, realpath '${t}'`);if(this.deleted.has(c))throw new Error(`ENOENT: no such file or directory, realpath '${t}'`);let f=this.memory.get(c),d=0,h=40;for(;f&&f.type==="symlink"&&d<h;){if(r.add(c),c=this.resolveSymlink(c,f.target),d++,r.has(c))throw new Error(`ELOOP: too many levels of symbolic links, realpath '${t}'`);if(this.deleted.has(c))throw new Error(`ENOENT: no such file or directory, realpath '${t}'`);f=this.memory.get(c)}if(d>=h)throw new Error(`ELOOP: too many levels of symbolic links, realpath '${t}'`);if(!f){let m=this.toRealPath(c),p=this.resolveRealPath_(m);if(p)try{if((await Y.promises.lstat(p)).isSymbolicLink()){if(!this.allowSymlinks)throw new Error(`ENOENT: no such file or directory, realpath '${t}'`);let S=await Y.promises.readlink(p),v=this.realTargetToVirtual(c,S);return r.add(c),c=this.resolveSymlink(c,v),n(c)}}catch(g){if(g.code==="ENOENT")throw new Error(`ENOENT: no such file or directory, realpath '${t}'`);this.sanitizeError(g,t,"realpath")}else if(!this.allowSymlinks){let g=this.resolveRealPathParent_(m);if(g)try{if((await Y.promises.lstat(g)).isSymbolicLink())throw new Error(`ENOENT: no such file or directory, realpath '${t}'`)}catch(S){if(S.message?.includes("ENOENT")||S.message?.includes("ELOOP"))throw new Error(`ENOENT: no such file or directory, realpath '${t}'`);this.sanitizeError(S,t,"realpath")}}}}return c||"/"},i=await n(s);if(!await this.existsInOverlay(i))throw new Error(`ENOENT: no such file or directory, realpath '${t}'`);return i}async utimes(t,s,r){z(t,"utimes"),this.assertWritable(`utimes '${t}'`);let n=W(t);if(!await this.existsInOverlay(n))throw new Error(`ENOENT: no such file or directory, utimes '${t}'`);let o=this.memory.get(n);if(o){o.mtime=r;return}let a=await this.stat(n);if(a.isFile){let l=await this.readFileBuffer(n);this.memory.set(n,{type:"file",content:l,mode:a.mode,mtime:r})}else a.isDirectory&&this.memory.set(n,{type:"directory",mode:a.mode,mtime:r})}};import*as M from"node:fs";import*as U from"node:path";var us=class{root;canonicalRoot;maxFileReadSize;allowSymlinks;constructor(t){this.root=U.resolve(t.root),this.maxFileReadSize=t.maxFileReadSize??10485760,this.allowSymlinks=t.allowSymlinks??!1,cs(this.root,"ReadWriteFs"),this.canonicalRoot=M.realpathSync(this.root)}resolveAndValidate(t,s){let r=this.allowSymlinks?Qe(t,this.canonicalRoot):ls(t,this.root,this.canonicalRoot);if(r===null)throw new Error(`EACCES: permission denied, '${s}' resolves outside sandbox`);return r}validateParent(t,s){let r=U.dirname(t),n=this.resolveAndValidate(r,s);return U.join(n,U.basename(t))}toRealPath(t){let s=W(t),r=U.join(this.root,s);return U.resolve(r)}async readFile(t,s){let r=await this.readFileBuffer(t),n=be(s);return et(r,n)}async readFileBuffer(t){z(t,"open");let s=this.toRealPath(t),r=this.resolveAndValidate(s,t);try{if(this.maxFileReadSize>0){let i=await M.promises.stat(r);if(i.size>this.maxFileReadSize)throw new Error(`EFBIG: file too large, read '${t}' (${i.size} bytes, max ${this.maxFileReadSize})`)}let n=await M.promises.readFile(r);return new Uint8Array(n)}catch(n){let i=n;if(i.code==="ENOENT")throw new Error(`ENOENT: no such file or directory, open '${t}'`);if(i.code==="EISDIR")throw new Error(`EISDIR: illegal operation on a directory, read '${t}'`);this.sanitizeError(n,t,"open")}}async writeFile(t,s,r){z(t,"write");let n=this.toRealPath(t),i=this.resolveAndValidate(n,t),o=be(r),a=Re(s,o),l=U.dirname(i);await M.promises.mkdir(l,{recursive:!0}),await M.promises.writeFile(i,a)}async appendFile(t,s,r){z(t,"append");let n=this.toRealPath(t),i=this.resolveAndValidate(n,t),o=be(r),a=Re(s,o),l=U.dirname(i);await M.promises.mkdir(l,{recursive:!0}),await M.promises.appendFile(i,a)}async exists(t){if(t.includes("\0"))return!1;let s=this.toRealPath(t);try{let r=this.resolveAndValidate(s,t);return await M.promises.access(r),!0}catch{return!1}}async stat(t){z(t,"stat");let s=this.toRealPath(t),r=this.resolveAndValidate(s,t);try{let n=await M.promises.stat(r);return{isFile:n.isFile(),isDirectory:n.isDirectory(),isSymbolicLink:!1,mode:n.mode,size:n.size,mtime:n.mtime}}catch(n){if(n.code==="ENOENT")throw new Error(`ENOENT: no such file or directory, stat '${t}'`);this.sanitizeError(n,t,"stat")}}async lstat(t){z(t,"lstat");let s=this.toRealPath(t),r=this.validateParent(s,t);try{let n=await M.promises.lstat(r);return{isFile:n.isFile(),isDirectory:n.isDirectory(),isSymbolicLink:n.isSymbolicLink(),mode:n.mode,size:n.size,mtime:n.mtime}}catch(n){if(n.code==="ENOENT")throw new Error(`ENOENT: no such file or directory, lstat '${t}'`);this.sanitizeError(n,t,"lstat")}}async mkdir(t,s){z(t,"mkdir");let r=this.toRealPath(t),n=this.resolveAndValidate(r,t);try{await M.promises.mkdir(n,{recursive:s?.recursive})}catch(i){let o=i;if(o.code==="EEXIST")throw new Error(`EEXIST: file already exists, mkdir '${t}'`);if(o.code==="ENOENT")throw new Error(`ENOENT: no such file or directory, mkdir '${t}'`);this.sanitizeError(i,t,"mkdir")}}async readdir(t){return(await this.readdirWithFileTypes(t)).map(r=>r.name)}async readdirWithFileTypes(t){z(t,"scandir");let s=this.toRealPath(t),r=this.resolveAndValidate(s,t);try{return(await M.promises.readdir(r,{withFileTypes:!0})).map(i=>({name:i.name,isFile:i.isFile(),isDirectory:i.isDirectory(),isSymbolicLink:i.isSymbolicLink()})).sort((i,o)=>i.name<o.name?-1:i.name>o.name?1:0)}catch(n){let i=n;if(i.code==="ENOENT")throw new Error(`ENOENT: no such file or directory, scandir '${t}'`);if(i.code==="ENOTDIR")throw new Error(`ENOTDIR: not a directory, scandir '${t}'`);this.sanitizeError(n,t,"scandir")}}async rm(t,s){z(t,"rm");let r=this.toRealPath(t),n=this.resolveAndValidate(r,t);try{await M.promises.rm(n,{recursive:s?.recursive??!1,force:s?.force??!1})}catch(i){let o=i;if(o.code==="ENOENT"&&!s?.force)throw new Error(`ENOENT: no such file or directory, rm '${t}'`);if(o.code==="ENOTEMPTY")throw new Error(`ENOTEMPTY: directory not empty, rm '${t}'`);this.sanitizeError(i,t,"rm")}}async cp(t,s,r){z(t,"cp"),z(s,"cp");let n=this.toRealPath(t),i=this.toRealPath(s),o=this.resolveAndValidate(n,t),a=this.resolveAndValidate(i,s);try{await M.promises.cp(o,a,{recursive:r?.recursive??!1,filter:async l=>{try{if(M.lstatSync(l).isSymbolicLink()){let u=await M.promises.realpath(l).catch(()=>null);return u===null?!1:Te(u,this.canonicalRoot)}return!0}catch{return!0}}})}catch(l){let c=l;if(c.code==="ENOENT")throw new Error(`ENOENT: no such file or directory, cp '${t}'`);if(c.code==="EISDIR")throw new Error(`EISDIR: is a directory, cp '${t}'`);this.sanitizeError(l,t,"cp")}}async mv(t,s){z(t,"mv"),z(s,"mv");let r=this.toRealPath(t),n=this.toRealPath(s),i=this.validateParent(r,t),o=this.validateParent(n,s);try{if((await M.promises.lstat(i)).isSymbolicLink()){let c=await M.promises.readlink(i),u=U.resolve(U.dirname(o),c),f=await M.promises.realpath(u).catch(()=>u);if(!Te(f,this.canonicalRoot))throw new Error(`EACCES: permission denied, mv '${t}' -> '${s}' would create symlink escaping sandbox`)}}catch(l){let c=l;if(c.code==="ENOENT")throw new Error(`ENOENT: no such file or directory, mv '${t}'`);if(c.message?.includes("EACCES")||c.message?.includes("escaping sandbox"))throw l}let a=U.dirname(o);await M.promises.mkdir(a,{recursive:!0});try{await M.promises.rename(i,o)}catch(l){let c=l;if(c.code==="ENOENT")throw new Error(`ENOENT: no such file or directory, mv '${t}'`);if(c.code==="EXDEV"){await this.cp(t,s,{recursive:!0}),await this.rm(t,{recursive:!0});return}this.sanitizeError(l,t,"mv")}try{if(M.lstatSync(o).isDirectory()&&this.findEscapingSymlinks(o).length>0)throw await M.promises.rename(o,i),new Error(`EACCES: permission denied, mv '${t}' -> '${s}' would create symlinks escaping sandbox`)}catch(l){if(l.message?.includes("EACCES")||l.message?.includes("escaping sandbox"))throw l}}resolvePath(t,s){if(s.startsWith("/"))return W(s);let r=t==="/"?`/${s}`:`${t}/${s}`;return W(r)}getAllPaths(){let t=[];return this.scanDir("/",t),t}sanitizeError(t,s,r){let n=t;if(n.message?.includes("EACCES")||n.message?.includes("escaping sandbox")||n.message?.includes("EFBIG"))throw t;let i=n.code||"EIO";throw new Error(`${i}: ${r} '${s}'`)}findEscapingSymlinks(t){let s=[];try{let r=M.readdirSync(t);for(let n of r){let i=U.join(t,n);try{let o=M.lstatSync(i);if(o.isSymbolicLink()){let a=M.readlinkSync(i),l=U.resolve(t,a),c;try{c=M.realpathSync(l)}catch{c=l}Te(c,this.canonicalRoot)||s.push(i)}else o.isDirectory()&&s.push(...this.findEscapingSymlinks(i))}catch{}}}catch{}return s}scanDir(t,s){let r=this.toRealPath(t),n;try{n=this.resolveAndValidate(r,t)}catch{return}try{let i=M.readdirSync(n);for(let o of i){let a=t==="/"?`/${o}`:`${t}/${o}`;s.push(a);let l=U.join(n,o);M.lstatSync(l).isDirectory()&&this.scanDir(a,s)}}catch{}}async chmod(t,s){z(t,"chmod");let r=this.toRealPath(t),n=this.resolveAndValidate(r,t);try{await M.promises.chmod(n,s)}catch(i){if(i.code==="ENOENT")throw new Error(`ENOENT: no such file or directory, chmod '${t}'`);this.sanitizeError(i,t,"chmod")}}async symlink(t,s){if(!this.allowSymlinks)throw new Error(`EPERM: operation not permitted, symlink '${s}'`);z(s,"symlink");let r=this.toRealPath(s),n=this.validateParent(r,s),i=W(s),o=W(U.dirname(i)),a=t.startsWith("/")?W(t):W(o==="/"?`/${t}`:`${o}/${t}`),l=U.join(this.canonicalRoot,a),c=U.dirname(n),u=t.startsWith("/")?l:U.relative(c,l);try{await M.promises.symlink(u,n)}catch(f){if(f.code==="EEXIST")throw new Error(`EEXIST: file already exists, symlink '${s}'`);this.sanitizeError(f,s,"symlink")}}async link(t,s){z(t,"link"),z(s,"link");let r=this.toRealPath(t),n=this.toRealPath(s),i=this.resolveAndValidate(r,t),o=this.resolveAndValidate(n,s);try{await M.promises.link(i,o)}catch(a){let l=a;if(l.code==="ENOENT")throw new Error(`ENOENT: no such file or directory, link '${t}'`);if(l.code==="EEXIST")throw new Error(`EEXIST: file already exists, link '${s}'`);if(l.code==="EPERM")throw new Error(`EPERM: operation not permitted, link '${t}'`);this.sanitizeError(a,t,"link")}}async readlink(t){z(t,"readlink");let s=this.toRealPath(t),r=this.validateParent(s,t);try{let n=await M.promises.readlink(r),i=W(t),o=U.dirname(i),a=U.isAbsolute(n)?n:U.resolve(U.dirname(r),n),l=await M.promises.realpath(a).catch(()=>a);if(Te(l,this.canonicalRoot)){let c=l.slice(this.canonicalRoot.length)||"/";return o==="/"?c.startsWith("/")?c.slice(1)||".":c:U.relative(o,c)}return U.isAbsolute(n)?U.basename(n):n}catch(n){let i=n;if(i.code==="ENOENT")throw new Error(`ENOENT: no such file or directory, readlink '${t}'`);if(i.code==="EINVAL")throw new Error(`EINVAL: invalid argument, readlink '${t}'`);this.sanitizeError(n,t,"readlink")}}async realpath(t){z(t,"realpath");let s=this.toRealPath(t);try{this.resolveAndValidate(s,t)}catch{throw new Error(`ENOENT: no such file or directory, realpath '${t}'`)}let r;try{r=await M.promises.realpath(s)}catch(n){let i=n;if(i.code==="ENOENT")throw new Error(`ENOENT: no such file or directory, realpath '${t}'`);if(i.code==="ELOOP")throw new Error(`ELOOP: too many levels of symbolic links, realpath '${t}'`);this.sanitizeError(n,t,"realpath")}if(Te(r,this.canonicalRoot))return r.slice(this.canonicalRoot.length)||"/";throw new Error(`ENOENT: no such file or directory, realpath '${t}'`)}async utimes(t,s,r){z(t,"utimes");let n=this.toRealPath(t),i=this.resolveAndValidate(n,t);try{await M.promises.utimes(i,s,r)}catch(o){if(o.code==="ENOENT")throw new Error(`ENOENT: no such file or directory, utimes '${t}'`);this.sanitizeError(o,t,"utimes")}}};var ht=class{cmdId;cwd;startedAt;exitCode;bashEnv;cmdLine;env;explicitCwd;resultPromise;constructor(t,s,r,n,i=!1){this.cmdId=crypto.randomUUID(),this.cwd=r,this.startedAt=new Date,this.bashEnv=t,this.cmdLine=s,this.env=n,this.explicitCwd=i,this.resultPromise=this.execute()}async execute(){let t=this.env||this.explicitCwd?{cwd:this.explicitCwd?this.cwd:void 0,env:this.env}:void 0,s=await this.bashEnv.exec(this.cmdLine,t);return this.exitCode=s.exitCode,s}async*logs(){let t=await this.resultPromise;t.stdout&&(yield{type:"stdout",data:t.stdout,timestamp:new Date}),t.stderr&&(yield{type:"stderr",data:t.stderr,timestamp:new Date})}async wait(){return await this.resultPromise,this}async output(){let t=await this.resultPromise;return t.stdout+t.stderr}async stdout(){return(await this.resultPromise).stdout}async stderr(){return(await this.resultPromise).stderr}async kill(){}};var fs=class e{bashEnv;constructor(t){this.bashEnv=t}static async create(t){let s=t?.fs;if(t?.overlayRoot){if(t?.fs)throw new Error("Cannot specify both 'fs' and 'overlayRoot' options");s=new dt({root:t.overlayRoot})}let r=new _t({env:t?.env,cwd:t?.cwd,fs:s,maxCallDepth:t?.maxCallDepth,maxCommandCount:t?.maxCommandCount,maxLoopIterations:t?.maxLoopIterations,network:t?.network});return new e(r)}async runCommand(t,s){let r=s?.cwd??this.bashEnv.getCwd(),n=s?.cwd!==void 0;return new ht(this.bashEnv,t,r,s?.env,n)}async writeFiles(t){for(let[s,r]of Object.entries(t)){let n;typeof r=="string"?n=r:r.encoding==="base64"?n=Buffer.from(r.content,"base64").toString("utf-8"):n=r.content;let i=s.substring(0,s.lastIndexOf("/"))||"/";i!=="/"&&await this.bashEnv.exec(`mkdir -p ${i}`),await this.bashEnv.writeFile(s,n)}}async readFile(t,s){let r=await this.bashEnv.readFile(t);return s==="base64"?Buffer.from(r).toString("base64"):r}async mkDir(t,s){let r=s?.recursive?"-p":"";await this.bashEnv.exec(`mkdir ${r} ${t}`)}async stop(){}async extendTimeout(t){}get domain(){}get bashEnvInstance(){return this.bashEnv}};var ds=class{violations=[];violationsByType=new Map;options;constructor(t={}){this.options={maxViolationsPerType:t.maxViolationsPerType??100,includeStackTraces:t.includeStackTraces??!0,onViolation:t.onViolation??(()=>{}),logToConsole:t.logToConsole??!1}}record(t){let s=this.options.includeStackTraces?t:{...t,stack:void 0};this.violations.unshift(s);let r=this.violationsByType.get(t.type);r||(r=[],this.violationsByType.set(t.type,r)),r.length<this.options.maxViolationsPerType&&r.push(s),this.options.logToConsole&&console.warn(`[SecurityViolation] ${t.type}: ${t.message}`,t.path),this.options.onViolation(s)}getViolations(){return[...this.violations]}getViolationsByType(t){return[...this.violationsByType.get(t)??[]]}getSummary(){let t=[];for(let[s,r]of this.violationsByType){if(r.length===0)continue;let n=new Set,i=Number.POSITIVE_INFINITY,o=0;for(let a of r)n.add(a.path),i=Math.min(i,a.timestamp),o=Math.max(o,a.timestamp);t.push({type:s,count:r.length,firstSeen:i,lastSeen:o,paths:Array.from(n)})}return t.sort((s,r)=>r.count-s.count),t}getTotalCount(){return this.violations.length}hasViolations(){return this.violations.length>0}clear(){this.violations=[],this.violationsByType.clear()}createCallback(){return t=>this.record(t)}};function $i(){return e=>{console.warn("[DefenseInDepth] Security violation detected:",`
706
706
  Type: ${e.type}`,`
707
707
  Path: ${e.path}`,`
708
708
  Message: ${e.message}`,e.executionId?`
@@ -100,6 +100,12 @@ export declare class OverlayFs implements IFileSystem {
100
100
  * follow). Returns the canonical parent + basename for I/O, or null.
101
101
  */
102
102
  private resolveRealPathParent_;
103
+ /**
104
+ * Sanitize an error to prevent leaking real OS paths in error messages.
105
+ * Node.js ErrnoException objects contain a `.path` property with the real
106
+ * filesystem path — this must never reach the sandbox user.
107
+ */
108
+ private sanitizeError;
103
109
  private ensureParentDirs;
104
110
  /**
105
111
  * Check if a path exists in the overlay (memory + real fs - deleted)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "just-bash",
3
- "version": "2.11.3",
3
+ "version": "2.11.4",
4
4
  "description": "A simulated bash environment with virtual filesystem",
5
5
  "repository": {
6
6
  "type": "git",