tsdf 17.0.0-alpha.6 → 17.0.0-alpha.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +140 -0
- package/README.md +3 -0
- package/dist/async-storage.d.ts +2 -0
- package/dist/async-storage.js +1 -0
- package/dist/asyncStorageAdapter-CgG3vm0t.js +1 -0
- package/dist/asyncStorageAdapter-kvAIhrBD.d.ts +6 -0
- package/dist/asyncStorageAdapters-eagaD77Y.js +1 -0
- package/dist/indexed-db-storage.d.ts +12 -0
- package/dist/indexed-db-storage.js +1 -0
- package/dist/indexedDbPersistentStorage-JYXvMjTG.js +1 -0
- package/dist/main.d.ts +192 -1676
- package/dist/main.js +1 -18927
- package/dist/opfs-storage.d.ts +2 -0
- package/dist/opfs-storage.js +1 -0
- package/dist/opfsPersistentStorage-DS-SWDSq.js +1 -0
- package/dist/opfsPersistentStorage-D_fecOJg.d.ts +6 -0
- package/dist/types-DkNgE_f9.d.ts +1406 -0
- package/docs/persistent-storage.md +12 -6
- package/package.json +19 -5
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{t as e}from"./opfsPersistentStorage-DS-SWDSq.js";export{e as opfsPersistentStorage};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{B as e,F as t,I as n,L as r,V as i,i as a}from"./asyncStorageAdapter-CgG3vm0t.js";import{getCompositeKey as o}from"@ls-stack/utils/getCompositeKey";import{__LEGIT_CAST__ as s}from"@ls-stack/utils/saferTyping";import{isObject as c}from"@ls-stack/utils/typeGuards";import{createCache as l}from"@ls-stack/utils/cache";import{asPossiblyUndefined as u}from"@ls-stack/utils/typingFnUtils";import{murmur3 as d}from"@ls-stack/utils/hash";const f=`tsdf`,p=`.json`;function m(e){switch(e){case`collection.item`:case`listQuery.item`:case`listQuery.query`:return!0;default:return!1}}function h(e){return encodeURIComponent(e)}function g(e){return decodeURIComponent(e)}function _(e){return h(e).replaceAll(`.`,`%2E`)}function v(...e){return e.filter(e=>e.length>0).join(`/`)}function y(e){switch(e){case`payload`:return`p`;case`raw`:return`r`}}function b(e){switch(e){case`p`:return`payload`;case`r`:return`raw`;default:return null}}function x(e,t){return e.kind===`document`&&t===`d`?`e`:_(t)}function S(e,t){return e===`document`&&t===`e`?`d`:g(t)}function C(t){return e(t)}function w(e,t){switch(e){case`payload`:return r(t);case`raw`:return t}}function T(e,n){let r=C(n),i=t(e.kind);return r.recordKind===`raw`?[i,_(r.rawKey),y(`raw`)].join(`.`)+p:[i,m(e.kind)?`h~${d(n,`uint32`)}`:x(e,r.userKey),y(`payload`)].join(`.`)+p}function E(e,t){if(!m(e.kind)||!c(t))return null;let n=t;if(!(`p`in n))return null;switch(e.kind){case`collection.item`:case`listQuery.item`:case`listQuery.query`:return r(o(n.p));default:return null}}function D(e){if(!e.endsWith(p))return null;let t=e.slice(0,-5).split(`.`);if(t.length!==3)return null;let n=t[0]??``,r=t[1]??``,a=t[2]??``,o=i(n);if(o===null)return null;let s=b(a);return s===null?null:s===`payload`&&m(o)&&r.startsWith(`h~`)?{kind:o,key:null,isHashedPayload:!0}:{kind:o,key:s===`raw`?g(r):w(s,S(o,r)),isHashedPayload:!1}}async function O(){let e=u(globalThis.navigator)?.storage;if(e?.getDirectory===void 0)throw Error(`[TSDF] OPFS is unavailable in this environment.`);return e.getDirectory()}async function k(e,t){try{return await e.getDirectoryHandle(t)}catch{return null}}async function A(e,t){try{return await e.getFileHandle(t)}catch{return null}}const j=a(new class{#e=null;#t={cleanupKnowledge:null,dirCache:l({maxCacheSize:500}),fileCache:l({maxCacheSize:1e4}),readValueCache:null};async get(e,t){let[n]=await this.getMany(e,[t]);return n??null}async set(e,t,n){await this.setMany(e,[{key:t,value:n}])}async remove(e,t){await this.removeMany(e,[t])}async getMany(e,t){return this.#r(e,t,this.#t)}async setMany(e,t){await this.#i(e,t,this.#t)}async removeMany(e,t){await this.#a(e,t,this.#t)}async listKeys(e){return this.#o(e,this.#t)}async clear(e){await this.#s(e,this.#t)}async listScopes(e){return this.#c(e,this.#t)}async listScopesWithKnownRecordKeys(e){return this.#l(e,this.#t)}async withIsolatedCleanupDriver(e){let t={cleanupKnowledge:{knownRemainingEntryCountByStorePath:new Map,knownStorePathsBySessionPath:new Map},dirCache:this.#t.dirCache.clone(),fileCache:this.#t.fileCache.clone(),readValueCache:new Map};return e(this.#n(t))}async cleanupRemoveKnownRecords(e,t){return this.#T(e,t,this.#t)}async cleanupRemoveKnownStoreDir(e,t){return this.#E(e,t,this.#t)}async cleanupRemoveKnownSessionDir(e){return this.#D(e,this.#t)}cleanupFinalizeRemovedRecords(e,t){this.#O(e,t,this.#t)}cleanupFinalizeRemovedStoreDir(e,t){this.#k(e,t,this.#t)}cleanupFinalizeRemovedSessionDir(e){this.#A(e,this.#t)}__resetForTests(){this.#e=null,this.#t.dirCache.clear(),this.#t.fileCache.clear()}#n(e){return{cleanupFinalizeRemovedRecords:(t,n)=>this.#O(t,n,e),cleanupFinalizeRemovedSessionDir:t=>this.#A(t,e),cleanupFinalizeRemovedStoreDir:(t,n)=>this.#k(t,n,e),cleanupRemoveKnownRecords:(t,n)=>this.#T(t,n,e),cleanupRemoveKnownSessionDir:t=>this.#D(t,e),cleanupRemoveKnownStoreDir:(t,n)=>this.#E(t,n,e),get:(t,n)=>this.#r(t,[n],e).then(([e])=>e??null),set:(t,n,r)=>this.#i(t,[{key:n,value:r}],e),remove:(t,n)=>this.#a(t,[n],e),listKeys:t=>this.#o(t,e),clear:t=>this.#s(t,e),listScopes:t=>this.#c(t,e),listScopesWithKnownRecordKeys:t=>this.#l(t,e),getMany:(t,n)=>this.#r(t,n,e),setMany:(t,n)=>this.#i(t,n,e),removeMany:(t,n)=>this.#a(t,n,e)}}async#r(e,t,n){if(t.length===0)return[];let r=await this.#y(e,{create:!1,cacheContext:n});return r===null?t.map(()=>null):Promise.all(t.map(async t=>{let i=await this.#b(e,t,{create:!1,cacheContext:n,storeDir:r});return i===null?null:this.#C(this.#_(e,t),i,n)}))}async#i(e,t,n){t.length!==0&&await Promise.all(t.map(t=>this.#w(e,t,n)))}async#a(e,t,n){let r=[...new Set(t)];if(r.length===0)return;let i=await this.#y(e,{create:!1,cacheContext:n});if(i===null||await this.#F(e,r,n))return;let a=await Promise.all(r.map(async t=>{let r=T(e,t);try{return await i.removeEntry(r),!0}catch{return!1}finally{this.#j(e,t,n)}}));this.#z(this.#g(e),n,a.filter(Boolean).length),this.#P(r,n)&&await this.#L(e,n)}async#o(e,t){let{resolvedEntries:n}=await this.#x(e,t);return n.map(e=>e.key).sort((e,t)=>e.localeCompare(t))}async#s(e,t){let n=await this.#y(e,{create:!1,cacheContext:t});if(n===null)return;let{allFileNames:r,resolvedEntries:i}=await this.#x(e,t),a=i.map(e=>e.key);if(r.length===a.length&&await this.#F(e,a,t))return;let o=this.#g(e),s=await Promise.all(r.map(async e=>{try{return await n.removeEntry(e),!0}catch{return!1}finally{this.#M(v(o,e),t)}}));this.#z(o,t,s.filter(Boolean).length),await this.#L(e,t)}async#c(e,t){return(await this.#l(e,t)).map(({scope:e})=>e)}async#l(e,t){let r=await this.#p(e,t),i=new Map;for(let e of r){if(e.handle===null)continue;let r=e.handle;this.#R(e.path,t,new Set);let a=g(e.name),o=await this.#H(r,e.path,t),s=await Promise.all(o.map(async e=>{let n=g(e.name);return{...await this.#u(e.handle,a,n,t),storeEntry:e}})),c=new Set;for(let{storeEntry:e,invalidEntryNames:a,knownEntryCount:o,scopeEntries:l}of s){if(await this.#V(e.handle,a,t),o>0)c.add(e.path);else if(t.cleanupKnowledge!==null&&a.length>0)try{await r.removeEntry(e.name,{recursive:!0}),this.#N(e.path,t)}catch{}t.cleanupKnowledge?.knownRemainingEntryCountByStorePath.set(e.path,o);for(let e of l){let t=n(e.scope),r=i.get(t);if(r!==void 0){r.knownRecordKeys.push(...e.knownRecordKeys);continue}i.set(t,e)}}if(this.#R(e.path,t,c),t.cleanupKnowledge!==null&&c.size===0)try{await(await this.#m()).removeEntry(e.name,{recursive:!0}),this.#N(e.path,t),this.#B(e.path,t)}catch{}}let a=[...i.values()];for(let e of a)e.knownRecordKeys.sort((e,t)=>e.localeCompare(t));return a.sort((e,t)=>{let n=e.scope.sessionKey.localeCompare(t.scope.sessionKey);if(n!==0)return n;let r=e.scope.storeName.localeCompare(t.scope.storeName);return r===0?e.scope.kind.localeCompare(t.scope.kind):r})}async#u(e,t,n,r){let i=[],a=this.#g({sessionKey:t,storeName:n}),o=new Map;for await(let[t,n]of e.entries()){if(n.kind!==`file`){i.push(t);continue}let e=D(t);if(e===null){i.push(t);continue}let r=s(n),c=o.get(e.kind)??[];c.push({fileHandle:r,fileName:t,filePath:v(a,t),isHashedPayload:e.isHashedPayload,key:e.key}),o.set(e.kind,c)}let c=[],l=0;for(let[e,a]of o){let o={sessionKey:t,storeName:n,kind:e},s=[],u=a.filter(e=>e.isHashedPayload),d=0,f=null,p=null;for(let e of a)e.isHashedPayload||e.key===null||(s.push(e.key),d+=1,e.key===`_i`&&(p=e.fileHandle,f=e.filePath,r.fileCache.set(this.#_(o,e.key),e.fileHandle)));if(u.length>0){let e=await this.#S(o,u,p,f,r);s.push(...e.resolvedEntries.map(e=>e.key)),d+=e.resolvedEntries.length,i.push(...e.invalidEntryNames)}d!==0&&(l+=d,s.sort((e,t)=>e.localeCompare(t)),c.push({knownRecordKeys:s,scope:o}))}return{invalidEntryNames:i,knownEntryCount:l,scopeEntries:c}}async#d(e,t,n,r,i){let a=await this.#C(t,r,i),o=a===null?null:E(e,a);return o!==null&&T(e,o)===n?o:null}async#f(e,t,n){if(t===null||e===null)return null;let i=await this.#C(e,t,n);if(!c(i))return null;let a=i.e;return typeof a!=`object`||!a||Array.isArray(a)?null:Object.keys(a).map(e=>r(e))}async#p(e,t){let n=await this.#m();return e===void 0?await this.#H(n,f,t):[{handle:await this.#v(e,{create:!1,cacheContext:t}),name:h(e),path:this.#h(e)}]}async#m(){return this.#e===null&&(this.#e=(async()=>(await O()).getDirectoryHandle(f,{create:!0}))()),this.#e}#h(e){return v(f,h(e))}#g(e){return v(this.#h(e.sessionKey),h(e.storeName))}#_(e,t){return v(this.#g(e),T(e,t))}async#v(e,t){let n=await this.#m(),r=this.#h(e),i=h(e);if(t.create){let e=await t.cacheContext.dirCache.getAsync(r);return e===void 0?t.cacheContext.dirCache.getOrInsertAsync(r,async()=>n.getDirectoryHandle(i,{create:!0})):e}let a=await t.cacheContext.dirCache.getAsync(r);return a===void 0?t.cacheContext.dirCache.getOrInsertAsync(r,async()=>k(n,i),{skipCachingWhen:e=>e===null}):a}async#y(e,t){let n=await this.#v(e.sessionKey,t);if(n===null)return null;let r=this.#g(e),i=h(e.storeName);if(t.create){let e=await t.cacheContext.dirCache.getAsync(r);return e===void 0?t.cacheContext.dirCache.getOrInsertAsync(r,async()=>n.getDirectoryHandle(i,{create:!0})):e}let a=await t.cacheContext.dirCache.getAsync(r);return a===void 0?t.cacheContext.dirCache.getOrInsertAsync(r,async()=>k(n,i),{skipCachingWhen:e=>e===null}):a}async#b(e,t,n){let r=T(e,t),i=v(this.#g(e),r);if(n.create){let e=await n.cacheContext.fileCache.getAsync(i);return e===void 0?n.cacheContext.fileCache.getOrInsertAsync(i,async()=>n.storeDir.getFileHandle(r,{create:!0})):e}let a=await n.cacheContext.fileCache.getAsync(i);return a===void 0?n.cacheContext.fileCache.getOrInsertAsync(i,async()=>A(n.storeDir,r),{skipCachingWhen:e=>e===null}):a}async#x(e,t){let n=await this.#y(e,{create:!1,cacheContext:t});if(n===null)return{allFileNames:[],resolvedEntries:[]};let r=[],i=[],a=[],o=null,c=null,l=this.#g(e);for await(let[t,u]of n.entries()){if(u.kind!==`file`)continue;let n=D(t);if(n===null||n.kind!==e.kind)continue;r.push(t);let d=v(l,t),f=s(u);if(n.isHashedPayload){a.push({fileHandle:f,fileName:t,filePath:d,isHashedPayload:!0,key:null});continue}n.key!==null&&(i.push({fileName:t,key:n.key}),n.key===`_i`&&(c=f,o=d))}return a.length>0&&i.push(...(await this.#S(e,a,c,o,t)).resolvedEntries),i.sort((e,t)=>e.key.localeCompare(t.key)),{allFileNames:r,resolvedEntries:i}}async#S(e,t,n,r,i){let a=new Map(t.map(e=>[e.fileName,e])),o=[],s=[],c=await this.#f(r,n,i);if(c!==null){let n=new Set,r=this.#g(e);for(let t of c){let s=T(e,t),c=a.get(s);c!==void 0&&(n.add(s),o.push({fileName:c.fileName,key:t}),i.fileCache.set(v(r,s),c.fileHandle))}return s.push(...t.flatMap(e=>n.has(e.fileName)?[]:[e.fileName])),{invalidEntryNames:s,resolvedEntries:o}}let l=await Promise.all(t.map(async t=>({file:t,key:await this.#d(e,t.filePath,t.fileName,t.fileHandle,i)})));for(let{file:e,key:t}of l){if(t===null){s.push(e.fileName);continue}o.push({fileName:e.fileName,key:t}),i.fileCache.set(e.filePath,e.fileHandle)}return{invalidEntryNames:s,resolvedEntries:o}}async#C(e,t,n){let r=n.readValueCache;if(r?.has(e)===!0)return r.get(e)??null;try{let n=await t.getFile(),i=JSON.parse(await n.text());return r?.set(e,i),i}catch{return r?.set(e,null),null}}async#w(e,t,n){let r;for(let i=0;i<2;i+=1)try{let r=await this.#y(e,{create:!0,cacheContext:n});if(r===null)throw Error(`[TSDF] Failed to open OPFS store directory.`);let i=await this.#b(e,t.key,{create:!0,cacheContext:n,storeDir:r});if(i===null)throw Error(`[TSDF] Failed to open OPFS file handle.`);let a=await i.createWritable(),o=t.serializedValue??JSON.stringify(t.value);try{await a.write(o)}finally{await a.close()}n.readValueCache?.set(this.#_(e,t.key),t.value);return}catch(a){if(i>0)throw r;r=a,this.#j(e,t.key,n),this.#N(this.#g(e),n),this.#N(this.#h(e.sessionKey),n)}}async#T(e,t,n){if(t.length===0)return[];let r=await this.#y(e,{create:!1,cacheContext:n});return r===null?[]:(await Promise.all(t.map(async t=>{let n=T(e,t);try{return await r.removeEntry(n),t}catch{return null}}))).flatMap(e=>e===null?[]:[e])}async#E(e,t,n){let r=n.cleanupKnowledge;if(r===null)return!1;let i=this.#g(e),a=r.knownRemainingEntryCountByStorePath.get(i);if(a===void 0||a!==t.length)return!1;let o=await this.#v(e.sessionKey,{create:!1,cacheContext:n});if(o===null)return!1;try{return await o.removeEntry(h(e.storeName),{recursive:!0}),!0}catch{return!1}}async#D(e,t){if(t.cleanupKnowledge===null)return!1;let n=this.#h(e),r=t.cleanupKnowledge.knownStorePathsBySessionPath.get(n);if(r!==void 0&&r.size>0)return!1;let i=await this.#m();try{return await i.removeEntry(h(e),{recursive:!0}),!0}catch{return!1}}#O(e,t,n){for(let r of t)this.#j(e,r,n);this.#z(this.#g(e),n,t.length)}#k(e,t,n){for(let r of t)this.#j(e,r,n);let r=this.#h(e.sessionKey),i=this.#g(e);this.#N(i,n),n.cleanupKnowledge?.knownStorePathsBySessionPath.get(r)?.delete(i),n.cleanupKnowledge?.knownRemainingEntryCountByStorePath.delete(i)}#A(e,t){let n=this.#h(e);this.#N(n,t),this.#B(n,t)}#j(e,t,n){let r=this.#_(e,t);this.#M(r,n)}#M(e,t){t.fileCache.delete(e),t.readValueCache?.delete(e),t!==this.#t&&this.#t.fileCache.delete(e)}#N(e,t){t.dirCache.delete(e),t!==this.#t&&this.#t.dirCache.delete(e)}#P(e,t){return t.cleanupKnowledge===null?e.includes(`_i`):!0}async#F(e,t,n){let r=n.cleanupKnowledge;if(r===null)return!1;let i=this.#g(e),a=r.knownRemainingEntryCountByStorePath.get(i);if(a===void 0||a!==t.length)return!1;let o=await this.#v(e.sessionKey,{create:!1,cacheContext:n});if(o===null)return!1;for(let r of t)this.#j(e,r,n);try{await o.removeEntry(h(e.storeName),{recursive:!0})}catch{return!1}return await this.#I(e,n),!0}async#I(e,t){let n=await this.#m(),r=this.#h(e.sessionKey),i=this.#g(e);this.#N(i,t),t.cleanupKnowledge?.knownStorePathsBySessionPath.get(r)?.delete(i),t.cleanupKnowledge?.knownRemainingEntryCountByStorePath.delete(i);let a=t.cleanupKnowledge?.knownStorePathsBySessionPath.get(r);if(a!==void 0&&a.size>0)return;let o=!1;try{await n.removeEntry(h(e.sessionKey),{recursive:t.cleanupKnowledge!==null}),o=!0}catch{}finally{o&&(this.#N(r,t),this.#B(r,t))}}async#L(e,t){let n=this.#g(e),r=await this.#v(e.sessionKey,{create:!1,cacheContext:t});if(r===null)return;let i=t.cleanupKnowledge?.knownRemainingEntryCountByStorePath.get(n);if(i!==void 0&&i>0)return;let a=!1;try{await r.removeEntry(h(e.storeName),{recursive:t.cleanupKnowledge!==null}),a=!0}catch{}a&&await this.#I(e,t)}#R(e,t,n){t.cleanupKnowledge?.knownStorePathsBySessionPath.set(e,n)}#z(e,t,n){if(n===0)return;let r=t.cleanupKnowledge;if(r===null)return;let i=r.knownRemainingEntryCountByStorePath.get(e);i!==void 0&&r.knownRemainingEntryCountByStorePath.set(e,Math.max(0,i-n))}#B(e,t){t.cleanupKnowledge?.knownStorePathsBySessionPath.delete(e)}async#V(e,t,n){n.cleanupKnowledge===null||t.length===0||await Promise.all(t.map(async t=>{try{await e.removeEntry(t,{recursive:!0})}catch{}}))}async#H(e,t,n){let r=[],i=[];for await(let a of e.values()){if(a.kind!==`directory`){i.push(a.name);continue}let e=s(a),o=v(t,a.name);n.dirCache.set(o,e),r.push({handle:e,name:a.name,path:o})}return await this.#V(e,i,n),r.sort((e,t)=>e.name.localeCompare(t.name)),r}});export{h as a,O as i,k as n,A as r,j as t};
|