@zenfs/dom 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,7 +10,7 @@ declare global {
10
10
  values(): IterableIterator<FileSystemHandle>;
11
11
  }
12
12
  }
13
- interface FileSystemAccessOptions {
13
+ export interface FileSystemAccessOptions {
14
14
  handle: FileSystemDirectoryHandle;
15
15
  }
16
16
  export declare class FileSystemAccessFile extends PreloadFile<FileSystemAccessFS> {
@@ -104,11 +104,11 @@ export class FileSystemAccessFS extends Async(FileSystem) {
104
104
  throw ApiError.OnPath(ErrorCode.ENOENT, path);
105
105
  }
106
106
  if (handle instanceof FileSystemDirectoryHandle) {
107
- return new Stats(FileType.DIRECTORY, 4096);
107
+ return new Stats({ mode: 0o777 | FileType.DIRECTORY, size: 4096 });
108
108
  }
109
109
  if (handle instanceof FileSystemFileHandle) {
110
110
  const { lastModified, size } = await handle.getFile();
111
- return new Stats(FileType.FILE, size, null, Date.now(), lastModified);
111
+ return new Stats({ mode: 0o777 | FileType.FILE, size, mtimeMs: lastModified });
112
112
  }
113
113
  }
114
114
  async openFile(path, flag) {
@@ -116,7 +116,7 @@ export class FileSystemAccessFS extends Async(FileSystem) {
116
116
  if (handle instanceof FileSystemFileHandle) {
117
117
  const file = await handle.getFile();
118
118
  const data = new Uint8Array(await file.arrayBuffer());
119
- const stats = new Stats(FileType.FILE, file.size, null, Date.now(), file.lastModified);
119
+ const stats = new Stats({ mode: 0o777 | FileType.FILE, size: file.size, mtimeMs: file.lastModified });
120
120
  return new FileSystemAccessFile(this, path, flag, stats, data);
121
121
  }
122
122
  }
package/dist/IndexedDB.js CHANGED
@@ -191,6 +191,7 @@ export const IndexedDB = {
191
191
  catch (e) {
192
192
  return false;
193
193
  }
194
+ return true;
194
195
  },
195
196
  create({ cacheSize = 100, storeName = 'zenfs', idbFactory = globalThis.indexedDB }) {
196
197
  const store = IndexedDBStore.create(storeName, idbFactory);
@@ -1,2 +1,2 @@
1
- var ZenFS_DOM=(()=>{var Z=Object.defineProperty;var Tt=Object.getOwnPropertyDescriptor;var Dt=Object.getOwnPropertyNames;var _t=Object.prototype.hasOwnProperty;var l=(r,t)=>Z(r,"name",{value:t,configurable:!0});var Rt=(r,t)=>{for(var e in t)Z(r,e,{get:t[e],enumerable:!0})},Ft=(r,t,e,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of Dt(t))!_t.call(r,i)&&i!==e&&Z(r,i,{get:()=>t[i],enumerable:!(s=Tt(t,i))||s.enumerable});return r};var At=r=>Ft(Z({},"__esModule",{value:!0}),r);var Lt={};Rt(Lt,{FileSystemAccess:()=>Ct,FileSystemAccessFS:()=>X,FileSystemAccessFile:()=>$,IndexedDB:()=>It,IndexedDBROTransaction:()=>Y,IndexedDBRWTransaction:()=>V,IndexedDBStore:()=>M,Storage:()=>xt,StorageStore:()=>Q});var vt="/",ft="/";function U(r,t){if(typeof r!="string")throw new TypeError(`"${t}" is not a string`)}l(U,"validateString");function mt(r,t){let e="",s=0,i=-1,n=0,o="\0";for(let c=0;c<=r.length;++c){if(c<r.length)o=r[c];else{if(o=="/")break;o="/"}if(o=="/"){if(!(i===c-1||n===1))if(n===2){if(e.length<2||s!==2||e.at(-1)!=="."||e.at(-2)!=="."){if(e.length>2){let d=e.lastIndexOf("/");d===-1?(e="",s=0):(e=e.slice(0,d),s=e.length-1-e.lastIndexOf("/")),i=c,n=0;continue}else if(e.length!==0){e="",s=0,i=c,n=0;continue}}t&&(e+=e.length>0?"/..":"..",s=2)}else e.length>0?e+="/"+r.slice(i+1,c):e=r.slice(i+1,c),s=c-i-1;i=c,n=0}else o==="."&&n!==-1?++n:n=-1}return e}l(mt,"normalizeString");function z(...r){let t="",e=!1;for(let s=r.length-1;s>=-1&&!e;s--){let i=s>=0?r[s]:vt;U(i,`paths[${s}]`),i.length!==0&&(t=`${i}/${t}`,e=i[0]==="/")}return t=mt(t,!e),e?`/${t}`:t.length>0?t:"."}l(z,"resolve");function Pt(r){if(U(r,"path"),r.length===0)return".";let t=r[0]==="/",e=r.at(-1)==="/";return r=mt(r,!t),r.length===0?t?"/":e?"./":".":(e&&(r+="/"),t?`/${r}`:r)}l(Pt,"normalize");function R(...r){if(r.length===0)return".";let t;for(let e=0;e<r.length;++e){let s=r[e];U(s,"path"),s.length>0&&(t===void 0?t=s:t+=`/${s}`)}return t===void 0?".":Pt(t)}l(R,"join");function w(r){if(U(r,"path"),r.length===0)return".";let t=r[0]==="/",e=-1,s=!0;for(let i=r.length-1;i>=1;--i)if(r[i]==="/"){if(!s){e=i;break}}else s=!1;return e===-1?t?"/":".":t&&e===1?"//":r.slice(0,e)}l(w,"dirname");function y(r,t){t!==void 0&&U(t,"ext"),U(r,"path");let e=0,s=-1,i=!0;if(t!==void 0&&t.length>0&&t.length<=r.length){if(t===r)return"";let n=t.length-1,o=-1;for(let c=r.length-1;c>=0;--c)if(r[c]==="/"){if(!i){e=c+1;break}}else o===-1&&(i=!1,o=c+1),n>=0&&(r[c]===t[n]?--n===-1&&(s=c):(n=-1,s=o));return e===s?s=o:s===-1&&(s=r.length),r.slice(e,s)}for(let n=r.length-1;n>=0;--n)if(r[n]==="/"){if(!i){e=n+1;break}}else s===-1&&(i=!1,s=n+1);return s===-1?"":r.slice(e,s)}l(y,"basename");var h;(function(r){r[r.EPERM=1]="EPERM",r[r.ENOENT=2]="ENOENT",r[r.EIO=5]="EIO",r[r.EBADF=9]="EBADF",r[r.EACCES=13]="EACCES",r[r.EBUSY=16]="EBUSY",r[r.EEXIST=17]="EEXIST",r[r.ENOTDIR=20]="ENOTDIR",r[r.EISDIR=21]="EISDIR",r[r.EINVAL=22]="EINVAL",r[r.EFBIG=27]="EFBIG",r[r.ENOSPC=28]="ENOSPC",r[r.EROFS=30]="EROFS",r[r.ENOTEMPTY=39]="ENOTEMPTY",r[r.ENOTSUP=95]="ENOTSUP"})(h=h||(h={}));var wt={[h.EPERM]:"Operation not permitted.",[h.ENOENT]:"No such file or directory.",[h.EIO]:"Input/output error.",[h.EBADF]:"Bad file descriptor.",[h.EACCES]:"Permission denied.",[h.EBUSY]:"Resource busy or locked.",[h.EEXIST]:"File exists.",[h.ENOTDIR]:"File is not a directory.",[h.EISDIR]:"File is a directory.",[h.EINVAL]:"Invalid argument.",[h.EFBIG]:"File is too big.",[h.ENOSPC]:"No space left on disk.",[h.EROFS]:"Cannot modify a read-only file system.",[h.ENOTEMPTY]:"Directory is not empty.",[h.ENOTSUP]:"Operation is not supported."},a=class extends Error{static fromJSON(t){let e=new a(t.errno,t.message,t.path);return e.code=t.code,e.stack=t.stack,e}static OnPath(t,e){return new a(t,wt[t],e)}static EACCES(t){return this.OnPath(h.EACCES,t)}static ENOENT(t){return this.OnPath(h.ENOENT,t)}static EEXIST(t){return this.OnPath(h.EEXIST,t)}static EISDIR(t){return this.OnPath(h.EISDIR,t)}static ENOTDIR(t){return this.OnPath(h.ENOTDIR,t)}static EPERM(t){return this.OnPath(h.EPERM,t)}static ENOTEMPTY(t){return this.OnPath(h.ENOTEMPTY,t)}constructor(t,e=wt[t],s){super(e),this.errno=t,this.path=s,this.syscall="",this.code=h[t],this.message=`${this.code}: ${e}${this.path?`, '${this.path}'`:""}`}toString(){return this.message}toJSON(){return{errno:this.errno,code:this.code,path:this.path,stack:this.stack,message:this.message}}bufferSize(){return 4+JSON.stringify(this.toJSON()).length}};l(a,"ApiError");var C=class{constructor(t,e,s,i,n,o){this.uid=t,this.gid=e,this.suid=s,this.sgid=i,this.euid=n,this.egid=o}};l(C,"Cred");C.Root=new C(0,0,0,0,0,0);var g;(function(r){r[r.FILE=32768]="FILE",r[r.DIRECTORY=16384]="DIRECTORY",r[r.SYMLINK=40960]="SYMLINK"})(g=g||(g={}));var tt=class{get _typename(){return this._isBigint?"bigint":"number"}get _typename_inverse(){return this._isBigint?"number":"bigint"}_convert(t){return this._isBigint?BigInt(t):Number(t)}get atime(){return new Date(Number(this.atimeMs))}set atime(t){this.atimeMs=this._convert(t.getTime())}get mtime(){return new Date(Number(this.mtimeMs))}set mtime(t){this.mtimeMs=this._convert(t.getTime())}get ctime(){return new Date(Number(this.ctimeMs))}set ctime(t){this.ctimeMs=this._convert(t.getTime())}get birthtime(){return new Date(Number(this.birthtimeMs))}set birthtime(t){this.birthtimeMs=this._convert(t.getTime())}constructor(t=g.FILE,e=-1,s,i,n,o,c,d,m){this.dev=this._convert(0),this.ino=this._convert(0),this.rdev=this._convert(0),this.nlink=this._convert(1),this.blksize=this._convert(4096),this.uid=this._convert(0),this.gid=this._convert(0),this.fileData=null;let f=Date.now(),u=l((E,p)=>typeof E==this._typename?E:this._convert(typeof E==this._typename_inverse?E:p),"resolveT");if(this.atimeMs=u(i,f),this.mtimeMs=u(n,f),this.ctimeMs=u(o,f),this.birthtimeMs=u(m,f),this.uid=u(c,0),this.gid=u(d,0),this.size=this._convert(e),s)this.mode=this._convert(s);else switch(t){case g.FILE:this.mode=this._convert(420);break;case g.DIRECTORY:default:this.mode=this._convert(511)}this.blocks=this._convert(Math.ceil(Number(e)/512)),this.mode&61440||(this.mode=this.mode|this._convert(t))}isFile(){return(this.mode&61440)===32768}isDirectory(){return(this.mode&61440)===16384}isSymbolicLink(){return(this.mode&61440)===40960}isSocket(){return!1}isBlockDevice(){return!1}isCharacterDevice(){return!1}isFIFO(){return!1}hasAccess(t,e){if(e.euid===0||e.egid===0)return!0;let s=this.mode&-61441,i=15,n=15,o=15;if(e.euid==this.uid){let m=(3840&s)>>8;i=(t^m)&t}if(e.egid==this.gid){let m=(240&s)>>4;n=(t^m)&t}let c=15&s;return o=(t^c)&t,!(i&n&o)}getCred(t=Number(this.uid),e=Number(this.gid)){return new C(t,e,Number(this.uid),Number(this.gid),t,e)}chmod(t){this.mode=this._convert(this.mode&61440|t)}chown(t,e){t=Number(t),e=Number(e),!isNaN(t)&&0<=t&&t<2**32&&(this.uid=this._convert(t)),!isNaN(e)&&0<=e&&e<2**32&&(this.gid=this._convert(e))}};l(tt,"StatsCommon");var N=class extends tt{constructor(){super(...arguments),this._isBigint=!1}static clone(t){return new N(t.mode&61440,t.size,t.mode&-61441,t.atimeMs,t.mtimeMs,t.ctimeMs,t.uid,t.gid,t.birthtimeMs)}};l(N,"Stats");var S;(function(r){r[r.ino=0]="ino",r[r.size=8]="size",r[r.mode=12]="mode",r[r.nlink=14]="nlink",r[r.uid=18]="uid",r[r.gid=22]="gid",r[r.atime=26]="atime",r[r.mtime=34]="mtime",r[r.ctime=42]="ctime"})(S||(S={}));var ut=2**32-1,D=0n;function Et(){return Math.round(Math.random()*2**32).toString(16)}l(Et,"_random");function W(){return BigInt("0x"+Et()+Et())}l(W,"randomIno");var T=class{get data(){return new Uint8Array(this.buffer)}constructor(t){let e=!t;if(t??(t=new ArrayBuffer(50)),this.view=new DataView(t),this.buffer=t,!e)return;this.ino=W(),this.nlink=1,this.size=4096;let s=Date.now();this.atime=s,this.mtime=s,this.ctime=s}get ino(){return this.view.getBigUint64(S.ino,!0)}set ino(t){this.view.setBigUint64(S.ino,t,!0)}get size(){return this.view.getUint32(S.size,!0)}set size(t){this.view.setUint32(S.size,t,!0)}get mode(){return this.view.getUint16(S.mode,!0)}set mode(t){this.view.setUint16(S.mode,t,!0)}get nlink(){return this.view.getUint32(S.nlink,!0)}set nlink(t){this.view.setUint32(S.nlink,t,!0)}get uid(){return this.view.getUint32(S.uid,!0)}set uid(t){this.view.setUint32(S.uid,t,!0)}get gid(){return this.view.getUint32(S.gid,!0)}set gid(t){this.view.setUint32(S.gid,t,!0)}get atime(){return this.view.getFloat64(S.atime,!0)}set atime(t){this.view.setFloat64(S.atime,t,!0)}get mtime(){return this.view.getFloat64(S.mtime,!0)}set mtime(t){this.view.setFloat64(S.mtime,t,!0)}get ctime(){return this.view.getFloat64(S.ctime,!0)}set ctime(t){this.view.setFloat64(S.ctime,t,!0)}toStats(){return new N((this.mode&61440)===g.DIRECTORY?g.DIRECTORY:g.FILE,this.size,this.mode,this.atime,this.mtime,this.ctime,this.uid,this.gid)}sizeof(){return this.buffer.byteLength}update(t){let e=!1;return this.size!==t.size&&(this.size=t.size,e=!0),this.mode!==t.mode&&(this.mode=t.mode,e=!0),this.nlink!==t.nlink&&(this.nlink=t.nlink,e=!0),this.uid!==t.uid&&(this.uid=t.uid,e=!0),this.uid!==t.uid&&(this.uid=t.uid,e=!0),this.atime!==t.atimeMs&&(this.atime=t.atimeMs,e=!0),this.mtime!==t.mtimeMs&&(this.mtime=t.mtimeMs,e=!0),this.ctime!==t.ctimeMs&&(this.ctime=t.ctimeMs,e=!0),e}};l(T,"Inode");var k;(function(r){r[r.NOP=0]="NOP",r[r.THROW=1]="THROW",r[r.TRUNCATE=2]="TRUNCATE",r[r.CREATE=3]="CREATE"})(k=k||(k={}));var x=class{static FromString(t){return x.flagCache.has(t)||x.flagCache.set(t,new x(t)),x.flagCache.get(t)}constructor(t){if(typeof t=="number"&&(t=x.NumberToString(t)),x.validFlagStrs.indexOf(t)<0)throw new a(h.EINVAL,"Invalid flag string: "+t);this.flagStr=t}static NumberToString(t){switch(t){case 0:return"r";case 1052672:return"rs";case 2:return"r+";case 1052674:return"rs+";case 577:return"w";case 705:return"wx";case 578:return"w+";case 706:return"wx+";case 1089:return"a";case 1217:return"ax";case 1090:return"a+";case 1218:return"ax+";default:throw new a(h.EINVAL,"Invalid flag number: "+t)}}toString(){return this.flagStr}get mode(){let t=0;return t<<=1,t+=+this.isReadable(),t<<=1,t+=+this.isWriteable(),t<<=1,t}isReadable(){return this.flagStr.indexOf("r")!==-1||this.flagStr.indexOf("+")!==-1}isWriteable(){return this.flagStr.indexOf("w")!==-1||this.flagStr.indexOf("a")!==-1||this.flagStr.indexOf("+")!==-1}isTruncating(){return this.flagStr.indexOf("w")!==-1}isAppendable(){return this.flagStr.indexOf("a")!==-1}isSynchronous(){return this.flagStr.indexOf("s")!==-1}isExclusive(){return this.flagStr.indexOf("x")!==-1}pathExistsAction(){return this.isExclusive()?k.THROW:this.isTruncating()?k.TRUNCATE:k.NOP}pathNotExistsAction(){return(this.isWriteable()||this.isAppendable())&&this.flagStr!=="r+"?k.CREATE:k.THROW}};l(x,"FileFlag");x.flagCache=new Map;x.validFlagStrs=["r","r+","rs","rs+","w","wx","w+","wx+","a","ax","a+","ax+"];var nt=class{datasync(){return this.sync()}datasyncSync(){return this.syncSync()}};l(nt,"File");var F=class extends nt{constructor(t,e,s,i,n=new Uint8Array(new ArrayBuffer(0,{maxByteLength:ut}))){if(super(),this.fs=t,this.path=e,this.flag=s,this.stats=i,this._buffer=n,this._position=0,this._dirty=!1,this.stats.size!=n.byteLength){if(this.flag.isReadable())throw new Error(`Size mismatch: buffer length ${n.byteLength}, stats size ${this.stats.size}`);this._dirty=!0}}get buffer(){return this._buffer}get position(){return this.flag.isAppendable()?this.stats.size:this._position}set position(t){this._position=t}async stat(){return N.clone(this.stats)}statSync(){return N.clone(this.stats)}truncate(t){if(this.truncateSync(t),this.flag.isSynchronous()&&!this.fs.metadata().synchronous)return this.sync()}truncateSync(t){if(this._dirty=!0,!this.flag.isWriteable())throw new a(h.EPERM,"File not opened with a writeable mode.");if(this.stats.mtimeMs=Date.now(),t>this._buffer.length){let e=new Uint8Array(t-this._buffer.length);this.writeSync(e,0,e.length,this._buffer.length),this.flag.isSynchronous()&&this.fs.metadata().synchronous&&this.syncSync();return}this.stats.size=t,this._buffer=this._buffer.subarray(0,t),this.flag.isSynchronous()&&this.fs.metadata().synchronous&&this.syncSync()}async write(t,e=0,s=this.stats.size,i=0){return this.writeSync(t,e,s,i)}writeSync(t,e=0,s=this.stats.size,i=0){if(this._dirty=!0,i??(i=this.position),!this.flag.isWriteable())throw new a(h.EPERM,"File not opened with a writeable mode.");let n=i+s;if(n>this.stats.size&&(this.stats.size=n,n>this._buffer.byteLength))if(this._buffer.buffer.resizable&&this._buffer.buffer.maxByteLength<=n)this._buffer.buffer.resize(n);else{let c=new Uint8Array(new ArrayBuffer(n,{maxByteLength:ut}));c.set(this._buffer),this._buffer=c}this._buffer.set(t.slice(e,e+s),i);let o=this._buffer.byteOffset;return this.stats.mtimeMs=Date.now(),this.flag.isSynchronous()?(this.syncSync(),o):(this.position=i+o,o)}async read(t,e=0,s=this.stats.size,i=0){return{bytesRead:this.readSync(t,e,s,i),buffer:t}}readSync(t,e=0,s=this.stats.size,i=0){if(!this.flag.isReadable())throw new a(h.EPERM,"File not opened with a readable mode.");i??(i=this.position);let n=i+s;n>this.stats.size&&(n=i+Math.max(this.stats.size-i,0)),this.stats.atimeMs=Date.now(),this._position=n;let o=n-i;return o==0||t.set(this._buffer.slice(i,n),e),o}async chmod(t){this.chmodSync(t)}chmodSync(t){if(!this.fs.metadata().supportsProperties)throw new a(h.ENOTSUP);this._dirty=!0,this.stats.chmod(t),this.syncSync()}async chown(t,e){this.chownSync(t,e)}chownSync(t,e){if(!this.fs.metadata().supportsProperties)throw new a(h.ENOTSUP);this._dirty=!0,this.stats.chown(t,e),this.syncSync()}async utimes(t,e){this.utimesSync(t,e)}utimesSync(t,e){if(!this.fs.metadata().supportsProperties)throw new a(h.ENOTSUP);this._dirty=!0,this.stats.atime=t,this.stats.mtime=e,this.syncSync()}isDirty(){return this._dirty}resetDirty(){this._dirty=!1}_setType(t){return this._dirty=!0,this.stats.mode=this.stats.mode&-61441|t,this.sync()}_setTypeSync(t){this._dirty=!0,this.stats.mode=this.stats.mode&-61441|t,this.syncSync()}};l(F,"PreloadFile");var A=class{metadata(){return{name:this.constructor.name,readonly:!1,synchronous:!1,supportsProperties:!1,totalSpace:0,freeSpace:0}}constructor(t){}async exists(t,e){try{return await this.stat(t,e),!0}catch{return!1}}existsSync(t,e){try{return this.statSync(t,e),!0}catch{return!1}}};l(A,"FileSystem");function bt(r){class t extends r{metadata(){return{...super.metadata(),synchronous:!0}}async ready(){return this}async exists(s,i){return this.existsSync(s,i)}async rename(s,i,n){return this.renameSync(s,i,n)}async stat(s,i){return this.statSync(s,i)}async createFile(s,i,n,o){return this.createFileSync(s,i,n,o)}async openFile(s,i,n){return this.openFileSync(s,i,n)}async unlink(s,i){return this.unlinkSync(s,i)}async rmdir(s,i){return this.rmdirSync(s,i)}async mkdir(s,i,n){return this.mkdirSync(s,i,n)}async readdir(s,i){return this.readdirSync(s,i)}async link(s,i,n){return this.linkSync(s,i,n)}async sync(s,i,n){return this.syncSync(s,i,n)}}return l(t,"_SyncFileSystem"),t}l(bt,"Sync");function ot(r){class t extends r{metadata(){return{...super.metadata(),synchronous:!1}}renameSync(s,i,n){throw new a(h.ENOTSUP)}statSync(s,i){throw new a(h.ENOTSUP)}createFileSync(s,i,n,o){throw new a(h.ENOTSUP)}openFileSync(s,i,n){throw new a(h.ENOTSUP)}unlinkSync(s,i){throw new a(h.ENOTSUP)}rmdirSync(s,i){throw new a(h.ENOTSUP)}mkdirSync(s,i,n){throw new a(h.ENOTSUP)}readdirSync(s,i){throw new a(h.ENOTSUP)}linkSync(s,i,n){throw new a(h.ENOTSUP)}syncSync(s,i,n){throw new a(h.ENOTSUP)}}return l(t,"_AsyncFileSystem"),t}l(ot,"Async");var at=l((r="",t)=>{throw t.name==="NotFoundError"?a.ENOENT(r):t},"handleError"),$=class extends F{constructor(t,e,s,i,n){super(t,e,s,i,n)}syncSync(){throw new a(h.ENOTSUP)}async sync(){this.isDirty()&&(await this.fs.sync(this.path,this.buffer,this.stats),this.resetDirty())}async close(){await this.sync()}closeSync(){throw new a(h.ENOTSUP)}};l($,"FileSystemAccessFile");var X=class extends ot(A){constructor({handle:e}){super();this._handles=new Map;this._handles.set("/",e)}async ready(){return this}metadata(){return{...super.metadata(),name:"FileSystemAccess"}}async sync(e,s,i){let n=await this.stat(e);i.mtime!==n.mtime&&await this.writeFile(e,s)}async rename(e,s){try{let i=await this.getHandle(e);if(i instanceof FileSystemDirectoryHandle){let f=await this.readdir(e);if(await this.mkdir(s),f.length==0)await this.unlink(e);else for(let u of f)await this.rename(R(e,u),R(s,u)),await this.unlink(e)}if(!(i instanceof FileSystemFileHandle))return;let n=await i.getFile(),o=await this.getHandle(w(s));if(!(o instanceof FileSystemDirectoryHandle))return;let d=await(await o.getFileHandle(y(s),{create:!0})).createWritable(),m=await n.arrayBuffer();await d.write(m),d.close(),await this.unlink(e)}catch(i){at(e,i)}}async writeFile(e,s){let i=await this.getHandle(w(e));if(!(i instanceof FileSystemDirectoryHandle))return;let o=await(await i.getFileHandle(y(e),{create:!0})).createWritable();await o.write(s),await o.close()}async createFile(e,s){return await this.writeFile(e,new Uint8Array),this.openFile(e,s)}async stat(e){let s=await this.getHandle(e);if(!s)throw a.OnPath(h.ENOENT,e);if(s instanceof FileSystemDirectoryHandle)return new N(g.DIRECTORY,4096);if(s instanceof FileSystemFileHandle){let{lastModified:i,size:n}=await s.getFile();return new N(g.FILE,n,null,Date.now(),i)}}async openFile(e,s){let i=await this.getHandle(e);if(i instanceof FileSystemFileHandle){let n=await i.getFile(),o=new Uint8Array(await n.arrayBuffer()),c=new N(g.FILE,n.size,null,Date.now(),n.lastModified);return new $(this,e,s,c,o)}}async unlink(e){let s=await this.getHandle(w(e));if(s instanceof FileSystemDirectoryHandle)try{await s.removeEntry(y(e),{recursive:!0})}catch(i){at(e,i)}}async link(){throw new a(h.ENOTSUP)}async rmdir(e){return this.unlink(e)}async mkdir(e){if(await this.getHandle(e))throw a.EEXIST(e);let i=await this.getHandle(w(e));i instanceof FileSystemDirectoryHandle&&await i.getDirectoryHandle(y(e),{create:!0})}async readdir(e){let s=await this.getHandle(e);if(!(s instanceof FileSystemDirectoryHandle))throw a.ENOTDIR(e);let i=[];for await(let n of s.keys())i.push(R(e,n));return i}async getHandle(e){if(this._handles.has(e))return this._handles.get(e);let s="/",[,...i]=e.split("/"),n=l(async([o,...c])=>{let d=R(s,o),m=l(u=>{if(s=d,this._handles.set(s,u),c.length===0)return this._handles.get(e);n(c)},"continueWalk"),f=this._handles.get(s);try{return m(await f.getDirectoryHandle(o))}catch(u){if(u.name==="TypeMismatchError")try{return m(await f.getFileHandle(o))}catch(E){at(d,E)}else{if(u.message==="Name is not allowed.")throw new a(h.ENOENT,u.message,d);at(d,u)}}},"getHandleParts");return await n(i)}};l(X,"FileSystemAccessFS");var Ct={name:"FileSystemAccess",options:{handle:{type:"object",required:!0,description:"The directory handle to use for the root"}},isAvailable(){return typeof FileSystemHandle=="function"},create(r){return new X(r)}};var ye=typeof globalThis.setImmediate=="function"?globalThis.setImmediate:r=>setTimeout(r,0);function v(r,t="utf8"){switch(t){case"ascii":return new globalThis.TextEncoder().encode(r).map(e=>e&127);case"latin1":case"binary":case"utf8":case"utf-8":case"base64":case"base64url":case"hex":return new globalThis.TextEncoder().encode(r);case"utf16le":case"ucs2":case"ucs-2":return new globalThis.TextEncoder().encode(r).slice(0,-1);default:throw new a(h.EINVAL,"Invalid encoding: "+t)}}l(v,"encode");function ct(r,t="utf8"){switch(t){case"ascii":case"utf8":case"utf-8":return new globalThis.TextDecoder().decode(r);case"latin1":case"binary":return new globalThis.TextDecoder("latin1").decode(r);case"utf16le":case"ucs2":case"ucs-2":let e="";for(let s=0;s<r.length;s+=2){let i=r[s]|r[s+1]<<8;e+=String.fromCharCode(i)}return e;case"base64":return btoa(Array.from(r).map(s=>String.fromCharCode(s)).join(""));case"base64url":return ct(r,"base64").replace("/","_").replace("+","-");case"hex":return Array.from(r).map(s=>s.toString(16)).join("");default:throw new a(h.EINVAL,"Invalid encoding: "+t)}}l(ct,"decode");function ht(r){return JSON.parse(ct(r),(t,e)=>t==""?e:BigInt(e))}l(ht,"decodeDirListing");function O(r){return v(JSON.stringify(r,(t,e)=>t==""?e:e.toString()))}l(O,"encodeDirListing");var lt=class{constructor(t){this.limit=t,this.cache=[]}set(t,e){let s=this.cache.findIndex(i=>i.key===t);s!=-1?this.cache.splice(s,1):this.cache.length>=this.limit&&this.cache.shift(),this.cache.push({key:t,value:e})}get(t){let e=this.cache.find(s=>s.key===t);if(e)return this.set(t,e.value),e.value}remove(t){let e=this.cache.findIndex(s=>s.key===t);e!==-1&&this.cache.splice(e,1)}reset(){this.cache=[]}};l(lt,"LRUCache");var K=class extends F{constructor(t,e,s,i,n){super(t,e,s,i,n)}async sync(){this.isDirty()&&(await this.fs.sync(this.path,this._buffer,this.stats),this.resetDirty())}syncSync(){throw new a(h.ENOTSUP)}async close(){this.sync()}closeSync(){throw new a(h.ENOTSUP)}};l(K,"AsyncFile");var q=class extends ot(A){ready(){return this._ready}metadata(){return{...super.metadata(),name:this.store.name}}constructor({store:t,cacheSize:e}){super(),e>0&&(this._cache=new lt(e)),this._ready=this._initialize(t)}async _initialize(t){return this.store=await t,await this.makeRootDirectory(),this}async empty(){this._cache&&this._cache.reset(),await this.store.clear(),await this.makeRootDirectory()}async rename(t,e,s){let i=this._cache;this._cache&&(this._cache=null,i.reset());try{let n=this.store.beginTransaction("readwrite"),o=w(t),c=y(t),d=w(e),m=y(e),f=await this.findINode(n,o),u=await this.getDirListing(n,f,o);if(!f.toStats().hasAccess(2,s))throw a.EACCES(t);if(!u[c])throw a.ENOENT(t);let E=u[c];if(delete u[c],(d+"/").indexOf(t+"/")===0)throw new a(h.EBUSY,o);let p,I;if(d===o?(p=f,I=u):(p=await this.findINode(n,d),I=await this.getDirListing(n,p,d)),I[m]){let B=await this.getINode(n,I[m],e);if(B.toStats().isFile())try{await n.remove(B.ino),await n.remove(I[m])}catch(Ot){throw await n.abort(),Ot}else throw a.EPERM(e)}I[m]=E;try{await n.put(f.ino,O(u),!0),await n.put(p.ino,O(I),!0)}catch(B){throw await n.abort(),B}await n.commit()}finally{i&&(this._cache=i)}}async stat(t,e){let s=this.store.beginTransaction("readonly"),i=await this.findINode(s,t);if(!i)throw a.ENOENT(t);let n=i.toStats();if(!n.hasAccess(4,e))throw a.EACCES(t);return n}async createFile(t,e,s,i){let n=this.store.beginTransaction("readwrite"),o=new Uint8Array(0),c=await this.commitNewFile(n,t,g.FILE,s,i,o);return new K(this,t,e,c.toStats(),o)}async openFile(t,e,s){let i=this.store.beginTransaction("readonly"),n=await this.findINode(i,t),o=await i.get(n.ino);if(!n.toStats().hasAccess(e.mode,s))throw a.EACCES(t);if(!o)throw a.ENOENT(t);return new K(this,t,e,n.toStats(),o)}async unlink(t,e){return this.removeEntry(t,!1,e)}async rmdir(t,e){if((await this.readdir(t,e)).length>0)throw a.ENOTEMPTY(t);await this.removeEntry(t,!0,e)}async mkdir(t,e,s){let i=this.store.beginTransaction("readwrite"),n=v("{}");await this.commitNewFile(i,t,g.DIRECTORY,e,s,n)}async readdir(t,e){let s=this.store.beginTransaction("readonly"),i=await this.findINode(s,t);if(!i.toStats().hasAccess(4,e))throw a.EACCES(t);return Object.keys(await this.getDirListing(s,i,t))}async sync(t,e,s){let i=this.store.beginTransaction("readwrite"),n=await this._findINode(i,w(t),y(t)),o=await this.getINode(i,n,t),c=o.update(s);try{await i.put(o.ino,e,!0),c&&await i.put(n,o.data,!0)}catch(d){throw await i.abort(),d}await i.commit()}async link(t,e,s){let i=this.store.beginTransaction("readwrite"),n=w(t);if(!(await this.findINode(i,n)).toStats().hasAccess(4,s))throw a.EACCES(n);let c=w(e),d=await this.findINode(i,c),m=await this.getDirListing(i,d,c);if(!d.toStats().hasAccess(2,s))throw a.EACCES(c);let f=await this._findINode(i,n,y(t)),u=await this.getINode(i,f,t);if(!u.toStats().hasAccess(2,s))throw a.EACCES(e);u.nlink++,m[y(e)]=f;try{i.put(f,u.data,!0),i.put(d.ino,O(m),!0)}catch(E){throw i.abort(),E}i.commit()}async makeRootDirectory(){let t=this.store.beginTransaction("readwrite");if(await t.get(D)===void 0){let e=new T;e.mode=511|g.DIRECTORY,await t.put(e.ino,v("{}"),!1),await t.put(D,e.data,!1),await t.commit()}}async _findINode(t,e,s,i=new Set){let n=R(e,s);if(i.has(n))throw new a(h.EIO,"Infinite loop detected while finding inode",n);if(i.add(n),this._cache){let o=this._cache.get(n);if(o)return o}if(e==="/"){if(s==="")return this._cache&&this._cache.set(n,D),D;{let o=await this.getINode(t,D,e),c=await this.getDirListing(t,o,e);if(c[s]){let d=c[s];return this._cache&&this._cache.set(n,d),d}else throw a.ENOENT(z(e,s))}}else{let o=await this.findINode(t,e,i),c=await this.getDirListing(t,o,e);if(c[s]){let d=c[s];return this._cache&&this._cache.set(n,d),d}else throw a.ENOENT(z(e,s))}}async findINode(t,e,s=new Set){let i=await this._findINode(t,w(e),y(e),s);return this.getINode(t,i,e)}async getINode(t,e,s){let i=await t.get(e);if(!i)throw a.ENOENT(s);return new T(i.buffer)}async getDirListing(t,e,s){if(!e.toStats().isDirectory())throw a.ENOTDIR(s);let i=await t.get(e.ino);if(!i)throw a.ENOENT(s);return ht(i)}async addNewNode(t,e){let s=0,i=l(async()=>{if(++s===5)throw new a(h.EIO,"Unable to commit data to key-value store.");{let n=W();return await t.put(n,e,!1)?n:i()}},"reroll");return i()}async commitNewFile(t,e,s,i,n,o){let c=w(e),d=y(e),m=await this.findINode(t,c),f=await this.getDirListing(t,m,c);if(!m.toStats().hasAccess(2,n))throw a.EACCES(e);if(e==="/")throw a.EEXIST(e);if(f[d])throw await t.abort(),a.EEXIST(e);try{let u=new T;return u.ino=await this.addNewNode(t,o),u.mode=i|s,u.uid=n.uid,u.gid=n.gid,u.size=o.length,f[d]=await this.addNewNode(t,u.data),await t.put(m.ino,O(f),!0),await t.commit(),u}catch(u){throw t.abort(),u}}async removeEntry(t,e,s){this._cache&&this._cache.remove(t);let i=this.store.beginTransaction("readwrite"),n=w(t),o=await this.findINode(i,n),c=await this.getDirListing(i,o,n),d=y(t);if(!c[d])throw a.ENOENT(t);let m=c[d],f=await this.getINode(i,m,t);if(!f.toStats().hasAccess(2,s))throw a.EACCES(t);if(delete c[d],!e&&f.toStats().isDirectory())throw a.EISDIR(t);if(e&&!f.toStats().isDirectory())throw a.ENOTDIR(t);try{await i.put(o.ino,O(c),!0),--f.nlink<1&&(await i.remove(f.ino),await i.remove(m))}catch(u){throw await i.abort(),u}await i.commit()}};l(q,"AsyncStoreFS");function j(r,t=r.toString()){switch(r.name){case"NotFoundError":return new a(h.ENOENT,t);case"QuotaExceededError":return new a(h.ENOSPC,t);default:return new a(h.EIO,t)}}l(j,"convertError");var Y=class{constructor(t,e){this.tx=t;this.store=e}get(t){return new Promise((e,s)=>{try{let i=this.store.get(t.toString());i.onerror=n=>{n.preventDefault(),s(new a(h.EIO))},i.onsuccess=()=>{let n=i.result;e(n===void 0?n:Uint8Array.from(n))}}catch(i){s(j(i))}})}};l(Y,"IndexedDBROTransaction");var V=class extends Y{constructor(t,e){super(t,e)}put(t,e,s){return new Promise((i,n)=>{try{let o=s?this.store.put(e,t.toString()):this.store.add(e,t.toString());o.onerror=c=>{c.preventDefault(),n(new a(h.EIO))},o.onsuccess=()=>i(!0)}catch(o){n(j(o))}})}remove(t){return new Promise((e,s)=>{try{let i=this.store.delete(t.toString());i.onerror=n=>{n.preventDefault(),s(new a(h.EIO))},i.onsuccess=()=>e}catch(i){s(j(i))}})}commit(){return new Promise(t=>setTimeout(t,0))}async abort(){try{this.tx.abort()}catch(t){throw j(t)}}};l(V,"IndexedDBRWTransaction");var M=class{constructor(t,e){this.db=t;this.storeName=e}static create(t,e){return new Promise((s,i)=>{let n=e.open(t,1);n.onupgradeneeded=()=>{let o=n.result;o.objectStoreNames.contains(t)&&o.deleteObjectStore(t),o.createObjectStore(t)},n.onsuccess=()=>s(new M(n.result,t)),n.onerror=o=>{o.preventDefault(),i(new a(h.EACCES))}})}get name(){return It.name+":"+this.storeName}clear(){return new Promise((t,e)=>{try{let s=this.db.transaction(this.storeName,"readwrite").objectStore(this.storeName).clear();s.onsuccess=()=>setTimeout(t,0),s.onerror=i=>{i.preventDefault(),e(new a(h.EIO))}}catch(s){e(j(s))}})}beginTransaction(t="readonly"){let e=this.db.transaction(this.storeName,t),s=e.objectStore(this.storeName);if(t==="readwrite")return new V(e,s);if(t==="readonly")return new Y(e,s);throw new a(h.EINVAL,"Invalid transaction type.")}};l(M,"IndexedDBStore");var It={name:"IndexedDB",options:{storeName:{type:"string",required:!1,description:"The name of this file system. You can have multiple IndexedDB file systems operating at once, but each must have a different name."},cacheSize:{type:"number",required:!1,description:"The size of the inode cache. Defaults to 100. A size of 0 or below disables caching."},idbFactory:{type:"object",required:!1,description:"The IDBFactory to use. Defaults to globalThis.indexedDB."}},isAvailable(r=globalThis.indexedDB){try{if(!(r instanceof IDBFactory))return!1;let t=r.open("__zenfs_test");if(!t)return!1;t.onsuccess=()=>r.deleteDatabase("__zenfs_test")}catch{return!1}},create({cacheSize:r=100,storeName:t="zenfs",idbFactory:e=globalThis.indexedDB}){let s=M.create(t,e);return new q({cacheSize:r,store:s})}};var G=class{constructor(t){this.store=t,this.originalData=new Map,this.modifiedKeys=new Set}get(t){let e=this.store.get(t);return this.stashOldValue(t,e),e}put(t,e,s){return this.markModified(t),this.store.put(t,e,s)}remove(t){this.markModified(t),this.store.remove(t)}commit(){}abort(){for(let t of this.modifiedKeys){let e=this.originalData.get(t);e?this.store.put(t,e,!0):this.store.remove(t)}}stashOldValue(t,e){this.originalData.has(t)||this.originalData.set(t,e)}markModified(t){this.modifiedKeys.add(t),this.originalData.has(t)||this.originalData.set(t,this.store.get(t))}};l(G,"SimpleSyncRWTransaction");var dt=class extends F{constructor(t,e,s,i,n){super(t,e,s,i,n)}async sync(){this.syncSync()}syncSync(){this.isDirty()&&(this.fs.syncSync(this.path,this._buffer,this.stats),this.resetDirty())}async close(){this.closeSync()}closeSync(){this.syncSync()}};l(dt,"SyncStoreFile");var J=class extends bt(A){constructor(t){super(),this.store=t.store,this.makeRootDirectory()}metadata(){return{...super.metadata(),name:this.store.name}}empty(){this.store.clear(),this.makeRootDirectory()}renameSync(t,e,s){let i=this.store.beginTransaction("readwrite"),n=w(t),o=y(t),c=w(e),d=y(e),m=this.findINode(i,n),f=this.getDirListing(i,m,n);if(!m.toStats().hasAccess(2,s))throw a.EACCES(t);if(!f[o])throw a.ENOENT(t);let u=f[o];if(delete f[o],(c+"/").indexOf(t+"/")==0)throw new a(h.EBUSY,n);let E,p;if(c===n?(E=m,p=f):(E=this.findINode(i,c),p=this.getDirListing(i,E,c)),p[d]){let I=this.getINode(i,p[d],e);if(I.toStats().isFile())try{i.remove(I.ino),i.remove(p[d])}catch(B){throw i.abort(),B}else throw a.EPERM(e)}p[d]=u;try{i.put(m.ino,O(f),!0),i.put(E.ino,O(p),!0)}catch(I){throw i.abort(),I}i.commit()}statSync(t,e){let s=this.findINode(this.store.beginTransaction("readonly"),t).toStats();if(!s.hasAccess(4,e))throw a.EACCES(t);return s}createFileSync(t,e,s,i){return this.commitNewFile(t,g.FILE,s,i),this.openFileSync(t,e,i)}openFileSync(t,e,s){let i=this.store.beginTransaction("readonly"),n=this.findINode(i,t),o=i.get(n.ino);if(!n.toStats().hasAccess(e.mode,s))throw a.EACCES(t);if(o===null)throw a.ENOENT(t);return new dt(this,t,e,n.toStats(),o)}unlinkSync(t,e){this.removeEntry(t,!1,e)}rmdirSync(t,e){if(this.readdirSync(t,e).length>0)throw a.ENOTEMPTY(t);this.removeEntry(t,!0,e)}mkdirSync(t,e,s){this.commitNewFile(t,g.DIRECTORY,e,s,v("{}"))}readdirSync(t,e){let s=this.store.beginTransaction("readonly"),i=this.findINode(s,t);if(!i.toStats().hasAccess(4,e))throw a.EACCES(t);return Object.keys(this.getDirListing(s,i,t))}syncSync(t,e,s){let i=this.store.beginTransaction("readwrite"),n=this._findINode(i,w(t),y(t)),o=this.getINode(i,n,t),c=o.update(s);try{i.put(o.ino,e,!0),c&&i.put(n,o.data,!0)}catch(d){throw i.abort(),d}i.commit()}linkSync(t,e,s){let i=this.store.beginTransaction("readwrite"),n=w(t);if(!this.findINode(i,n).toStats().hasAccess(4,s))throw a.EACCES(n);let c=w(e),d=this.findINode(i,c),m=this.getDirListing(i,d,c);if(!d.toStats().hasAccess(2,s))throw a.EACCES(c);let f=this._findINode(i,n,y(t)),u=this.getINode(i,f,t);if(!u.toStats().hasAccess(2,s))throw a.EACCES(e);u.nlink++,m[y(e)]=f;try{i.put(f,u.data,!0),i.put(d.ino,O(m),!0)}catch(E){throw i.abort(),E}i.commit()}makeRootDirectory(){let t=this.store.beginTransaction("readwrite");if(t.get(D))return;let e=new T;e.mode=511|g.DIRECTORY,t.put(e.ino,v("{}"),!1),t.put(D,e.data,!1),t.commit()}_findINode(t,e,s,i=new Set){let n=R(e,s);if(i.has(n))throw new a(h.EIO,"Infinite loop detected while finding inode",n);if(i.add(n),e!="/"){let o=this._findINode(t,w(e),y(e),i),c=this.getDirListing(t,this.getINode(t,o,e+ft+s),e);if(!(s in c))throw a.ENOENT(z(e,s));return c[s]}if(s!=""){let o=this.getDirListing(t,this.getINode(t,D,e),e);if(!(s in o))throw a.ENOENT(z(e,s));return o[s]}return D}findINode(t,e){let s=this._findINode(t,w(e),y(e));return this.getINode(t,s,e)}getINode(t,e,s){let i=t.get(e);if(!i)throw a.ENOENT(s);return new T(i.buffer)}getDirListing(t,e,s){if(!e.toStats().isDirectory())throw a.ENOTDIR(s);let i=t.get(e.ino);if(!i)throw a.ENOENT(s);return ht(i)}addNewNode(t,e){let i;for(;0<5;)try{return i=W(),t.put(i,e,!1),i}catch{}throw new a(h.EIO,"Unable to commit data to key-value store.")}commitNewFile(t,e,s,i,n=new Uint8Array){let o=this.store.beginTransaction("readwrite"),c=w(t),d=y(t),m=this.findINode(o,c),f=this.getDirListing(o,m,c);if(!m.toStats().hasAccess(2,i))throw a.EACCES(t);if(t==="/")throw a.EEXIST(t);if(f[d])throw a.EEXIST(t);let u=new T;try{u.ino=this.addNewNode(o,n),u.size=n.length,u.mode=s|e,u.uid=i.uid,u.gid=i.gid,f[d]=this.addNewNode(o,u.data),o.put(m.ino,O(f),!0)}catch(E){throw o.abort(),E}return o.commit(),u}removeEntry(t,e,s){let i=this.store.beginTransaction("readwrite"),n=w(t),o=this.findINode(i,n),c=this.getDirListing(i,o,n),d=y(t),m=c[d];if(!m)throw a.ENOENT(t);let f=this.getINode(i,m,t);if(!f.toStats().hasAccess(2,s))throw a.EACCES(t);if(delete c[d],!e&&f.toStats().isDirectory())throw a.EISDIR(t);if(e&&!f.toStats().isDirectory())throw a.ENOTDIR(t);try{i.put(o.ino,O(c),!0),--f.nlink<1&&(i.remove(f.ino),i.remove(m))}catch(u){throw i.abort(),u}i.commit()}};l(J,"SyncStoreFS");var Q=class{constructor(t){this._storage=t}get name(){return xt.name}clear(){this._storage.clear()}beginTransaction(){return new G(this)}get(t){let e=this._storage.getItem(t.toString());if(typeof e=="string")return v(e)}put(t,e,s){try{return!s&&this._storage.getItem(t.toString())!==null?!1:(this._storage.setItem(t.toString(),ct(e)),!0)}catch{throw new a(h.ENOSPC,"Storage is full.")}}remove(t){try{this._storage.removeItem(t.toString())}catch(e){throw new a(h.EIO,"Unable to delete key "+t+": "+e)}}};l(Q,"StorageStore");var xt={name:"Storage",options:{storage:{type:"object",required:!1,description:"The Storage to use. Defaults to globalThis.localStorage."}},isAvailable(r=globalThis.localStorage){return r instanceof globalThis.Storage},create({storage:r=globalThis.localStorage}){return new J({store:new Q(r)})}};return At(Lt);})();
1
+ var ZenFS_DOM=(()=>{var st=Object.defineProperty;var _t=Object.getOwnPropertyDescriptor;var Dt=Object.getOwnPropertyNames;var Tt=Object.prototype.hasOwnProperty;var l=(s,t)=>st(s,"name",{value:t,configurable:!0});var At=(s,t)=>{for(var e in t)st(s,e,{get:t[e],enumerable:!0})},Rt=(s,t,e,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of Dt(t))!Tt.call(s,i)&&i!==e&&st(s,i,{get:()=>t[i],enumerable:!(r=_t(t,i))||r.enumerable});return s};var Ft=s=>Rt(st({},"__esModule",{value:!0}),s);var Ct={};At(Ct,{FileSystemAccess:()=>Pt,FileSystemAccessFS:()=>j,FileSystemAccessFile:()=>V,IndexedDB:()=>xt,IndexedDBROTransaction:()=>K,IndexedDBRWTransaction:()=>tt,IndexedDBStore:()=>Y,Storage:()=>It,StorageStore:()=>rt});var vt="/",wt="/";function $(s,t){if(typeof s!="string")throw new TypeError(`"${t}" is not a string`)}l($,"validateString");function yt(s,t){let e="",r=0,i=-1,n=0,o="\0";for(let c=0;c<=s.length;++c){if(c<s.length)o=s[c];else{if(o=="/")break;o="/"}if(o=="/"){if(!(i===c-1||n===1))if(n===2){if(e.length<2||r!==2||e.at(-1)!=="."||e.at(-2)!=="."){if(e.length>2){let d=e.lastIndexOf("/");d===-1?(e="",r=0):(e=e.slice(0,d),r=e.length-1-e.lastIndexOf("/")),i=c,n=0;continue}else if(e.length!==0){e="",r=0,i=c,n=0;continue}}t&&(e+=e.length>0?"/..":"..",r=2)}else e.length>0?e+="/"+s.slice(i+1,c):e=s.slice(i+1,c),r=c-i-1;i=c,n=0}else o==="."&&n!==-1?++n:n=-1}return e}l(yt,"normalizeString");function X(...s){let t="",e=!1;for(let r=s.length-1;r>=-1&&!e;r--){let i=r>=0?s[r]:vt;$(i,`paths[${r}]`),i.length!==0&&(t=`${i}/${t}`,e=i[0]==="/")}return t=yt(t,!e),e?`/${t}`:t.length>0?t:"."}l(X,"resolve");function kt(s){if($(s,"path"),s.length===0)return".";let t=s[0]==="/",e=s.at(-1)==="/";return s=yt(s,!t),s.length===0?t?"/":e?"./":".":(e&&(s+="/"),t?`/${s}`:s)}l(kt,"normalize");function v(...s){if(s.length===0)return".";let t;for(let e=0;e<s.length;++e){let r=s[e];$(r,"path"),r.length>0&&(t===void 0?t=r:t+=`/${r}`)}return t===void 0?".":kt(t)}l(v,"join");function w(s){if($(s,"path"),s.length===0)return".";let t=s[0]==="/",e=-1,r=!0;for(let i=s.length-1;i>=1;--i)if(s[i]==="/"){if(!r){e=i;break}}else r=!1;return e===-1?t?"/":".":t&&e===1?"//":s.slice(0,e)}l(w,"dirname");function g(s,t){t!==void 0&&$(t,"ext"),$(s,"path");let e=0,r=-1,i=!0;if(t!==void 0&&t.length>0&&t.length<=s.length){if(t===s)return"";let n=t.length-1,o=-1;for(let c=s.length-1;c>=0;--c)if(s[c]==="/"){if(!i){e=c+1;break}}else o===-1&&(i=!1,o=c+1),n>=0&&(s[c]===t[n]?--n===-1&&(r=c):(n=-1,r=o));return e===r?r=o:r===-1&&(r=s.length),s.slice(e,r)}for(let n=s.length-1;n>=0;--n)if(s[n]==="/"){if(!i){e=n+1;break}}else r===-1&&(i=!1,r=n+1);return r===-1?"":s.slice(e,r)}l(g,"basename");var h;(function(s){s[s.EPERM=1]="EPERM",s[s.ENOENT=2]="ENOENT",s[s.EIO=5]="EIO",s[s.EBADF=9]="EBADF",s[s.EACCES=13]="EACCES",s[s.EBUSY=16]="EBUSY",s[s.EEXIST=17]="EEXIST",s[s.ENOTDIR=20]="ENOTDIR",s[s.EISDIR=21]="EISDIR",s[s.EINVAL=22]="EINVAL",s[s.EFBIG=27]="EFBIG",s[s.ENOSPC=28]="ENOSPC",s[s.EROFS=30]="EROFS",s[s.ENOTEMPTY=39]="ENOTEMPTY",s[s.ENOTSUP=95]="ENOTSUP"})(h=h||(h={}));var gt={[h.EPERM]:"Operation not permitted.",[h.ENOENT]:"No such file or directory.",[h.EIO]:"Input/output error.",[h.EBADF]:"Bad file descriptor.",[h.EACCES]:"Permission denied.",[h.EBUSY]:"Resource busy or locked.",[h.EEXIST]:"File exists.",[h.ENOTDIR]:"File is not a directory.",[h.EISDIR]:"File is a directory.",[h.EINVAL]:"Invalid argument.",[h.EFBIG]:"File is too big.",[h.ENOSPC]:"No space left on disk.",[h.EROFS]:"Cannot modify a read-only file system.",[h.ENOTEMPTY]:"Directory is not empty.",[h.ENOTSUP]:"Operation is not supported."},a=class extends Error{static fromJSON(t){let e=new a(t.errno,t.message,t.path);return e.code=t.code,e.stack=t.stack,e}static OnPath(t,e){return new a(t,gt[t],e)}static EACCES(t){return this.OnPath(h.EACCES,t)}static ENOENT(t){return this.OnPath(h.ENOENT,t)}static EEXIST(t){return this.OnPath(h.EEXIST,t)}static EISDIR(t){return this.OnPath(h.EISDIR,t)}static ENOTDIR(t){return this.OnPath(h.ENOTDIR,t)}static EPERM(t){return this.OnPath(h.EPERM,t)}static ENOTEMPTY(t){return this.OnPath(h.ENOTEMPTY,t)}constructor(t,e=gt[t],r){super(e),this.errno=t,this.path=r,this.syscall="",this.code=h[t],this.message=`${this.code}: ${e}${this.path?`, '${this.path}'`:""}`}toString(){return this.message}toJSON(){return{errno:this.errno,code:this.code,path:this.path,stack:this.stack,message:this.message}}bufferSize(){return 4+JSON.stringify(this.toJSON()).length}};l(a,"ApiError");var z=class{constructor(t,e,r,i,n,o){this.uid=t,this.gid=e,this.suid=r,this.sgid=i,this.euid=n,this.egid=o}};l(z,"Cred");z.Root=new z(0,0,0,0,0,0);var E;(function(s){s[s.FILE=32768]="FILE",s[s.DIRECTORY=16384]="DIRECTORY",s[s.SYMLINK=40960]="SYMLINK"})(E=E||(E={}));var nt=class{get _typename(){return this._isBigint?"bigint":"number"}get _typename_inverse(){return this._isBigint?"number":"bigint"}_convert(t){return this._isBigint?BigInt(t):Number(t)}get atime(){return new Date(Number(this.atimeMs))}set atime(t){this.atimeMs=this._convert(t.getTime())}get mtime(){return new Date(Number(this.mtimeMs))}set mtime(t){this.mtimeMs=this._convert(t.getTime())}get ctime(){return new Date(Number(this.ctimeMs))}set ctime(t){this.ctimeMs=this._convert(t.getTime())}get birthtime(){return new Date(Number(this.birthtimeMs))}set birthtime(t){this.birthtimeMs=this._convert(t.getTime())}constructor({atimeMs:t,mtimeMs:e,ctimeMs:r,birthtimeMs:i,uid:n,gid:o,size:c,mode:d}={}){this.dev=this._convert(0),this.ino=this._convert(0),this.rdev=this._convert(0),this.nlink=this._convert(1),this.blksize=this._convert(4096),this.uid=this._convert(0),this.gid=this._convert(0),this.fileData=null;let m=Date.now(),u=l((S,N)=>typeof S==this._typename?S:this._convert(typeof S==this._typename_inverse?S:N),"resolveT");this.atimeMs=u(t,m),this.mtimeMs=u(e,m),this.ctimeMs=u(r,m),this.birthtimeMs=u(i,m),this.uid=u(n,0),this.gid=u(o,0),this.size=this._convert(c);let f=Number(d)&61440||E.FILE;if(d)this.mode=this._convert(d);else switch(f){case E.FILE:this.mode=this._convert(420);break;case E.DIRECTORY:default:this.mode=this._convert(511)}this.blocks=this._convert(Math.ceil(Number(c)/512)),this.mode&61440||(this.mode=this.mode|this._convert(f))}isFile(){return(this.mode&61440)===32768}isDirectory(){return(this.mode&61440)===16384}isSymbolicLink(){return(this.mode&61440)===40960}isSocket(){return!1}isBlockDevice(){return!1}isCharacterDevice(){return!1}isFIFO(){return!1}hasAccess(t,e){if(e.euid===0||e.egid===0)return!0;let r=this.mode&-61441,i=15,n=15,o=15;if(e.euid==this.uid){let m=(3840&r)>>8;i=(t^m)&t}if(e.egid==this.gid){let m=(240&r)>>4;n=(t^m)&t}let c=15&r;return o=(t^c)&t,!(i&n&o)}getCred(t=Number(this.uid),e=Number(this.gid)){return new z(t,e,Number(this.uid),Number(this.gid),t,e)}chmod(t){this.mode=this._convert(this.mode&61440|t)}chown(t,e){t=Number(t),e=Number(e),!isNaN(t)&&0<=t&&t<2**32&&(this.uid=this._convert(t)),!isNaN(e)&&0<=e&&e<2**32&&(this.gid=this._convert(e))}};l(nt,"StatsCommon");var b=class extends nt{constructor(){super(...arguments),this._isBigint=!1}static clone(t){return new b(t)}};l(b,"Stats");var mt=2**32-1,T=0n;function Nt(){return Math.round(Math.random()*2**32).toString(16)}l(Nt,"_random");function q(){return BigInt("0x"+Nt()+Nt())}l(q,"randomIno");var y;(function(s){s[s.ino=0]="ino",s[s.size=8]="size",s[s.mode=12]="mode",s[s.nlink=14]="nlink",s[s.uid=18]="uid",s[s.gid=22]="gid",s[s.atime=26]="atime",s[s.birthtime=34]="birthtime",s[s.mtime=42]="mtime",s[s.ctime=50]="ctime",s[s.end=58]="end"})(y||(y={}));var D=class{get data(){return new Uint8Array(this.buffer)}constructor(t){let e=!t;if(t??(t=new ArrayBuffer(y.end)),t?.byteLength<y.end)throw new RangeError(`Can not create an inode from a buffer less than ${y.end} bytes`);if(this.view=new DataView(t),this.buffer=t,!e)return;this.ino=q(),this.nlink=1,this.size=4096;let r=Date.now();this.atimeMs=r,this.mtimeMs=r,this.ctimeMs=r,this.birthtimeMs=r}get ino(){return this.view.getBigUint64(y.ino,!0)}set ino(t){this.view.setBigUint64(y.ino,t,!0)}get size(){return this.view.getUint32(y.size,!0)}set size(t){this.view.setUint32(y.size,t,!0)}get mode(){return this.view.getUint16(y.mode,!0)}set mode(t){this.view.setUint16(y.mode,t,!0)}get nlink(){return this.view.getUint32(y.nlink,!0)}set nlink(t){this.view.setUint32(y.nlink,t,!0)}get uid(){return this.view.getUint32(y.uid,!0)}set uid(t){this.view.setUint32(y.uid,t,!0)}get gid(){return this.view.getUint32(y.gid,!0)}set gid(t){this.view.setUint32(y.gid,t,!0)}get atimeMs(){return this.view.getFloat64(y.atime,!0)}set atimeMs(t){this.view.setFloat64(y.atime,t,!0)}get birthtimeMs(){return this.view.getFloat64(y.birthtime,!0)}set birthtimeMs(t){this.view.setFloat64(y.birthtime,t,!0)}get mtimeMs(){return this.view.getFloat64(y.mtime,!0)}set mtimeMs(t){this.view.setFloat64(y.mtime,t,!0)}get ctimeMs(){return this.view.getFloat64(y.ctime,!0)}set ctimeMs(t){this.view.setFloat64(y.ctime,t,!0)}toStats(){return new b(this)}update(t){let e=!1;return this.size!==t.size&&(this.size=t.size,e=!0),this.mode!==t.mode&&(this.mode=t.mode,e=!0),this.nlink!==t.nlink&&(this.nlink=t.nlink,e=!0),this.uid!==t.uid&&(this.uid=t.uid,e=!0),this.uid!==t.uid&&(this.uid=t.uid,e=!0),this.atimeMs!==t.atimeMs&&(this.atimeMs=t.atimeMs,e=!0),this.mtimeMs!==t.mtimeMs&&(this.mtimeMs=t.mtimeMs,e=!0),this.ctimeMs!==t.ctimeMs&&(this.ctimeMs=t.ctimeMs,e=!0),e}};l(D,"Inode");var B;(function(s){s[s.NOP=0]="NOP",s[s.THROW=1]="THROW",s[s.TRUNCATE=2]="TRUNCATE",s[s.CREATE=3]="CREATE"})(B=B||(B={}));var O=class{static Get(t){return O.cache.has(t)||O.cache.set(t,new O(t)),O.cache.get(t)}constructor(t){if(typeof t=="number"&&(t=O.StringOf(t)),!O.validStrings.includes(t))throw new a(h.EINVAL,"Invalid flag string: "+t);this._flag=t}static StringOf(t){switch(t){case 0:return"r";case 1052672:return"rs";case 2:return"r+";case 1052674:return"rs+";case 577:return"w";case 705:return"wx";case 578:return"w+";case 706:return"wx+";case 1089:return"a";case 1217:return"ax";case 1090:return"a+";case 1218:return"ax+";default:throw new a(h.EINVAL,"Invalid flag number: "+t)}}static NumberOf(t){switch(t){case"r":return 0;case"rs":return 1052672;case"r+":return 2;case"rs+":return 1052674;case"w":return 577;case"wx":return 705;case"w+":return 578;case"wx+":return 706;case"a":return 1089;case"ax":return 1217;case"a+":return 1090;case"ax+":return 1218;default:throw new a(h.EINVAL,"Invalid flag string: "+t)}}toString(){return this._flag}get mode(){let t=0;return t<<=1,t+=+this.isReadable(),t<<=1,t+=+this.isWriteable(),t<<=1,t}isReadable(){return this._flag.indexOf("r")!=-1||this._flag.indexOf("+")!=-1}isWriteable(){return this._flag.indexOf("w")!=-1||this._flag.indexOf("a")!=-1||this._flag.indexOf("+")!=-1}isTruncating(){return this._flag.indexOf("w")!==-1}isAppendable(){return this._flag.indexOf("a")!==-1}isSynchronous(){return this._flag.indexOf("s")!==-1}isExclusive(){return this._flag.indexOf("x")!==-1}pathExistsAction(){return this.isExclusive()?B.THROW:this.isTruncating()?B.TRUNCATE:B.NOP}pathNotExistsAction(){return(this.isWriteable()||this.isAppendable())&&this._flag!="r+"?B.CREATE:B.THROW}};l(O,"FileFlag");O.cache=new Map;O.validStrings=["r","r+","rs","rs+","w","wx","w+","wx+","a","ax","a+","ax+"];var ct=class{datasync(){return this.sync()}datasyncSync(){return this.syncSync()}};l(ct,"File");var k=class extends ct{constructor(t,e,r,i,n=new Uint8Array(new ArrayBuffer(0,{maxByteLength:mt}))){if(super(),this.fs=t,this.path=e,this.flag=r,this.stats=i,this._buffer=n,this._position=0,this._dirty=!1,this.stats.size!=n.byteLength){if(this.flag.isReadable())throw new Error(`Size mismatch: buffer length ${n.byteLength}, stats size ${this.stats.size}`);this._dirty=!0}}get buffer(){return this._buffer}get position(){return this.flag.isAppendable()?this.stats.size:this._position}set position(t){this._position=t}async stat(){return new b(this.stats)}statSync(){return new b(this.stats)}truncate(t){if(this.truncateSync(t),this.flag.isSynchronous()&&!this.fs.metadata().synchronous)return this.sync()}truncateSync(t){if(this._dirty=!0,!this.flag.isWriteable())throw new a(h.EPERM,"File not opened with a writeable mode.");if(this.stats.mtimeMs=Date.now(),t>this._buffer.length){let e=new Uint8Array(t-this._buffer.length);this.writeSync(e,0,e.length,this._buffer.length),this.flag.isSynchronous()&&this.fs.metadata().synchronous&&this.syncSync();return}this.stats.size=t,this._buffer=this._buffer.subarray(0,t),this.flag.isSynchronous()&&this.fs.metadata().synchronous&&this.syncSync()}async write(t,e=0,r=this.stats.size,i=0){return this.writeSync(t,e,r,i)}writeSync(t,e=0,r=this.stats.size,i=0){if(this._dirty=!0,i??(i=this.position),!this.flag.isWriteable())throw new a(h.EPERM,"File not opened with a writeable mode.");let n=i+r;if(n>this.stats.size&&(this.stats.size=n,n>this._buffer.byteLength))if(this._buffer.buffer.resizable&&this._buffer.buffer.maxByteLength<=n)this._buffer.buffer.resize(n);else{let c=new Uint8Array(new ArrayBuffer(n,{maxByteLength:mt}));c.set(this._buffer),this._buffer=c}this._buffer.set(t.slice(e,e+r),i);let o=this._buffer.byteOffset;return this.stats.mtimeMs=Date.now(),this.flag.isSynchronous()?(this.syncSync(),o):(this.position=i+o,o)}async read(t,e=0,r=this.stats.size,i=0){return{bytesRead:this.readSync(t,e,r,i),buffer:t}}readSync(t,e=0,r=this.stats.size,i=0){if(!this.flag.isReadable())throw new a(h.EPERM,"File not opened with a readable mode.");i??(i=this.position);let n=i+r;n>this.stats.size&&(n=i+Math.max(this.stats.size-i,0)),this.stats.atimeMs=Date.now(),this._position=n;let o=n-i;return o==0||t.set(this._buffer.slice(i,n),e),o}async chmod(t){this.chmodSync(t)}chmodSync(t){if(!this.fs.metadata().supportsProperties)throw new a(h.ENOTSUP);this._dirty=!0,this.stats.chmod(t),this.syncSync()}async chown(t,e){this.chownSync(t,e)}chownSync(t,e){if(!this.fs.metadata().supportsProperties)throw new a(h.ENOTSUP);this._dirty=!0,this.stats.chown(t,e),this.syncSync()}async utimes(t,e){this.utimesSync(t,e)}utimesSync(t,e){if(!this.fs.metadata().supportsProperties)throw new a(h.ENOTSUP);this._dirty=!0,this.stats.atime=t,this.stats.mtime=e,this.syncSync()}isDirty(){return this._dirty}resetDirty(){this._dirty=!1}_setType(t){return this._dirty=!0,this.stats.mode=this.stats.mode&-61441|t,this.sync()}_setTypeSync(t){this._dirty=!0,this.stats.mode=this.stats.mode&-61441|t,this.syncSync()}};l(k,"PreloadFile");var P=class{metadata(){return{name:this.constructor.name,readonly:!1,synchronous:!1,supportsProperties:!1,totalSpace:0,freeSpace:0}}constructor(t){}async exists(t,e){try{return await this.stat(t,e),!0}catch{return!1}}existsSync(t,e){try{return this.statSync(t,e),!0}catch{return!1}}};l(P,"FileSystem");function bt(s){class t extends s{metadata(){return{...super.metadata(),synchronous:!0}}async ready(){return this}async exists(r,i){return this.existsSync(r,i)}async rename(r,i,n){return this.renameSync(r,i,n)}async stat(r,i){return this.statSync(r,i)}async createFile(r,i,n,o){return this.createFileSync(r,i,n,o)}async openFile(r,i,n){return this.openFileSync(r,i,n)}async unlink(r,i){return this.unlinkSync(r,i)}async rmdir(r,i){return this.rmdirSync(r,i)}async mkdir(r,i,n){return this.mkdirSync(r,i,n)}async readdir(r,i){return this.readdirSync(r,i)}async link(r,i,n){return this.linkSync(r,i,n)}async sync(r,i,n){return this.syncSync(r,i,n)}}return l(t,"_SyncFileSystem"),t}l(bt,"Sync");function ht(s){class t extends s{metadata(){return{...super.metadata(),synchronous:!1}}renameSync(r,i,n){throw new a(h.ENOTSUP)}statSync(r,i){throw new a(h.ENOTSUP)}createFileSync(r,i,n,o){throw new a(h.ENOTSUP)}openFileSync(r,i,n){throw new a(h.ENOTSUP)}unlinkSync(r,i){throw new a(h.ENOTSUP)}rmdirSync(r,i){throw new a(h.ENOTSUP)}mkdirSync(r,i,n){throw new a(h.ENOTSUP)}readdirSync(r,i){throw new a(h.ENOTSUP)}linkSync(r,i,n){throw new a(h.ENOTSUP)}syncSync(r,i,n){throw new a(h.ENOTSUP)}}return l(t,"_AsyncFileSystem"),t}l(ht,"Async");var lt=l((s="",t)=>{throw t.name==="NotFoundError"?a.ENOENT(s):t},"handleError"),V=class extends k{constructor(t,e,r,i,n){super(t,e,r,i,n)}syncSync(){throw new a(h.ENOTSUP)}async sync(){this.isDirty()&&(await this.fs.sync(this.path,this.buffer,this.stats),this.resetDirty())}async close(){await this.sync()}closeSync(){throw new a(h.ENOTSUP)}};l(V,"FileSystemAccessFile");var j=class extends ht(P){constructor({handle:e}){super();this._handles=new Map;this._handles.set("/",e)}async ready(){return this}metadata(){return{...super.metadata(),name:"FileSystemAccess"}}async sync(e,r,i){let n=await this.stat(e);i.mtime!==n.mtime&&await this.writeFile(e,r)}async rename(e,r){try{let i=await this.getHandle(e);if(i instanceof FileSystemDirectoryHandle){let u=await this.readdir(e);if(await this.mkdir(r),u.length==0)await this.unlink(e);else for(let f of u)await this.rename(v(e,f),v(r,f)),await this.unlink(e)}if(!(i instanceof FileSystemFileHandle))return;let n=await i.getFile(),o=await this.getHandle(w(r));if(!(o instanceof FileSystemDirectoryHandle))return;let d=await(await o.getFileHandle(g(r),{create:!0})).createWritable(),m=await n.arrayBuffer();await d.write(m),d.close(),await this.unlink(e)}catch(i){lt(e,i)}}async writeFile(e,r){let i=await this.getHandle(w(e));if(!(i instanceof FileSystemDirectoryHandle))return;let o=await(await i.getFileHandle(g(e),{create:!0})).createWritable();await o.write(r),await o.close()}async createFile(e,r){return await this.writeFile(e,new Uint8Array),this.openFile(e,r)}async stat(e){let r=await this.getHandle(e);if(!r)throw a.OnPath(h.ENOENT,e);if(r instanceof FileSystemDirectoryHandle)return new b({mode:511|E.DIRECTORY,size:4096});if(r instanceof FileSystemFileHandle){let{lastModified:i,size:n}=await r.getFile();return new b({mode:511|E.FILE,size:n,mtimeMs:i})}}async openFile(e,r){let i=await this.getHandle(e);if(i instanceof FileSystemFileHandle){let n=await i.getFile(),o=new Uint8Array(await n.arrayBuffer()),c=new b({mode:511|E.FILE,size:n.size,mtimeMs:n.lastModified});return new V(this,e,r,c,o)}}async unlink(e){let r=await this.getHandle(w(e));if(r instanceof FileSystemDirectoryHandle)try{await r.removeEntry(g(e),{recursive:!0})}catch(i){lt(e,i)}}async link(){throw new a(h.ENOTSUP)}async rmdir(e){return this.unlink(e)}async mkdir(e){if(await this.getHandle(e))throw a.EEXIST(e);let i=await this.getHandle(w(e));i instanceof FileSystemDirectoryHandle&&await i.getDirectoryHandle(g(e),{create:!0})}async readdir(e){let r=await this.getHandle(e);if(!(r instanceof FileSystemDirectoryHandle))throw a.ENOTDIR(e);let i=[];for await(let n of r.keys())i.push(v(e,n));return i}async getHandle(e){if(this._handles.has(e))return this._handles.get(e);let r="/",[,...i]=e.split("/"),n=l(async([o,...c])=>{let d=v(r,o),m=l(f=>{if(r=d,this._handles.set(r,f),c.length===0)return this._handles.get(e);n(c)},"continueWalk"),u=this._handles.get(r);try{return m(await u.getDirectoryHandle(o))}catch(f){if(f.name==="TypeMismatchError")try{return m(await u.getFileHandle(o))}catch(S){lt(d,S)}else{if(f.message==="Name is not allowed.")throw new a(h.ENOENT,f.message,d);lt(d,f)}}},"getHandleParts");return await n(i)}};l(j,"FileSystemAccessFS");var Pt={name:"FileSystemAccess",options:{handle:{type:"object",required:!0,description:"The directory handle to use for the root"}},isAvailable(){return typeof FileSystemHandle=="function"},create(s){return new j(s)}};var ye=typeof globalThis.setImmediate=="function"?globalThis.setImmediate:s=>setTimeout(s,0);function A(s,t="utf8"){if(typeof s!="string")throw new a(h.EINVAL,"Can not encode a non-string");switch(t){case"ascii":case"latin1":case"binary":return new Uint8Array(Array.from(s).map(r=>r.charCodeAt(0)));case"utf8":case"utf-8":return new Uint8Array(Array.from(s).flatMap(r=>{let i=r.charCodeAt(0);if(i<128)return i;let n=i&63|128;if(i<2048)return[i>>6|192,n];let o=i>>6&63|128;return i<65536?[i>>12|224,o,n]:[i>>18|240,i>>12&63|128,o,n]}));case"base64":return A(atob(s),"utf-8");case"base64url":return A(s.replace("_","/").replace("-","+"),"base64");case"hex":return new Uint8Array(s.match(/.{1,2}/g).map(r=>parseInt(r,16)));case"utf16le":case"ucs2":case"ucs-2":let e=new Uint16Array(new ArrayBuffer(s.length*2));for(let r=0;r<s.length;r++)e[r]=s.charCodeAt(r);return new Uint8Array(e.buffer);default:throw new a(h.EINVAL,"Invalid encoding: "+t)}}l(A,"encode");function G(s,t="utf8"){if(!(s instanceof Uint8Array))throw new a(h.EINVAL,"Can not decode a non-Uint8Array");switch(t){case"ascii":case"latin1":case"binary":return Array.from(s).map(i=>String.fromCharCode(i)).join("");case"utf8":case"utf-8":let e="";for(let i=0;i<s.length;i++){let n;s[i]<128?n=s[i]:s[i]<224?n=(s[i]&31)<<6|s[++i]&63:s[i]<240?n=(s[i]&15)<<12|(s[++i]&63)<<6|s[++i]&63:n=(s[i]&7)<<18|(s[++i]&63)<<12|(s[++i]&63)<<6|s[++i]&63,e+=String.fromCharCode(n)}return e;case"utf16le":case"ucs2":case"ucs-2":let r="";for(let i=0;i<s.length;i+=2){let n=s[i]|s[i+1]<<8;r+=String.fromCharCode(n)}return r;case"base64":return btoa(G(s,"utf-8"));case"base64url":return G(s,"base64").replace("/","_").replace("+","-");case"hex":return Array.from(s).map(i=>i.toString(16).padStart(2,"0")).join("");default:throw new a(h.EINVAL,"Invalid encoding: "+t)}}l(G,"decode");function dt(s){return JSON.parse(G(s),(t,e)=>t==""?e:BigInt(e))}l(dt,"decodeDirListing");function _(s){return A(JSON.stringify(s,(t,e)=>t==""?e:e.toString()))}l(_,"encodeDirListing");var ut=class{constructor(t){this.limit=t,this.cache=[]}set(t,e){let r=this.cache.findIndex(i=>i.key===t);r!=-1?this.cache.splice(r,1):this.cache.length>=this.limit&&this.cache.shift(),this.cache.push({key:t,value:e})}get(t){let e=this.cache.find(r=>r.key===t);if(e)return this.set(t,e.value),e.value}remove(t){let e=this.cache.findIndex(r=>r.key===t);e!==-1&&this.cache.splice(e,1)}reset(){this.cache=[]}};l(ut,"LRUCache");var J=class extends k{constructor(t,e,r,i,n){super(t,e,r,i,n)}async sync(){this.isDirty()&&(await this.fs.sync(this.path,this._buffer,this.stats),this.resetDirty())}syncSync(){throw new a(h.ENOTSUP)}async close(){this.sync()}closeSync(){throw new a(h.ENOTSUP)}};l(J,"AsyncFile");var Q=class extends ht(P){ready(){return this._ready}metadata(){return{...super.metadata(),name:this.store.name}}constructor({store:t,cacheSize:e}){super(),e>0&&(this._cache=new ut(e)),this._ready=this._initialize(t)}async _initialize(t){return this.store=await t,await this.makeRootDirectory(),this}async empty(){this._cache&&this._cache.reset(),await this.store.clear(),await this.makeRootDirectory()}async rename(t,e,r){let i=this._cache;this._cache&&(this._cache=null,i.reset());try{let n=this.store.beginTransaction("readwrite"),o=w(t),c=g(t),d=w(e),m=g(e),u=await this.findINode(n,o),f=await this.getDirListing(n,u,o);if(!u.toStats().hasAccess(2,r))throw a.EACCES(t);if(!f[c])throw a.ENOENT(t);let S=f[c];if(delete f[c],(d+"/").indexOf(t+"/")===0)throw new a(h.EBUSY,o);let N,x;if(d===o?(N=u,x=f):(N=await this.findINode(n,d),x=await this.getDirListing(n,N,d)),x[m]){let W=await this.getINode(n,x[m],e);if(W.toStats().isFile())try{await n.remove(W.ino),await n.remove(x[m])}catch(Ot){throw await n.abort(),Ot}else throw a.EPERM(e)}x[m]=S;try{await n.put(u.ino,_(f),!0),await n.put(N.ino,_(x),!0)}catch(W){throw await n.abort(),W}await n.commit()}finally{i&&(this._cache=i)}}async stat(t,e){let r=this.store.beginTransaction("readonly"),i=await this.findINode(r,t);if(!i)throw a.ENOENT(t);let n=i.toStats();if(!n.hasAccess(4,e))throw a.EACCES(t);return n}async createFile(t,e,r,i){let n=this.store.beginTransaction("readwrite"),o=new Uint8Array(0),c=await this.commitNewFile(n,t,E.FILE,r,i,o);return new J(this,t,e,c.toStats(),o)}async openFile(t,e,r){let i=this.store.beginTransaction("readonly"),n=await this.findINode(i,t),o=await i.get(n.ino);if(!n.toStats().hasAccess(e.mode,r))throw a.EACCES(t);if(!o)throw a.ENOENT(t);return new J(this,t,e,n.toStats(),o)}async unlink(t,e){return this.removeEntry(t,!1,e)}async rmdir(t,e){if((await this.readdir(t,e)).length>0)throw a.ENOTEMPTY(t);await this.removeEntry(t,!0,e)}async mkdir(t,e,r){let i=this.store.beginTransaction("readwrite"),n=A("{}");await this.commitNewFile(i,t,E.DIRECTORY,e,r,n)}async readdir(t,e){let r=this.store.beginTransaction("readonly"),i=await this.findINode(r,t);if(!i.toStats().hasAccess(4,e))throw a.EACCES(t);return Object.keys(await this.getDirListing(r,i,t))}async sync(t,e,r){let i=this.store.beginTransaction("readwrite"),n=await this._findINode(i,w(t),g(t)),o=await this.getINode(i,n,t),c=o.update(r);try{await i.put(o.ino,e,!0),c&&await i.put(n,o.data,!0)}catch(d){throw await i.abort(),d}await i.commit()}async link(t,e,r){let i=this.store.beginTransaction("readwrite"),n=w(t);if(!(await this.findINode(i,n)).toStats().hasAccess(4,r))throw a.EACCES(n);let c=w(e),d=await this.findINode(i,c),m=await this.getDirListing(i,d,c);if(!d.toStats().hasAccess(2,r))throw a.EACCES(c);let u=await this._findINode(i,n,g(t)),f=await this.getINode(i,u,t);if(!f.toStats().hasAccess(2,r))throw a.EACCES(e);f.nlink++,m[g(e)]=u;try{i.put(u,f.data,!0),i.put(d.ino,_(m),!0)}catch(S){throw i.abort(),S}i.commit()}async makeRootDirectory(){let t=this.store.beginTransaction("readwrite");if(await t.get(T)===void 0){let e=new D;e.mode=511|E.DIRECTORY,await t.put(e.ino,A("{}"),!1),await t.put(T,e.data,!1),await t.commit()}}async _findINode(t,e,r,i=new Set){let n=v(e,r);if(i.has(n))throw new a(h.EIO,"Infinite loop detected while finding inode",n);if(i.add(n),this._cache){let o=this._cache.get(n);if(o)return o}if(e==="/"){if(r==="")return this._cache&&this._cache.set(n,T),T;{let o=await this.getINode(t,T,e),c=await this.getDirListing(t,o,e);if(c[r]){let d=c[r];return this._cache&&this._cache.set(n,d),d}else throw a.ENOENT(X(e,r))}}else{let o=await this.findINode(t,e,i),c=await this.getDirListing(t,o,e);if(c[r]){let d=c[r];return this._cache&&this._cache.set(n,d),d}else throw a.ENOENT(X(e,r))}}async findINode(t,e,r=new Set){let i=await this._findINode(t,w(e),g(e),r);return this.getINode(t,i,e)}async getINode(t,e,r){let i=await t.get(e);if(!i)throw a.ENOENT(r);return new D(i.buffer)}async getDirListing(t,e,r){if(!e.toStats().isDirectory())throw a.ENOTDIR(r);let i=await t.get(e.ino);if(!i)throw a.ENOENT(r);return dt(i)}async addNewNode(t,e){let r=0,i=l(async()=>{if(++r===5)throw new a(h.EIO,"Unable to commit data to key-value store.");{let n=q();return await t.put(n,e,!1)?n:i()}},"reroll");return i()}async commitNewFile(t,e,r,i,n,o){let c=w(e),d=g(e),m=await this.findINode(t,c),u=await this.getDirListing(t,m,c);if(!m.toStats().hasAccess(2,n))throw a.EACCES(e);if(e==="/")throw a.EEXIST(e);if(u[d])throw await t.abort(),a.EEXIST(e);try{let f=new D;return f.ino=await this.addNewNode(t,o),f.mode=i|r,f.uid=n.uid,f.gid=n.gid,f.size=o.length,u[d]=await this.addNewNode(t,f.data),await t.put(m.ino,_(u),!0),await t.commit(),f}catch(f){throw t.abort(),f}}async removeEntry(t,e,r){this._cache&&this._cache.remove(t);let i=this.store.beginTransaction("readwrite"),n=w(t),o=await this.findINode(i,n),c=await this.getDirListing(i,o,n),d=g(t);if(!c[d])throw a.ENOENT(t);let m=c[d],u=await this.getINode(i,m,t);if(!u.toStats().hasAccess(2,r))throw a.EACCES(t);if(delete c[d],!e&&u.toStats().isDirectory())throw a.EISDIR(t);if(e&&!u.toStats().isDirectory())throw a.ENOTDIR(t);try{await i.put(o.ino,_(c),!0),--u.nlink<1&&(await i.remove(u.ino),await i.remove(m))}catch(f){throw await i.abort(),f}await i.commit()}};l(Q,"AsyncStoreFS");function Z(s,t=s.toString()){switch(s.name){case"NotFoundError":return new a(h.ENOENT,t);case"QuotaExceededError":return new a(h.ENOSPC,t);default:return new a(h.EIO,t)}}l(Z,"convertError");var K=class{constructor(t,e){this.tx=t;this.store=e}get(t){return new Promise((e,r)=>{try{let i=this.store.get(t.toString());i.onerror=n=>{n.preventDefault(),r(new a(h.EIO))},i.onsuccess=()=>{let n=i.result;e(n===void 0?n:Uint8Array.from(n))}}catch(i){r(Z(i))}})}};l(K,"IndexedDBROTransaction");var tt=class extends K{constructor(t,e){super(t,e)}put(t,e,r){return new Promise((i,n)=>{try{let o=r?this.store.put(e,t.toString()):this.store.add(e,t.toString());o.onerror=c=>{c.preventDefault(),n(new a(h.EIO))},o.onsuccess=()=>i(!0)}catch(o){n(Z(o))}})}remove(t){return new Promise((e,r)=>{try{let i=this.store.delete(t.toString());i.onerror=n=>{n.preventDefault(),r(new a(h.EIO))},i.onsuccess=()=>e}catch(i){r(Z(i))}})}commit(){return new Promise(t=>setTimeout(t,0))}async abort(){try{this.tx.abort()}catch(t){throw Z(t)}}};l(tt,"IndexedDBRWTransaction");var Y=class{constructor(t,e){this.db=t;this.storeName=e}static create(t,e){return new Promise((r,i)=>{let n=e.open(t,1);n.onupgradeneeded=()=>{let o=n.result;o.objectStoreNames.contains(t)&&o.deleteObjectStore(t),o.createObjectStore(t)},n.onsuccess=()=>r(new Y(n.result,t)),n.onerror=o=>{o.preventDefault(),i(new a(h.EACCES))}})}get name(){return xt.name+":"+this.storeName}clear(){return new Promise((t,e)=>{try{let r=this.db.transaction(this.storeName,"readwrite").objectStore(this.storeName).clear();r.onsuccess=()=>setTimeout(t,0),r.onerror=i=>{i.preventDefault(),e(new a(h.EIO))}}catch(r){e(Z(r))}})}beginTransaction(t="readonly"){let e=this.db.transaction(this.storeName,t),r=e.objectStore(this.storeName);if(t==="readwrite")return new tt(e,r);if(t==="readonly")return new K(e,r);throw new a(h.EINVAL,"Invalid transaction type.")}};l(Y,"IndexedDBStore");var xt={name:"IndexedDB",options:{storeName:{type:"string",required:!1,description:"The name of this file system. You can have multiple IndexedDB file systems operating at once, but each must have a different name."},cacheSize:{type:"number",required:!1,description:"The size of the inode cache. Defaults to 100. A size of 0 or below disables caching."},idbFactory:{type:"object",required:!1,description:"The IDBFactory to use. Defaults to globalThis.indexedDB."}},isAvailable(s=globalThis.indexedDB){try{if(!(s instanceof IDBFactory))return!1;let t=s.open("__zenfs_test");if(!t)return!1;t.onsuccess=()=>s.deleteDatabase("__zenfs_test")}catch{return!1}return!0},create({cacheSize:s=100,storeName:t="zenfs",idbFactory:e=globalThis.indexedDB}){let r=Y.create(t,e);return new Q({cacheSize:s,store:r})}};var et=class{constructor(t){this.store=t,this.originalData=new Map,this.modifiedKeys=new Set}get(t){let e=this.store.get(t);return this.stashOldValue(t,e),e}put(t,e,r){return this.markModified(t),this.store.put(t,e,r)}remove(t){this.markModified(t),this.store.remove(t)}commit(){}abort(){for(let t of this.modifiedKeys){let e=this.originalData.get(t);e?this.store.put(t,e,!0):this.store.remove(t)}}stashOldValue(t,e){this.originalData.has(t)||this.originalData.set(t,e)}markModified(t){this.modifiedKeys.add(t),this.originalData.has(t)||this.originalData.set(t,this.store.get(t))}};l(et,"SimpleSyncRWTransaction");var ft=class extends k{constructor(t,e,r,i,n){super(t,e,r,i,n)}async sync(){this.syncSync()}syncSync(){this.isDirty()&&(this.fs.syncSync(this.path,this._buffer,this.stats),this.resetDirty())}async close(){this.closeSync()}closeSync(){this.syncSync()}};l(ft,"SyncStoreFile");var it=class extends bt(P){constructor(t){super(),this.store=t.store,this.makeRootDirectory()}metadata(){return{...super.metadata(),name:this.store.name}}empty(){this.store.clear(),this.makeRootDirectory()}renameSync(t,e,r){let i=this.store.beginTransaction("readwrite"),n=w(t),o=g(t),c=w(e),d=g(e),m=this.findINode(i,n),u=this.getDirListing(i,m,n);if(!m.toStats().hasAccess(2,r))throw a.EACCES(t);if(!u[o])throw a.ENOENT(t);let f=u[o];if(delete u[o],(c+"/").indexOf(t+"/")==0)throw new a(h.EBUSY,n);let S,N;if(c===n?(S=m,N=u):(S=this.findINode(i,c),N=this.getDirListing(i,S,c)),N[d]){let x=this.getINode(i,N[d],e);if(x.toStats().isFile())try{i.remove(x.ino),i.remove(N[d])}catch(W){throw i.abort(),W}else throw a.EPERM(e)}N[d]=f;try{i.put(m.ino,_(u),!0),i.put(S.ino,_(N),!0)}catch(x){throw i.abort(),x}i.commit()}statSync(t,e){let r=this.findINode(this.store.beginTransaction("readonly"),t).toStats();if(!r.hasAccess(4,e))throw a.EACCES(t);return r}createFileSync(t,e,r,i){return this.commitNewFile(t,E.FILE,r,i),this.openFileSync(t,e,i)}openFileSync(t,e,r){let i=this.store.beginTransaction("readonly"),n=this.findINode(i,t),o=i.get(n.ino);if(!n.toStats().hasAccess(e.mode,r))throw a.EACCES(t);if(o===null)throw a.ENOENT(t);return new ft(this,t,e,n.toStats(),o)}unlinkSync(t,e){this.removeEntry(t,!1,e)}rmdirSync(t,e){if(this.readdirSync(t,e).length>0)throw a.ENOTEMPTY(t);this.removeEntry(t,!0,e)}mkdirSync(t,e,r){this.commitNewFile(t,E.DIRECTORY,e,r,A("{}"))}readdirSync(t,e){let r=this.store.beginTransaction("readonly"),i=this.findINode(r,t);if(!i.toStats().hasAccess(4,e))throw a.EACCES(t);return Object.keys(this.getDirListing(r,i,t))}syncSync(t,e,r){let i=this.store.beginTransaction("readwrite"),n=this._findINode(i,w(t),g(t)),o=this.getINode(i,n,t),c=o.update(r);try{i.put(o.ino,e,!0),c&&i.put(n,o.data,!0)}catch(d){throw i.abort(),d}i.commit()}linkSync(t,e,r){let i=this.store.beginTransaction("readwrite"),n=w(t);if(!this.findINode(i,n).toStats().hasAccess(4,r))throw a.EACCES(n);let c=w(e),d=this.findINode(i,c),m=this.getDirListing(i,d,c);if(!d.toStats().hasAccess(2,r))throw a.EACCES(c);let u=this._findINode(i,n,g(t)),f=this.getINode(i,u,t);if(!f.toStats().hasAccess(2,r))throw a.EACCES(e);f.nlink++,m[g(e)]=u;try{i.put(u,f.data,!0),i.put(d.ino,_(m),!0)}catch(S){throw i.abort(),S}i.commit()}makeRootDirectory(){let t=this.store.beginTransaction("readwrite");if(t.get(T))return;let e=new D;e.mode=511|E.DIRECTORY,t.put(e.ino,A("{}"),!1),t.put(T,e.data,!1),t.commit()}_findINode(t,e,r,i=new Set){let n=v(e,r);if(i.has(n))throw new a(h.EIO,"Infinite loop detected while finding inode",n);if(i.add(n),e!="/"){let o=this._findINode(t,w(e),g(e),i),c=this.getDirListing(t,this.getINode(t,o,e+wt+r),e);if(!(r in c))throw a.ENOENT(X(e,r));return c[r]}if(r!=""){let o=this.getDirListing(t,this.getINode(t,T,e),e);if(!(r in o))throw a.ENOENT(X(e,r));return o[r]}return T}findINode(t,e){let r=this._findINode(t,w(e),g(e));return this.getINode(t,r,e)}getINode(t,e,r){let i=t.get(e);if(!i)throw a.ENOENT(r);return new D(i.buffer)}getDirListing(t,e,r){if(!e.toStats().isDirectory())throw a.ENOTDIR(r);let i=t.get(e.ino);if(!i)throw a.ENOENT(r);return dt(i)}addNewNode(t,e){let i;for(;0<5;)try{return i=q(),t.put(i,e,!1),i}catch{}throw new a(h.EIO,"Unable to commit data to key-value store.")}commitNewFile(t,e,r,i,n=new Uint8Array){let o=this.store.beginTransaction("readwrite"),c=w(t),d=g(t),m=this.findINode(o,c),u=this.getDirListing(o,m,c);if(!m.toStats().hasAccess(2,i))throw a.EACCES(t);if(t==="/")throw a.EEXIST(t);if(u[d])throw a.EEXIST(t);let f=new D;try{f.ino=this.addNewNode(o,n),f.size=n.length,f.mode=r|e,f.uid=i.uid,f.gid=i.gid,u[d]=this.addNewNode(o,f.data),o.put(m.ino,_(u),!0)}catch(S){throw o.abort(),S}return o.commit(),f}removeEntry(t,e,r){let i=this.store.beginTransaction("readwrite"),n=w(t),o=this.findINode(i,n),c=this.getDirListing(i,o,n),d=g(t),m=c[d];if(!m)throw a.ENOENT(t);let u=this.getINode(i,m,t);if(!u.toStats().hasAccess(2,r))throw a.EACCES(t);if(delete c[d],!e&&u.toStats().isDirectory())throw a.EISDIR(t);if(e&&!u.toStats().isDirectory())throw a.ENOTDIR(t);try{i.put(o.ino,_(c),!0),--u.nlink<1&&(i.remove(u.ino),i.remove(m))}catch(f){throw i.abort(),f}i.commit()}};l(it,"SyncStoreFS");var rt=class{constructor(t){this._storage=t}get name(){return It.name}clear(){this._storage.clear()}beginTransaction(){return new et(this)}get(t){let e=this._storage.getItem(t.toString());if(typeof e=="string")return A(e)}put(t,e,r){try{return!r&&this._storage.getItem(t.toString())!==null?!1:(this._storage.setItem(t.toString(),G(e)),!0)}catch{throw new a(h.ENOSPC,"Storage is full.")}}remove(t){try{this._storage.removeItem(t.toString())}catch(e){throw new a(h.EIO,"Unable to delete key "+t+": "+e)}}};l(rt,"StorageStore");var It={name:"Storage",options:{storage:{type:"object",required:!1,description:"The Storage to use. Defaults to globalThis.localStorage."}},isAvailable(s=globalThis.localStorage){return s instanceof globalThis.Storage},create({storage:s=globalThis.localStorage}){return new it({store:new rt(s)})}};return Ft(Ct);})();
2
2
  //# sourceMappingURL=browser.min.js.map