tsdf 16.0.3 → 17.0.0-alpha.10
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 +5 -0
- package/dist/async-storage.d.ts +2 -0
- package/dist/async-storage.js +2 -0
- package/dist/asyncStorageAdapter-BMYAF6rt.d.ts +6 -0
- package/dist/asyncStorageAdapter-BZSj1ZO5.js +1868 -0
- package/dist/asyncStorageAdapters-DbyKyARM.js +3 -0
- package/dist/indexed-db-storage.d.ts +14 -0
- package/dist/indexed-db-storage.js +2 -0
- package/dist/indexedDbPersistentStorage-DlUjN1qY.js +902 -0
- package/dist/main.d.ts +1487 -373
- package/dist/main.js +16146 -2475
- package/dist/opfs-storage.d.ts +2 -0
- package/dist/opfs-storage.js +2 -0
- package/dist/opfsPersistentStorage-BO_uDxe4.js +838 -0
- package/dist/opfsPersistentStorage-CBnUebnf.d.ts +6 -0
- package/dist/types-2zQlbxtw.d.ts +1475 -0
- package/docs/README.md +98 -0
- package/docs/batch-fetching.md +125 -0
- package/docs/browser-tabs-sync.md +61 -0
- package/docs/cache-limits.md +105 -0
- package/docs/collection-store.md +173 -0
- package/docs/document-store.md +149 -0
- package/docs/fetch-scheduling.md +93 -0
- package/docs/hooks.md +416 -0
- package/docs/invalidation.md +135 -0
- package/docs/list-query-store.md +352 -0
- package/docs/mutations.md +266 -0
- package/docs/offline.md +207 -0
- package/docs/offset-pagination.md +111 -0
- package/docs/optimistic-list-updates.md +180 -0
- package/docs/partial-resources.md +168 -0
- package/docs/persistent-storage.md +290 -0
- package/docs/real-time-updates.md +158 -0
- package/docs/shared-types.md +181 -0
- package/docs/store-manager.md +147 -0
- package/package.json +81 -47
- package/dist/main.cjs +0 -2511
- package/dist/main.d.cts +0 -400
|
@@ -0,0 +1,838 @@
|
|
|
1
|
+
import { B as parseAsyncStorageRecordKey, F as encodePersistedAsyncNamespaceKind, I as getNamespaceId, L as getPayloadRecordKey, V as parsePersistedAsyncNamespaceKind, i as createAsyncStorageAdapter } from "./asyncStorageAdapter-BZSj1ZO5.js";
|
|
2
|
+
import { getCompositeKey } from "@ls-stack/utils/getCompositeKey";
|
|
3
|
+
import { __LEGIT_CAST__ } from "@ls-stack/utils/saferTyping";
|
|
4
|
+
import { isObject } from "@ls-stack/utils/typeGuards";
|
|
5
|
+
import { createCache } from "@ls-stack/utils/cache";
|
|
6
|
+
import { asPossiblyUndefined } from "@ls-stack/utils/typingFnUtils";
|
|
7
|
+
import { murmur3 } from "@ls-stack/utils/hash";
|
|
8
|
+
//#region src/persistentStorage/opfsFileNaming.ts
|
|
9
|
+
const OPFS_ROOT_DIR = "tsdf";
|
|
10
|
+
const JSON_FILE_EXTENSION = ".json";
|
|
11
|
+
const OPFS_SINGLETON_ENTRY_TOKEN = "e";
|
|
12
|
+
const HASHED_PAYLOAD_ENTRY_TOKEN_PREFIX = "h~";
|
|
13
|
+
function shouldHashPayloadRecordForKind(kind) {
|
|
14
|
+
switch (kind) {
|
|
15
|
+
case "collection.item":
|
|
16
|
+
case "listQuery.item":
|
|
17
|
+
case "listQuery.query": return true;
|
|
18
|
+
default: return false;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
function encodePathSegment(value) {
|
|
22
|
+
return encodeURIComponent(value);
|
|
23
|
+
}
|
|
24
|
+
function decodePathSegment(value) {
|
|
25
|
+
return decodeURIComponent(value);
|
|
26
|
+
}
|
|
27
|
+
function encodeFileNameSegment(value) {
|
|
28
|
+
return encodePathSegment(value).replaceAll(".", "%2E");
|
|
29
|
+
}
|
|
30
|
+
function joinPath(...segments) {
|
|
31
|
+
return segments.filter((segment) => segment.length > 0).join("/");
|
|
32
|
+
}
|
|
33
|
+
function getRecordKindAlias(kind) {
|
|
34
|
+
switch (kind) {
|
|
35
|
+
case "payload": return "p";
|
|
36
|
+
case "raw": return "r";
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function parseRecordKindAlias(value) {
|
|
40
|
+
switch (value) {
|
|
41
|
+
case "p": return "payload";
|
|
42
|
+
case "r": return "raw";
|
|
43
|
+
default: return null;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
function getEntryToken(scope, userKey) {
|
|
47
|
+
if (scope.kind === "document" && userKey === "d") return OPFS_SINGLETON_ENTRY_TOKEN;
|
|
48
|
+
return encodeFileNameSegment(userKey);
|
|
49
|
+
}
|
|
50
|
+
function parseEntryToken(scopeKind, token) {
|
|
51
|
+
if (scopeKind === "document" && token === OPFS_SINGLETON_ENTRY_TOKEN) return "d";
|
|
52
|
+
return decodePathSegment(token);
|
|
53
|
+
}
|
|
54
|
+
function parseRecordKey(key) {
|
|
55
|
+
return parseAsyncStorageRecordKey(key);
|
|
56
|
+
}
|
|
57
|
+
function toRecordKey(recordKind, userKey) {
|
|
58
|
+
switch (recordKind) {
|
|
59
|
+
case "payload": return getPayloadRecordKey(userKey);
|
|
60
|
+
case "raw": return userKey;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
function buildFileName(scope, key) {
|
|
64
|
+
const parsedRecordKey = parseRecordKey(key);
|
|
65
|
+
const kindAlias = encodePersistedAsyncNamespaceKind(scope.kind);
|
|
66
|
+
if (parsedRecordKey.recordKind === "raw") return [
|
|
67
|
+
kindAlias,
|
|
68
|
+
encodeFileNameSegment(parsedRecordKey.rawKey),
|
|
69
|
+
getRecordKindAlias("raw")
|
|
70
|
+
].join(".") + JSON_FILE_EXTENSION;
|
|
71
|
+
return [
|
|
72
|
+
kindAlias,
|
|
73
|
+
shouldHashPayloadRecordForKind(scope.kind) ? `${HASHED_PAYLOAD_ENTRY_TOKEN_PREFIX}${murmur3(key, "uint32")}` : getEntryToken(scope, parsedRecordKey.userKey),
|
|
74
|
+
getRecordKindAlias("payload")
|
|
75
|
+
].join(".") + JSON_FILE_EXTENSION;
|
|
76
|
+
}
|
|
77
|
+
function resolveHashedPayloadRecordKeyFromValue(scope, value) {
|
|
78
|
+
if (!shouldHashPayloadRecordForKind(scope.kind)) return null;
|
|
79
|
+
if (!isObject(value)) return null;
|
|
80
|
+
const record = value;
|
|
81
|
+
if (!("p" in record)) return null;
|
|
82
|
+
switch (scope.kind) {
|
|
83
|
+
case "collection.item":
|
|
84
|
+
case "listQuery.item":
|
|
85
|
+
case "listQuery.query": return getPayloadRecordKey(getCompositeKey(record.p));
|
|
86
|
+
default: return null;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
function parseFileNameInfo(fileName) {
|
|
90
|
+
if (!fileName.endsWith(JSON_FILE_EXTENSION)) return null;
|
|
91
|
+
const parts = fileName.slice(0, -5).split(".");
|
|
92
|
+
if (parts.length !== 3) return null;
|
|
93
|
+
const kindPart = parts[0] ?? "";
|
|
94
|
+
const entryPart = parts[1] ?? "";
|
|
95
|
+
const recordPart = parts[2] ?? "";
|
|
96
|
+
const kind = parsePersistedAsyncNamespaceKind(kindPart);
|
|
97
|
+
if (kind === null) return null;
|
|
98
|
+
const recordKind = parseRecordKindAlias(recordPart);
|
|
99
|
+
if (recordKind === null) return null;
|
|
100
|
+
if (recordKind === "payload" && shouldHashPayloadRecordForKind(kind) && entryPart.startsWith(HASHED_PAYLOAD_ENTRY_TOKEN_PREFIX)) return {
|
|
101
|
+
kind,
|
|
102
|
+
key: null,
|
|
103
|
+
isHashedPayload: true
|
|
104
|
+
};
|
|
105
|
+
return {
|
|
106
|
+
kind,
|
|
107
|
+
key: recordKind === "raw" ? decodePathSegment(entryPart) : toRecordKey(recordKind, parseEntryToken(kind, entryPart)),
|
|
108
|
+
isHashedPayload: false
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
//#endregion
|
|
112
|
+
//#region src/persistentStorage/opfsHelpers.ts
|
|
113
|
+
async function getNavigatorStorageDirectory() {
|
|
114
|
+
const storage = asPossiblyUndefined(globalThis.navigator)?.storage;
|
|
115
|
+
if (storage?.getDirectory === void 0) throw new Error("[TSDF] OPFS is unavailable in this environment.");
|
|
116
|
+
return storage.getDirectory();
|
|
117
|
+
}
|
|
118
|
+
async function getDirectoryHandleIfExists(parent, name) {
|
|
119
|
+
try {
|
|
120
|
+
return await parent.getDirectoryHandle(name);
|
|
121
|
+
} catch {
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
async function getFileHandleIfExists(parent, name) {
|
|
126
|
+
try {
|
|
127
|
+
return await parent.getFileHandle(name);
|
|
128
|
+
} catch {
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
//#endregion
|
|
133
|
+
//#region src/persistentStorage/opfsAsyncStorageAdapter.ts
|
|
134
|
+
const OPFS_DIR_HANDLE_CACHE_MAX_SIZE = 500;
|
|
135
|
+
const OPFS_FILE_HANDLE_CACHE_MAX_SIZE = 1e4;
|
|
136
|
+
var OpfsAsyncStorageDriver = class {
|
|
137
|
+
#rootDirPromise = null;
|
|
138
|
+
#mainCacheContext = {
|
|
139
|
+
cleanupKnowledge: null,
|
|
140
|
+
dirCache: createCache({ maxCacheSize: OPFS_DIR_HANDLE_CACHE_MAX_SIZE }),
|
|
141
|
+
fileCache: createCache({ maxCacheSize: OPFS_FILE_HANDLE_CACHE_MAX_SIZE }),
|
|
142
|
+
readValueCache: null
|
|
143
|
+
};
|
|
144
|
+
async get(scope, key) {
|
|
145
|
+
const [value] = await this.getMany(scope, [key]);
|
|
146
|
+
return value ?? null;
|
|
147
|
+
}
|
|
148
|
+
async set(scope, key, value) {
|
|
149
|
+
await this.setMany(scope, [{
|
|
150
|
+
key,
|
|
151
|
+
value
|
|
152
|
+
}]);
|
|
153
|
+
}
|
|
154
|
+
async remove(scope, key) {
|
|
155
|
+
await this.removeMany(scope, [key]);
|
|
156
|
+
}
|
|
157
|
+
async getMany(scope, keys) {
|
|
158
|
+
return this.#getManyWithContext(scope, keys, this.#mainCacheContext);
|
|
159
|
+
}
|
|
160
|
+
async setMany(scope, entries) {
|
|
161
|
+
await this.#setManyWithContext(scope, entries, this.#mainCacheContext);
|
|
162
|
+
}
|
|
163
|
+
async removeMany(scope, keys) {
|
|
164
|
+
await this.#removeManyWithContext(scope, keys, this.#mainCacheContext);
|
|
165
|
+
}
|
|
166
|
+
async listKeys(scope) {
|
|
167
|
+
return this.#listKeysWithContext(scope, this.#mainCacheContext);
|
|
168
|
+
}
|
|
169
|
+
async clear(scope) {
|
|
170
|
+
await this.#clearWithContext(scope, this.#mainCacheContext);
|
|
171
|
+
}
|
|
172
|
+
async listScopes(sessionKey) {
|
|
173
|
+
return this.#listScopesWithContext(sessionKey, this.#mainCacheContext);
|
|
174
|
+
}
|
|
175
|
+
async listScopesWithKnownRecordKeys(sessionKey) {
|
|
176
|
+
return this.#listDiscoveredScopesWithContext(sessionKey, this.#mainCacheContext);
|
|
177
|
+
}
|
|
178
|
+
async withIsolatedCleanupDriver(callback) {
|
|
179
|
+
const cleanupCacheContext = {
|
|
180
|
+
cleanupKnowledge: {
|
|
181
|
+
knownRemainingEntryCountByStorePath: /* @__PURE__ */ new Map(),
|
|
182
|
+
knownStorePathsBySessionPath: /* @__PURE__ */ new Map()
|
|
183
|
+
},
|
|
184
|
+
dirCache: this.#mainCacheContext.dirCache.clone(),
|
|
185
|
+
fileCache: this.#mainCacheContext.fileCache.clone(),
|
|
186
|
+
readValueCache: /* @__PURE__ */ new Map()
|
|
187
|
+
};
|
|
188
|
+
return callback(this.#createScopedDriver(cleanupCacheContext));
|
|
189
|
+
}
|
|
190
|
+
async cleanupRemoveKnownRecords(scope, keys) {
|
|
191
|
+
return this.#cleanupRemoveKnownRecordsWithContext(scope, keys, this.#mainCacheContext);
|
|
192
|
+
}
|
|
193
|
+
async cleanupRemoveKnownStoreDir(scope, keys) {
|
|
194
|
+
return this.#cleanupRemoveKnownStoreDirWithContext(scope, keys, this.#mainCacheContext);
|
|
195
|
+
}
|
|
196
|
+
async cleanupRemoveKnownSessionDir(sessionKey) {
|
|
197
|
+
return this.#cleanupRemoveKnownSessionDirWithContext(sessionKey, this.#mainCacheContext);
|
|
198
|
+
}
|
|
199
|
+
cleanupFinalizeRemovedRecords(scope, removedKeys) {
|
|
200
|
+
this.#cleanupFinalizeRemovedRecordsWithContext(scope, removedKeys, this.#mainCacheContext);
|
|
201
|
+
}
|
|
202
|
+
cleanupFinalizeRemovedStoreDir(scope, removedKeys) {
|
|
203
|
+
this.#cleanupFinalizeRemovedStoreDirWithContext(scope, removedKeys, this.#mainCacheContext);
|
|
204
|
+
}
|
|
205
|
+
cleanupFinalizeRemovedSessionDir(sessionKey) {
|
|
206
|
+
this.#cleanupFinalizeRemovedSessionDirWithContext(sessionKey, this.#mainCacheContext);
|
|
207
|
+
}
|
|
208
|
+
__resetForTests() {
|
|
209
|
+
this.#rootDirPromise = null;
|
|
210
|
+
this.#mainCacheContext.dirCache.clear();
|
|
211
|
+
this.#mainCacheContext.fileCache.clear();
|
|
212
|
+
}
|
|
213
|
+
#createScopedDriver(cacheContext) {
|
|
214
|
+
return {
|
|
215
|
+
cleanupFinalizeRemovedRecords: (scope, removedKeys) => this.#cleanupFinalizeRemovedRecordsWithContext(scope, removedKeys, cacheContext),
|
|
216
|
+
cleanupFinalizeRemovedSessionDir: (sessionKey) => this.#cleanupFinalizeRemovedSessionDirWithContext(sessionKey, cacheContext),
|
|
217
|
+
cleanupFinalizeRemovedStoreDir: (scope, removedKeys) => this.#cleanupFinalizeRemovedStoreDirWithContext(scope, removedKeys, cacheContext),
|
|
218
|
+
cleanupRemoveKnownRecords: (scope, keys) => this.#cleanupRemoveKnownRecordsWithContext(scope, keys, cacheContext),
|
|
219
|
+
cleanupRemoveKnownSessionDir: (sessionKey) => this.#cleanupRemoveKnownSessionDirWithContext(sessionKey, cacheContext),
|
|
220
|
+
cleanupRemoveKnownStoreDir: (scope, keys) => this.#cleanupRemoveKnownStoreDirWithContext(scope, keys, cacheContext),
|
|
221
|
+
get: (scope, key) => this.#getManyWithContext(scope, [key], cacheContext).then(([value]) => value ?? null),
|
|
222
|
+
set: (scope, key, value) => this.#setManyWithContext(scope, [{
|
|
223
|
+
key,
|
|
224
|
+
value
|
|
225
|
+
}], cacheContext),
|
|
226
|
+
remove: (scope, key) => this.#removeManyWithContext(scope, [key], cacheContext),
|
|
227
|
+
listKeys: (scope) => this.#listKeysWithContext(scope, cacheContext),
|
|
228
|
+
clear: (scope) => this.#clearWithContext(scope, cacheContext),
|
|
229
|
+
listScopes: (currentSessionKey) => this.#listScopesWithContext(currentSessionKey, cacheContext),
|
|
230
|
+
listScopesWithKnownRecordKeys: (currentSessionKey) => this.#listDiscoveredScopesWithContext(currentSessionKey, cacheContext),
|
|
231
|
+
getMany: (scope, keys) => this.#getManyWithContext(scope, keys, cacheContext),
|
|
232
|
+
setMany: (scope, entries) => this.#setManyWithContext(scope, entries, cacheContext),
|
|
233
|
+
removeMany: (scope, keys) => this.#removeManyWithContext(scope, keys, cacheContext)
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
async #getManyWithContext(scope, keys, cacheContext) {
|
|
237
|
+
if (keys.length === 0) return [];
|
|
238
|
+
const storeDir = await this.#getStoreDir(scope, {
|
|
239
|
+
create: false,
|
|
240
|
+
cacheContext
|
|
241
|
+
});
|
|
242
|
+
if (storeDir === null) return keys.map(() => null);
|
|
243
|
+
return Promise.all(keys.map(async (key) => {
|
|
244
|
+
const fileHandle = await this.#getFileHandle(scope, key, {
|
|
245
|
+
create: false,
|
|
246
|
+
cacheContext,
|
|
247
|
+
storeDir
|
|
248
|
+
});
|
|
249
|
+
if (fileHandle === null) return null;
|
|
250
|
+
return this.#readJsonFile(this.#getFilePath(scope, key), fileHandle, cacheContext);
|
|
251
|
+
}));
|
|
252
|
+
}
|
|
253
|
+
async #setManyWithContext(scope, entries, cacheContext) {
|
|
254
|
+
if (entries.length === 0) return;
|
|
255
|
+
await Promise.all(entries.map((entry) => this.#writeEntryWithRetry(scope, entry, cacheContext)));
|
|
256
|
+
}
|
|
257
|
+
async #removeManyWithContext(scope, keys, cacheContext) {
|
|
258
|
+
const uniqueKeys = [...new Set(keys)];
|
|
259
|
+
if (uniqueKeys.length === 0) return;
|
|
260
|
+
const storeDir = await this.#getStoreDir(scope, {
|
|
261
|
+
create: false,
|
|
262
|
+
cacheContext
|
|
263
|
+
});
|
|
264
|
+
if (storeDir === null) return;
|
|
265
|
+
if (await this.#removeWholeStoreDirIfAllKnownEntriesAreBeingRemoved(scope, uniqueKeys, cacheContext)) return;
|
|
266
|
+
const removeResults = await Promise.all(uniqueKeys.map(async (key) => {
|
|
267
|
+
const fileName = buildFileName(scope, key);
|
|
268
|
+
try {
|
|
269
|
+
await storeDir.removeEntry(fileName);
|
|
270
|
+
return true;
|
|
271
|
+
} catch {
|
|
272
|
+
return false;
|
|
273
|
+
} finally {
|
|
274
|
+
this.#invalidateFileHandle(scope, key, cacheContext);
|
|
275
|
+
}
|
|
276
|
+
}));
|
|
277
|
+
this.#decrementKnownStoreEntryCount(this.#getStoreDirPath(scope), cacheContext, removeResults.filter(Boolean).length);
|
|
278
|
+
if (!this.#shouldPruneStoreDirAfterRemovingKeys(uniqueKeys, cacheContext)) return;
|
|
279
|
+
await this.#pruneEmptyDirectories(scope, cacheContext);
|
|
280
|
+
}
|
|
281
|
+
async #listKeysWithContext(scope, cacheContext) {
|
|
282
|
+
const { resolvedEntries } = await this.#listScopedFiles(scope, cacheContext);
|
|
283
|
+
return resolvedEntries.map((entry) => entry.key).sort((left, right) => left.localeCompare(right));
|
|
284
|
+
}
|
|
285
|
+
async #clearWithContext(scope, cacheContext) {
|
|
286
|
+
const storeDir = await this.#getStoreDir(scope, {
|
|
287
|
+
create: false,
|
|
288
|
+
cacheContext
|
|
289
|
+
});
|
|
290
|
+
if (storeDir === null) return;
|
|
291
|
+
const { allFileNames, resolvedEntries } = await this.#listScopedFiles(scope, cacheContext);
|
|
292
|
+
const keys = resolvedEntries.map((entry) => entry.key);
|
|
293
|
+
if (allFileNames.length === keys.length && await this.#removeWholeStoreDirIfAllKnownEntriesAreBeingRemoved(scope, keys, cacheContext)) return;
|
|
294
|
+
const storeDirPath = this.#getStoreDirPath(scope);
|
|
295
|
+
const removeResults = await Promise.all(allFileNames.map(async (fileName) => {
|
|
296
|
+
try {
|
|
297
|
+
await storeDir.removeEntry(fileName);
|
|
298
|
+
return true;
|
|
299
|
+
} catch {
|
|
300
|
+
return false;
|
|
301
|
+
} finally {
|
|
302
|
+
this.#invalidateFilePath(joinPath(storeDirPath, fileName), cacheContext);
|
|
303
|
+
}
|
|
304
|
+
}));
|
|
305
|
+
this.#decrementKnownStoreEntryCount(storeDirPath, cacheContext, removeResults.filter(Boolean).length);
|
|
306
|
+
await this.#pruneEmptyDirectories(scope, cacheContext);
|
|
307
|
+
}
|
|
308
|
+
async #listScopesWithContext(sessionKey, cacheContext) {
|
|
309
|
+
return (await this.#listDiscoveredScopesWithContext(sessionKey, cacheContext)).map(({ scope }) => scope);
|
|
310
|
+
}
|
|
311
|
+
async #listDiscoveredScopesWithContext(sessionKey, cacheContext) {
|
|
312
|
+
const sessionEntries = await this.#getSessionEntries(sessionKey, cacheContext);
|
|
313
|
+
const discoveredScopes = /* @__PURE__ */ new Map();
|
|
314
|
+
for (const sessionEntry of sessionEntries) {
|
|
315
|
+
if (sessionEntry.handle === null) continue;
|
|
316
|
+
const sessionDir = sessionEntry.handle;
|
|
317
|
+
this.#setKnownSessionStorePaths(sessionEntry.path, cacheContext, /* @__PURE__ */ new Set());
|
|
318
|
+
const decodedSessionKey = decodePathSegment(sessionEntry.name);
|
|
319
|
+
const storeEntries = await this.#listDirectoryEntries(sessionDir, sessionEntry.path, cacheContext);
|
|
320
|
+
const storeScanResults = await Promise.all(storeEntries.map(async (storeEntry) => {
|
|
321
|
+
const decodedStoreName = decodePathSegment(storeEntry.name);
|
|
322
|
+
return {
|
|
323
|
+
...await this.#scanStoreScopeEntries(storeEntry.handle, decodedSessionKey, decodedStoreName, cacheContext),
|
|
324
|
+
storeEntry
|
|
325
|
+
};
|
|
326
|
+
}));
|
|
327
|
+
const knownNonEmptyStorePaths = /* @__PURE__ */ new Set();
|
|
328
|
+
for (const { storeEntry, invalidEntryNames, knownEntryCount, scopeEntries } of storeScanResults) {
|
|
329
|
+
await this.#removeInvalidEntries(storeEntry.handle, invalidEntryNames, cacheContext);
|
|
330
|
+
if (knownEntryCount > 0) knownNonEmptyStorePaths.add(storeEntry.path);
|
|
331
|
+
else if (cacheContext.cleanupKnowledge !== null && invalidEntryNames.length > 0) try {
|
|
332
|
+
await sessionDir.removeEntry(storeEntry.name, { recursive: true });
|
|
333
|
+
this.#invalidateDirectory(storeEntry.path, cacheContext);
|
|
334
|
+
} catch {}
|
|
335
|
+
cacheContext.cleanupKnowledge?.knownRemainingEntryCountByStorePath.set(storeEntry.path, knownEntryCount);
|
|
336
|
+
for (const scopeEntry of scopeEntries) {
|
|
337
|
+
const scopeId = getNamespaceId(scopeEntry.scope);
|
|
338
|
+
const existing = discoveredScopes.get(scopeId);
|
|
339
|
+
if (existing !== void 0) {
|
|
340
|
+
existing.knownRecordKeys.push(...scopeEntry.knownRecordKeys);
|
|
341
|
+
continue;
|
|
342
|
+
}
|
|
343
|
+
discoveredScopes.set(scopeId, scopeEntry);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
this.#setKnownSessionStorePaths(sessionEntry.path, cacheContext, knownNonEmptyStorePaths);
|
|
347
|
+
if (cacheContext.cleanupKnowledge !== null && knownNonEmptyStorePaths.size === 0) try {
|
|
348
|
+
await (await this.#getRootDir()).removeEntry(sessionEntry.name, { recursive: true });
|
|
349
|
+
this.#invalidateDirectory(sessionEntry.path, cacheContext);
|
|
350
|
+
this.#clearKnownSessionStores(sessionEntry.path, cacheContext);
|
|
351
|
+
} catch {}
|
|
352
|
+
}
|
|
353
|
+
const results = [...discoveredScopes.values()];
|
|
354
|
+
for (const entry of results) entry.knownRecordKeys.sort((left, right) => left.localeCompare(right));
|
|
355
|
+
return results.sort((left, right) => {
|
|
356
|
+
const sessionComparison = left.scope.sessionKey.localeCompare(right.scope.sessionKey);
|
|
357
|
+
if (sessionComparison !== 0) return sessionComparison;
|
|
358
|
+
const storeComparison = left.scope.storeName.localeCompare(right.scope.storeName);
|
|
359
|
+
if (storeComparison !== 0) return storeComparison;
|
|
360
|
+
return left.scope.kind.localeCompare(right.scope.kind);
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
async #scanStoreScopeEntries(storeDir, sessionKey, storeName, cacheContext) {
|
|
364
|
+
const invalidEntryNames = [];
|
|
365
|
+
const storeDirPath = this.#getStoreDirPath({
|
|
366
|
+
sessionKey,
|
|
367
|
+
storeName
|
|
368
|
+
});
|
|
369
|
+
const filesByKind = /* @__PURE__ */ new Map();
|
|
370
|
+
for await (const [fileName, entryHandle] of storeDir.entries()) {
|
|
371
|
+
if (entryHandle.kind !== "file") {
|
|
372
|
+
invalidEntryNames.push(fileName);
|
|
373
|
+
continue;
|
|
374
|
+
}
|
|
375
|
+
const parsed = parseFileNameInfo(fileName);
|
|
376
|
+
if (parsed === null) {
|
|
377
|
+
invalidEntryNames.push(fileName);
|
|
378
|
+
continue;
|
|
379
|
+
}
|
|
380
|
+
const fileHandle = __LEGIT_CAST__(entryHandle);
|
|
381
|
+
const nextEntries = filesByKind.get(parsed.kind) ?? [];
|
|
382
|
+
nextEntries.push({
|
|
383
|
+
fileHandle,
|
|
384
|
+
fileName,
|
|
385
|
+
filePath: joinPath(storeDirPath, fileName),
|
|
386
|
+
isHashedPayload: parsed.isHashedPayload,
|
|
387
|
+
key: parsed.key
|
|
388
|
+
});
|
|
389
|
+
filesByKind.set(parsed.kind, nextEntries);
|
|
390
|
+
}
|
|
391
|
+
const scopeEntries = [];
|
|
392
|
+
let knownEntryCount = 0;
|
|
393
|
+
for (const [kind, files] of filesByKind) {
|
|
394
|
+
const scope = {
|
|
395
|
+
sessionKey,
|
|
396
|
+
storeName,
|
|
397
|
+
kind
|
|
398
|
+
};
|
|
399
|
+
const knownRecordKeys = [];
|
|
400
|
+
const hashedPayloadFiles = files.filter((file) => file.isHashedPayload);
|
|
401
|
+
let scopeKnownEntryCount = 0;
|
|
402
|
+
let indexFilePath = null;
|
|
403
|
+
let indexHandle = null;
|
|
404
|
+
for (const file of files) {
|
|
405
|
+
if (file.isHashedPayload || file.key === null) continue;
|
|
406
|
+
knownRecordKeys.push(file.key);
|
|
407
|
+
scopeKnownEntryCount += 1;
|
|
408
|
+
if (file.key === "_i") {
|
|
409
|
+
indexHandle = file.fileHandle;
|
|
410
|
+
indexFilePath = file.filePath;
|
|
411
|
+
cacheContext.fileCache.set(this.#getFilePath(scope, file.key), file.fileHandle);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
if (hashedPayloadFiles.length > 0) {
|
|
415
|
+
const resolvedHashedFiles = await this.#resolveHashedScopedFiles(scope, hashedPayloadFiles, indexHandle, indexFilePath, cacheContext);
|
|
416
|
+
knownRecordKeys.push(...resolvedHashedFiles.resolvedEntries.map((entry) => entry.key));
|
|
417
|
+
scopeKnownEntryCount += resolvedHashedFiles.resolvedEntries.length;
|
|
418
|
+
invalidEntryNames.push(...resolvedHashedFiles.invalidEntryNames);
|
|
419
|
+
}
|
|
420
|
+
if (scopeKnownEntryCount === 0) continue;
|
|
421
|
+
knownEntryCount += scopeKnownEntryCount;
|
|
422
|
+
knownRecordKeys.sort((left, right) => left.localeCompare(right));
|
|
423
|
+
scopeEntries.push({
|
|
424
|
+
knownRecordKeys,
|
|
425
|
+
scope
|
|
426
|
+
});
|
|
427
|
+
}
|
|
428
|
+
return {
|
|
429
|
+
invalidEntryNames,
|
|
430
|
+
knownEntryCount,
|
|
431
|
+
scopeEntries
|
|
432
|
+
};
|
|
433
|
+
}
|
|
434
|
+
async #readHashedPayloadRecordKeyFromHandle(scope, filePath, fileName, fileHandle, cacheContext) {
|
|
435
|
+
const value = await this.#readJsonFile(filePath, fileHandle, cacheContext);
|
|
436
|
+
const resolvedKey = value === null ? null : resolveHashedPayloadRecordKeyFromValue(scope, value);
|
|
437
|
+
return resolvedKey !== null && buildFileName(scope, resolvedKey) === fileName ? resolvedKey : null;
|
|
438
|
+
}
|
|
439
|
+
async #readIndexedHashedPayloadRecordKeys(filePath, indexHandle, cacheContext) {
|
|
440
|
+
if (indexHandle === null || filePath === null) return null;
|
|
441
|
+
const value = await this.#readJsonFile(filePath, indexHandle, cacheContext);
|
|
442
|
+
if (!isObject(value)) return null;
|
|
443
|
+
const rawEntries = value.e;
|
|
444
|
+
if (typeof rawEntries !== "object" || rawEntries === null || Array.isArray(rawEntries)) return null;
|
|
445
|
+
return Object.keys(rawEntries).map((key) => getPayloadRecordKey(key));
|
|
446
|
+
}
|
|
447
|
+
async #getSessionEntries(sessionKey, cacheContext) {
|
|
448
|
+
const root = await this.#getRootDir();
|
|
449
|
+
return sessionKey === void 0 ? await this.#listDirectoryEntries(root, OPFS_ROOT_DIR, cacheContext) : [{
|
|
450
|
+
handle: await this.#getSessionDir(sessionKey, {
|
|
451
|
+
create: false,
|
|
452
|
+
cacheContext
|
|
453
|
+
}),
|
|
454
|
+
name: encodePathSegment(sessionKey),
|
|
455
|
+
path: this.#getSessionDirPath(sessionKey)
|
|
456
|
+
}];
|
|
457
|
+
}
|
|
458
|
+
async #getRootDir() {
|
|
459
|
+
if (this.#rootDirPromise === null) this.#rootDirPromise = (async () => {
|
|
460
|
+
return (await getNavigatorStorageDirectory()).getDirectoryHandle(OPFS_ROOT_DIR, { create: true });
|
|
461
|
+
})();
|
|
462
|
+
return this.#rootDirPromise;
|
|
463
|
+
}
|
|
464
|
+
#getSessionDirPath(sessionKey) {
|
|
465
|
+
return joinPath(OPFS_ROOT_DIR, encodePathSegment(sessionKey));
|
|
466
|
+
}
|
|
467
|
+
#getStoreDirPath(scope) {
|
|
468
|
+
return joinPath(this.#getSessionDirPath(scope.sessionKey), encodePathSegment(scope.storeName));
|
|
469
|
+
}
|
|
470
|
+
#getFilePath(scope, key) {
|
|
471
|
+
return joinPath(this.#getStoreDirPath(scope), buildFileName(scope, key));
|
|
472
|
+
}
|
|
473
|
+
async #getSessionDir(sessionKey, options) {
|
|
474
|
+
const root = await this.#getRootDir();
|
|
475
|
+
const sessionDirPath = this.#getSessionDirPath(sessionKey);
|
|
476
|
+
const encodedSessionKey = encodePathSegment(sessionKey);
|
|
477
|
+
if (options.create) {
|
|
478
|
+
const cachedSessionDir = await options.cacheContext.dirCache.getAsync(sessionDirPath);
|
|
479
|
+
if (cachedSessionDir !== void 0) return cachedSessionDir;
|
|
480
|
+
return options.cacheContext.dirCache.getOrInsertAsync(sessionDirPath, async () => root.getDirectoryHandle(encodedSessionKey, { create: true }));
|
|
481
|
+
}
|
|
482
|
+
const cachedSessionDir = await options.cacheContext.dirCache.getAsync(sessionDirPath);
|
|
483
|
+
if (cachedSessionDir !== void 0) return cachedSessionDir;
|
|
484
|
+
return options.cacheContext.dirCache.getOrInsertAsync(sessionDirPath, async () => getDirectoryHandleIfExists(root, encodedSessionKey), { skipCachingWhen: (dir) => dir === null });
|
|
485
|
+
}
|
|
486
|
+
async #getStoreDir(scope, options) {
|
|
487
|
+
const sessionDir = await this.#getSessionDir(scope.sessionKey, options);
|
|
488
|
+
if (sessionDir === null) return null;
|
|
489
|
+
const storeDirPath = this.#getStoreDirPath(scope);
|
|
490
|
+
const encodedStoreName = encodePathSegment(scope.storeName);
|
|
491
|
+
if (options.create) {
|
|
492
|
+
const cachedStoreDir = await options.cacheContext.dirCache.getAsync(storeDirPath);
|
|
493
|
+
if (cachedStoreDir !== void 0) return cachedStoreDir;
|
|
494
|
+
return options.cacheContext.dirCache.getOrInsertAsync(storeDirPath, async () => sessionDir.getDirectoryHandle(encodedStoreName, { create: true }));
|
|
495
|
+
}
|
|
496
|
+
const cachedStoreDir = await options.cacheContext.dirCache.getAsync(storeDirPath);
|
|
497
|
+
if (cachedStoreDir !== void 0) return cachedStoreDir;
|
|
498
|
+
return options.cacheContext.dirCache.getOrInsertAsync(storeDirPath, async () => getDirectoryHandleIfExists(sessionDir, encodedStoreName), { skipCachingWhen: (dir) => dir === null });
|
|
499
|
+
}
|
|
500
|
+
async #getFileHandle(scope, key, options) {
|
|
501
|
+
const fileName = buildFileName(scope, key);
|
|
502
|
+
const filePath = joinPath(this.#getStoreDirPath(scope), fileName);
|
|
503
|
+
if (options.create) {
|
|
504
|
+
const cachedFileHandle = await options.cacheContext.fileCache.getAsync(filePath);
|
|
505
|
+
if (cachedFileHandle !== void 0) return cachedFileHandle;
|
|
506
|
+
return options.cacheContext.fileCache.getOrInsertAsync(filePath, async () => options.storeDir.getFileHandle(fileName, { create: true }));
|
|
507
|
+
}
|
|
508
|
+
const cachedFileHandle = await options.cacheContext.fileCache.getAsync(filePath);
|
|
509
|
+
if (cachedFileHandle !== void 0) return cachedFileHandle;
|
|
510
|
+
return options.cacheContext.fileCache.getOrInsertAsync(filePath, async () => getFileHandleIfExists(options.storeDir, fileName), { skipCachingWhen: (fileHandle) => fileHandle === null });
|
|
511
|
+
}
|
|
512
|
+
async #listScopedFiles(scope, cacheContext) {
|
|
513
|
+
const storeDir = await this.#getStoreDir(scope, {
|
|
514
|
+
create: false,
|
|
515
|
+
cacheContext
|
|
516
|
+
});
|
|
517
|
+
if (storeDir === null) return {
|
|
518
|
+
allFileNames: [],
|
|
519
|
+
resolvedEntries: []
|
|
520
|
+
};
|
|
521
|
+
const allFileNames = [];
|
|
522
|
+
const resolvedEntries = [];
|
|
523
|
+
const hashedFiles = [];
|
|
524
|
+
let indexFilePath = null;
|
|
525
|
+
let indexHandle = null;
|
|
526
|
+
const storeDirPath = this.#getStoreDirPath(scope);
|
|
527
|
+
for await (const [fileName, entryHandle] of storeDir.entries()) {
|
|
528
|
+
if (entryHandle.kind !== "file") continue;
|
|
529
|
+
const parsed = parseFileNameInfo(fileName);
|
|
530
|
+
if (parsed === null || parsed.kind !== scope.kind) continue;
|
|
531
|
+
allFileNames.push(fileName);
|
|
532
|
+
const filePath = joinPath(storeDirPath, fileName);
|
|
533
|
+
const fileHandle = __LEGIT_CAST__(entryHandle);
|
|
534
|
+
if (parsed.isHashedPayload) {
|
|
535
|
+
hashedFiles.push({
|
|
536
|
+
fileHandle,
|
|
537
|
+
fileName,
|
|
538
|
+
filePath,
|
|
539
|
+
isHashedPayload: true,
|
|
540
|
+
key: null
|
|
541
|
+
});
|
|
542
|
+
continue;
|
|
543
|
+
}
|
|
544
|
+
if (parsed.key !== null) {
|
|
545
|
+
resolvedEntries.push({
|
|
546
|
+
fileName,
|
|
547
|
+
key: parsed.key
|
|
548
|
+
});
|
|
549
|
+
if (parsed.key === "_i") {
|
|
550
|
+
indexHandle = fileHandle;
|
|
551
|
+
indexFilePath = filePath;
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
if (hashedFiles.length > 0) resolvedEntries.push(...(await this.#resolveHashedScopedFiles(scope, hashedFiles, indexHandle, indexFilePath, cacheContext)).resolvedEntries);
|
|
556
|
+
resolvedEntries.sort((left, right) => left.key.localeCompare(right.key));
|
|
557
|
+
return {
|
|
558
|
+
allFileNames,
|
|
559
|
+
resolvedEntries
|
|
560
|
+
};
|
|
561
|
+
}
|
|
562
|
+
async #resolveHashedScopedFiles(scope, hashedFiles, indexHandle, indexFilePath, cacheContext) {
|
|
563
|
+
const hashedFilesByName = new Map(hashedFiles.map((file) => [file.fileName, file]));
|
|
564
|
+
const resolvedEntries = [];
|
|
565
|
+
const invalidEntryNames = [];
|
|
566
|
+
const indexedPayloadRecordKeys = await this.#readIndexedHashedPayloadRecordKeys(indexFilePath, indexHandle, cacheContext);
|
|
567
|
+
if (indexedPayloadRecordKeys !== null) {
|
|
568
|
+
const matchedFileNames = /* @__PURE__ */ new Set();
|
|
569
|
+
const storeDirPath = this.#getStoreDirPath(scope);
|
|
570
|
+
for (const key of indexedPayloadRecordKeys) {
|
|
571
|
+
const expectedFileName = buildFileName(scope, key);
|
|
572
|
+
const matchedFile = hashedFilesByName.get(expectedFileName);
|
|
573
|
+
if (matchedFile === void 0) continue;
|
|
574
|
+
matchedFileNames.add(expectedFileName);
|
|
575
|
+
resolvedEntries.push({
|
|
576
|
+
fileName: matchedFile.fileName,
|
|
577
|
+
key
|
|
578
|
+
});
|
|
579
|
+
cacheContext.fileCache.set(joinPath(storeDirPath, expectedFileName), matchedFile.fileHandle);
|
|
580
|
+
}
|
|
581
|
+
invalidEntryNames.push(...hashedFiles.flatMap((file) => matchedFileNames.has(file.fileName) ? [] : [file.fileName]));
|
|
582
|
+
return {
|
|
583
|
+
invalidEntryNames,
|
|
584
|
+
resolvedEntries
|
|
585
|
+
};
|
|
586
|
+
}
|
|
587
|
+
const resolvedHashedEntries = await Promise.all(hashedFiles.map(async (file) => ({
|
|
588
|
+
file,
|
|
589
|
+
key: await this.#readHashedPayloadRecordKeyFromHandle(scope, file.filePath, file.fileName, file.fileHandle, cacheContext)
|
|
590
|
+
})));
|
|
591
|
+
for (const { file, key } of resolvedHashedEntries) {
|
|
592
|
+
if (key === null) {
|
|
593
|
+
invalidEntryNames.push(file.fileName);
|
|
594
|
+
continue;
|
|
595
|
+
}
|
|
596
|
+
resolvedEntries.push({
|
|
597
|
+
fileName: file.fileName,
|
|
598
|
+
key
|
|
599
|
+
});
|
|
600
|
+
cacheContext.fileCache.set(file.filePath, file.fileHandle);
|
|
601
|
+
}
|
|
602
|
+
return {
|
|
603
|
+
invalidEntryNames,
|
|
604
|
+
resolvedEntries
|
|
605
|
+
};
|
|
606
|
+
}
|
|
607
|
+
async #readJsonFile(filePath, fileHandle, cacheContext) {
|
|
608
|
+
const valueCache = cacheContext.readValueCache;
|
|
609
|
+
if (valueCache?.has(filePath) === true) return valueCache.get(filePath) ?? null;
|
|
610
|
+
try {
|
|
611
|
+
const file = await fileHandle.getFile();
|
|
612
|
+
const parsed = JSON.parse(await file.text());
|
|
613
|
+
valueCache?.set(filePath, parsed);
|
|
614
|
+
return parsed;
|
|
615
|
+
} catch {
|
|
616
|
+
valueCache?.set(filePath, null);
|
|
617
|
+
return null;
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
async #writeEntryWithRetry(scope, entry, cacheContext) {
|
|
621
|
+
let firstError;
|
|
622
|
+
for (let attempt = 0; attempt < 2; attempt += 1) try {
|
|
623
|
+
const storeDir = await this.#getStoreDir(scope, {
|
|
624
|
+
create: true,
|
|
625
|
+
cacheContext
|
|
626
|
+
});
|
|
627
|
+
if (storeDir === null) throw new Error("[TSDF] Failed to open OPFS store directory.");
|
|
628
|
+
const fileHandle = await this.#getFileHandle(scope, entry.key, {
|
|
629
|
+
create: true,
|
|
630
|
+
cacheContext,
|
|
631
|
+
storeDir
|
|
632
|
+
});
|
|
633
|
+
if (fileHandle === null) throw new Error("[TSDF] Failed to open OPFS file handle.");
|
|
634
|
+
const writable = await fileHandle.createWritable();
|
|
635
|
+
const rawValue = entry.serializedValue ?? JSON.stringify(entry.value);
|
|
636
|
+
try {
|
|
637
|
+
await writable.write(rawValue);
|
|
638
|
+
} finally {
|
|
639
|
+
await writable.close();
|
|
640
|
+
}
|
|
641
|
+
cacheContext.readValueCache?.set(this.#getFilePath(scope, entry.key), entry.value);
|
|
642
|
+
return;
|
|
643
|
+
} catch (error) {
|
|
644
|
+
if (attempt > 0) throw firstError;
|
|
645
|
+
firstError = error;
|
|
646
|
+
this.#invalidateFileHandle(scope, entry.key, cacheContext);
|
|
647
|
+
this.#invalidateDirectory(this.#getStoreDirPath(scope), cacheContext);
|
|
648
|
+
this.#invalidateDirectory(this.#getSessionDirPath(scope.sessionKey), cacheContext);
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
async #cleanupRemoveKnownRecordsWithContext(scope, keys, cacheContext) {
|
|
652
|
+
if (keys.length === 0) return [];
|
|
653
|
+
const storeDir = await this.#getStoreDir(scope, {
|
|
654
|
+
create: false,
|
|
655
|
+
cacheContext
|
|
656
|
+
});
|
|
657
|
+
if (storeDir === null) return [];
|
|
658
|
+
return (await Promise.all(keys.map(async (key) => {
|
|
659
|
+
const fileName = buildFileName(scope, key);
|
|
660
|
+
try {
|
|
661
|
+
await storeDir.removeEntry(fileName);
|
|
662
|
+
return key;
|
|
663
|
+
} catch {
|
|
664
|
+
return null;
|
|
665
|
+
}
|
|
666
|
+
}))).flatMap((key) => key === null ? [] : [key]);
|
|
667
|
+
}
|
|
668
|
+
async #cleanupRemoveKnownStoreDirWithContext(scope, keys, cacheContext) {
|
|
669
|
+
const cleanupKnowledge = cacheContext.cleanupKnowledge;
|
|
670
|
+
if (cleanupKnowledge === null) return false;
|
|
671
|
+
const storeDirPath = this.#getStoreDirPath(scope);
|
|
672
|
+
const knownStoreEntryCount = cleanupKnowledge.knownRemainingEntryCountByStorePath.get(storeDirPath);
|
|
673
|
+
if (knownStoreEntryCount === void 0 || knownStoreEntryCount !== keys.length) return false;
|
|
674
|
+
const sessionDir = await this.#getSessionDir(scope.sessionKey, {
|
|
675
|
+
create: false,
|
|
676
|
+
cacheContext
|
|
677
|
+
});
|
|
678
|
+
if (sessionDir === null) return false;
|
|
679
|
+
try {
|
|
680
|
+
await sessionDir.removeEntry(encodePathSegment(scope.storeName), { recursive: true });
|
|
681
|
+
return true;
|
|
682
|
+
} catch {
|
|
683
|
+
return false;
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
async #cleanupRemoveKnownSessionDirWithContext(sessionKey, cacheContext) {
|
|
687
|
+
if (cacheContext.cleanupKnowledge === null) return false;
|
|
688
|
+
const sessionDirPath = this.#getSessionDirPath(sessionKey);
|
|
689
|
+
const knownStorePaths = cacheContext.cleanupKnowledge.knownStorePathsBySessionPath.get(sessionDirPath);
|
|
690
|
+
if (knownStorePaths !== void 0 && knownStorePaths.size > 0) return false;
|
|
691
|
+
const root = await this.#getRootDir();
|
|
692
|
+
try {
|
|
693
|
+
await root.removeEntry(encodePathSegment(sessionKey), { recursive: true });
|
|
694
|
+
return true;
|
|
695
|
+
} catch {
|
|
696
|
+
return false;
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
#cleanupFinalizeRemovedRecordsWithContext(scope, removedKeys, cacheContext) {
|
|
700
|
+
for (const key of removedKeys) this.#invalidateFileHandle(scope, key, cacheContext);
|
|
701
|
+
this.#decrementKnownStoreEntryCount(this.#getStoreDirPath(scope), cacheContext, removedKeys.length);
|
|
702
|
+
}
|
|
703
|
+
#cleanupFinalizeRemovedStoreDirWithContext(scope, removedKeys, cacheContext) {
|
|
704
|
+
for (const key of removedKeys) this.#invalidateFileHandle(scope, key, cacheContext);
|
|
705
|
+
const sessionDirPath = this.#getSessionDirPath(scope.sessionKey);
|
|
706
|
+
const storeDirPath = this.#getStoreDirPath(scope);
|
|
707
|
+
this.#invalidateDirectory(storeDirPath, cacheContext);
|
|
708
|
+
cacheContext.cleanupKnowledge?.knownStorePathsBySessionPath.get(sessionDirPath)?.delete(storeDirPath);
|
|
709
|
+
cacheContext.cleanupKnowledge?.knownRemainingEntryCountByStorePath.delete(storeDirPath);
|
|
710
|
+
}
|
|
711
|
+
#cleanupFinalizeRemovedSessionDirWithContext(sessionKey, cacheContext) {
|
|
712
|
+
const sessionDirPath = this.#getSessionDirPath(sessionKey);
|
|
713
|
+
this.#invalidateDirectory(sessionDirPath, cacheContext);
|
|
714
|
+
this.#clearKnownSessionStores(sessionDirPath, cacheContext);
|
|
715
|
+
}
|
|
716
|
+
#invalidateFileHandle(scope, key, cacheContext) {
|
|
717
|
+
const filePath = this.#getFilePath(scope, key);
|
|
718
|
+
this.#invalidateFilePath(filePath, cacheContext);
|
|
719
|
+
}
|
|
720
|
+
#invalidateFilePath(path, cacheContext) {
|
|
721
|
+
cacheContext.fileCache.delete(path);
|
|
722
|
+
cacheContext.readValueCache?.delete(path);
|
|
723
|
+
if (cacheContext !== this.#mainCacheContext) this.#mainCacheContext.fileCache.delete(path);
|
|
724
|
+
}
|
|
725
|
+
#invalidateDirectory(path, cacheContext) {
|
|
726
|
+
cacheContext.dirCache.delete(path);
|
|
727
|
+
if (cacheContext !== this.#mainCacheContext) this.#mainCacheContext.dirCache.delete(path);
|
|
728
|
+
}
|
|
729
|
+
#shouldPruneStoreDirAfterRemovingKeys(keys, cacheContext) {
|
|
730
|
+
if (cacheContext.cleanupKnowledge !== null) return true;
|
|
731
|
+
return keys.includes("_i");
|
|
732
|
+
}
|
|
733
|
+
async #removeWholeStoreDirIfAllKnownEntriesAreBeingRemoved(scope, keys, cacheContext) {
|
|
734
|
+
const cleanupKnowledge = cacheContext.cleanupKnowledge;
|
|
735
|
+
if (cleanupKnowledge === null) return false;
|
|
736
|
+
const storeDirPath = this.#getStoreDirPath(scope);
|
|
737
|
+
const knownStoreEntryCount = cleanupKnowledge.knownRemainingEntryCountByStorePath.get(storeDirPath);
|
|
738
|
+
if (knownStoreEntryCount === void 0 || knownStoreEntryCount !== keys.length) return false;
|
|
739
|
+
const sessionDir = await this.#getSessionDir(scope.sessionKey, {
|
|
740
|
+
create: false,
|
|
741
|
+
cacheContext
|
|
742
|
+
});
|
|
743
|
+
if (sessionDir === null) return false;
|
|
744
|
+
for (const key of keys) this.#invalidateFileHandle(scope, key, cacheContext);
|
|
745
|
+
try {
|
|
746
|
+
await sessionDir.removeEntry(encodePathSegment(scope.storeName), { recursive: true });
|
|
747
|
+
} catch {
|
|
748
|
+
return false;
|
|
749
|
+
}
|
|
750
|
+
await this.#handleRemovedStoreDir(scope, cacheContext);
|
|
751
|
+
return true;
|
|
752
|
+
}
|
|
753
|
+
async #handleRemovedStoreDir(scope, cacheContext) {
|
|
754
|
+
const root = await this.#getRootDir();
|
|
755
|
+
const sessionDirPath = this.#getSessionDirPath(scope.sessionKey);
|
|
756
|
+
const storeDirPath = this.#getStoreDirPath(scope);
|
|
757
|
+
this.#invalidateDirectory(storeDirPath, cacheContext);
|
|
758
|
+
cacheContext.cleanupKnowledge?.knownStorePathsBySessionPath.get(sessionDirPath)?.delete(storeDirPath);
|
|
759
|
+
cacheContext.cleanupKnowledge?.knownRemainingEntryCountByStorePath.delete(storeDirPath);
|
|
760
|
+
const knownStorePaths = cacheContext.cleanupKnowledge?.knownStorePathsBySessionPath.get(sessionDirPath);
|
|
761
|
+
if (knownStorePaths !== void 0 && knownStorePaths.size > 0) return;
|
|
762
|
+
let removedSessionDir = false;
|
|
763
|
+
try {
|
|
764
|
+
await root.removeEntry(encodePathSegment(scope.sessionKey), { recursive: cacheContext.cleanupKnowledge !== null });
|
|
765
|
+
removedSessionDir = true;
|
|
766
|
+
} catch {} finally {
|
|
767
|
+
if (removedSessionDir) {
|
|
768
|
+
this.#invalidateDirectory(sessionDirPath, cacheContext);
|
|
769
|
+
this.#clearKnownSessionStores(sessionDirPath, cacheContext);
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
async #pruneEmptyDirectories(scope, cacheContext) {
|
|
774
|
+
const storeDirPath = this.#getStoreDirPath(scope);
|
|
775
|
+
const sessionDir = await this.#getSessionDir(scope.sessionKey, {
|
|
776
|
+
create: false,
|
|
777
|
+
cacheContext
|
|
778
|
+
});
|
|
779
|
+
if (sessionDir === null) return;
|
|
780
|
+
const knownStoreEntryCount = cacheContext.cleanupKnowledge?.knownRemainingEntryCountByStorePath.get(storeDirPath);
|
|
781
|
+
if (knownStoreEntryCount !== void 0 && knownStoreEntryCount > 0) return;
|
|
782
|
+
let removedStoreDir = false;
|
|
783
|
+
try {
|
|
784
|
+
await sessionDir.removeEntry(encodePathSegment(scope.storeName), { recursive: cacheContext.cleanupKnowledge !== null });
|
|
785
|
+
removedStoreDir = true;
|
|
786
|
+
} catch {}
|
|
787
|
+
if (!removedStoreDir) return;
|
|
788
|
+
await this.#handleRemovedStoreDir(scope, cacheContext);
|
|
789
|
+
}
|
|
790
|
+
#setKnownSessionStorePaths(sessionDirPath, cacheContext, storePaths) {
|
|
791
|
+
cacheContext.cleanupKnowledge?.knownStorePathsBySessionPath.set(sessionDirPath, storePaths);
|
|
792
|
+
}
|
|
793
|
+
#decrementKnownStoreEntryCount(storeDirPath, cacheContext, removedEntryCount) {
|
|
794
|
+
if (removedEntryCount === 0) return;
|
|
795
|
+
const cleanupKnowledge = cacheContext.cleanupKnowledge;
|
|
796
|
+
if (cleanupKnowledge === null) return;
|
|
797
|
+
const knownEntryCount = cleanupKnowledge.knownRemainingEntryCountByStorePath.get(storeDirPath);
|
|
798
|
+
if (knownEntryCount === void 0) return;
|
|
799
|
+
cleanupKnowledge.knownRemainingEntryCountByStorePath.set(storeDirPath, Math.max(0, knownEntryCount - removedEntryCount));
|
|
800
|
+
}
|
|
801
|
+
#clearKnownSessionStores(sessionDirPath, cacheContext) {
|
|
802
|
+
cacheContext.cleanupKnowledge?.knownStorePathsBySessionPath.delete(sessionDirPath);
|
|
803
|
+
}
|
|
804
|
+
async #removeInvalidEntries(dir, invalidEntryNames, cacheContext) {
|
|
805
|
+
if (cacheContext.cleanupKnowledge === null || invalidEntryNames.length === 0) return;
|
|
806
|
+
await Promise.all(invalidEntryNames.map(async (entryName) => {
|
|
807
|
+
try {
|
|
808
|
+
await dir.removeEntry(entryName, { recursive: true });
|
|
809
|
+
} catch {}
|
|
810
|
+
}));
|
|
811
|
+
}
|
|
812
|
+
async #listDirectoryEntries(dir, parentPath, cacheContext) {
|
|
813
|
+
const entries = [];
|
|
814
|
+
const invalidEntryNames = [];
|
|
815
|
+
for await (const entry of dir.values()) {
|
|
816
|
+
if (entry.kind !== "directory") {
|
|
817
|
+
invalidEntryNames.push(entry.name);
|
|
818
|
+
continue;
|
|
819
|
+
}
|
|
820
|
+
const dirHandle = __LEGIT_CAST__(entry);
|
|
821
|
+
const path = joinPath(parentPath, entry.name);
|
|
822
|
+
cacheContext.dirCache.set(path, dirHandle);
|
|
823
|
+
entries.push({
|
|
824
|
+
handle: dirHandle,
|
|
825
|
+
name: entry.name,
|
|
826
|
+
path
|
|
827
|
+
});
|
|
828
|
+
}
|
|
829
|
+
await this.#removeInvalidEntries(dir, invalidEntryNames, cacheContext);
|
|
830
|
+
entries.sort((left, right) => left.name.localeCompare(right.name));
|
|
831
|
+
return entries;
|
|
832
|
+
}
|
|
833
|
+
};
|
|
834
|
+
//#endregion
|
|
835
|
+
//#region src/persistentStorage/opfsPersistentStorage.ts
|
|
836
|
+
const opfsPersistentStorage = /* @__PURE__ */ createAsyncStorageAdapter(new OpfsAsyncStorageDriver());
|
|
837
|
+
//#endregion
|
|
838
|
+
export { encodePathSegment as a, getNavigatorStorageDirectory as i, getDirectoryHandleIfExists as n, getFileHandleIfExists as r, opfsPersistentStorage as t };
|