@waffle-os/runtime 0.0.153 → 0.0.155
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/net/helper.ts +114 -19
- package/browser/net/ring.ts +18 -6
- package/browser/net/runtime.ts +2 -2
- package/browser/notification-relay.ts +105 -18
- package/browser/notification-wait.ts +45 -4
- package/browser/notification-worker.ts +169 -76
- package/browser/preview.ts +15 -2
- package/core/filesystem.ts +18 -0
- package/core/wasi.ts +75 -4
- package/dist/browser/net/helper.js +83 -18
- package/dist/browser/net/ring.js +6 -5
- package/dist/browser/net/runtime.js +2 -2
- package/dist/browser/notification-relay.js +78 -15
- package/dist/browser/notification-wait.js +37 -5
- package/dist/browser/notification-worker.js +146 -67
- package/dist/core/wasi.js +62 -4
- package/dist/fs/backend.js +7 -0
- package/dist/fs/client.js +75 -17
- package/dist/fs/dirty-admission.js +15 -8
- package/dist/fs/guest-watch.js +267 -94
- package/dist/fs/indexeddb-chunks.js +88 -11
- package/dist/fs/indexeddb.js +366 -110
- package/dist/fs/memory.js +106 -20
- package/dist/fs/multitab-backend.js +291 -74
- package/dist/fs/multitab-protocol.js +1 -0
- package/dist/fs/persistent.js +70 -16
- package/dist/fs/protocol.js +15 -7
- package/dist/fs/publication-attachment.js +9 -7
- package/dist/fs/service.js +62 -9
- package/dist/fs/shared.js +100 -6
- package/dist/fs/storage.js +107 -8
- package/dist/fs/watch-service.js +97 -2
- package/dist/fs/watch-subscriptions.js +121 -8
- package/dist/proc/command-stdio.js +1 -1
- package/dist/proc/intrinsics/core.js +5 -0
- package/dist/proc/intrinsics.js +1 -0
- package/dist/tooling/program/program-transformer.json +1 -1
- package/fs/backend.ts +17 -0
- package/fs/client.ts +99 -16
- package/fs/dirty-admission.ts +22 -7
- package/fs/guest-watch.ts +299 -103
- package/fs/indexeddb-chunks.ts +103 -9
- package/fs/indexeddb.ts +477 -121
- package/fs/memory.ts +120 -19
- package/fs/multitab-backend.ts +440 -95
- package/fs/multitab-protocol.ts +1 -0
- package/fs/persistent.ts +88 -16
- package/fs/protocol.ts +26 -7
- package/fs/publication-attachment.ts +9 -7
- package/fs/service.ts +62 -7
- package/fs/shared.ts +110 -8
- package/fs/storage.ts +128 -10
- package/fs/watch-service.ts +110 -3
- package/fs/watch-subscriptions.ts +115 -7
- package/package.json +1 -1
- package/proc/command-stdio.ts +1 -1
- package/proc/intrinsics/core.ts +7 -0
- package/proc/intrinsics.ts +1 -0
- package/testing/idle-workspace-cancellation-browser.ts +22 -13
- package/testing/indexeddb-storage-browser.ts +564 -1
- package/tooling/program/program-transformer.json +1 -1
package/dist/fs/indexeddb.js
CHANGED
|
@@ -23,6 +23,7 @@ import {
|
|
|
23
23
|
parseManifest,
|
|
24
24
|
preparePayload,
|
|
25
25
|
preparePayloads,
|
|
26
|
+
putPayload,
|
|
26
27
|
copyLegacyChunks,
|
|
27
28
|
validateRange
|
|
28
29
|
} from "./indexeddb-chunks.js";
|
|
@@ -321,6 +322,36 @@ export async function readIndexedDBEpochMetadata(name, options = {}) {
|
|
|
321
322
|
} catch {}
|
|
322
323
|
}
|
|
323
324
|
}
|
|
325
|
+
|
|
326
|
+
class PayloadSizes {
|
|
327
|
+
entries = new Map;
|
|
328
|
+
add(nodes) {
|
|
329
|
+
for (const node of nodes) {
|
|
330
|
+
if (node.data === undefined)
|
|
331
|
+
continue;
|
|
332
|
+
const entry = this.entries.get(node.data);
|
|
333
|
+
if (entry)
|
|
334
|
+
entry.count++;
|
|
335
|
+
else
|
|
336
|
+
this.entries.set(node.data, { size: node.size, count: 1 });
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
remove(nodes) {
|
|
340
|
+
for (const node of nodes) {
|
|
341
|
+
if (node.data === undefined)
|
|
342
|
+
continue;
|
|
343
|
+
const entry = this.entries.get(node.data);
|
|
344
|
+
if (entry && --entry.count === 0)
|
|
345
|
+
this.entries.delete(node.data);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
size(key) {
|
|
349
|
+
return this.entries.get(key)?.size;
|
|
350
|
+
}
|
|
351
|
+
has(key) {
|
|
352
|
+
return this.entries.has(key);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
324
355
|
function validateDatabaseLayout(committed, format, keys, journalKeys, dataKeys) {
|
|
325
356
|
const empty = committed && !keys.includes("format") && keys.every((key) => key === "epoch" || key === "lastExclusiveEpoch") && !journalKeys.length && !dataKeys.length;
|
|
326
357
|
if (committed && !empty && format !== STAGED_FORMAT)
|
|
@@ -386,12 +417,138 @@ function request(value) {
|
|
|
386
417
|
result.catch(() => {});
|
|
387
418
|
return result;
|
|
388
419
|
}
|
|
420
|
+
function manifestKeyRange() {
|
|
421
|
+
return IDBKeyRange.bound("", new ArrayBuffer(0), false, true);
|
|
422
|
+
}
|
|
423
|
+
function pairManifests(names, records) {
|
|
424
|
+
if (names.length !== records.length)
|
|
425
|
+
throw new VfsError("INVALID_VFS_FORMAT", "indexeddb", "Invalid payload manifests");
|
|
426
|
+
const manifests = new Map;
|
|
427
|
+
for (const [index, key] of names.entries())
|
|
428
|
+
manifests.set(key, records[index]);
|
|
429
|
+
return manifests;
|
|
430
|
+
}
|
|
431
|
+
function formatAndManifests(metadata, data) {
|
|
432
|
+
return new Promise((resolve, reject) => {
|
|
433
|
+
const format = metadata.get("format");
|
|
434
|
+
format.onerror = requestFailed(format, reject);
|
|
435
|
+
format.onsuccess = () => {
|
|
436
|
+
if (format.result !== INDEXEDDB_FORMAT && format.result !== STAGED_FORMAT) {
|
|
437
|
+
resolve({ format: format.result });
|
|
438
|
+
return;
|
|
439
|
+
}
|
|
440
|
+
placeManifestReads(data, format.result, resolve, reject);
|
|
441
|
+
};
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
function requestFailed(failure, reject) {
|
|
445
|
+
return () => reject(failure.error ?? new Error("IndexedDB request failed"));
|
|
446
|
+
}
|
|
447
|
+
function placeManifestReads(data, format, resolve, reject) {
|
|
448
|
+
const range = manifestKeyRange();
|
|
449
|
+
const keys = data.getAllKeys(range);
|
|
450
|
+
const values = data.getAll(range);
|
|
451
|
+
keys.onerror = requestFailed(keys, reject);
|
|
452
|
+
values.onerror = requestFailed(values, reject);
|
|
453
|
+
values.onsuccess = () => {
|
|
454
|
+
try {
|
|
455
|
+
resolve({ format, manifests: pairManifests(keys.result, values.result) });
|
|
456
|
+
} catch (error) {
|
|
457
|
+
reject(error);
|
|
458
|
+
}
|
|
459
|
+
};
|
|
460
|
+
}
|
|
461
|
+
function raceAbort(promise, signal) {
|
|
462
|
+
if (!signal)
|
|
463
|
+
return promise;
|
|
464
|
+
signal.throwIfAborted();
|
|
465
|
+
return new Promise((resolve, reject) => {
|
|
466
|
+
const onAbort = () => reject(signal.reason);
|
|
467
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
468
|
+
promise.then(resolve, reject).finally(() => signal.removeEventListener("abort", onAbort));
|
|
469
|
+
});
|
|
470
|
+
}
|
|
389
471
|
function completed(tx) {
|
|
390
472
|
return new Promise((resolve, reject) => {
|
|
391
473
|
tx.oncomplete = () => resolve();
|
|
392
474
|
tx.onabort = () => reject(tx.error ?? new Error("IndexedDB transaction aborted"));
|
|
393
475
|
});
|
|
394
476
|
}
|
|
477
|
+
function corruption(parse, fallback) {
|
|
478
|
+
try {
|
|
479
|
+
return parse();
|
|
480
|
+
} catch (error) {
|
|
481
|
+
throw new PayloadCorruption(error instanceof Error ? error.message : fallback);
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
const CHUNK_READ_WINDOW = 16;
|
|
485
|
+
function placeChunkReads(store, size, read) {
|
|
486
|
+
const range = read.range;
|
|
487
|
+
const length = Math.max(0, Math.min(range.length, size - range.offset));
|
|
488
|
+
read.result = new Uint8Array(length);
|
|
489
|
+
read.end = range.offset + length;
|
|
490
|
+
read.next = Math.floor(range.offset / CHUNK_SIZE);
|
|
491
|
+
if (length)
|
|
492
|
+
placeMoreChunks(store, read);
|
|
493
|
+
}
|
|
494
|
+
function placeMoreChunks(store, read) {
|
|
495
|
+
while (read.pending < CHUNK_READ_WINDOW && read.next * CHUNK_SIZE < read.end)
|
|
496
|
+
placeChunk(store, read, read.next++);
|
|
497
|
+
}
|
|
498
|
+
function placeChunk(store, read, index) {
|
|
499
|
+
read.pending++;
|
|
500
|
+
const get = store.get([read.key, index]);
|
|
501
|
+
get.onsuccess = () => {
|
|
502
|
+
try {
|
|
503
|
+
copyChunk(get.result, index, read);
|
|
504
|
+
read.pending--;
|
|
505
|
+
placeMoreChunks(store, read);
|
|
506
|
+
if (read.pending === 0 && read.manifest)
|
|
507
|
+
read.arrived();
|
|
508
|
+
} catch (error) {
|
|
509
|
+
read.fail(error);
|
|
510
|
+
}
|
|
511
|
+
};
|
|
512
|
+
}
|
|
513
|
+
function acceptManifest(store, value, read) {
|
|
514
|
+
const key = read.key;
|
|
515
|
+
if (value === undefined)
|
|
516
|
+
throw new Error(`Missing filesystem payload: ${key}`);
|
|
517
|
+
const manifest = corruption(() => parseManifest(value, key), "Invalid payload manifest");
|
|
518
|
+
if (read.expectedSize !== undefined && manifest.size !== read.expectedSize)
|
|
519
|
+
throw new PayloadCorruption("Retained payload size mismatch");
|
|
520
|
+
read.manifest = manifest;
|
|
521
|
+
if (read.expectedSize === undefined)
|
|
522
|
+
placeChunkReads(store, manifest.size, read);
|
|
523
|
+
if (read.pending === 0)
|
|
524
|
+
read.arrived();
|
|
525
|
+
}
|
|
526
|
+
function copyChunk(record, index, read) {
|
|
527
|
+
const offset = read.range.offset;
|
|
528
|
+
const length = chunkLength(read.manifest, index);
|
|
529
|
+
const chunk = corruption(() => parseChunk(record, length, read.key), "Invalid payload chunk");
|
|
530
|
+
const base = index * CHUNK_SIZE;
|
|
531
|
+
const start = Math.max(offset, base);
|
|
532
|
+
const stop = Math.min(read.end, base + length);
|
|
533
|
+
const result = read.result;
|
|
534
|
+
result.set(chunk.bytes.subarray(start - base, stop - base), start - offset);
|
|
535
|
+
const whole = start === base && stop === base + length;
|
|
536
|
+
read.checks.push({
|
|
537
|
+
checksum: chunk.checksum,
|
|
538
|
+
bytes: whole ? result.subarray(start - offset, stop - offset) : chunk.bytes
|
|
539
|
+
});
|
|
540
|
+
}
|
|
541
|
+
const CHECKSUM_BATCH = 16;
|
|
542
|
+
async function verifyChecksums(key, checks, admit) {
|
|
543
|
+
for (let start = 0;start < checks.length; start += CHECKSUM_BATCH) {
|
|
544
|
+
admit();
|
|
545
|
+
const batch = checks.slice(start, start + CHECKSUM_BATCH);
|
|
546
|
+
const digests = await Promise.all(batch.map((check) => chunkChecksum(check.bytes)));
|
|
547
|
+
if (digests.some((digest, index) => digest !== batch[index].checksum))
|
|
548
|
+
throw new PayloadCorruption(`Invalid filesystem payload chunks: ${key}`);
|
|
549
|
+
}
|
|
550
|
+
admit();
|
|
551
|
+
}
|
|
395
552
|
function payloads(nodes, keys) {
|
|
396
553
|
for (const node of nodes)
|
|
397
554
|
if (node.data !== undefined)
|
|
@@ -407,6 +564,33 @@ function recoveryManifest(value, key) {
|
|
|
407
564
|
return;
|
|
408
565
|
}
|
|
409
566
|
}
|
|
567
|
+
function validateChunks(keys, records) {
|
|
568
|
+
const manifests = new Map;
|
|
569
|
+
const chunks = new Map;
|
|
570
|
+
for (const key of keys) {
|
|
571
|
+
if (typeof key === "string") {
|
|
572
|
+
const manifest = recoveryManifest(records.get(key), key);
|
|
573
|
+
if (manifest)
|
|
574
|
+
manifests.set(key, manifest);
|
|
575
|
+
continue;
|
|
576
|
+
}
|
|
577
|
+
if (!Array.isArray(key) || key.length !== 2 || typeof key[0] !== "string" || !validInteger(key[1]))
|
|
578
|
+
chunkError(String(key));
|
|
579
|
+
const indices = chunks.get(key[0]) ?? new Set;
|
|
580
|
+
indices.add(key[1]);
|
|
581
|
+
chunks.set(key[0], indices);
|
|
582
|
+
}
|
|
583
|
+
for (const [key, manifest] of manifests) {
|
|
584
|
+
const indices = chunks.get(key);
|
|
585
|
+
if ((indices?.size ?? 0) !== manifest.count) {
|
|
586
|
+
manifests.delete(key);
|
|
587
|
+
continue;
|
|
588
|
+
}
|
|
589
|
+
if (indices && Array.from(indices).some((index) => index >= manifest.count))
|
|
590
|
+
manifests.delete(key);
|
|
591
|
+
}
|
|
592
|
+
return manifests;
|
|
593
|
+
}
|
|
410
594
|
function referenceIndex(nodes) {
|
|
411
595
|
const references = new Map, parents = new Map;
|
|
412
596
|
for (const node of nodes)
|
|
@@ -569,6 +753,30 @@ function applyChanges(state, staged, selection) {
|
|
|
569
753
|
state.root = selection.root;
|
|
570
754
|
state.publication = selection.publication;
|
|
571
755
|
}
|
|
756
|
+
function replaceNodes(state, staged) {
|
|
757
|
+
for (const ino of Array.from(staged.removes).concat(Array.from(staged.upserts.keys()))) {
|
|
758
|
+
const previous = state.nodes.get(ino);
|
|
759
|
+
if (previous)
|
|
760
|
+
state.payloadSizes.remove([previous]);
|
|
761
|
+
}
|
|
762
|
+
state.payloadSizes.add(staged.upserts.values());
|
|
763
|
+
}
|
|
764
|
+
function installBases(state, bases) {
|
|
765
|
+
for (const slot of [0, 1]) {
|
|
766
|
+
if (state.bases[slot] === bases[slot])
|
|
767
|
+
continue;
|
|
768
|
+
state.payloadSizes.remove(state.bases[slot]?.nodes ?? []);
|
|
769
|
+
state.payloadSizes.add(bases[slot]?.nodes ?? []);
|
|
770
|
+
}
|
|
771
|
+
state.bases = bases;
|
|
772
|
+
}
|
|
773
|
+
function dropJournal(state, generation) {
|
|
774
|
+
const journal = state.journals.get(generation);
|
|
775
|
+
if (!journal)
|
|
776
|
+
return;
|
|
777
|
+
state.journals.delete(generation);
|
|
778
|
+
state.payloadSizes.remove(journal.upserts);
|
|
779
|
+
}
|
|
572
780
|
export function recoverIndex(bases, journals, payloadKeys) {
|
|
573
781
|
const generations = Array.from(new Set(Array.from(journals.keys()).concat(bases.flatMap((base) => base ? [base.generation] : []))));
|
|
574
782
|
generations.sort((a, b) => b - a);
|
|
@@ -625,6 +833,7 @@ export class IndexedDBStorage {
|
|
|
625
833
|
tail = Promise.resolve();
|
|
626
834
|
readers = new PayloadRetentions;
|
|
627
835
|
readerWork = new Set;
|
|
836
|
+
nativeReads = new Set;
|
|
628
837
|
deletingPayloads = new Set;
|
|
629
838
|
persistenceError;
|
|
630
839
|
initialized = false;
|
|
@@ -634,6 +843,7 @@ export class IndexedDBStorage {
|
|
|
634
843
|
ownershipError;
|
|
635
844
|
ownershipRetirement;
|
|
636
845
|
transactions = new Set;
|
|
846
|
+
epochWitnesses = new Set;
|
|
637
847
|
committedEnvelope;
|
|
638
848
|
committedRecord;
|
|
639
849
|
committedPending = false;
|
|
@@ -664,6 +874,32 @@ export class IndexedDBStorage {
|
|
|
664
874
|
if (this.closed || this.ownershipError)
|
|
665
875
|
return false;
|
|
666
876
|
signal?.throwIfAborted();
|
|
877
|
+
const witnessed = await this.witnessedEpoch(signal);
|
|
878
|
+
if (witnessed !== undefined)
|
|
879
|
+
return witnessed && !this.closed && !this.ownershipError;
|
|
880
|
+
return this.readEpoch(signal);
|
|
881
|
+
}
|
|
882
|
+
async witnessedEpoch(signal) {
|
|
883
|
+
for (const witness of Array.from(this.epochWitnesses)) {
|
|
884
|
+
const result = await raceAbort(witness, signal);
|
|
885
|
+
if (result !== undefined)
|
|
886
|
+
return result;
|
|
887
|
+
}
|
|
888
|
+
return;
|
|
889
|
+
}
|
|
890
|
+
witnessEpoch(tx, read) {
|
|
891
|
+
const { promise: witness, resolve } = Promise.withResolvers();
|
|
892
|
+
const end = () => {
|
|
893
|
+
this.epochWitnesses.delete(witness);
|
|
894
|
+
resolve(undefined);
|
|
895
|
+
};
|
|
896
|
+
read.addEventListener("success", () => resolve(read.result === this.epoch));
|
|
897
|
+
read.addEventListener("error", () => resolve(undefined));
|
|
898
|
+
tx.addEventListener("complete", end);
|
|
899
|
+
tx.addEventListener("abort", end);
|
|
900
|
+
this.epochWitnesses.add(witness);
|
|
901
|
+
}
|
|
902
|
+
async readEpoch(signal) {
|
|
667
903
|
let tx;
|
|
668
904
|
const onAbort = () => {
|
|
669
905
|
try {
|
|
@@ -907,7 +1143,7 @@ export class IndexedDBStorage {
|
|
|
907
1143
|
} catch {}
|
|
908
1144
|
}
|
|
909
1145
|
this.db.close();
|
|
910
|
-
this.ownershipRetirement = Promise.allSettled(Array.from(this.readerWork).concat([this.tail])).then(() => this.retireMaintenance());
|
|
1146
|
+
this.ownershipRetirement = Promise.allSettled(Array.from(this.readerWork).concat([this.tail])).then(() => this.joinNativeReads()).then(() => this.retireMaintenance());
|
|
911
1147
|
this.ownershipRetirement.catch(() => {});
|
|
912
1148
|
this.onOwnershipLost?.();
|
|
913
1149
|
return error;
|
|
@@ -976,10 +1212,11 @@ export class IndexedDBStorage {
|
|
|
976
1212
|
chunkError(key);
|
|
977
1213
|
if (value.length !== this.payloadSize(key))
|
|
978
1214
|
chunkError(key);
|
|
1215
|
+
signal?.throwIfAborted();
|
|
1216
|
+
this.assertOwner();
|
|
979
1217
|
return value;
|
|
980
1218
|
}
|
|
981
|
-
|
|
982
|
-
return this.readChunks(key, manifest, { offset: 0, length: manifest.size, signal });
|
|
1219
|
+
return this.readPayload(key, { offset: 0, length: Number.MAX_SAFE_INTEGER, signal }, this.knownSize(key));
|
|
983
1220
|
});
|
|
984
1221
|
}
|
|
985
1222
|
retainPayload(key, expectedSize, metadata) {
|
|
@@ -996,23 +1233,10 @@ export class IndexedDBStorage {
|
|
|
996
1233
|
throw new Error("Missing retained filesystem payload");
|
|
997
1234
|
}
|
|
998
1235
|
payloadSize(key) {
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
if (node.data === key)
|
|
1004
|
-
return node.size;
|
|
1005
|
-
for (const base of this.state.bases) {
|
|
1006
|
-
for (const node of base?.nodes ?? [])
|
|
1007
|
-
if (node.data === key)
|
|
1008
|
-
return node.size;
|
|
1009
|
-
}
|
|
1010
|
-
for (const journal of this.state.journals.values()) {
|
|
1011
|
-
for (const node of journal.upserts)
|
|
1012
|
-
if (node.data === key)
|
|
1013
|
-
return node.size;
|
|
1014
|
-
}
|
|
1015
|
-
return;
|
|
1236
|
+
return this.readers.size(key) ?? this.state.payloadSizes.size(key);
|
|
1237
|
+
}
|
|
1238
|
+
knownSize(key) {
|
|
1239
|
+
return this.readers.size(key) ?? this.state?.payloadSizes.size(key);
|
|
1016
1240
|
}
|
|
1017
1241
|
resources() {
|
|
1018
1242
|
return { ...this.readers.resources(), indexeddbReaderWork: this.readerWork.size };
|
|
@@ -1020,11 +1244,8 @@ export class IndexedDBStorage {
|
|
|
1020
1244
|
retainedRange(key, range) {
|
|
1021
1245
|
if (this.closed)
|
|
1022
1246
|
return Promise.reject(new Error("Filesystem storage is closed"));
|
|
1023
|
-
const
|
|
1024
|
-
|
|
1025
|
-
throw new PayloadCorruption("Retained payload size mismatch");
|
|
1026
|
-
return this.readChunks(key, manifest, range);
|
|
1027
|
-
});
|
|
1247
|
+
const size = this.readers.size(key);
|
|
1248
|
+
const work = size === undefined ? Promise.reject(new PayloadCorruption("Retained payload size mismatch")) : this.readPayload(key, range, size);
|
|
1028
1249
|
this.readerWork.add(work);
|
|
1029
1250
|
work.then(() => this.readerWork.delete(work), () => this.readerWork.delete(work));
|
|
1030
1251
|
return work;
|
|
@@ -1041,8 +1262,7 @@ export class IndexedDBStorage {
|
|
|
1041
1262
|
return this.enqueue(async () => {
|
|
1042
1263
|
range.signal?.throwIfAborted();
|
|
1043
1264
|
await this.initialize(range.signal);
|
|
1044
|
-
|
|
1045
|
-
return this.readChunks(key, manifest, range);
|
|
1265
|
+
return this.readPayload(key, range, this.knownSize(key));
|
|
1046
1266
|
}).catch((error) => {
|
|
1047
1267
|
range.signal?.throwIfAborted();
|
|
1048
1268
|
throw error;
|
|
@@ -1075,43 +1295,72 @@ export class IndexedDBStorage {
|
|
|
1075
1295
|
this.transactions.delete(tx);
|
|
1076
1296
|
}
|
|
1077
1297
|
}
|
|
1078
|
-
async
|
|
1079
|
-
const
|
|
1080
|
-
|
|
1081
|
-
|
|
1298
|
+
async readPayload(key, range, expectedSize) {
|
|
1299
|
+
const signal = range.signal;
|
|
1300
|
+
signal?.throwIfAborted();
|
|
1301
|
+
this.assertOwner();
|
|
1302
|
+
const tx = this.db.transaction("data", "readonly");
|
|
1303
|
+
this.transactions.add(tx);
|
|
1304
|
+
const abort = () => {
|
|
1305
|
+
try {
|
|
1306
|
+
tx.abort();
|
|
1307
|
+
} catch {}
|
|
1308
|
+
};
|
|
1309
|
+
signal?.addEventListener("abort", abort, { once: true });
|
|
1310
|
+
const settled = completed(tx);
|
|
1311
|
+
const native = settled.then(() => this.nativeSettled(tx, native), () => this.nativeSettled(tx, native));
|
|
1312
|
+
this.nativeReads.add(native);
|
|
1313
|
+
const arrival = Promise.withResolvers();
|
|
1314
|
+
const read = {
|
|
1315
|
+
key,
|
|
1316
|
+
range,
|
|
1317
|
+
expectedSize,
|
|
1318
|
+
end: 0,
|
|
1319
|
+
next: 0,
|
|
1320
|
+
checks: [],
|
|
1321
|
+
pending: 0,
|
|
1322
|
+
arrived: arrival.resolve,
|
|
1323
|
+
fail: (error) => {
|
|
1324
|
+
read.failure ??= { cause: error };
|
|
1325
|
+
abort();
|
|
1326
|
+
}
|
|
1327
|
+
};
|
|
1082
1328
|
try {
|
|
1083
|
-
|
|
1329
|
+
this.placePayloadReads(tx, read);
|
|
1330
|
+
await Promise.race([arrival.promise, settled.then(() => arrival.promise)]);
|
|
1084
1331
|
} catch (error) {
|
|
1085
|
-
|
|
1332
|
+
abort();
|
|
1333
|
+
throw signal?.aborted ? signal.reason : this.ownershipError ?? read.failure?.cause ?? error;
|
|
1334
|
+
} finally {
|
|
1335
|
+
signal?.removeEventListener("abort", abort);
|
|
1086
1336
|
}
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
const length = Math.max(0, Math.min(range.length, manifest.size - range.offset));
|
|
1090
|
-
const result = new Uint8Array(length);
|
|
1091
|
-
range.signal?.throwIfAborted();
|
|
1092
|
-
if (!length)
|
|
1093
|
-
return result;
|
|
1094
|
-
const end = range.offset + length;
|
|
1095
|
-
for (let index = Math.floor(range.offset / CHUNK_SIZE);index * CHUNK_SIZE < end; index++) {
|
|
1096
|
-
const record = await this.readRecord([key, index], range.signal);
|
|
1097
|
-
const chunk = this.validChunk(record, chunkLength(manifest, index), key);
|
|
1098
|
-
if (await chunkChecksum(chunk.bytes) !== chunk.checksum)
|
|
1099
|
-
throw new PayloadCorruption(`Invalid filesystem payload chunks: ${key}`);
|
|
1100
|
-
range.signal?.throwIfAborted();
|
|
1337
|
+
const admit = () => {
|
|
1338
|
+
signal?.throwIfAborted();
|
|
1101
1339
|
this.assertOwner();
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1340
|
+
};
|
|
1341
|
+
await verifyChecksums(key, read.checks, admit);
|
|
1342
|
+
admit();
|
|
1343
|
+
return read.result;
|
|
1344
|
+
}
|
|
1345
|
+
placePayloadReads(tx, read) {
|
|
1346
|
+
const store = tx.objectStore("data");
|
|
1347
|
+
const manifest = store.get(read.key);
|
|
1348
|
+
manifest.onsuccess = () => {
|
|
1349
|
+
try {
|
|
1350
|
+
acceptManifest(store, manifest.result, read);
|
|
1351
|
+
} catch (error) {
|
|
1352
|
+
read.fail(error);
|
|
1353
|
+
}
|
|
1354
|
+
};
|
|
1355
|
+
if (read.expectedSize !== undefined)
|
|
1356
|
+
placeChunkReads(store, read.expectedSize, read);
|
|
1108
1357
|
}
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1358
|
+
joinNativeReads() {
|
|
1359
|
+
return Promise.all(Array.from(this.nativeReads));
|
|
1360
|
+
}
|
|
1361
|
+
nativeSettled(tx, native) {
|
|
1362
|
+
this.transactions.delete(tx);
|
|
1363
|
+
this.nativeReads.delete(native);
|
|
1115
1364
|
}
|
|
1116
1365
|
recoverCommitted() {
|
|
1117
1366
|
return this.enqueue(async () => {
|
|
@@ -1218,6 +1467,7 @@ export class IndexedDBStorage {
|
|
|
1218
1467
|
if (!this.closing) {
|
|
1219
1468
|
this.closed = true;
|
|
1220
1469
|
this.closing = Promise.all(Array.from(this.readerWork).concat([this.tail])).then(async () => {
|
|
1470
|
+
await this.joinNativeReads();
|
|
1221
1471
|
this.db.close();
|
|
1222
1472
|
if (this.readOnly) {
|
|
1223
1473
|
this.state = undefined;
|
|
@@ -1251,7 +1501,7 @@ export class IndexedDBStorage {
|
|
|
1251
1501
|
const settled = completed(tx);
|
|
1252
1502
|
settled.catch(() => {});
|
|
1253
1503
|
signal?.addEventListener("abort", abort, { once: true });
|
|
1254
|
-
const metadata = tx.objectStore("metadata"), journal = tx.objectStore("journal");
|
|
1504
|
+
const metadata = tx.objectStore("metadata"), journal = tx.objectStore("journal"), data = tx.objectStore("data");
|
|
1255
1505
|
try {
|
|
1256
1506
|
const records = await Promise.all([
|
|
1257
1507
|
request(metadata.get(0)),
|
|
@@ -1259,8 +1509,8 @@ export class IndexedDBStorage {
|
|
|
1259
1509
|
request(metadata.getAllKeys()),
|
|
1260
1510
|
request(journal.getAll()),
|
|
1261
1511
|
request(journal.getAllKeys()),
|
|
1262
|
-
request(
|
|
1263
|
-
|
|
1512
|
+
request(data.getAllKeys()),
|
|
1513
|
+
formatAndManifests(metadata, data),
|
|
1264
1514
|
request(metadata.get("commit")),
|
|
1265
1515
|
request(metadata.get("active")),
|
|
1266
1516
|
settled
|
|
@@ -1287,7 +1537,7 @@ export class IndexedDBStorage {
|
|
|
1287
1537
|
journalValues,
|
|
1288
1538
|
journalKeys,
|
|
1289
1539
|
dataKeys,
|
|
1290
|
-
format,
|
|
1540
|
+
{ format, manifests },
|
|
1291
1541
|
storedCommit,
|
|
1292
1542
|
active
|
|
1293
1543
|
] = await this.indexRecords(signal);
|
|
@@ -1299,7 +1549,7 @@ export class IndexedDBStorage {
|
|
|
1299
1549
|
else if (Array.isArray(key) && typeof key[0] === "string")
|
|
1300
1550
|
payloadKeys.add(key[0]);
|
|
1301
1551
|
}
|
|
1302
|
-
const physical =
|
|
1552
|
+
const physical = manifests && validateChunks(dataKeys, manifests);
|
|
1303
1553
|
const available = physical ? new Set(physical.keys()) : payloadKeys;
|
|
1304
1554
|
const decoded = await Promise.all([decodeIndex(first), decodeIndex(second)]);
|
|
1305
1555
|
signal?.throwIfAborted();
|
|
@@ -1322,6 +1572,13 @@ export class IndexedDBStorage {
|
|
|
1322
1572
|
}
|
|
1323
1573
|
if (newest) {
|
|
1324
1574
|
const nodes = new Map(newest.nodes.map((node) => [node.ino, node]));
|
|
1575
|
+
const payloadSizes = new PayloadSizes;
|
|
1576
|
+
payloadSizes.add(nodes.values());
|
|
1577
|
+
for (const base of bases)
|
|
1578
|
+
if (base)
|
|
1579
|
+
payloadSizes.add(base.nodes);
|
|
1580
|
+
for (const journal of journals.values())
|
|
1581
|
+
payloadSizes.add(journal.upserts);
|
|
1325
1582
|
const lastBase = Math.max(...bases.filter((base) => !!base).map((base) => base.generation));
|
|
1326
1583
|
this.state = {
|
|
1327
1584
|
generation: newest.generation,
|
|
@@ -1333,6 +1590,7 @@ export class IndexedDBStorage {
|
|
|
1333
1590
|
journals,
|
|
1334
1591
|
journalKeys,
|
|
1335
1592
|
payloadKeys,
|
|
1593
|
+
payloadSizes,
|
|
1336
1594
|
sinceCompaction: Array.from(journals.values()).filter((entry) => entry.generation > lastBase).length
|
|
1337
1595
|
};
|
|
1338
1596
|
}
|
|
@@ -1365,46 +1623,18 @@ export class IndexedDBStorage {
|
|
|
1365
1623
|
this.committedRecord = await encodeCommit(envelope);
|
|
1366
1624
|
}
|
|
1367
1625
|
recordRecovery(state, extra) {
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1626
|
+
if (!extra?.length) {
|
|
1627
|
+
this.readers.recovery(state.payloadSizes);
|
|
1628
|
+
return;
|
|
1629
|
+
}
|
|
1630
|
+
const extraRoots = new Set(extra);
|
|
1631
|
+
this.readers.recovery({
|
|
1632
|
+
has: (key) => extraRoots.has(key) || state.payloadSizes.has(key)
|
|
1633
|
+
});
|
|
1376
1634
|
}
|
|
1377
1635
|
matchSizes(nodes, manifests) {
|
|
1378
1636
|
return !manifests || nodes.every((node) => node.data === undefined || manifests.get(node.data)?.size === node.size);
|
|
1379
1637
|
}
|
|
1380
|
-
async validateChunks(keys, signal) {
|
|
1381
|
-
const manifests = new Map;
|
|
1382
|
-
const chunks = new Map;
|
|
1383
|
-
for (const key of keys) {
|
|
1384
|
-
signal?.throwIfAborted();
|
|
1385
|
-
if (typeof key === "string") {
|
|
1386
|
-
const manifest = recoveryManifest(await this.readRecord(key, signal), key);
|
|
1387
|
-
if (manifest)
|
|
1388
|
-
manifests.set(key, manifest);
|
|
1389
|
-
continue;
|
|
1390
|
-
}
|
|
1391
|
-
if (!Array.isArray(key) || key.length !== 2 || typeof key[0] !== "string" || !validInteger(key[1]))
|
|
1392
|
-
chunkError(String(key));
|
|
1393
|
-
const indices = chunks.get(key[0]) ?? new Set;
|
|
1394
|
-
indices.add(key[1]);
|
|
1395
|
-
chunks.set(key[0], indices);
|
|
1396
|
-
}
|
|
1397
|
-
for (const [key, manifest] of manifests) {
|
|
1398
|
-
const indices = chunks.get(key);
|
|
1399
|
-
if ((indices?.size ?? 0) !== manifest.count) {
|
|
1400
|
-
manifests.delete(key);
|
|
1401
|
-
continue;
|
|
1402
|
-
}
|
|
1403
|
-
if (indices && Array.from(indices).some((index) => index >= manifest.count))
|
|
1404
|
-
manifests.delete(key);
|
|
1405
|
-
}
|
|
1406
|
-
return manifests;
|
|
1407
|
-
}
|
|
1408
1638
|
async fencedWrite(work, signal) {
|
|
1409
1639
|
signal?.throwIfAborted();
|
|
1410
1640
|
this.assertOwner();
|
|
@@ -1420,6 +1650,7 @@ export class IndexedDBStorage {
|
|
|
1420
1650
|
const done = completed(tx);
|
|
1421
1651
|
const metadata = tx.objectStore("metadata");
|
|
1422
1652
|
const epoch = metadata.get("epoch");
|
|
1653
|
+
this.witnessEpoch(tx, epoch);
|
|
1423
1654
|
epoch.onsuccess = () => {
|
|
1424
1655
|
try {
|
|
1425
1656
|
if (epoch.result !== this.epoch)
|
|
@@ -1480,7 +1711,12 @@ export class IndexedDBStorage {
|
|
|
1480
1711
|
async commit(batch, committed) {
|
|
1481
1712
|
if (this.committedMode !== !!committed)
|
|
1482
1713
|
throw new Error("Committed storage requires a complete durable group");
|
|
1483
|
-
const
|
|
1714
|
+
const checkActive = () => {
|
|
1715
|
+
committed?.control.checkAbort();
|
|
1716
|
+
this.assertOwner();
|
|
1717
|
+
};
|
|
1718
|
+
const prepared = await preparePayloads(batch.data, true, checkActive);
|
|
1719
|
+
checkActive();
|
|
1484
1720
|
const staged = stageChanges(this.state, batch), old = this.state;
|
|
1485
1721
|
const discarded = new Set(old?.journalKeys.filter((key) => typeof key !== "number" || key > old.generation));
|
|
1486
1722
|
const compact = !old || old.sinceCompaction + 1 >= this.compactEvery;
|
|
@@ -1593,26 +1829,43 @@ export class IndexedDBStorage {
|
|
|
1593
1829
|
abort(error);
|
|
1594
1830
|
}
|
|
1595
1831
|
};
|
|
1596
|
-
|
|
1832
|
+
const newKeys = [];
|
|
1833
|
+
const verifyKeys = [];
|
|
1834
|
+
for (const key of checking) {
|
|
1835
|
+
if (prepared.has(key) && !old?.payloadKeys.has(key))
|
|
1836
|
+
newKeys.push(key);
|
|
1837
|
+
else
|
|
1838
|
+
verifyKeys.push(key);
|
|
1839
|
+
}
|
|
1840
|
+
let remaining = 1 + verifyKeys.length + pinChecks.size + (committed ? 1 : 0);
|
|
1597
1841
|
const checked = () => {
|
|
1598
1842
|
if (--remaining === 0)
|
|
1599
1843
|
finish();
|
|
1600
1844
|
};
|
|
1601
1845
|
const checkEpoch = tx.objectStore("metadata").get("epoch");
|
|
1846
|
+
this.witnessEpoch(tx, checkEpoch);
|
|
1602
1847
|
checkEpoch.onsuccess = () => {
|
|
1603
1848
|
if (checkEpoch.result !== this.epoch) {
|
|
1604
1849
|
abort(this.loseOwnership());
|
|
1605
1850
|
return;
|
|
1606
1851
|
}
|
|
1607
|
-
|
|
1852
|
+
try {
|
|
1853
|
+
for (const key of newKeys) {
|
|
1854
|
+
const supplied = prepared.get(key);
|
|
1855
|
+
const size = expected.get(key);
|
|
1856
|
+
if (size !== undefined && supplied.manifest.size !== size)
|
|
1857
|
+
throw new Error(`Invalid filesystem payload size: ${key}`);
|
|
1858
|
+
putPayload(data, key, supplied);
|
|
1859
|
+
}
|
|
1608
1860
|
checked();
|
|
1861
|
+
} catch (error) {
|
|
1862
|
+
abort(error);
|
|
1863
|
+
}
|
|
1609
1864
|
};
|
|
1610
1865
|
if (committed)
|
|
1611
1866
|
checkCommitHead(tx, committed, checked, abort);
|
|
1612
1867
|
try {
|
|
1613
|
-
|
|
1614
|
-
finish();
|
|
1615
|
-
for (const key of checking) {
|
|
1868
|
+
for (const key of verifyKeys) {
|
|
1616
1869
|
const get = data.get(key);
|
|
1617
1870
|
get.onsuccess = () => {
|
|
1618
1871
|
try {
|
|
@@ -1641,28 +1894,31 @@ export class IndexedDBStorage {
|
|
|
1641
1894
|
nodes: new Map,
|
|
1642
1895
|
references: new Map,
|
|
1643
1896
|
parents: new Map,
|
|
1644
|
-
bases,
|
|
1897
|
+
bases: [undefined, undefined],
|
|
1645
1898
|
journals: new Map,
|
|
1646
1899
|
journalKeys: [],
|
|
1647
1900
|
payloadKeys: new Set,
|
|
1901
|
+
payloadSizes: new PayloadSizes,
|
|
1648
1902
|
sinceCompaction: 0
|
|
1649
1903
|
};
|
|
1650
1904
|
for (const key of discarded)
|
|
1651
1905
|
if (typeof key === "number")
|
|
1652
|
-
state
|
|
1906
|
+
dropJournal(state, key);
|
|
1653
1907
|
state.journalKeys = state.journalKeys.filter((key) => !discarded.has(key));
|
|
1908
|
+
replaceNodes(state, staged);
|
|
1654
1909
|
applyChanges(state, staged, batch);
|
|
1655
|
-
state
|
|
1910
|
+
installBases(state, bases);
|
|
1656
1911
|
if (entry) {
|
|
1657
1912
|
state.journals.set(entry.generation, entry);
|
|
1658
1913
|
state.journalKeys.push(entry.generation);
|
|
1914
|
+
state.payloadSizes.add(entry.upserts);
|
|
1659
1915
|
}
|
|
1660
1916
|
for (const key of batch.data.keys())
|
|
1661
1917
|
state.payloadKeys.add(key);
|
|
1662
1918
|
if (compact) {
|
|
1663
1919
|
for (const key of state.journals.keys())
|
|
1664
1920
|
if (key <= cutoff)
|
|
1665
|
-
state
|
|
1921
|
+
dropJournal(state, key);
|
|
1666
1922
|
state.journalKeys = state.journalKeys.filter((key) => typeof key !== "number" || key > cutoff);
|
|
1667
1923
|
for (const key of deletedReaders) {
|
|
1668
1924
|
state.payloadKeys.delete(key);
|