@waffle-os/runtime 0.0.147 → 0.0.149
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/process-broker.ts +6 -3
- package/browser/run-command.ts +18 -7
- 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/process-broker.js +3 -2
- package/dist/browser/run-command.js +8 -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/proc/registry.js +8 -8
- package/dist/proc/start-command.js +10 -0
- package/dist/proc/supervisor.js +4 -4
- 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/proc/registry.ts +13 -19
- package/proc/start-command.ts +29 -0
- package/proc/supervisor.ts +8 -6
- package/runtime.ts +12 -3
- package/tooling/program/program-transformer.json +1 -1
package/fs/indexeddb.ts
CHANGED
|
@@ -61,6 +61,31 @@ import {
|
|
|
61
61
|
|
|
62
62
|
export const OWNERSHIP_LOST_MESSAGE =
|
|
63
63
|
"This workspace was opened in another tab. Reload to continue here.";
|
|
64
|
+
export const INDEXEDDB_ARB_LOCK_PREFIX = "bun-wasm:filesystem:indexeddb-arb:";
|
|
65
|
+
|
|
66
|
+
export class ExpectedEpochMismatchError extends Error {
|
|
67
|
+
override readonly name = "ExpectedEpochMismatchError";
|
|
68
|
+
constructor(
|
|
69
|
+
readonly expectedEpoch: number,
|
|
70
|
+
readonly actualEpoch: number,
|
|
71
|
+
) {
|
|
72
|
+
super(`Expected IndexedDB epoch ${expectedEpoch}, found ${actualEpoch}`);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export class StealTargetMismatchError extends Error {
|
|
77
|
+
override readonly name = "StealTargetMismatchError";
|
|
78
|
+
constructor(message = "IndexedDB steal target verification failed") {
|
|
79
|
+
super(message);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export class IndexedDBStorageBusyError extends Error {
|
|
84
|
+
override readonly name = "IndexedDBStorageBusyError";
|
|
85
|
+
constructor(message = "IndexedDB storage lock is currently held") {
|
|
86
|
+
super(message);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
64
89
|
|
|
65
90
|
export interface IndexedDBStorageOptions {
|
|
66
91
|
readonly retainOpeningDebt?: (debt: Promise<void>) => void;
|
|
@@ -71,8 +96,16 @@ export interface IndexedDBStorageOptions {
|
|
|
71
96
|
onOwnershipLost?: () => void;
|
|
72
97
|
onStartupStage?: RuntimeStartupObserver;
|
|
73
98
|
signal?: AbortSignal;
|
|
99
|
+
/** Aborts only pre-steal setup and arbitration; disconnected synchronously when steal submits. */
|
|
100
|
+
preStealSignal?: AbortSignal;
|
|
74
101
|
onWaiting?: () => void;
|
|
75
|
-
whenBusy?: "takeover" | "wait";
|
|
102
|
+
whenBusy?: "takeover" | "wait" | "ifAvailable";
|
|
103
|
+
/** "exclusive" writes lastExclusiveEpoch; "attach" reads it without overwriting. */
|
|
104
|
+
epochOwner?: "exclusive" | "attach";
|
|
105
|
+
/** Atomic CAS check inside allocateEpoch; mismatch aborts with ExpectedEpochMismatchError. */
|
|
106
|
+
expectedEpoch?: number;
|
|
107
|
+
/** Verified under exclusive arbLock before submitting steal:true on the writer lock. */
|
|
108
|
+
verifyStealTarget?: (snapshot: LockManagerSnapshot) => boolean;
|
|
76
109
|
}
|
|
77
110
|
/** Retains physical native completion independently of caller-visible startup rejection. */
|
|
78
111
|
class NativeDatabaseOpening {
|
|
@@ -191,14 +224,29 @@ function requestStorageLock<T>(
|
|
|
191
224
|
return Promise.reject(error);
|
|
192
225
|
}
|
|
193
226
|
}
|
|
227
|
+
interface AllocatedEpoch {
|
|
228
|
+
readonly epoch: number;
|
|
229
|
+
readonly lastExclusiveEpoch: number;
|
|
230
|
+
}
|
|
231
|
+
|
|
194
232
|
function allocateEpoch(
|
|
195
233
|
transaction: IDBTransaction,
|
|
196
234
|
completedEpoch: Promise<void>,
|
|
197
|
-
|
|
235
|
+
epochOwner: "exclusive" | "attach" = "exclusive",
|
|
236
|
+
expectedEpoch?: number,
|
|
237
|
+
): Promise<AllocatedEpoch> {
|
|
198
238
|
let epoch = 0;
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
239
|
+
let lastExclusiveEpoch = 0;
|
|
240
|
+
let mismatch: ExpectedEpochMismatchError | undefined;
|
|
241
|
+
const store = transaction.objectStore("metadata");
|
|
242
|
+
const request = store.get("epoch");
|
|
243
|
+
const exclusiveRequest = store.get("lastExclusiveEpoch");
|
|
244
|
+
let epochLoaded = false;
|
|
245
|
+
let exclusiveLoaded = false;
|
|
246
|
+
let previous: unknown;
|
|
247
|
+
let previousExclusive: unknown;
|
|
248
|
+
const finish = () => {
|
|
249
|
+
if (!epochLoaded || !exclusiveLoaded) return;
|
|
202
250
|
if (
|
|
203
251
|
previous !== undefined &&
|
|
204
252
|
(!validInteger(previous) || previous >= Number.MAX_SAFE_INTEGER)
|
|
@@ -206,10 +254,151 @@ function allocateEpoch(
|
|
|
206
254
|
transaction.abort();
|
|
207
255
|
return;
|
|
208
256
|
}
|
|
209
|
-
|
|
210
|
-
|
|
257
|
+
if (
|
|
258
|
+
previousExclusive !== undefined &&
|
|
259
|
+
(!validInteger(previousExclusive) || previousExclusive >= Number.MAX_SAFE_INTEGER)
|
|
260
|
+
) {
|
|
261
|
+
transaction.abort();
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
const prevNumber = typeof previous === "number" ? previous : 0;
|
|
265
|
+
if (expectedEpoch !== undefined && prevNumber !== expectedEpoch) {
|
|
266
|
+
mismatch = new ExpectedEpochMismatchError(expectedEpoch, prevNumber);
|
|
267
|
+
transaction.abort();
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
epoch = prevNumber + 1;
|
|
271
|
+
store.put(epoch, "epoch");
|
|
272
|
+
if (epochOwner === "exclusive") {
|
|
273
|
+
lastExclusiveEpoch = epoch;
|
|
274
|
+
store.put(lastExclusiveEpoch, "lastExclusiveEpoch");
|
|
275
|
+
} else {
|
|
276
|
+
lastExclusiveEpoch = typeof previousExclusive === "number" ? previousExclusive : 0;
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
request.onsuccess = () => {
|
|
280
|
+
previous = request.result;
|
|
281
|
+
epochLoaded = true;
|
|
282
|
+
finish();
|
|
283
|
+
};
|
|
284
|
+
exclusiveRequest.onsuccess = () => {
|
|
285
|
+
previousExclusive = exclusiveRequest.result;
|
|
286
|
+
exclusiveLoaded = true;
|
|
287
|
+
finish();
|
|
211
288
|
};
|
|
212
|
-
return completedEpoch.then(
|
|
289
|
+
return completedEpoch.then(
|
|
290
|
+
() => ({ epoch, lastExclusiveEpoch }),
|
|
291
|
+
(error) => {
|
|
292
|
+
if (mismatch) throw mismatch;
|
|
293
|
+
throw error;
|
|
294
|
+
},
|
|
295
|
+
);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/** Bounded, cancellable readonly read of IndexedDB epoch and lastExclusiveEpoch metadata. */
|
|
299
|
+
export async function readIndexedDBEpochMetadata(
|
|
300
|
+
name: string,
|
|
301
|
+
options: { indexedDB?: IDBFactory; signal?: AbortSignal; timeoutMs?: number } = {},
|
|
302
|
+
): Promise<{ epoch: number; lastExclusiveEpoch: number } | undefined> {
|
|
303
|
+
const factory = options.indexedDB ?? globalThis.indexedDB;
|
|
304
|
+
if (!factory) return undefined;
|
|
305
|
+
const controller = new AbortController();
|
|
306
|
+
const timer =
|
|
307
|
+
options.timeoutMs !== undefined
|
|
308
|
+
? setTimeout(
|
|
309
|
+
() => controller.abort(new Error("IndexedDB epoch check timed out")),
|
|
310
|
+
options.timeoutMs,
|
|
311
|
+
)
|
|
312
|
+
: undefined;
|
|
313
|
+
const signal = options.signal
|
|
314
|
+
? AbortSignal.any([options.signal, controller.signal])
|
|
315
|
+
: controller.signal;
|
|
316
|
+
signal.throwIfAborted();
|
|
317
|
+
let db: IDBDatabase | undefined;
|
|
318
|
+
let tx: IDBTransaction | undefined;
|
|
319
|
+
const onAbort = () => {
|
|
320
|
+
try {
|
|
321
|
+
tx?.abort();
|
|
322
|
+
} catch {}
|
|
323
|
+
try {
|
|
324
|
+
db?.close();
|
|
325
|
+
} catch {}
|
|
326
|
+
};
|
|
327
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
328
|
+
try {
|
|
329
|
+
db = await new Promise<IDBDatabase>((resolve, reject) => {
|
|
330
|
+
const openReq = factory.open(name, 1);
|
|
331
|
+
let settled = false;
|
|
332
|
+
const rejectAbort = () => {
|
|
333
|
+
if (settled) return;
|
|
334
|
+
settled = true;
|
|
335
|
+
reject(signal.reason);
|
|
336
|
+
};
|
|
337
|
+
signal.addEventListener("abort", rejectAbort, { once: true });
|
|
338
|
+
openReq.onupgradeneeded = () => {
|
|
339
|
+
try {
|
|
340
|
+
openReq.transaction?.abort();
|
|
341
|
+
} catch {}
|
|
342
|
+
};
|
|
343
|
+
openReq.onsuccess = () => {
|
|
344
|
+
signal.removeEventListener("abort", rejectAbort);
|
|
345
|
+
const opened = openReq.result;
|
|
346
|
+
if (settled || signal.aborted) {
|
|
347
|
+
try {
|
|
348
|
+
opened.close();
|
|
349
|
+
} catch {}
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
settled = true;
|
|
353
|
+
resolve(opened);
|
|
354
|
+
};
|
|
355
|
+
openReq.onerror = () => {
|
|
356
|
+
signal.removeEventListener("abort", rejectAbort);
|
|
357
|
+
if (settled) return;
|
|
358
|
+
settled = true;
|
|
359
|
+
reject(openReq.error ?? new Error("Failed to open IndexedDB for epoch metadata"));
|
|
360
|
+
};
|
|
361
|
+
openReq.onblocked = () => {
|
|
362
|
+
signal.removeEventListener("abort", rejectAbort);
|
|
363
|
+
if (settled) return;
|
|
364
|
+
settled = true;
|
|
365
|
+
reject(new Error("IndexedDB blocked during epoch check"));
|
|
366
|
+
};
|
|
367
|
+
});
|
|
368
|
+
if (signal.aborted) throw signal.reason;
|
|
369
|
+
if (!db.objectStoreNames.contains("metadata")) return undefined;
|
|
370
|
+
tx = db.transaction("metadata", "readonly");
|
|
371
|
+
const store = tx.objectStore("metadata");
|
|
372
|
+
const result = await new Promise<{ epoch: number; lastExclusiveEpoch: number }>(
|
|
373
|
+
(resolve, reject) => {
|
|
374
|
+
let epochVal = 0;
|
|
375
|
+
let exclusiveVal = 0;
|
|
376
|
+
const g1 = store.get("epoch");
|
|
377
|
+
const g2 = store.get("lastExclusiveEpoch");
|
|
378
|
+
g1.onsuccess = () => {
|
|
379
|
+
if (typeof g1.result === "number" && validInteger(g1.result)) epochVal = g1.result;
|
|
380
|
+
};
|
|
381
|
+
g2.onsuccess = () => {
|
|
382
|
+
if (typeof g2.result === "number" && validInteger(g2.result)) exclusiveVal = g2.result;
|
|
383
|
+
};
|
|
384
|
+
tx!.oncomplete = () => resolve({ epoch: epochVal, lastExclusiveEpoch: exclusiveVal });
|
|
385
|
+
tx!.onerror = () => reject(tx!.error ?? new Error("Epoch metadata transaction failed"));
|
|
386
|
+
tx!.onabort = () =>
|
|
387
|
+
reject(
|
|
388
|
+
signal.aborted
|
|
389
|
+
? signal.reason
|
|
390
|
+
: (tx!.error ?? new Error("Epoch metadata transaction aborted")),
|
|
391
|
+
);
|
|
392
|
+
},
|
|
393
|
+
);
|
|
394
|
+
return result;
|
|
395
|
+
} finally {
|
|
396
|
+
if (timer !== undefined) clearTimeout(timer);
|
|
397
|
+
signal.removeEventListener("abort", onAbort);
|
|
398
|
+
try {
|
|
399
|
+
db?.close();
|
|
400
|
+
} catch {}
|
|
401
|
+
}
|
|
213
402
|
}
|
|
214
403
|
interface Journal {
|
|
215
404
|
version: 1;
|
|
@@ -251,7 +440,7 @@ function validateDatabaseLayout(
|
|
|
251
440
|
const empty =
|
|
252
441
|
committed &&
|
|
253
442
|
!keys.includes("format") &&
|
|
254
|
-
keys.every((key) => key === "epoch") &&
|
|
443
|
+
keys.every((key) => key === "epoch" || key === "lastExclusiveEpoch") &&
|
|
255
444
|
!journalKeys.length &&
|
|
256
445
|
!dataKeys.length;
|
|
257
446
|
if (committed && !empty && format !== STAGED_FORMAT) invalidCommit();
|
|
@@ -266,7 +455,15 @@ function validateDatabaseLayout(
|
|
|
266
455
|
"indexeddb",
|
|
267
456
|
"Incompatible IndexedDB filesystem format identity",
|
|
268
457
|
);
|
|
269
|
-
const allowed: IDBValidKey[] = [
|
|
458
|
+
const allowed: IDBValidKey[] = [
|
|
459
|
+
0,
|
|
460
|
+
1,
|
|
461
|
+
"epoch",
|
|
462
|
+
"lastExclusiveEpoch",
|
|
463
|
+
"active",
|
|
464
|
+
"format",
|
|
465
|
+
"identity",
|
|
466
|
+
];
|
|
270
467
|
if (committed) allowed.push("commit");
|
|
271
468
|
if (keys.some((key) => !allowed.includes(key)))
|
|
272
469
|
throw new VfsError("INVALID_VFS_FORMAT", "indexeddb", "Incompatible IndexedDB metadata layout");
|
|
@@ -611,6 +808,7 @@ export class IndexedDBStorage implements StorageBackend {
|
|
|
611
808
|
private readonly committedMode = false,
|
|
612
809
|
private readonly readOnly = false,
|
|
613
810
|
private readonly retireMaintenance: () => Promise<void> = () => Promise.resolve(),
|
|
811
|
+
private readonly initialLastExclusiveEpoch = 0,
|
|
614
812
|
) {
|
|
615
813
|
db.onversionchange = () => {
|
|
616
814
|
if (this.readOnly) this.loseOwnership();
|
|
@@ -618,6 +816,43 @@ export class IndexedDBStorage implements StorageBackend {
|
|
|
618
816
|
};
|
|
619
817
|
}
|
|
620
818
|
|
|
819
|
+
get currentEpoch(): number {
|
|
820
|
+
return this.epoch;
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
get lastExclusiveEpoch(): number {
|
|
824
|
+
return this.initialLastExclusiveEpoch;
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
/** Readonly IndexedDB check verifying this connection still owns the latest epoch. */
|
|
828
|
+
async verifyEpoch(signal?: AbortSignal): Promise<boolean> {
|
|
829
|
+
if (this.closed || this.ownershipError) return false;
|
|
830
|
+
signal?.throwIfAborted();
|
|
831
|
+
let tx: IDBTransaction | undefined;
|
|
832
|
+
const onAbort = () => {
|
|
833
|
+
try {
|
|
834
|
+
tx?.abort();
|
|
835
|
+
} catch {}
|
|
836
|
+
};
|
|
837
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
838
|
+
try {
|
|
839
|
+
tx = this.db.transaction("metadata", "readonly");
|
|
840
|
+
const current = await new Promise<unknown>((resolve, reject) => {
|
|
841
|
+
const req = tx!.objectStore("metadata").get("epoch");
|
|
842
|
+
req.onsuccess = () => resolve(req.result);
|
|
843
|
+
req.onerror = () => reject(req.error);
|
|
844
|
+
tx!.onabort = () => reject(signal?.aborted ? signal.reason : tx!.error);
|
|
845
|
+
});
|
|
846
|
+
if (signal?.aborted) throw signal.reason;
|
|
847
|
+
return current === this.epoch && !this.closed && !this.ownershipError;
|
|
848
|
+
} catch (_error) {
|
|
849
|
+
if (signal?.aborted) throw signal.reason;
|
|
850
|
+
return false;
|
|
851
|
+
} finally {
|
|
852
|
+
signal?.removeEventListener("abort", onAbort);
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
|
|
621
856
|
static open(name: string, options: IndexedDBStorageOptions = {}): Promise<IndexedDBStorage> {
|
|
622
857
|
return IndexedDBStorage.openMode(name, options, false);
|
|
623
858
|
}
|
|
@@ -658,13 +893,11 @@ export class IndexedDBStorage implements StorageBackend {
|
|
|
658
893
|
): Promise<IndexedDBStorage> {
|
|
659
894
|
const locks = options.locks ?? globalThis.navigator?.locks;
|
|
660
895
|
if (!locks) throw new Error("IndexedDB storage requires IndexedDB and Web Locks");
|
|
661
|
-
const
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
options.onWaiting,
|
|
667
|
-
);
|
|
896
|
+
const preSignal =
|
|
897
|
+
options.preStealSignal && options.signal
|
|
898
|
+
? AbortSignal.any([options.preStealSignal, options.signal])
|
|
899
|
+
: (options.preStealSignal ?? options.signal);
|
|
900
|
+
const maintenance = await maintenanceLock(locks, name, "shared", preSignal, options.onWaiting);
|
|
668
901
|
if (maintenance === "busy") throw new Error("Unexpected shared maintenance lock refusal");
|
|
669
902
|
const debts: Promise<void>[] = [];
|
|
670
903
|
try {
|
|
@@ -702,17 +935,27 @@ export class IndexedDBStorage implements StorageBackend {
|
|
|
702
935
|
// Open before requesting the lock so its callback starts epoch allocation without
|
|
703
936
|
// awaiting another task. Losing a grant aborts allocation as well as later commits.
|
|
704
937
|
const versionChangeAbort = options.whenBusy === "wait" ? new AbortController() : undefined;
|
|
705
|
-
const
|
|
938
|
+
const postStealSignal = versionChangeAbort
|
|
706
939
|
? options.signal
|
|
707
940
|
? AbortSignal.any([options.signal, versionChangeAbort.signal])
|
|
708
941
|
: versionChangeAbort.signal
|
|
709
942
|
: options.signal;
|
|
710
|
-
const
|
|
943
|
+
const preSignal =
|
|
944
|
+
options.preStealSignal && postStealSignal
|
|
945
|
+
? AbortSignal.any([options.preStealSignal, postStealSignal])
|
|
946
|
+
: (options.preStealSignal ?? postStealSignal);
|
|
947
|
+
const opening = new NativeDatabaseOpening(
|
|
948
|
+
factory,
|
|
949
|
+
name,
|
|
950
|
+
preSignal,
|
|
951
|
+
versionChangeAbort,
|
|
952
|
+
options,
|
|
953
|
+
);
|
|
711
954
|
const db = await opening.ready;
|
|
712
955
|
reportRuntimeStartup(options.onStartupStage, "indexeddb-open-resolved");
|
|
713
|
-
if (
|
|
956
|
+
if (preSignal?.aborted) {
|
|
714
957
|
await opening.retire();
|
|
715
|
-
throw
|
|
958
|
+
throw preSignal.reason;
|
|
716
959
|
}
|
|
717
960
|
try {
|
|
718
961
|
validateSchema(db);
|
|
@@ -733,6 +976,10 @@ export class IndexedDBStorage implements StorageBackend {
|
|
|
733
976
|
let allocationDone: Promise<void> | undefined;
|
|
734
977
|
let lost = false;
|
|
735
978
|
let granted = false;
|
|
979
|
+
let grantedResolve: (() => void) | undefined;
|
|
980
|
+
const grantedPromise = new Promise<void>((resolve) => {
|
|
981
|
+
grantedResolve = resolve;
|
|
982
|
+
});
|
|
736
983
|
// Once granted, Web Locks no longer observes abort. Retire an in-flight epoch
|
|
737
984
|
// transaction through its normal completion/abort event before closing ownership.
|
|
738
985
|
const abortAllocation = () => {
|
|
@@ -742,52 +989,133 @@ export class IndexedDBStorage implements StorageBackend {
|
|
|
742
989
|
/* Completion already won. */
|
|
743
990
|
}
|
|
744
991
|
};
|
|
745
|
-
|
|
746
|
-
|
|
992
|
+
postStealSignal?.addEventListener("abort", abortAllocation, { once: true });
|
|
993
|
+
const storageLockName = `bun-wasm:filesystem:indexeddb:${name}`;
|
|
994
|
+
const arbLockName = `${INDEXEDDB_ARB_LOCK_PREFIX}${name}`;
|
|
747
995
|
const lockOptions =
|
|
748
996
|
options.whenBusy === "wait"
|
|
749
|
-
? { mode: "exclusive" as const, signal }
|
|
750
|
-
:
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
997
|
+
? { mode: "exclusive" as const, signal: postStealSignal }
|
|
998
|
+
: options.whenBusy === "ifAvailable"
|
|
999
|
+
? { mode: "exclusive" as const, ifAvailable: true as const }
|
|
1000
|
+
: { mode: "exclusive" as const, steal: true as const };
|
|
1001
|
+
const startStorageRequest = () => {
|
|
1002
|
+
const lifetimePromise = requestStorageLock(
|
|
1003
|
+
locks,
|
|
1004
|
+
storageLockName,
|
|
1005
|
+
lockOptions,
|
|
1006
|
+
async (lock) => {
|
|
1007
|
+
if (lock === null) {
|
|
1008
|
+
grantedResolve?.();
|
|
1009
|
+
refused(new IndexedDBStorageBusyError());
|
|
1010
|
+
return;
|
|
1011
|
+
}
|
|
1012
|
+
granted = true;
|
|
1013
|
+
grantedResolve?.();
|
|
1014
|
+
if (lost) return;
|
|
1015
|
+
if (postStealSignal?.aborted) {
|
|
1016
|
+
refused(postStealSignal.reason);
|
|
1017
|
+
return;
|
|
1018
|
+
}
|
|
1019
|
+
reportRuntimeStartup(options.onStartupStage, "filesystem-lock-acquired");
|
|
1020
|
+
reportRuntimeStartup(options.onStartupStage, "filesystem-epoch-requested");
|
|
1021
|
+
if (postStealSignal?.aborted) throw postStealSignal.reason;
|
|
1022
|
+
allocation = db.transaction("metadata", "readwrite", { durability: "strict" });
|
|
1023
|
+
allocationDone = completed(allocation);
|
|
1024
|
+
const allocated = await allocateEpoch(
|
|
1025
|
+
allocation,
|
|
1026
|
+
allocationDone,
|
|
1027
|
+
options.epochOwner ?? "exclusive",
|
|
1028
|
+
options.expectedEpoch,
|
|
1029
|
+
);
|
|
1030
|
+
reportRuntimeStartup(options.onStartupStage, "filesystem-epoch-resolved");
|
|
1031
|
+
allocation = undefined;
|
|
1032
|
+
if (lost) return;
|
|
1033
|
+
if (postStealSignal?.aborted) throw postStealSignal.reason;
|
|
1034
|
+
owner = new IndexedDBStorage(
|
|
1035
|
+
db,
|
|
1036
|
+
async () => {
|
|
1037
|
+
release();
|
|
1038
|
+
await lifetimePromise.catch(() => {});
|
|
1039
|
+
},
|
|
1040
|
+
compactEvery,
|
|
1041
|
+
allocated.epoch,
|
|
1042
|
+
options.onOwnershipLost,
|
|
1043
|
+
committedMode,
|
|
1044
|
+
false,
|
|
1045
|
+
retireMaintenance,
|
|
1046
|
+
allocated.lastExclusiveEpoch,
|
|
1047
|
+
);
|
|
1048
|
+
acquired(owner);
|
|
1049
|
+
await held;
|
|
1050
|
+
},
|
|
1051
|
+
);
|
|
1052
|
+
reportRuntimeStartup(options.onStartupStage, "filesystem-lock-requested");
|
|
1053
|
+
void lifetimePromise.catch((error) => {
|
|
1054
|
+
lost = true;
|
|
1055
|
+
grantedResolve?.();
|
|
1056
|
+
try {
|
|
1057
|
+
allocation?.abort();
|
|
1058
|
+
} catch {
|
|
1059
|
+
/* A completed allocation is already durable. */
|
|
761
1060
|
}
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
1061
|
+
owner?.loseOwnership();
|
|
1062
|
+
const stolen =
|
|
1063
|
+
!postStealSignal?.aborted && error instanceof DOMException && error.name === "AbortError";
|
|
1064
|
+
refused(owner?.ownershipError ?? (stolen ? new Error(OWNERSHIP_LOST_MESSAGE) : error));
|
|
1065
|
+
if (stolen && !owner) options.onOwnershipLost?.();
|
|
1066
|
+
});
|
|
1067
|
+
return lifetimePromise;
|
|
1068
|
+
};
|
|
1069
|
+
let lifetime: Promise<void> | undefined;
|
|
1070
|
+
try {
|
|
1071
|
+
if (options.verifyStealTarget) {
|
|
1072
|
+
// Attach recovery: acquire exclusive arbitration lock, verify exact target under broker
|
|
1073
|
+
// snapshot, and synchronously submit steal:true before releasing arbLock.
|
|
1074
|
+
await requestStorageLock(
|
|
1075
|
+
locks,
|
|
1076
|
+
arbLockName,
|
|
1077
|
+
{ mode: "exclusive", signal: preSignal },
|
|
774
1078
|
async () => {
|
|
775
|
-
|
|
776
|
-
await
|
|
1079
|
+
if (preSignal?.aborted) throw preSignal.reason;
|
|
1080
|
+
const snapshot = (await locks.query?.()) as LockManagerSnapshot | undefined;
|
|
1081
|
+
if (preSignal?.aborted) throw preSignal.reason;
|
|
1082
|
+
if (!snapshot || !options.verifyStealTarget!(snapshot)) {
|
|
1083
|
+
throw new StealTargetMismatchError();
|
|
1084
|
+
}
|
|
1085
|
+
// Synchronous steal submission boundary: from here on preStealSignal is disconnected;
|
|
1086
|
+
// only postStealSignal (dispose) can abort allocateEpoch.
|
|
1087
|
+
lifetime = startStorageRequest();
|
|
1088
|
+
await grantedPromise;
|
|
777
1089
|
},
|
|
778
|
-
compactEvery,
|
|
779
|
-
value,
|
|
780
|
-
options.onOwnershipLost,
|
|
781
|
-
committedMode,
|
|
782
|
-
false,
|
|
783
|
-
retireMaintenance,
|
|
784
1090
|
);
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
1091
|
+
} else {
|
|
1092
|
+
// Normal legacy writers & attach non-queuing startup: submit storageLock under shared
|
|
1093
|
+
// arbitration lock and confirm broker visibility before releasing arbLock.
|
|
1094
|
+
await requestStorageLock(
|
|
1095
|
+
locks,
|
|
1096
|
+
arbLockName,
|
|
1097
|
+
{ mode: "shared", signal: preSignal },
|
|
1098
|
+
async () => {
|
|
1099
|
+
if (preSignal?.aborted) throw preSignal.reason;
|
|
1100
|
+
lifetime = startStorageRequest();
|
|
1101
|
+
if (options.whenBusy === "wait") {
|
|
1102
|
+
await Promise.race([grantedPromise, Promise.resolve().then(() => locks.query?.())]);
|
|
1103
|
+
} else {
|
|
1104
|
+
await grantedPromise;
|
|
1105
|
+
}
|
|
1106
|
+
},
|
|
1107
|
+
);
|
|
1108
|
+
}
|
|
1109
|
+
} catch (error) {
|
|
1110
|
+
release();
|
|
1111
|
+
await allocationDone?.catch(() => {});
|
|
1112
|
+
await lifetime?.catch(() => {});
|
|
1113
|
+
await opening.retire();
|
|
1114
|
+
postStealSignal?.removeEventListener("abort", abortAllocation);
|
|
1115
|
+
throw preSignal?.aborted && !lifetime ? preSignal.reason : error;
|
|
1116
|
+
}
|
|
789
1117
|
queueMicrotask(() => {
|
|
790
|
-
if (options.whenBusy === "wait" && !granted && !lost && !
|
|
1118
|
+
if (options.whenBusy === "wait" && !granted && !lost && !postStealSignal?.aborted) {
|
|
791
1119
|
try {
|
|
792
1120
|
options.onWaiting?.();
|
|
793
1121
|
} catch {
|
|
@@ -795,32 +1123,19 @@ export class IndexedDBStorage implements StorageBackend {
|
|
|
795
1123
|
}
|
|
796
1124
|
}
|
|
797
1125
|
});
|
|
798
|
-
void lifetime.catch((error) => {
|
|
799
|
-
lost = true;
|
|
800
|
-
try {
|
|
801
|
-
allocation?.abort();
|
|
802
|
-
} catch {
|
|
803
|
-
/* A completed allocation is already durable. */
|
|
804
|
-
}
|
|
805
|
-
owner?.loseOwnership();
|
|
806
|
-
const stolen =
|
|
807
|
-
!signal?.aborted && error instanceof DOMException && error.name === "AbortError";
|
|
808
|
-
refused(owner?.ownershipError ?? (stolen ? new Error(OWNERSHIP_LOST_MESSAGE) : error));
|
|
809
|
-
if (stolen && !owner) options.onOwnershipLost?.();
|
|
810
|
-
});
|
|
811
1126
|
try {
|
|
812
1127
|
const storage = await ready;
|
|
813
|
-
if (
|
|
1128
|
+
if (postStealSignal?.aborted) throw postStealSignal.reason;
|
|
814
1129
|
opening.transfer();
|
|
815
1130
|
return storage;
|
|
816
1131
|
} catch (error) {
|
|
817
1132
|
release();
|
|
818
1133
|
await allocationDone?.catch(() => {});
|
|
819
|
-
await lifetime
|
|
1134
|
+
await lifetime?.catch(() => {});
|
|
820
1135
|
await opening.retire();
|
|
821
|
-
throw
|
|
1136
|
+
throw postStealSignal?.aborted ? postStealSignal.reason : error;
|
|
822
1137
|
} finally {
|
|
823
|
-
|
|
1138
|
+
postStealSignal?.removeEventListener("abort", abortAllocation);
|
|
824
1139
|
}
|
|
825
1140
|
}
|
|
826
1141
|
|
|
@@ -1295,7 +1610,10 @@ export class IndexedDBStorage implements StorageBackend {
|
|
|
1295
1610
|
!newest &&
|
|
1296
1611
|
(metadataKeys.filter(
|
|
1297
1612
|
(key) =>
|
|
1298
|
-
key !== "format" &&
|
|
1613
|
+
key !== "format" &&
|
|
1614
|
+
key !== "identity" &&
|
|
1615
|
+
key !== "lastExclusiveEpoch" &&
|
|
1616
|
+
(!this.committedMode || key !== "commit"),
|
|
1299
1617
|
).length > 1 ||
|
|
1300
1618
|
journalKeys.length > 0 ||
|
|
1301
1619
|
dataKeys.length > 0)
|
package/fs/memory.ts
CHANGED
|
@@ -355,12 +355,27 @@ export class MemFS implements FileSystem {
|
|
|
355
355
|
if (node) pages.set({ name, ino: node.ino, kind: nodeKind(node) });
|
|
356
356
|
else pages.delete(name);
|
|
357
357
|
}
|
|
358
|
-
readdirPage(
|
|
358
|
+
readdirPage(
|
|
359
|
+
handle: FileHandle,
|
|
360
|
+
cookie: bigint,
|
|
361
|
+
byteLength: number,
|
|
362
|
+
cookieTag?: bigint,
|
|
363
|
+
maxBytes?: number,
|
|
364
|
+
hardCapBytes?: number,
|
|
365
|
+
): Uint8Array {
|
|
359
366
|
checkDirectoryPage(cookie, byteLength);
|
|
360
367
|
const node = this.directory(handle);
|
|
361
368
|
this.require(node, 4);
|
|
362
369
|
const parent = this.directoryParents.get(node) ?? node;
|
|
363
|
-
return this.directoryPages(node).page(
|
|
370
|
+
return this.directoryPages(node).page(
|
|
371
|
+
cookie,
|
|
372
|
+
byteLength,
|
|
373
|
+
node.ino,
|
|
374
|
+
parent.ino,
|
|
375
|
+
cookieTag,
|
|
376
|
+
maxBytes,
|
|
377
|
+
hardCapBytes,
|
|
378
|
+
);
|
|
364
379
|
}
|
|
365
380
|
private linkCounts = new WeakMap<Node, number>();
|
|
366
381
|
|
|
@@ -637,18 +652,23 @@ export class MemFS implements FileSystem {
|
|
|
637
652
|
this.changed(node);
|
|
638
653
|
return 0;
|
|
639
654
|
}
|
|
640
|
-
|
|
655
|
+
forEachDirEntry(handle: FileHandle, visit: (entry: DirectoryEntry) => void): void {
|
|
641
656
|
const node = this.directory(handle);
|
|
642
657
|
this.require(node, 4);
|
|
643
658
|
const parent = this.directoryParents.get(node) ?? node;
|
|
644
|
-
const entry = (name: string,
|
|
659
|
+
const entry = (name: string, item: Node): DirectoryEntry => ({
|
|
645
660
|
name,
|
|
646
|
-
ino:
|
|
647
|
-
kind: nodeKind(
|
|
661
|
+
ino: item.ino,
|
|
662
|
+
kind: nodeKind(item),
|
|
648
663
|
});
|
|
649
|
-
|
|
664
|
+
visit(entry(".", node));
|
|
665
|
+
visit(entry("..", parent));
|
|
666
|
+
for (const [name, child] of node.children) visit(entry(name, child));
|
|
667
|
+
}
|
|
668
|
+
readdir(handle: FileHandle): DirectoryEntry[] {
|
|
669
|
+
const list: DirectoryEntry[] = [];
|
|
650
670
|
// One list per call, not one list per entry or cookie. No file buffers cross the boundary.
|
|
651
|
-
|
|
671
|
+
this.forEachDirEntry(handle, (entry) => list.push(entry));
|
|
652
672
|
return list;
|
|
653
673
|
}
|
|
654
674
|
private parentOf(path: ResolvedPath): DirNode | number {
|
|
@@ -1261,10 +1281,11 @@ export class MemFS implements FileSystem {
|
|
|
1261
1281
|
}
|
|
1262
1282
|
|
|
1263
1283
|
/** Every regular file under `dir`, as absolute paths, sorted. */
|
|
1264
|
-
|
|
1284
|
+
collectBoundedFilePaths(dir = "/", onFile?: (path: string) => void): string[] {
|
|
1265
1285
|
const out: string[] = [];
|
|
1266
1286
|
const walk = (node: Node, path: string) => {
|
|
1267
1287
|
if (node.kind === "file") {
|
|
1288
|
+
onFile?.(path);
|
|
1268
1289
|
out.push(path);
|
|
1269
1290
|
return;
|
|
1270
1291
|
}
|
|
@@ -1281,6 +1302,9 @@ export class MemFS implements FileSystem {
|
|
|
1281
1302
|
if (start.node !== undefined) walk(this.nodeForHandle(start.node), start.canonicalPath);
|
|
1282
1303
|
return out.sort();
|
|
1283
1304
|
}
|
|
1305
|
+
listFiles(dir = "/"): string[] {
|
|
1306
|
+
return this.collectBoundedFilePaths(dir);
|
|
1307
|
+
}
|
|
1284
1308
|
}
|
|
1285
1309
|
|
|
1286
1310
|
/** Grow a file's buffer geometrically so many small writes stay linear. */
|