@waffle-os/runtime 0.0.147 → 0.0.148
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/browser/filesystem-backend.ts +26 -5
- package/browser/fs-worker.ts +5 -2
- package/browser/shared-fs.ts +93 -10
- package/core/filesystem.ts +2 -0
- package/dist/browser/filesystem-backend.js +19 -2
- package/dist/browser/fs-worker.js +7 -2
- package/dist/browser/shared-fs.js +77 -8
- package/dist/fs/client.js +82 -21
- package/dist/fs/directory-pages.js +22 -7
- package/dist/fs/guest-watch.js +9 -3
- package/dist/fs/idle-database.js +1 -1
- package/dist/fs/idle-owner.js +35 -4
- package/dist/fs/indexeddb.js +322 -63
- package/dist/fs/memory.js +18 -9
- package/dist/fs/multitab-backend.js +5065 -0
- package/dist/fs/multitab-protocol.js +301 -0
- package/dist/fs/opfs.js +21 -0
- package/dist/fs/persistent.js +70 -1
- package/dist/fs/protocol.js +1 -1
- package/dist/fs/service.js +266 -86
- package/dist/fs/shared.js +45 -0
- package/dist/fs/vfs.js +26 -8
- package/dist/fs/watch-ring.js +7 -3
- package/dist/proc/command-stdio.js +56 -1
- package/dist/runtime.js +4 -2
- package/dist/tooling/program/program-transformer.json +1 -1
- package/fs/backend.ts +36 -0
- package/fs/client.ts +81 -19
- package/fs/directory-pages.ts +40 -6
- package/fs/guest-watch.ts +8 -3
- package/fs/idle-database.ts +1 -0
- package/fs/idle-owner.ts +48 -12
- package/fs/indexeddb.ts +396 -78
- package/fs/memory.ts +33 -9
- package/fs/multitab-backend.ts +6870 -0
- package/fs/multitab-protocol.ts +655 -0
- package/fs/opfs.ts +21 -0
- package/fs/persistent.ts +70 -1
- package/fs/protocol.ts +1 -1
- package/fs/service.ts +314 -109
- package/fs/shared.ts +45 -0
- package/fs/storage.ts +3 -0
- package/fs/vfs.ts +44 -15
- package/fs/watch-ring.ts +5 -2
- package/package.json +1 -1
- package/proc/command-stdio.ts +72 -2
- package/runtime.ts +12 -3
- package/tooling/program/program-transformer.json +1 -1
package/dist/fs/indexeddb.js
CHANGED
|
@@ -43,6 +43,32 @@ import {
|
|
|
43
43
|
validInteger
|
|
44
44
|
} from "./storage.js";
|
|
45
45
|
export const OWNERSHIP_LOST_MESSAGE = "This workspace was opened in another tab. Reload to continue here.";
|
|
46
|
+
export const INDEXEDDB_ARB_LOCK_PREFIX = "bun-wasm:filesystem:indexeddb-arb:";
|
|
47
|
+
|
|
48
|
+
export class ExpectedEpochMismatchError extends Error {
|
|
49
|
+
expectedEpoch;
|
|
50
|
+
actualEpoch;
|
|
51
|
+
name = "ExpectedEpochMismatchError";
|
|
52
|
+
constructor(expectedEpoch, actualEpoch) {
|
|
53
|
+
super(`Expected IndexedDB epoch ${expectedEpoch}, found ${actualEpoch}`);
|
|
54
|
+
this.expectedEpoch = expectedEpoch;
|
|
55
|
+
this.actualEpoch = actualEpoch;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export class StealTargetMismatchError extends Error {
|
|
60
|
+
name = "StealTargetMismatchError";
|
|
61
|
+
constructor(message = "IndexedDB steal target verification failed") {
|
|
62
|
+
super(message);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export class IndexedDBStorageBusyError extends Error {
|
|
67
|
+
name = "IndexedDBStorageBusyError";
|
|
68
|
+
constructor(message = "IndexedDB storage lock is currently held") {
|
|
69
|
+
super(message);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
46
72
|
|
|
47
73
|
class NativeDatabaseOpening {
|
|
48
74
|
signal;
|
|
@@ -147,27 +173,169 @@ function requestStorageLock(locks, name, options, callback) {
|
|
|
147
173
|
return Promise.reject(error);
|
|
148
174
|
}
|
|
149
175
|
}
|
|
150
|
-
function allocateEpoch(transaction, completedEpoch) {
|
|
176
|
+
function allocateEpoch(transaction, completedEpoch, epochOwner = "exclusive", expectedEpoch) {
|
|
151
177
|
let epoch = 0;
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
178
|
+
let lastExclusiveEpoch = 0;
|
|
179
|
+
let mismatch;
|
|
180
|
+
const store = transaction.objectStore("metadata");
|
|
181
|
+
const request = store.get("epoch");
|
|
182
|
+
const exclusiveRequest = store.get("lastExclusiveEpoch");
|
|
183
|
+
let epochLoaded = false;
|
|
184
|
+
let exclusiveLoaded = false;
|
|
185
|
+
let previous;
|
|
186
|
+
let previousExclusive;
|
|
187
|
+
const finish = () => {
|
|
188
|
+
if (!epochLoaded || !exclusiveLoaded)
|
|
189
|
+
return;
|
|
155
190
|
if (previous !== undefined && (!validInteger(previous) || previous >= Number.MAX_SAFE_INTEGER)) {
|
|
156
191
|
transaction.abort();
|
|
157
192
|
return;
|
|
158
193
|
}
|
|
159
|
-
|
|
160
|
-
|
|
194
|
+
if (previousExclusive !== undefined && (!validInteger(previousExclusive) || previousExclusive >= Number.MAX_SAFE_INTEGER)) {
|
|
195
|
+
transaction.abort();
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
const prevNumber = typeof previous === "number" ? previous : 0;
|
|
199
|
+
if (expectedEpoch !== undefined && prevNumber !== expectedEpoch) {
|
|
200
|
+
mismatch = new ExpectedEpochMismatchError(expectedEpoch, prevNumber);
|
|
201
|
+
transaction.abort();
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
epoch = prevNumber + 1;
|
|
205
|
+
store.put(epoch, "epoch");
|
|
206
|
+
if (epochOwner === "exclusive") {
|
|
207
|
+
lastExclusiveEpoch = epoch;
|
|
208
|
+
store.put(lastExclusiveEpoch, "lastExclusiveEpoch");
|
|
209
|
+
} else {
|
|
210
|
+
lastExclusiveEpoch = typeof previousExclusive === "number" ? previousExclusive : 0;
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
request.onsuccess = () => {
|
|
214
|
+
previous = request.result;
|
|
215
|
+
epochLoaded = true;
|
|
216
|
+
finish();
|
|
217
|
+
};
|
|
218
|
+
exclusiveRequest.onsuccess = () => {
|
|
219
|
+
previousExclusive = exclusiveRequest.result;
|
|
220
|
+
exclusiveLoaded = true;
|
|
221
|
+
finish();
|
|
161
222
|
};
|
|
162
|
-
return completedEpoch.then(() => epoch)
|
|
223
|
+
return completedEpoch.then(() => ({ epoch, lastExclusiveEpoch }), (error) => {
|
|
224
|
+
if (mismatch)
|
|
225
|
+
throw mismatch;
|
|
226
|
+
throw error;
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
export async function readIndexedDBEpochMetadata(name, options = {}) {
|
|
230
|
+
const factory = options.indexedDB ?? globalThis.indexedDB;
|
|
231
|
+
if (!factory)
|
|
232
|
+
return;
|
|
233
|
+
const controller = new AbortController;
|
|
234
|
+
const timer = options.timeoutMs !== undefined ? setTimeout(() => controller.abort(new Error("IndexedDB epoch check timed out")), options.timeoutMs) : undefined;
|
|
235
|
+
const signal = options.signal ? AbortSignal.any([options.signal, controller.signal]) : controller.signal;
|
|
236
|
+
signal.throwIfAborted();
|
|
237
|
+
let db;
|
|
238
|
+
let tx;
|
|
239
|
+
const onAbort = () => {
|
|
240
|
+
try {
|
|
241
|
+
tx?.abort();
|
|
242
|
+
} catch {}
|
|
243
|
+
try {
|
|
244
|
+
db?.close();
|
|
245
|
+
} catch {}
|
|
246
|
+
};
|
|
247
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
248
|
+
try {
|
|
249
|
+
db = await new Promise((resolve, reject) => {
|
|
250
|
+
const openReq = factory.open(name, 1);
|
|
251
|
+
let settled = false;
|
|
252
|
+
const rejectAbort = () => {
|
|
253
|
+
if (settled)
|
|
254
|
+
return;
|
|
255
|
+
settled = true;
|
|
256
|
+
reject(signal.reason);
|
|
257
|
+
};
|
|
258
|
+
signal.addEventListener("abort", rejectAbort, { once: true });
|
|
259
|
+
openReq.onupgradeneeded = () => {
|
|
260
|
+
try {
|
|
261
|
+
openReq.transaction?.abort();
|
|
262
|
+
} catch {}
|
|
263
|
+
};
|
|
264
|
+
openReq.onsuccess = () => {
|
|
265
|
+
signal.removeEventListener("abort", rejectAbort);
|
|
266
|
+
const opened = openReq.result;
|
|
267
|
+
if (settled || signal.aborted) {
|
|
268
|
+
try {
|
|
269
|
+
opened.close();
|
|
270
|
+
} catch {}
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
settled = true;
|
|
274
|
+
resolve(opened);
|
|
275
|
+
};
|
|
276
|
+
openReq.onerror = () => {
|
|
277
|
+
signal.removeEventListener("abort", rejectAbort);
|
|
278
|
+
if (settled)
|
|
279
|
+
return;
|
|
280
|
+
settled = true;
|
|
281
|
+
reject(openReq.error ?? new Error("Failed to open IndexedDB for epoch metadata"));
|
|
282
|
+
};
|
|
283
|
+
openReq.onblocked = () => {
|
|
284
|
+
signal.removeEventListener("abort", rejectAbort);
|
|
285
|
+
if (settled)
|
|
286
|
+
return;
|
|
287
|
+
settled = true;
|
|
288
|
+
reject(new Error("IndexedDB blocked during epoch check"));
|
|
289
|
+
};
|
|
290
|
+
});
|
|
291
|
+
if (signal.aborted)
|
|
292
|
+
throw signal.reason;
|
|
293
|
+
if (!db.objectStoreNames.contains("metadata"))
|
|
294
|
+
return;
|
|
295
|
+
tx = db.transaction("metadata", "readonly");
|
|
296
|
+
const store = tx.objectStore("metadata");
|
|
297
|
+
const result = await new Promise((resolve, reject) => {
|
|
298
|
+
let epochVal = 0;
|
|
299
|
+
let exclusiveVal = 0;
|
|
300
|
+
const g1 = store.get("epoch");
|
|
301
|
+
const g2 = store.get("lastExclusiveEpoch");
|
|
302
|
+
g1.onsuccess = () => {
|
|
303
|
+
if (typeof g1.result === "number" && validInteger(g1.result))
|
|
304
|
+
epochVal = g1.result;
|
|
305
|
+
};
|
|
306
|
+
g2.onsuccess = () => {
|
|
307
|
+
if (typeof g2.result === "number" && validInteger(g2.result))
|
|
308
|
+
exclusiveVal = g2.result;
|
|
309
|
+
};
|
|
310
|
+
tx.oncomplete = () => resolve({ epoch: epochVal, lastExclusiveEpoch: exclusiveVal });
|
|
311
|
+
tx.onerror = () => reject(tx.error ?? new Error("Epoch metadata transaction failed"));
|
|
312
|
+
tx.onabort = () => reject(signal.aborted ? signal.reason : tx.error ?? new Error("Epoch metadata transaction aborted"));
|
|
313
|
+
});
|
|
314
|
+
return result;
|
|
315
|
+
} finally {
|
|
316
|
+
if (timer !== undefined)
|
|
317
|
+
clearTimeout(timer);
|
|
318
|
+
signal.removeEventListener("abort", onAbort);
|
|
319
|
+
try {
|
|
320
|
+
db?.close();
|
|
321
|
+
} catch {}
|
|
322
|
+
}
|
|
163
323
|
}
|
|
164
324
|
function validateDatabaseLayout(committed, format, keys, journalKeys, dataKeys) {
|
|
165
|
-
const empty = committed && !keys.includes("format") && keys.every((key) => key === "epoch") && !journalKeys.length && !dataKeys.length;
|
|
325
|
+
const empty = committed && !keys.includes("format") && keys.every((key) => key === "epoch" || key === "lastExclusiveEpoch") && !journalKeys.length && !dataKeys.length;
|
|
166
326
|
if (committed && !empty && format !== STAGED_FORMAT)
|
|
167
327
|
invalidCommit();
|
|
168
328
|
if (!committed && keys.includes("format") && format !== INDEXEDDB_FORMAT && format !== "waffle-indexeddb-v1")
|
|
169
329
|
throw new VfsError("INVALID_VFS_FORMAT", "indexeddb", "Incompatible IndexedDB filesystem format identity");
|
|
170
|
-
const allowed = [
|
|
330
|
+
const allowed = [
|
|
331
|
+
0,
|
|
332
|
+
1,
|
|
333
|
+
"epoch",
|
|
334
|
+
"lastExclusiveEpoch",
|
|
335
|
+
"active",
|
|
336
|
+
"format",
|
|
337
|
+
"identity"
|
|
338
|
+
];
|
|
171
339
|
if (committed)
|
|
172
340
|
allowed.push("commit");
|
|
173
341
|
if (keys.some((key) => !allowed.includes(key)))
|
|
@@ -453,6 +621,7 @@ export class IndexedDBStorage {
|
|
|
453
621
|
committedMode;
|
|
454
622
|
readOnly;
|
|
455
623
|
retireMaintenance;
|
|
624
|
+
initialLastExclusiveEpoch;
|
|
456
625
|
tail = Promise.resolve();
|
|
457
626
|
readers = new PayloadRetentions;
|
|
458
627
|
readerWork = new Set;
|
|
@@ -469,7 +638,7 @@ export class IndexedDBStorage {
|
|
|
469
638
|
committedRecord;
|
|
470
639
|
committedPending = false;
|
|
471
640
|
readOnlyLegacy = false;
|
|
472
|
-
constructor(db, unlock, compactEvery, epoch, onOwnershipLost, committedMode = false, readOnly = false, retireMaintenance = () => Promise.resolve()) {
|
|
641
|
+
constructor(db, unlock, compactEvery, epoch, onOwnershipLost, committedMode = false, readOnly = false, retireMaintenance = () => Promise.resolve(), initialLastExclusiveEpoch = 0) {
|
|
473
642
|
this.db = db;
|
|
474
643
|
this.unlock = unlock;
|
|
475
644
|
this.compactEvery = compactEvery;
|
|
@@ -478,12 +647,49 @@ export class IndexedDBStorage {
|
|
|
478
647
|
this.committedMode = committedMode;
|
|
479
648
|
this.readOnly = readOnly;
|
|
480
649
|
this.retireMaintenance = retireMaintenance;
|
|
650
|
+
this.initialLastExclusiveEpoch = initialLastExclusiveEpoch;
|
|
481
651
|
db.onversionchange = () => {
|
|
482
652
|
if (this.readOnly)
|
|
483
653
|
this.loseOwnership();
|
|
484
654
|
this.close().catch(() => {});
|
|
485
655
|
};
|
|
486
656
|
}
|
|
657
|
+
get currentEpoch() {
|
|
658
|
+
return this.epoch;
|
|
659
|
+
}
|
|
660
|
+
get lastExclusiveEpoch() {
|
|
661
|
+
return this.initialLastExclusiveEpoch;
|
|
662
|
+
}
|
|
663
|
+
async verifyEpoch(signal) {
|
|
664
|
+
if (this.closed || this.ownershipError)
|
|
665
|
+
return false;
|
|
666
|
+
signal?.throwIfAborted();
|
|
667
|
+
let tx;
|
|
668
|
+
const onAbort = () => {
|
|
669
|
+
try {
|
|
670
|
+
tx?.abort();
|
|
671
|
+
} catch {}
|
|
672
|
+
};
|
|
673
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
674
|
+
try {
|
|
675
|
+
tx = this.db.transaction("metadata", "readonly");
|
|
676
|
+
const current = await new Promise((resolve, reject) => {
|
|
677
|
+
const req = tx.objectStore("metadata").get("epoch");
|
|
678
|
+
req.onsuccess = () => resolve(req.result);
|
|
679
|
+
req.onerror = () => reject(req.error);
|
|
680
|
+
tx.onabort = () => reject(signal?.aborted ? signal.reason : tx.error);
|
|
681
|
+
});
|
|
682
|
+
if (signal?.aborted)
|
|
683
|
+
throw signal.reason;
|
|
684
|
+
return current === this.epoch && !this.closed && !this.ownershipError;
|
|
685
|
+
} catch (_error) {
|
|
686
|
+
if (signal?.aborted)
|
|
687
|
+
throw signal.reason;
|
|
688
|
+
return false;
|
|
689
|
+
} finally {
|
|
690
|
+
signal?.removeEventListener("abort", onAbort);
|
|
691
|
+
}
|
|
692
|
+
}
|
|
487
693
|
static open(name, options = {}) {
|
|
488
694
|
return IndexedDBStorage.openMode(name, options, false);
|
|
489
695
|
}
|
|
@@ -514,7 +720,8 @@ export class IndexedDBStorage {
|
|
|
514
720
|
const locks = options.locks ?? globalThis.navigator?.locks;
|
|
515
721
|
if (!locks)
|
|
516
722
|
throw new Error("IndexedDB storage requires IndexedDB and Web Locks");
|
|
517
|
-
const
|
|
723
|
+
const preSignal = options.preStealSignal && options.signal ? AbortSignal.any([options.preStealSignal, options.signal]) : options.preStealSignal ?? options.signal;
|
|
724
|
+
const maintenance = await maintenanceLock(locks, name, "shared", preSignal, options.onWaiting);
|
|
518
725
|
if (maintenance === "busy")
|
|
519
726
|
throw new Error("Unexpected shared maintenance lock refusal");
|
|
520
727
|
const debts = [];
|
|
@@ -540,13 +747,14 @@ export class IndexedDBStorage {
|
|
|
540
747
|
if (!Number.isSafeInteger(compactEvery) || compactEvery < 1)
|
|
541
748
|
throw new Error("Invalid compaction interval");
|
|
542
749
|
const versionChangeAbort = options.whenBusy === "wait" ? new AbortController : undefined;
|
|
543
|
-
const
|
|
544
|
-
const
|
|
750
|
+
const postStealSignal = versionChangeAbort ? options.signal ? AbortSignal.any([options.signal, versionChangeAbort.signal]) : versionChangeAbort.signal : options.signal;
|
|
751
|
+
const preSignal = options.preStealSignal && postStealSignal ? AbortSignal.any([options.preStealSignal, postStealSignal]) : options.preStealSignal ?? postStealSignal;
|
|
752
|
+
const opening = new NativeDatabaseOpening(factory, name, preSignal, versionChangeAbort, options);
|
|
545
753
|
const db = await opening.ready;
|
|
546
754
|
reportRuntimeStartup(options.onStartupStage, "indexeddb-open-resolved");
|
|
547
|
-
if (
|
|
755
|
+
if (preSignal?.aborted) {
|
|
548
756
|
await opening.retire();
|
|
549
|
-
throw
|
|
757
|
+
throw preSignal.reason;
|
|
550
758
|
}
|
|
551
759
|
try {
|
|
552
760
|
validateSchema(db);
|
|
@@ -567,74 +775,125 @@ export class IndexedDBStorage {
|
|
|
567
775
|
let allocationDone;
|
|
568
776
|
let lost = false;
|
|
569
777
|
let granted = false;
|
|
778
|
+
let grantedResolve;
|
|
779
|
+
const grantedPromise = new Promise((resolve) => {
|
|
780
|
+
grantedResolve = resolve;
|
|
781
|
+
});
|
|
570
782
|
const abortAllocation = () => {
|
|
571
783
|
try {
|
|
572
784
|
allocation?.abort();
|
|
573
785
|
} catch {}
|
|
574
786
|
};
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
const
|
|
578
|
-
const
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
787
|
+
postStealSignal?.addEventListener("abort", abortAllocation, { once: true });
|
|
788
|
+
const storageLockName = `bun-wasm:filesystem:indexeddb:${name}`;
|
|
789
|
+
const arbLockName = `${INDEXEDDB_ARB_LOCK_PREFIX}${name}`;
|
|
790
|
+
const lockOptions = options.whenBusy === "wait" ? { mode: "exclusive", signal: postStealSignal } : options.whenBusy === "ifAvailable" ? { mode: "exclusive", ifAvailable: true } : { mode: "exclusive", steal: true };
|
|
791
|
+
const startStorageRequest = () => {
|
|
792
|
+
const lifetimePromise = requestStorageLock(locks, storageLockName, lockOptions, async (lock) => {
|
|
793
|
+
if (lock === null) {
|
|
794
|
+
grantedResolve?.();
|
|
795
|
+
refused(new IndexedDBStorageBusyError);
|
|
796
|
+
return;
|
|
797
|
+
}
|
|
798
|
+
granted = true;
|
|
799
|
+
grantedResolve?.();
|
|
800
|
+
if (lost)
|
|
801
|
+
return;
|
|
802
|
+
if (postStealSignal?.aborted) {
|
|
803
|
+
refused(postStealSignal.reason);
|
|
804
|
+
return;
|
|
805
|
+
}
|
|
806
|
+
reportRuntimeStartup(options.onStartupStage, "filesystem-lock-acquired");
|
|
807
|
+
reportRuntimeStartup(options.onStartupStage, "filesystem-epoch-requested");
|
|
808
|
+
if (postStealSignal?.aborted)
|
|
809
|
+
throw postStealSignal.reason;
|
|
810
|
+
allocation = db.transaction("metadata", "readwrite", { durability: "strict" });
|
|
811
|
+
allocationDone = completed(allocation);
|
|
812
|
+
const allocated = await allocateEpoch(allocation, allocationDone, options.epochOwner ?? "exclusive", options.expectedEpoch);
|
|
813
|
+
reportRuntimeStartup(options.onStartupStage, "filesystem-epoch-resolved");
|
|
814
|
+
allocation = undefined;
|
|
815
|
+
if (lost)
|
|
816
|
+
return;
|
|
817
|
+
if (postStealSignal?.aborted)
|
|
818
|
+
throw postStealSignal.reason;
|
|
819
|
+
owner = new IndexedDBStorage(db, async () => {
|
|
820
|
+
release();
|
|
821
|
+
await lifetimePromise.catch(() => {});
|
|
822
|
+
}, compactEvery, allocated.epoch, options.onOwnershipLost, committedMode, false, retireMaintenance, allocated.lastExclusiveEpoch);
|
|
823
|
+
acquired(owner);
|
|
824
|
+
await held;
|
|
825
|
+
});
|
|
826
|
+
reportRuntimeStartup(options.onStartupStage, "filesystem-lock-requested");
|
|
827
|
+
lifetimePromise.catch((error) => {
|
|
828
|
+
lost = true;
|
|
829
|
+
grantedResolve?.();
|
|
830
|
+
try {
|
|
831
|
+
allocation?.abort();
|
|
832
|
+
} catch {}
|
|
833
|
+
owner?.loseOwnership();
|
|
834
|
+
const stolen = !postStealSignal?.aborted && error instanceof DOMException && error.name === "AbortError";
|
|
835
|
+
refused(owner?.ownershipError ?? (stolen ? new Error(OWNERSHIP_LOST_MESSAGE) : error));
|
|
836
|
+
if (stolen && !owner)
|
|
837
|
+
options.onOwnershipLost?.();
|
|
838
|
+
});
|
|
839
|
+
return lifetimePromise;
|
|
840
|
+
};
|
|
841
|
+
let lifetime;
|
|
842
|
+
try {
|
|
843
|
+
if (options.verifyStealTarget) {
|
|
844
|
+
await requestStorageLock(locks, arbLockName, { mode: "exclusive", signal: preSignal }, async () => {
|
|
845
|
+
if (preSignal?.aborted)
|
|
846
|
+
throw preSignal.reason;
|
|
847
|
+
const snapshot = await locks.query?.();
|
|
848
|
+
if (preSignal?.aborted)
|
|
849
|
+
throw preSignal.reason;
|
|
850
|
+
if (!snapshot || !options.verifyStealTarget(snapshot)) {
|
|
851
|
+
throw new StealTargetMismatchError;
|
|
852
|
+
}
|
|
853
|
+
lifetime = startStorageRequest();
|
|
854
|
+
await grantedPromise;
|
|
855
|
+
});
|
|
856
|
+
} else {
|
|
857
|
+
await requestStorageLock(locks, arbLockName, { mode: "shared", signal: preSignal }, async () => {
|
|
858
|
+
if (preSignal?.aborted)
|
|
859
|
+
throw preSignal.reason;
|
|
860
|
+
lifetime = startStorageRequest();
|
|
861
|
+
if (options.whenBusy === "wait") {
|
|
862
|
+
await Promise.race([grantedPromise, Promise.resolve().then(() => locks.query?.())]);
|
|
863
|
+
} else {
|
|
864
|
+
await grantedPromise;
|
|
865
|
+
}
|
|
866
|
+
});
|
|
585
867
|
}
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
allocation = undefined;
|
|
595
|
-
if (lost)
|
|
596
|
-
return;
|
|
597
|
-
if (signal?.aborted)
|
|
598
|
-
throw signal.reason;
|
|
599
|
-
owner = new IndexedDBStorage(db, async () => {
|
|
600
|
-
release();
|
|
601
|
-
await lifetime.catch(() => {});
|
|
602
|
-
}, compactEvery, value, options.onOwnershipLost, committedMode, false, retireMaintenance);
|
|
603
|
-
acquired(owner);
|
|
604
|
-
await held;
|
|
605
|
-
});
|
|
868
|
+
} catch (error) {
|
|
869
|
+
release();
|
|
870
|
+
await allocationDone?.catch(() => {});
|
|
871
|
+
await lifetime?.catch(() => {});
|
|
872
|
+
await opening.retire();
|
|
873
|
+
postStealSignal?.removeEventListener("abort", abortAllocation);
|
|
874
|
+
throw preSignal?.aborted && !lifetime ? preSignal.reason : error;
|
|
875
|
+
}
|
|
606
876
|
queueMicrotask(() => {
|
|
607
|
-
if (options.whenBusy === "wait" && !granted && !lost && !
|
|
877
|
+
if (options.whenBusy === "wait" && !granted && !lost && !postStealSignal?.aborted) {
|
|
608
878
|
try {
|
|
609
879
|
options.onWaiting?.();
|
|
610
880
|
} catch {}
|
|
611
881
|
}
|
|
612
882
|
});
|
|
613
|
-
lifetime.catch((error) => {
|
|
614
|
-
lost = true;
|
|
615
|
-
try {
|
|
616
|
-
allocation?.abort();
|
|
617
|
-
} catch {}
|
|
618
|
-
owner?.loseOwnership();
|
|
619
|
-
const stolen = !signal?.aborted && error instanceof DOMException && error.name === "AbortError";
|
|
620
|
-
refused(owner?.ownershipError ?? (stolen ? new Error(OWNERSHIP_LOST_MESSAGE) : error));
|
|
621
|
-
if (stolen && !owner)
|
|
622
|
-
options.onOwnershipLost?.();
|
|
623
|
-
});
|
|
624
883
|
try {
|
|
625
884
|
const storage = await ready;
|
|
626
|
-
if (
|
|
627
|
-
throw
|
|
885
|
+
if (postStealSignal?.aborted)
|
|
886
|
+
throw postStealSignal.reason;
|
|
628
887
|
opening.transfer();
|
|
629
888
|
return storage;
|
|
630
889
|
} catch (error) {
|
|
631
890
|
release();
|
|
632
891
|
await allocationDone?.catch(() => {});
|
|
633
|
-
await lifetime
|
|
892
|
+
await lifetime?.catch(() => {});
|
|
634
893
|
await opening.retire();
|
|
635
|
-
throw
|
|
894
|
+
throw postStealSignal?.aborted ? postStealSignal.reason : error;
|
|
636
895
|
} finally {
|
|
637
|
-
|
|
896
|
+
postStealSignal?.removeEventListener("abort", abortAllocation);
|
|
638
897
|
}
|
|
639
898
|
}
|
|
640
899
|
loseOwnership() {
|
|
@@ -1058,7 +1317,7 @@ export class IndexedDBStorage {
|
|
|
1058
1317
|
const newest = recoverIndex(bases, journals, available);
|
|
1059
1318
|
if (this.committedMode && !stagedEmpty)
|
|
1060
1319
|
await this.recoverRequiredHead(storedCommit, active, newest, physical, journalKeys, dataKeys);
|
|
1061
|
-
if (!newest && (metadataKeys.filter((key) => key !== "format" && key !== "identity" && (!this.committedMode || key !== "commit")).length > 1 || journalKeys.length > 0 || dataKeys.length > 0)) {
|
|
1320
|
+
if (!newest && (metadataKeys.filter((key) => key !== "format" && key !== "identity" && key !== "lastExclusiveEpoch" && (!this.committedMode || key !== "commit")).length > 1 || journalKeys.length > 0 || dataKeys.length > 0)) {
|
|
1062
1321
|
throw new VfsError("INVALID_VFS_FORMAT", "indexeddb", "Existing filesystem storage is corrupt; no valid snapshot can be recovered");
|
|
1063
1322
|
}
|
|
1064
1323
|
if (newest) {
|
package/dist/fs/memory.js
CHANGED
|
@@ -245,12 +245,12 @@ export class MemFS {
|
|
|
245
245
|
else
|
|
246
246
|
pages.delete(name);
|
|
247
247
|
}
|
|
248
|
-
readdirPage(handle, cookie, byteLength) {
|
|
248
|
+
readdirPage(handle, cookie, byteLength, cookieTag, maxBytes, hardCapBytes) {
|
|
249
249
|
checkDirectoryPage(cookie, byteLength);
|
|
250
250
|
const node = this.directory(handle);
|
|
251
251
|
this.require(node, 4);
|
|
252
252
|
const parent = this.directoryParents.get(node) ?? node;
|
|
253
|
-
return this.directoryPages(node).page(cookie, byteLength, node.ino, parent.ino);
|
|
253
|
+
return this.directoryPages(node).page(cookie, byteLength, node.ino, parent.ino, cookieTag, maxBytes, hardCapBytes);
|
|
254
254
|
}
|
|
255
255
|
linkCounts = new WeakMap;
|
|
256
256
|
linkCount(node) {
|
|
@@ -528,18 +528,23 @@ export class MemFS {
|
|
|
528
528
|
this.changed(node);
|
|
529
529
|
return 0;
|
|
530
530
|
}
|
|
531
|
-
|
|
531
|
+
forEachDirEntry(handle, visit) {
|
|
532
532
|
const node = this.directory(handle);
|
|
533
533
|
this.require(node, 4);
|
|
534
534
|
const parent = this.directoryParents.get(node) ?? node;
|
|
535
|
-
const entry = (name,
|
|
535
|
+
const entry = (name, item) => ({
|
|
536
536
|
name,
|
|
537
|
-
ino:
|
|
538
|
-
kind: nodeKind(
|
|
537
|
+
ino: item.ino,
|
|
538
|
+
kind: nodeKind(item)
|
|
539
539
|
});
|
|
540
|
-
|
|
540
|
+
visit(entry(".", node));
|
|
541
|
+
visit(entry("..", parent));
|
|
541
542
|
for (const [name, child] of node.children)
|
|
542
|
-
|
|
543
|
+
visit(entry(name, child));
|
|
544
|
+
}
|
|
545
|
+
readdir(handle) {
|
|
546
|
+
const list = [];
|
|
547
|
+
this.forEachDirEntry(handle, (entry) => list.push(entry));
|
|
543
548
|
return list;
|
|
544
549
|
}
|
|
545
550
|
parentOf(path) {
|
|
@@ -1087,10 +1092,11 @@ export class MemFS {
|
|
|
1087
1092
|
}
|
|
1088
1093
|
return removed;
|
|
1089
1094
|
}
|
|
1090
|
-
|
|
1095
|
+
collectBoundedFilePaths(dir = "/", onFile) {
|
|
1091
1096
|
const out = [];
|
|
1092
1097
|
const walk = (node, path) => {
|
|
1093
1098
|
if (node.kind === "file") {
|
|
1099
|
+
onFile?.(path);
|
|
1094
1100
|
out.push(path);
|
|
1095
1101
|
return;
|
|
1096
1102
|
}
|
|
@@ -1110,6 +1116,9 @@ export class MemFS {
|
|
|
1110
1116
|
walk(this.nodeForHandle(start.node), start.canonicalPath);
|
|
1111
1117
|
return out.sort();
|
|
1112
1118
|
}
|
|
1119
|
+
listFiles(dir = "/") {
|
|
1120
|
+
return this.collectBoundedFilePaths(dir);
|
|
1121
|
+
}
|
|
1113
1122
|
}
|
|
1114
1123
|
function ensureSize(node, size) {
|
|
1115
1124
|
if (size <= node.data.length)
|