forked from oraoto/pib
-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy path774.71f6224c.chunk.js
2 lines (2 loc) · 10.8 KB
/
774.71f6224c.chunk.js
1
2
"use strict";(self.webpackChunkdemo_source=self.webpackChunkdemo_source||[]).push([[774],{51774:(t,e,i)=>{i.d(e,{OpfsAhpFS:()=>P});var s=i(77560),a=i(49699);(0,a.j)();var h,l,o,r,n,c,d,p,f,w,y,u,g,m,S,k,O,b,E,N,F=16384,M=32768,P=class extends s.f{constructor(t){let{initialPoolSize:e=1e3,maintainedPoolSize:i=100,debug:s=!1}=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};super(t,{debug:s}),(0,a.f)(this,u),(0,a.f)(this,h),(0,a.f)(this,l),(0,a.f)(this,o),(0,a.f)(this,r),(0,a.f)(this,n),(0,a.f)(this,c,new Map),(0,a.f)(this,d,new Map),(0,a.f)(this,p,0),(0,a.f)(this,f,new Map),(0,a.f)(this,w,new Map),this.lastCheckpoint=0,this.checkpointInterval=6e4,this.poolCounter=0,(0,a.f)(this,y,new Set),this.initialPoolSize=e,this.maintainedPoolSize=i}async init(t,e){return await(0,a.h)(this,u,g).call(this),super.init(t,e)}async syncToFs(){let t=arguments.length>0&&void 0!==arguments[0]&&arguments[0];await this.maybeCheckpointState(),await this.maintainPool(),t||this.flush()}async closeFs(){for(let t of(0,a.e)(this,d).values())t.close();(0,a.e)(this,n).flush(),(0,a.e)(this,n).close(),this.pg.Module.FS.quit()}async maintainPool(t){let e=(t=t||this.maintainedPoolSize)-this.state.pool.length,i=[];for(let s=0;s<e;s++)i.push(new Promise((async t=>{++this.poolCounter;let e=`${(Date.now()-1704063600).toString(16).padStart(8,"0")}-${this.poolCounter.toString(16).padStart(8,"0")}`,i=await(0,a.e)(this,o).getFileHandle(e,{create:!0}),s=await i.createSyncAccessHandle();(0,a.e)(this,c).set(e,i),(0,a.e)(this,d).set(e,s),(0,a.h)(this,u,S).call(this,{opp:"createPoolFile",args:[e]}),this.state.pool.push(e),t()})));for(let s=0;s>e;s--)i.push(new Promise((async t=>{let e=this.state.pool.pop();(0,a.h)(this,u,S).call(this,{opp:"deletePoolFile",args:[e]});let i=(0,a.e)(this,c).get(e);(0,a.e)(this,d).get(e)?.close(),await(0,a.e)(this,o).removeEntry(i.name),(0,a.e)(this,c).delete(e),(0,a.e)(this,d).delete(e),t()})));await Promise.all(i)}_createPoolFileState(t){this.state.pool.push(t)}_deletePoolFileState(t){let e=this.state.pool.indexOf(t);e>-1&&this.state.pool.splice(e,1)}async maybeCheckpointState(){Date.now()-this.lastCheckpoint>this.checkpointInterval&&await this.checkpointState()}async checkpointState(){let t=(new TextEncoder).encode(JSON.stringify(this.state));(0,a.e)(this,n).truncate(0),(0,a.e)(this,n).write(t,{at:0}),(0,a.e)(this,n).flush(),this.lastCheckpoint=Date.now()}flush(){for(let t of(0,a.e)(this,y))try{t.flush()}catch{}(0,a.e)(this,y).clear()}chmod(t,e){(0,a.h)(this,u,m).call(this,{opp:"chmod",args:[t,e]},(()=>{this._chmodState(t,e)}))}_chmodState(t,e){(0,a.h)(this,u,O).call(this,t).mode=e}close(t){let e=(0,a.h)(this,u,b).call(this,t);(0,a.e)(this,f).delete(t),(0,a.e)(this,w).delete(e)}fstat(t){let e=(0,a.h)(this,u,b).call(this,t);return this.lstat(e)}lstat(t){let e=(0,a.h)(this,u,O).call(this,t),i="file"===e.type?(0,a.e)(this,d).get(e.backingFilename).getSize():0;return{dev:0,ino:0,mode:e.mode,nlink:1,uid:0,gid:0,rdev:0,size:i,blksize:4096,blocks:Math.ceil(i/4096),atime:e.lastModified,mtime:e.lastModified,ctime:e.lastModified}}mkdir(t,e){(0,a.h)(this,u,m).call(this,{opp:"mkdir",args:[t,e]},(()=>{this._mkdirState(t,e)}))}_mkdirState(t,e){let i=(0,a.h)(this,u,k).call(this,t),s=i.pop(),h=[],l=this.state.root;for(let a of i){if(h.push(t),!Object.prototype.hasOwnProperty.call(l.children,a)){if(!e?.recursive)throw new D("ENOENT","No such file or directory");this.mkdir(h.join("/"))}if("directory"!==l.children[a].type)throw new D("ENOTDIR","Not a directory");l=l.children[a]}if(Object.prototype.hasOwnProperty.call(l.children,s))throw new D("EEXIST","File exists");let o={type:"directory",lastModified:Date.now(),mode:e?.mode||F,children:{}};l.children[s]=o}open(t,e,i){if("file"!==(0,a.h)(this,u,O).call(this,t).type)throw new D("EISDIR","Is a directory");let s=(0,a.h)(this,u,E).call(this);return(0,a.e)(this,f).set(s,t),(0,a.e)(this,w).set(t,s),s}readdir(t){let e=(0,a.h)(this,u,O).call(this,t);if("directory"!==e.type)throw new D("ENOTDIR","Not a directory");return Object.keys(e.children)}read(t,e,i,s,h){let l=(0,a.h)(this,u,b).call(this,t),o=(0,a.h)(this,u,O).call(this,l);if("file"!==o.type)throw new D("EISDIR","Is a directory");return(0,a.e)(this,d).get(o.backingFilename).read(new Uint8Array(e.buffer,i,s),{at:h})}rename(t,e){(0,a.h)(this,u,m).call(this,{opp:"rename",args:[t,e]},(()=>{this._renameState(t,e,!0)}))}_renameState(t,e){let i=arguments.length>2&&void 0!==arguments[2]&&arguments[2],s=(0,a.h)(this,u,k).call(this,t),h=s.pop(),l=(0,a.h)(this,u,O).call(this,s.join("/"));if(!Object.prototype.hasOwnProperty.call(l.children,h))throw new D("ENOENT","No such file or directory");let o=(0,a.h)(this,u,k).call(this,e),r=o.pop(),n=(0,a.h)(this,u,O).call(this,o.join("/"));if(i&&Object.prototype.hasOwnProperty.call(n.children,r)){let t=n.children[r];(0,a.e)(this,d).get(t.backingFilename).truncate(0),this.state.pool.push(t.backingFilename)}n.children[r]=l.children[h],delete l.children[h]}rmdir(t){(0,a.h)(this,u,m).call(this,{opp:"rmdir",args:[t]},(()=>{this._rmdirState(t)}))}_rmdirState(t){let e=(0,a.h)(this,u,k).call(this,t),i=e.pop(),s=(0,a.h)(this,u,O).call(this,e.join("/"));if(!Object.prototype.hasOwnProperty.call(s.children,i))throw new D("ENOENT","No such file or directory");let h=s.children[i];if("directory"!==h.type)throw new D("ENOTDIR","Not a directory");if(Object.keys(h.children).length>0)throw new D("ENOTEMPTY","Directory not empty");delete s.children[i]}truncate(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,i=(0,a.h)(this,u,O).call(this,t);if("file"!==i.type)throw new D("EISDIR","Is a directory");let s=(0,a.e)(this,d).get(i.backingFilename);if(!s)throw new D("ENOENT","No such file or directory");s.truncate(e),(0,a.e)(this,y).add(s)}unlink(t){(0,a.h)(this,u,m).call(this,{opp:"unlink",args:[t]},(()=>{this._unlinkState(t,!0)}))}_unlinkState(t){let e=arguments.length>1&&void 0!==arguments[1]&&arguments[1],i=(0,a.h)(this,u,k).call(this,t),s=i.pop(),h=(0,a.h)(this,u,O).call(this,i.join("/"));if(!Object.prototype.hasOwnProperty.call(h.children,s))throw new D("ENOENT","No such file or directory");let l=h.children[s];if("file"!==l.type)throw new D("EISDIR","Is a directory");if(delete h.children[s],e){let e=(0,a.e)(this,d).get(l.backingFilename);e?.truncate(0),(0,a.e)(this,y).add(e),(0,a.e)(this,w).has(t)&&((0,a.e)(this,f).delete((0,a.e)(this,w).get(t)),(0,a.e)(this,w).delete(t))}this.state.pool.push(l.backingFilename)}utimes(t,e,i){(0,a.h)(this,u,m).call(this,{opp:"utimes",args:[t,e,i]},(()=>{this._utimesState(t,e,i)}))}_utimesState(t,e,i){(0,a.h)(this,u,O).call(this,t).lastModified=i}writeFile(t,e,i){let s=(0,a.h)(this,u,k).call(this,t),h=s.pop(),l=(0,a.h)(this,u,O).call(this,s.join("/"));if(Object.prototype.hasOwnProperty.call(l.children,h)){let e=l.children[h];e.lastModified=Date.now(),(0,a.h)(this,u,S).call(this,{opp:"setLastModified",args:[t,e.lastModified]})}else{if(0===this.state.pool.length)throw new Error("No more file handles available in the pool");let e={type:"file",lastModified:Date.now(),mode:i?.mode||M,backingFilename:this.state.pool.pop()};l.children[h]=e,(0,a.h)(this,u,S).call(this,{opp:"createFileNode",args:[t,e]})}let o=l.children[h],r=(0,a.e)(this,d).get(o.backingFilename);e.length>0&&(r.write("string"==typeof e?(new TextEncoder).encode(e):new Uint8Array(e),{at:0}),t.startsWith("/pg_wal")&&(0,a.e)(this,y).add(r))}_createFileNodeState(t,e){let i=(0,a.h)(this,u,k).call(this,t),s=i.pop();(0,a.h)(this,u,O).call(this,i.join("/")).children[s]=e;let h=this.state.pool.indexOf(e.backingFilename);return h>-1&&this.state.pool.splice(h,1),e}_setLastModifiedState(t,e){(0,a.h)(this,u,O).call(this,t).lastModified=e}write(t,e,i,s,h){let l=(0,a.h)(this,u,b).call(this,t),o=(0,a.h)(this,u,O).call(this,l);if("file"!==o.type)throw new D("EISDIR","Is a directory");let r=(0,a.e)(this,d).get(o.backingFilename);if(!r)throw new D("EBADF","Bad file descriptor");let n=r.write(new Uint8Array(e,i,s),{at:h});return l.startsWith("/pg_wal")&&(0,a.e)(this,y).add(r),n}};h=new WeakMap,l=new WeakMap,o=new WeakMap,r=new WeakMap,n=new WeakMap,c=new WeakMap,d=new WeakMap,p=new WeakMap,f=new WeakMap,w=new WeakMap,y=new WeakMap,u=new WeakSet,g=async function(){(0,a.g)(this,h,await navigator.storage.getDirectory()),(0,a.g)(this,l,await(0,a.h)(this,u,N).call(this,this.dataDir,{create:!0})),(0,a.g)(this,o,await(0,a.h)(this,u,N).call(this,"data",{from:(0,a.e)(this,l),create:!0})),(0,a.g)(this,r,await(0,a.e)(this,l).getFileHandle("state.txt",{create:!0})),(0,a.g)(this,n,await(0,a.e)(this,r).createSyncAccessHandle());let t=new ArrayBuffer((0,a.e)(this,n).getSize());(0,a.e)(this,n).read(t,{at:0});let e,i=(new TextDecoder).decode(t).split("\n"),s=!1;try{e=JSON.parse(i[0])}catch{e={root:{type:"directory",lastModified:Date.now(),mode:F,children:{}},pool:[]},(0,a.e)(this,n).truncate(0),(0,a.e)(this,n).write((new TextEncoder).encode(JSON.stringify(e)),{at:0}),s=!0}this.state=e;let p=i.slice(1).filter(Boolean).map((t=>JSON.parse(t)));for(let a of p){let t=`_${a.opp}State`;if("function"==typeof this[t])try{this[t].bind(this)(...a.args)}catch(g){console.warn("Error applying OPFS AHP WAL entry",a,g)}}let f=[],w=async t=>{if("file"===t.type)try{let e=await(0,a.e)(this,o).getFileHandle(t.backingFilename),i=await e.createSyncAccessHandle();(0,a.e)(this,c).set(t.backingFilename,e),(0,a.e)(this,d).set(t.backingFilename,i)}catch(e){console.error("Error opening file handle for node",t,e)}else for(let i of Object.values(t.children))f.push(w(i))};await w(this.state.root);let y=[];for(let h of this.state.pool)y.push(new Promise((async t=>{(0,a.e)(this,c).has(h)&&console.warn("File handle already exists for pool file",h);let e=await(0,a.e)(this,o).getFileHandle(h),i=await e.createSyncAccessHandle();(0,a.e)(this,c).set(h,e),(0,a.e)(this,d).set(h,i),t()})));await Promise.all([...f,...y]),await this.maintainPool(s?this.initialPoolSize:this.maintainedPoolSize)},m=function(t,e){let i=(0,a.h)(this,u,S).call(this,t);try{e()}catch(s){throw(0,a.e)(this,n).truncate(i),s}},S=function(t){let e=JSON.stringify(t),i=(new TextEncoder).encode(`\n${e}`),s=(0,a.e)(this,n).getSize();return(0,a.e)(this,n).write(i,{at:s}),(0,a.e)(this,y).add((0,a.e)(this,n)),s},k=function(t){return t.split("/").filter(Boolean)},O=function(t,e){let i=(0,a.h)(this,u,k).call(this,t),s=e||this.state.root;for(let a of i){if("directory"!==s.type)throw new D("ENOTDIR","Not a directory");if(!Object.prototype.hasOwnProperty.call(s.children,a))throw new D("ENOENT","No such file or directory");s=s.children[a]}return s},b=function(t){let e=(0,a.e)(this,f).get(t);if(!e)throw new D("EBADF","Bad file descriptor");return e},E=function(){let t=++(0,a.i)(this,p)._;for(;(0,a.e)(this,f).has(t);)(0,a.i)(this,p)._++;return t},N=async function(t,e){let i=(0,a.h)(this,u,k).call(this,t),s=e?.from||(0,a.e)(this,h);for(let a of i)s=await s.getDirectoryHandle(a,{create:e?.create});return s};var D=class extends Error{constructor(t,e){super(e),"number"==typeof t?this.code=t:"string"==typeof t&&(this.code=s.g[t])}}}}]);
//# sourceMappingURL=774.71f6224c.chunk.js.map