import{x as W,P as K,L as y,h as E,g as l,z as B,D as Y,a as c,R as L}from"./index-C8iGwwE4.js";W();W();W();var $={EBADF:8,EBADFD:127,EEXIST:20,EINVAL:28,EISDIR:31,ENODEV:43,ENOENT:44,ENOTDIR:54,ENOTEMPTY:55},w=class extends Error{constructor(d,t){super(t),typeof d=="number"?this.code=d:typeof d=="string"&&(this.code=$[d])}},q=(d,t)=>{let a=d.FS,i={tryFSOperation(e){try{return e()}catch(r){throw r.code?r.code==="UNKNOWN"?new a.ErrnoError($.EINVAL):new a.ErrnoError(r.code):r}},mount(e){return i.createNode(null,"/",16895,0)},syncfs(e,r,s){},createNode(e,r,s,n){if(!a.isDir(s)&&!a.isFile(s))throw new a.ErrnoError(28);let o=a.createNode(e,r,s);return o.node_ops=i.node_ops,o.stream_ops=i.stream_ops,o},getMode:function(e){return i.tryFSOperation(()=>t.lstat(e).mode)},realPath:function(e){let r=[];for(;e.parent!==e;)r.push(e.name),e=e.parent;return r.push(e.mount.opts.root),r.reverse(),r.join("/")},node_ops:{getattr(e){m("getattr",i.realPath(e));let r=i.realPath(e);return i.tryFSOperation(()=>{let s=t.lstat(r);return{...s,dev:0,ino:e.id,nlink:1,rdev:e.rdev,atime:new Date(s.atime),mtime:new Date(s.mtime),ctime:new Date(s.ctime)}})},setattr(e,r){m("setattr",i.realPath(e),r);let s=i.realPath(e);i.tryFSOperation(()=>{r.mode!==void 0&&t.chmod(s,r.mode),r.size!==void 0&&t.truncate(s,r.size),r.timestamp!==void 0&&t.utimes(s,r.timestamp,r.timestamp),r.size!==void 0&&t.truncate(s,r.size)})},lookup(e,r){m("lookup",i.realPath(e),r);let s=[i.realPath(e),r].join("/"),n=i.getMode(s);return i.createNode(e,r,n)},mknod(e,r,s,n){m("mknod",i.realPath(e),r,s,n);let o=i.createNode(e,r,s,n),p=i.realPath(o);return i.tryFSOperation(()=>(a.isDir(o.mode)?t.mkdir(p,{mode:s}):t.writeFile(p,"",{mode:s}),o))},rename(e,r,s){m("rename",i.realPath(e),i.realPath(r),s);let n=i.realPath(e),o=[i.realPath(r),s].join("/");i.tryFSOperation(()=>{t.rename(n,o)}),e.name=s},unlink(e,r){m("unlink",i.realPath(e),r);let s=[i.realPath(e),r].join("/");try{t.unlink(s)}catch{}},rmdir(e,r){m("rmdir",i.realPath(e),r);let s=[i.realPath(e),r].join("/");return i.tryFSOperation(()=>{t.rmdir(s)})},readdir(e){m("readdir",i.realPath(e));let r=i.realPath(e);return i.tryFSOperation(()=>t.readdir(r))},symlink(e,r,s){throw m("symlink",i.realPath(e),r,s),new a.ErrnoError(63)},readlink(e){throw m("readlink",i.realPath(e)),new a.ErrnoError(63)}},stream_ops:{open(e){m("open stream",i.realPath(e.node));let r=i.realPath(e.node);return i.tryFSOperation(()=>{a.isFile(e.node.mode)&&(e.shared.refcount=1,e.nfd=t.open(r))})},close(e){return m("close stream",i.realPath(e.node)),i.tryFSOperation(()=>{a.isFile(e.node.mode)&&e.nfd&&--e.shared.refcount===0&&t.close(e.nfd)})},dup(e){m("dup stream",i.realPath(e.node)),e.shared.refcount++},read(e,r,s,n,o){return m("read stream",i.realPath(e.node),s,n,o),n===0?0:i.tryFSOperation(()=>t.read(e.nfd,r,s,n,o))},write(e,r,s,n,o){return m("write stream",i.realPath(e.node),s,n,o),i.tryFSOperation(()=>t.write(e.nfd,r.buffer,s,n,o))},llseek(e,r,s){m("llseek stream",i.realPath(e.node),r,s);let n=r;if(s===1?n+=e.position:s===2&&a.isFile(e.node.mode)&&i.tryFSOperation(()=>{let o=t.fstat(e.nfd);n+=o.size}),n<0)throw new a.ErrnoError(28);return n},mmap(e,r,s,n,o){if(m("mmap stream",i.realPath(e.node),r,s,n,o),!a.isFile(e.node.mode))throw new a.ErrnoError($.ENODEV);let p=d.mmapAlloc(r);return i.stream_ops.read(e,d.HEAP8,p,r,s),{ptr:p,allocated:!0}},msync(e,r,s,n,o){return m("msync stream",i.realPath(e.node),s,n,o),i.stream_ops.write(e,r,0,n,s),0}}};return i};function m(...d){}W();var G="state.txt",Q="data",H={DIR:16384,FILE:32768},R,T,b,I,j,u,O,S,v,D,F,k,h,C,N,M,g,f,_,J,x,U=class V{constructor({root:t,initialPoolSize:a,maintainedPoolSize:i}){y(this,h),y(this,R,!1),y(this,T),y(this,b),y(this,I),y(this,j),y(this,u),y(this,O,new Map),y(this,S,new Map),y(this,v,0),y(this,D,new Map),y(this,F,new Map),this.lastCheckpoint=0,this.checkpointInterval=1e3*60,this.poolCounter=0,y(this,k,new Set),this.root=t,this.initialPoolSize=a||1e3,this.maintainedPoolSize=i||100,this.readyPromise=c(this,h,C).call(this)}static async create(t){let a=new V(t);return await a.readyPromise,a}get ready(){return l(this,R)}async maintainPool(t){t=t||this.maintainedPoolSize;let a=t-this.state.pool.length,i=[];for(let e=0;e{++this.poolCounter;let s=`${(Date.now()-1704063600).toString(16).padStart(8,"0")}-${this.poolCounter.toString(16).padStart(8,"0")}`,n=await l(this,I).getFileHandle(s,{create:!0}),o=await n.createSyncAccessHandle();l(this,O).set(s,n),l(this,S).set(s,o),c(this,h,M).call(this,{opp:"createPoolFile",args:[s]}),this.state.pool.push(s),r()}));for(let e=0;e>a;e--)i.push(new Promise(async r=>{var o;let s=this.state.pool.pop();c(this,h,M).call(this,{opp:"deletePoolFile",args:[s]});let n=l(this,O).get(s);(o=l(this,S).get(s))==null||o.close(),await n.remove().then(()=>{l(this,O).delete(s),l(this,S).delete(s),r()})}));await Promise.all(i)}_createPoolFileState(t){this.state.pool.push(t)}_deletePoolFileState(t){let a=this.state.pool.indexOf(t);a>-1&&this.state.pool.splice(a,1)}async maybeCheckpointState(){Date.now()-this.lastCheckpoint>this.checkpointInterval&&await this.checkpointState()}async checkpointState(){let t=new TextEncoder().encode(JSON.stringify(this.state));l(this,u).truncate(0),l(this,u).write(t,{at:0}),l(this,u).flush(),this.lastCheckpoint=Date.now()}flush(){for(let t of l(this,k))try{t.flush()}catch{}l(this,k).clear()}exit(){for(let t of l(this,S).values())t.close();l(this,u).flush(),l(this,u).close()}chmod(t,a){c(this,h,N).call(this,{opp:"chmod",args:[t,a]},()=>{this._chmodState(t,a)})}_chmodState(t,a){let i=c(this,h,f).call(this,t);i.mode=a}close(t){let a=c(this,h,_).call(this,t);l(this,D).delete(t),l(this,F).delete(a)}fstat(t){let a=c(this,h,_).call(this,t);return this.lstat(a)}lstat(t){let a=c(this,h,f).call(this,t),i=a.type==="file"?l(this,S).get(a.backingFilename).getSize():0,e=4096;return{dev:0,ino:0,mode:a.mode,nlink:1,uid:0,gid:0,rdev:0,size:i,blksize:e,blocks:Math.ceil(i/e),atime:a.lastModified,mtime:a.lastModified,ctime:a.lastModified}}mkdir(t,a){c(this,h,N).call(this,{opp:"mkdir",args:[t,a]},()=>{this._mkdirState(t,a)})}_mkdirState(t,a){let i=c(this,h,g).call(this,t),e=i.pop(),r=[],s=this.state.root;for(let o of i){if(r.push(t),!Object.prototype.hasOwnProperty.call(s.children,o))if(a!=null&&a.recursive)this.mkdir(r.join("/"));else throw new w("ENOENT","No such file or directory");if(s.children[o].type!=="directory")throw new w("ENOTDIR","Not a directory");s=s.children[o]}if(Object.prototype.hasOwnProperty.call(s.children,e))throw new w("EEXIST","File exists");let n={type:"directory",lastModified:Date.now(),mode:(a==null?void 0:a.mode)||H.DIR,children:{}};s.children[e]=n}open(t,a,i){if(c(this,h,f).call(this,t).type!=="file")throw new w("EISDIR","Is a directory");let e=c(this,h,J).call(this);return l(this,D).set(e,t),l(this,F).set(t,e),e}readdir(t){let a=c(this,h,f).call(this,t);if(a.type!=="directory")throw new w("ENOTDIR","Not a directory");return Object.keys(a.children)}read(t,a,i,e,r){let s=c(this,h,_).call(this,t),n=c(this,h,f).call(this,s);if(n.type!=="file")throw new w("EISDIR","Is a directory");return l(this,S).get(n.backingFilename).read(new Int8Array(a.buffer,i,e),{at:r})}rename(t,a){c(this,h,N).call(this,{opp:"rename",args:[t,a]},()=>{this._renameState(t,a,!0)})}_renameState(t,a,i=!1){let e=c(this,h,g).call(this,t),r=e.pop(),s=c(this,h,f).call(this,e.join("/"));if(!Object.prototype.hasOwnProperty.call(s.children,r))throw new w("ENOENT","No such file or directory");let n=c(this,h,g).call(this,a),o=n.pop(),p=c(this,h,f).call(this,n.join("/"));if(i&&Object.prototype.hasOwnProperty.call(p.children,o)){let P=p.children[o];l(this,S).get(P.backingFilename).truncate(0),this.state.pool.push(P.backingFilename)}p.children[o]=s.children[r],delete s.children[r]}rmdir(t){c(this,h,N).call(this,{opp:"rmdir",args:[t]},()=>{this._rmdirState(t)})}_rmdirState(t){let a=c(this,h,g).call(this,t),i=a.pop(),e=c(this,h,f).call(this,a.join("/"));if(!Object.prototype.hasOwnProperty.call(e.children,i))throw new w("ENOENT","No such file or directory");let r=e.children[i];if(r.type!=="directory")throw new w("ENOTDIR","Not a directory");if(Object.keys(r.children).length>0)throw new w("ENOTEMPTY","Directory not empty");delete e.children[i]}truncate(t,a=0){let i=c(this,h,f).call(this,t);if(i.type!=="file")throw new w("EISDIR","Is a directory");let e=l(this,S).get(i.backingFilename);if(!e)throw new w("ENOENT","No such file or directory");e.truncate(a),l(this,k).add(e)}unlink(t){c(this,h,N).call(this,{opp:"unlink",args:[t]},()=>{this._unlinkState(t,!0)})}_unlinkState(t,a=!1){let i=c(this,h,g).call(this,t),e=i.pop(),r=c(this,h,f).call(this,i.join("/"));if(!Object.prototype.hasOwnProperty.call(r.children,e))throw new w("ENOENT","No such file or directory");let s=r.children[e];if(s.type!=="file")throw new w("EISDIR","Is a directory");if(delete r.children[e],a){let n=l(this,S).get(s.backingFilename);n==null||n.truncate(0),l(this,k).add(n),l(this,F).has(t)&&(l(this,D).delete(l(this,F).get(t)),l(this,F).delete(t))}this.state.pool.push(s.backingFilename)}utimes(t,a,i){c(this,h,N).call(this,{opp:"utimes",args:[t,a,i]},()=>{this._utimesState(t,a,i)})}_utimesState(t,a,i){let e=c(this,h,f).call(this,t);e.lastModified=i}writeFile(t,a,i){let e=c(this,h,g).call(this,t),r=e.pop(),s=c(this,h,f).call(this,e.join("/"));if(Object.prototype.hasOwnProperty.call(s.children,r)){let p=s.children[r];p.lastModified=Date.now(),c(this,h,M).call(this,{opp:"setLastModified",args:[t,p.lastModified]})}else{if(this.state.pool.length===0)throw new Error("No more file handles available in the pool");let p={type:"file",lastModified:Date.now(),mode:(i==null?void 0:i.mode)||H.FILE,backingFilename:this.state.pool.pop()};s.children[r]=p,c(this,h,M).call(this,{opp:"createFileNode",args:[t,p]})}let n=s.children[r],o=l(this,S).get(n.backingFilename);a.length>0&&(o.write(typeof a=="string"?new TextEncoder().encode(a):new Int8Array(a),{at:0}),t.startsWith("/pg_wal")&&l(this,k).add(o))}_createFileNodeState(t,a){let i=c(this,h,g).call(this,t),e=i.pop(),r=c(this,h,f).call(this,i.join("/"));r.children[e]=a;let s=this.state.pool.indexOf(a.backingFilename);return s>-1&&this.state.pool.splice(s,1),a}_setLastModifiedState(t,a){let i=c(this,h,f).call(this,t);i.lastModified=a}write(t,a,i,e,r){let s=c(this,h,_).call(this,t),n=c(this,h,f).call(this,s);if(n.type!=="file")throw new w("EISDIR","Is a directory");let o=l(this,S).get(n.backingFilename);if(!o)throw new w("EBADF","Bad file descriptor");let p=o.write(new Int8Array(a,i,e),{at:r});return s.startsWith("/pg_wal")&&l(this,k).add(o),p}};R=new WeakMap,T=new WeakMap,b=new WeakMap,I=new WeakMap,j=new WeakMap,u=new WeakMap,O=new WeakMap,S=new WeakMap,v=new WeakMap,D=new WeakMap,F=new WeakMap,k=new WeakMap,h=new WeakSet,C=async function(){E(this,T,await navigator.storage.getDirectory()),E(this,b,await c(this,h,x).call(this,this.root,{create:!0})),E(this,I,await c(this,h,x).call(this,Q,{from:l(this,b),create:!0})),E(this,j,await l(this,b).getFileHandle(G,{create:!0})),E(this,u,await l(this,j).createSyncAccessHandle());let d=new ArrayBuffer(l(this,u).getSize());l(this,u).read(d,{at:0});let t,a=new TextDecoder().decode(d).split(` `),i=!1;try{t=JSON.parse(a[0])}catch{t={root:{type:"directory",lastModified:Date.now(),mode:H.DIR,children:{}},pool:[]},l(this,u).truncate(0),l(this,u).write(new TextEncoder().encode(JSON.stringify(t)),{at:0}),i=!0}this.state=t;let e=a.slice(1).filter(Boolean).map(o=>JSON.parse(o));for(let o of e){let p=`_${o.opp}State`;if(typeof this[p]=="function")try{this[p].bind(this)(...o.args)}catch(P){console.warn("Error applying OPFS AHP WAL entry",o,P)}}let r=[],s=async o=>{if(o.type==="file")try{let p=await l(this,I).getFileHandle(o.backingFilename),P=await p.createSyncAccessHandle();l(this,O).set(o.backingFilename,p),l(this,S).set(o.backingFilename,P)}catch(p){console.error("Error opening file handle for node",o,p)}else for(let p of Object.values(o.children))r.push(s(p))};await s(this.state.root);let n=[];for(let o of this.state.pool)n.push(new Promise(async p=>{l(this,O).has(o)&&console.warn("File handle already exists for pool file",o);let P=await l(this,I).getFileHandle(o),X=await P.createSyncAccessHandle();l(this,O).set(o,P),l(this,S).set(o,X),p()}));await Promise.all([...r,...n]),await this.maintainPool(i?this.initialPoolSize:this.maintainedPoolSize),E(this,R,!0)},N=function(d,t){let a=c(this,h,M).call(this,d);try{t()}catch(i){throw l(this,u).truncate(a),i}},M=function(d){let t=JSON.stringify(d),a=new TextEncoder().encode(` ${t}`),i=l(this,u).getSize();return l(this,u).write(a,{at:i}),l(this,k).add(l(this,u)),i},g=function(d){return d.split("/").filter(Boolean)},f=function(d,t){let a=c(this,h,g).call(this,d),i=t||this.state.root;for(let e of a){if(i.type!=="directory")throw new w("ENOTDIR","Not a directory");if(!Object.prototype.hasOwnProperty.call(i.children,e))throw new w("ENOENT","No such file or directory");i=i.children[e]}return i},_=function(d){let t=l(this,D).get(d);if(!t)throw new w("EBADF","Bad file descriptor");return t},J=function(){let d=++L(this,v)._;for(;l(this,D).has(d);)L(this,v)._++;return d},x=async function(d,t){let a=c(this,h,g).call(this,d),i=(t==null?void 0:t.from)||l(this,T);for(let e of a)i=await i.getDirectoryHandle(e,{create:t==null?void 0:t.create});return i};var Z=U,z,A,et=class extends K{constructor(d,{initialPoolSize:t,maintainedPoolSize:a}={}){super(d),y(this,z),y(this,A),E(this,z,t??1e3),E(this,A,a??100)}async emscriptenOpts(d){return this.opfsAhp=await Z.create({root:this.dataDir,initialPoolSize:l(this,z),maintainedPoolSize:l(this,A)}),{...d,preRun:[...d.preRun||[],t=>{let a=q(t,this.opfsAhp);t.FS.mkdir(B),t.FS.mount(a,{},B)}]}}async syncToFs(d,t=!1){var a,i,e;await((a=this.opfsAhp)==null?void 0:a.maybeCheckpointState()),await((i=this.opfsAhp)==null?void 0:i.maintainPool()),t||((e=this.opfsAhp)==null||e.flush())}async dumpTar(d,t){return Y(d,t)}async close(d){var t;(t=this.opfsAhp)==null||t.exit(),d.quit()}};z=new WeakMap,A=new WeakMap;export{et as OpfsAhpFS};