@superbee/core 0.2.0-pre.3 → 0.2.0-pre.5
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/dist/backend.d.ts +2 -1
- package/dist/backend.d.ts.map +1 -1
- package/dist/backend.js +34 -32
- package/dist/backend.js.map +1 -1
- package/dist/bundle.d.ts +2 -1
- package/dist/bundle.d.ts.map +1 -1
- package/dist/bundle.js +3 -3
- package/dist/bundle.js.map +1 -1
- package/dist/filesystem-host.d.ts +15 -0
- package/dist/filesystem-host.d.ts.map +1 -0
- package/dist/filesystem-host.js +20 -0
- package/dist/filesystem-host.js.map +1 -0
- package/dist/filesystem-identity.d.ts +6 -4
- package/dist/filesystem-identity.d.ts.map +1 -1
- package/dist/filesystem-identity.js +106 -96
- package/dist/filesystem-identity.js.map +1 -1
- package/dist/filesystem-lock.d.ts +6 -3
- package/dist/filesystem-lock.d.ts.map +1 -1
- package/dist/filesystem-lock.js +41 -56
- package/dist/filesystem-lock.js.map +1 -1
- package/dist/filesystem.d.ts +17 -0
- package/dist/filesystem.d.ts.map +1 -0
- package/dist/filesystem.js +18 -0
- package/dist/filesystem.js.map +1 -0
- package/dist/governed-body-write.d.ts +99 -0
- package/dist/governed-body-write.d.ts.map +1 -0
- package/dist/governed-body-write.js +244 -0
- package/dist/governed-body-write.js.map +1 -0
- package/dist/host-policy-snapshot.d.ts +9 -0
- package/dist/host-policy-snapshot.d.ts.map +1 -0
- package/dist/host-policy-snapshot.js +33 -0
- package/dist/host-policy-snapshot.js.map +1 -0
- package/dist/index.d.ts +12 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -2
- package/dist/index.js.map +1 -1
- package/dist/indexeddb-backend.d.ts +23 -6
- package/dist/indexeddb-backend.d.ts.map +1 -1
- package/dist/indexeddb-backend.js +395 -115
- package/dist/indexeddb-backend.js.map +1 -1
- package/dist/journaled-backend.d.ts +148 -9
- package/dist/journaled-backend.d.ts.map +1 -1
- package/dist/journaled-backend.js +209 -5
- package/dist/journaled-backend.js.map +1 -1
- package/dist/remote-backend.d.ts +9 -54
- package/dist/remote-backend.d.ts.map +1 -1
- package/dist/remote-backend.js +13 -173
- package/dist/remote-backend.js.map +1 -1
- package/dist/remote-error.d.ts +22 -0
- package/dist/remote-error.d.ts.map +1 -0
- package/dist/remote-error.js +29 -0
- package/dist/remote-error.js.map +1 -0
- package/dist/remote-parsers.d.ts +86 -0
- package/dist/remote-parsers.d.ts.map +1 -0
- package/dist/remote-parsers.js +181 -0
- package/dist/remote-parsers.js.map +1 -0
- package/dist/remote.d.ts +5 -2
- package/dist/remote.d.ts.map +1 -1
- package/dist/remote.js +5 -1
- package/dist/remote.js.map +1 -1
- package/dist/storage.d.ts +1 -1
- package/dist/storage.d.ts.map +1 -1
- package/dist/storage.js.map +1 -1
- package/dist/uncertain-write.d.ts +4 -2
- package/dist/uncertain-write.d.ts.map +1 -1
- package/dist/uncertain-write.js +6 -3
- package/dist/uncertain-write.js.map +1 -1
- package/package.json +10 -2
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
import { resolveContentType } from "./content-type.js";
|
|
33
33
|
import { MalformedDocumentError, parseMarkdown, stringifyDoc } from "./frontmatter.js";
|
|
34
34
|
import { mutationActorFromFrontmatter } from "./mutation-attribution.js";
|
|
35
|
-
import { assertJournalResolutionOptions, assertJournalSnapshot, IntentHoldConflict, IntentStateConflict, } from "./journaled-backend.js";
|
|
35
|
+
import { assertJournalGuard, assertJournalIntentChanges, assertJournalMetaChanges, assertMetaWrite, captureJournalGuardOption, captureJournalDeleteOptions, captureJournalWriteOptions, captureIntentUpdate, captureMetaWrite, captureJournalValue, JournalGuardConflict, assertJournalResolutionOptions, assertJournalSnapshot, IntentHoldConflict, IntentStateConflict, } from "./journaled-backend.js";
|
|
36
36
|
import { assertSafeBlobKey, assertSafeConceptId, assertSafeReservedDir, assertSafeReservedFilename, compareStorageKeys, pathFromConceptId } from "./paths.js";
|
|
37
37
|
import { parseLeadingFrontmatter } from "./portable-frontmatter.js";
|
|
38
38
|
import { blobVersion, defaultActor, VersionConflict, versionOfBytes } from "./versioning.js";
|
|
@@ -105,12 +105,31 @@ function editionOf(index) {
|
|
|
105
105
|
throw error;
|
|
106
106
|
}
|
|
107
107
|
}
|
|
108
|
+
/**
|
|
109
|
+
* One listing row from a stored record: the frontmatter is what `readWithJournal` parses for
|
|
110
|
+
* the record under `edition` (the split takes the body as a substring it never parses), or
|
|
111
|
+
* `null` with the parser's error when the leading block does not parse.
|
|
112
|
+
*/
|
|
113
|
+
function headOf(record, intents, meta, edition) {
|
|
114
|
+
const head = { id: record.id, version: record.version, updatedBy: record.updatedBy, updatedAt: record.updatedAt, raw: record.raw, intents, meta };
|
|
115
|
+
let frontmatter;
|
|
116
|
+
try {
|
|
117
|
+
frontmatter = parseMarkdown(record.raw, pathFromConceptId(record.id), { okfVersion: edition }).frontmatter;
|
|
118
|
+
}
|
|
119
|
+
catch (error) {
|
|
120
|
+
if (!(error instanceof MalformedDocumentError))
|
|
121
|
+
throw error;
|
|
122
|
+
return { ...head, frontmatter: null, malformed: error };
|
|
123
|
+
}
|
|
124
|
+
return { ...head, frontmatter };
|
|
125
|
+
}
|
|
108
126
|
/**
|
|
109
127
|
* A persistent browser-local OKF store implementing the {@link JournaledBackend} contract over
|
|
110
128
|
* IndexedDB. One instance per bundle per JavaScript realm; several instances over the same
|
|
111
129
|
* database name are peers whose conditional writes are serialized by the database.
|
|
112
130
|
*/
|
|
113
131
|
export class IndexedDbBackend {
|
|
132
|
+
journalSnapshotCas = true;
|
|
114
133
|
/**
|
|
115
134
|
* What a resolved write promises: the IndexedDB transaction reported `complete`. Whether the
|
|
116
135
|
* committed pages survive power loss is the browser's and the operating system's decision.
|
|
@@ -251,9 +270,47 @@ export class IndexedDbBackend {
|
|
|
251
270
|
* report the transaction's AbortError and the cause would be lost.
|
|
252
271
|
*/
|
|
253
272
|
async #transact(stores, mode, body) {
|
|
254
|
-
|
|
273
|
+
// Starting a transaction throws synchronously on a handle closed between the open resolving
|
|
274
|
+
// and this line, which is what a `close()` issued in the same turn as an in-flight call
|
|
275
|
+
// produces. `close()` promises that the next operation reopens lazily, and the cold path gets
|
|
276
|
+
// that for free by joining the next open; this is the warm equivalent. Retrying is safe here
|
|
277
|
+
// and only here: the throw means no transaction exists, so nothing was attempted and the
|
|
278
|
+
// retry cannot repeat work. A second failure is the host's own answer and propagates, which
|
|
279
|
+
// is also what a genuinely bad store name does after one wasted reopen.
|
|
280
|
+
let db = await this.#open();
|
|
281
|
+
let transaction;
|
|
282
|
+
try {
|
|
283
|
+
transaction = db.transaction(stores, mode);
|
|
284
|
+
}
|
|
285
|
+
catch (firstFailure) {
|
|
286
|
+
if (this.#db === db)
|
|
287
|
+
this.close();
|
|
288
|
+
db = await this.#open();
|
|
289
|
+
try {
|
|
290
|
+
transaction = db.transaction(stores, mode);
|
|
291
|
+
}
|
|
292
|
+
catch (retryFailure) {
|
|
293
|
+
// The retry's failure is what the caller acts on. When the two differ the first one was
|
|
294
|
+
// never about a closed handle, so it is the more informative of the pair and is kept as
|
|
295
|
+
// the cause rather than discarded.
|
|
296
|
+
const failure = asError(retryFailure);
|
|
297
|
+
if (failure !== firstFailure && failure.cause == null) {
|
|
298
|
+
// Attaching a cause is a convenience and is never worth replacing the host's own error.
|
|
299
|
+
// This error belongs to the host, which may hand back a frozen one or one whose `cause`
|
|
300
|
+
// is a getter; assigning to either throws in strict mode, and that TypeError would reach
|
|
301
|
+
// the caller in place of the real failure.
|
|
302
|
+
try {
|
|
303
|
+
failure.cause = firstFailure;
|
|
304
|
+
}
|
|
305
|
+
catch {
|
|
306
|
+
// Surface the host's error exactly as it came.
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
throw failure;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
255
312
|
return new Promise((resolve, reject) => {
|
|
256
|
-
const tx =
|
|
313
|
+
const tx = transaction;
|
|
257
314
|
let result = null;
|
|
258
315
|
let failure = null;
|
|
259
316
|
tx.oncomplete = () => {
|
|
@@ -481,6 +538,33 @@ export class IndexedDbBackend {
|
|
|
481
538
|
// pending-change record and no record exists for an edit that never committed. Settling an
|
|
482
539
|
// intent is a compare-and-swap on the intent's own `state`, so a stale realm cannot settle
|
|
483
540
|
// an intent another realm already settled.
|
|
541
|
+
/** Read and compare a complete guard inside its caller's still-active transaction. */
|
|
542
|
+
#checkJournalGuard(tx, expected, next, fail, guard) {
|
|
543
|
+
if (expected === undefined) {
|
|
544
|
+
next();
|
|
545
|
+
return;
|
|
546
|
+
}
|
|
547
|
+
const current = { target: expected.target, document: null, intents: [], meta: [] };
|
|
548
|
+
let pending = 2 + expected.meta.length;
|
|
549
|
+
const finish = () => { if (--pending === 0) {
|
|
550
|
+
assertJournalGuard(expected, current);
|
|
551
|
+
next();
|
|
552
|
+
} };
|
|
553
|
+
const read = (request, accept) => {
|
|
554
|
+
request.onerror = () => fail(requestError(request, "IndexedDB journal guard read failed"));
|
|
555
|
+
request.onsuccess = guard(() => { accept(request.result); finish(); });
|
|
556
|
+
};
|
|
557
|
+
read(tx.objectStore(DOCUMENTS).get(expected.target), value => {
|
|
558
|
+
const row = value;
|
|
559
|
+
current.document = row ? { version: row.version, raw: row.raw } : null;
|
|
560
|
+
});
|
|
561
|
+
read(tx.objectStore(INTENTS).getAll(), value => { current.intents = value.filter(row => row.target === expected.target); });
|
|
562
|
+
for (const entry of expected.meta)
|
|
563
|
+
read(tx.objectStore(META).get(entry.key), value => {
|
|
564
|
+
const row = value;
|
|
565
|
+
current.meta.push({ key: entry.key, expected: row === undefined ? { present: false } : { present: true, value: row.value } });
|
|
566
|
+
});
|
|
567
|
+
}
|
|
484
568
|
/**
|
|
485
569
|
* One transaction over documents, intents, and meta: the document compare-and-swap of
|
|
486
570
|
* {@link write}, plus (optionally) deleting a superseded intent, recording a new intent for
|
|
@@ -491,15 +575,25 @@ export class IndexedDbBackend {
|
|
|
491
575
|
* {@link IntentHoldConflict} before the document is touched.
|
|
492
576
|
*/
|
|
493
577
|
async writeJournaled(id, doc, options = {}) {
|
|
578
|
+
const snapshotGuard = captureJournalGuardOption(options, id);
|
|
579
|
+
if (snapshotGuard !== undefined) {
|
|
580
|
+
options = captureJournalWriteOptions(options);
|
|
581
|
+
doc = captureJournalValue(doc);
|
|
582
|
+
}
|
|
494
583
|
assertSafeConceptId(id);
|
|
495
584
|
assertJournalResolutionOptions(id, options);
|
|
496
585
|
const raw = stringifyDoc(doc.frontmatter, doc.body ?? "");
|
|
497
586
|
const version = versionOfBytes(raw);
|
|
587
|
+
if (snapshotGuard && (doc.id !== id || (options.intent && options.intent.target !== id)))
|
|
588
|
+
throw new JournalGuardConflict(id);
|
|
589
|
+
const suppliedMeta = typeof options.meta === "function" ? options.meta({ version, raw }) : options.meta;
|
|
590
|
+
assertJournalMetaChanges(snapshotGuard, suppliedMeta ?? [], options.removeMeta);
|
|
591
|
+
options = structuredClone({ ...options, meta: suppliedMeta, ...(snapshotGuard ? { guard: snapshotGuard } : {}) });
|
|
498
592
|
const updatedBy = options.actor?.trim() || defaultActor();
|
|
499
593
|
const now = new Date().toISOString();
|
|
500
594
|
const expected = options.expectedVersion;
|
|
501
595
|
const { intent, supersede, requireSettled } = options;
|
|
502
|
-
const meta =
|
|
596
|
+
const meta = options.meta;
|
|
503
597
|
return this.#transact([DOCUMENTS, INTENTS, META], "readwrite", (tx, done, fail, guard) => {
|
|
504
598
|
const documents = tx.objectStore(DOCUMENTS);
|
|
505
599
|
const intents = tx.objectStore(INTENTS);
|
|
@@ -513,6 +607,10 @@ export class IndexedDbBackend {
|
|
|
513
607
|
const put = metaStore.put(row);
|
|
514
608
|
put.onerror = () => fail(requestError(put, `IndexedDB meta write failed for '${row.key}'`));
|
|
515
609
|
}
|
|
610
|
+
for (const key of options.removeMeta ?? []) {
|
|
611
|
+
const remove = metaStore.delete(key);
|
|
612
|
+
remove.onerror = () => fail(requestError(remove, `IndexedDB meta delete failed for '${key}'`));
|
|
613
|
+
}
|
|
516
614
|
};
|
|
517
615
|
const recordIntent = () => {
|
|
518
616
|
if (!intent) {
|
|
@@ -569,31 +667,55 @@ export class IndexedDbBackend {
|
|
|
569
667
|
removeSuperseded();
|
|
570
668
|
});
|
|
571
669
|
};
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
const
|
|
577
|
-
|
|
670
|
+
const apply = () => {
|
|
671
|
+
if (options.resolveIntents) {
|
|
672
|
+
request(intents.getAll(), "resolution snapshot", (rows) => {
|
|
673
|
+
assertJournalSnapshot(id, options.resolveIntents.expected, rows, intent?.requestId);
|
|
674
|
+
for (const row of options.resolveIntents.expected) {
|
|
675
|
+
const removal = intents.delete(row.requestId);
|
|
676
|
+
removal.onerror = () => fail(requestError(removal, "IndexedDB resolution delete failed"));
|
|
677
|
+
}
|
|
678
|
+
writeDocument();
|
|
679
|
+
});
|
|
680
|
+
return;
|
|
681
|
+
}
|
|
682
|
+
if (!requireSettled) {
|
|
683
|
+
writeDocument();
|
|
684
|
+
return;
|
|
685
|
+
}
|
|
686
|
+
// The journal has no target index; this scan shares the write's transaction.
|
|
687
|
+
request(intents.getAll(), "intent scan", (rows) => {
|
|
688
|
+
const holder = rows.find((row) => row.target === id && row.state !== "acknowledged");
|
|
689
|
+
if (holder) {
|
|
690
|
+
fail(new IntentHoldConflict(id, holder.requestId, holder.state));
|
|
691
|
+
return;
|
|
578
692
|
}
|
|
579
693
|
writeDocument();
|
|
580
694
|
});
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
return;
|
|
586
|
-
}
|
|
587
|
-
// The journal has no index by target, so the hold check scans it; the scan runs inside
|
|
588
|
-
// this transaction, which is what makes the answer hold for the write that follows.
|
|
589
|
-
request(intents.getAll(), "intent scan", (rows) => {
|
|
590
|
-
const holder = rows.find((row) => row.target === id && row.state !== "acknowledged");
|
|
591
|
-
if (holder) {
|
|
592
|
-
fail(new IntentHoldConflict(id, holder.requestId, holder.state));
|
|
695
|
+
};
|
|
696
|
+
const checkIdentities = () => {
|
|
697
|
+
if (!snapshotGuard) {
|
|
698
|
+
apply();
|
|
593
699
|
return;
|
|
594
700
|
}
|
|
595
|
-
|
|
596
|
-
|
|
701
|
+
const checkSuperseded = (existingIdentity) => {
|
|
702
|
+
if (!supersede) {
|
|
703
|
+
assertJournalIntentChanges(snapshotGuard, existingIdentity, undefined);
|
|
704
|
+
apply();
|
|
705
|
+
return;
|
|
706
|
+
}
|
|
707
|
+
request(intents.get(supersede.requestId), "superseded identity read", row => {
|
|
708
|
+
assertJournalIntentChanges(snapshotGuard, existingIdentity, row);
|
|
709
|
+
apply();
|
|
710
|
+
});
|
|
711
|
+
};
|
|
712
|
+
if (!intent) {
|
|
713
|
+
checkSuperseded(undefined);
|
|
714
|
+
return;
|
|
715
|
+
}
|
|
716
|
+
request(intents.get(intent.requestId), "new identity read", row => checkSuperseded(row));
|
|
717
|
+
};
|
|
718
|
+
this.#checkJournalGuard(tx, snapshotGuard, checkIdentities, fail, guard);
|
|
597
719
|
});
|
|
598
720
|
}
|
|
599
721
|
/**
|
|
@@ -606,6 +728,7 @@ export class IndexedDbBackend {
|
|
|
606
728
|
* with `false` even under a compare-and-swap.
|
|
607
729
|
*/
|
|
608
730
|
async deleteJournaled(id, options = {}) {
|
|
731
|
+
options = captureJournalDeleteOptions(id, options);
|
|
609
732
|
assertSafeConceptId(id);
|
|
610
733
|
assertJournalResolutionOptions(id, options);
|
|
611
734
|
const expected = options.expectedVersion;
|
|
@@ -613,78 +736,80 @@ export class IndexedDbBackend {
|
|
|
613
736
|
const puts = options.meta ?? [];
|
|
614
737
|
const removals = options.removeMeta ?? [];
|
|
615
738
|
return this.#transact([DOCUMENTS, INTENTS, META], "readwrite", (tx, done, fail, guard) => {
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
const
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
};
|
|
629
|
-
const applyMeta = () => {
|
|
630
|
-
putMeta(puts);
|
|
631
|
-
for (const key of removals) {
|
|
632
|
-
const removal = metaStore.delete(key);
|
|
633
|
-
removal.onerror = () => fail(requestError(removal, `IndexedDB meta delete failed for '${key}'`));
|
|
634
|
-
}
|
|
635
|
-
};
|
|
636
|
-
const deleteDocument = () => {
|
|
637
|
-
request(documents.get(id), "read", (current) => {
|
|
638
|
-
const record = current;
|
|
639
|
-
if (options.resolveIntents && expected !== (record?.version ?? null)) {
|
|
640
|
-
fail(new VersionConflict(id, expected, record?.version ?? null));
|
|
641
|
-
return;
|
|
739
|
+
this.#checkJournalGuard(tx, options.guard, () => {
|
|
740
|
+
const documents = tx.objectStore(DOCUMENTS);
|
|
741
|
+
const intents = tx.objectStore(INTENTS);
|
|
742
|
+
const metaStore = tx.objectStore(META);
|
|
743
|
+
const request = (req, label, next) => {
|
|
744
|
+
req.onerror = () => fail(requestError(req, `IndexedDB ${label} failed for '${id}'`));
|
|
745
|
+
req.onsuccess = guard(() => next(req.result));
|
|
746
|
+
};
|
|
747
|
+
const putMeta = (rows) => {
|
|
748
|
+
for (const row of rows) {
|
|
749
|
+
const put = metaStore.put(row);
|
|
750
|
+
put.onerror = () => fail(requestError(put, `IndexedDB meta write failed for '${row.key}'`));
|
|
642
751
|
}
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
752
|
+
};
|
|
753
|
+
const applyMeta = () => {
|
|
754
|
+
putMeta(puts);
|
|
755
|
+
for (const key of removals) {
|
|
756
|
+
const removal = metaStore.delete(key);
|
|
757
|
+
removal.onerror = () => fail(requestError(removal, `IndexedDB meta delete failed for '${key}'`));
|
|
647
758
|
}
|
|
648
|
-
|
|
649
|
-
|
|
759
|
+
};
|
|
760
|
+
const deleteDocument = () => {
|
|
761
|
+
request(documents.get(id), "read", (current) => {
|
|
762
|
+
const record = current;
|
|
763
|
+
if (options.resolveIntents && expected !== (record?.version ?? null)) {
|
|
764
|
+
fail(new VersionConflict(id, expected, record?.version ?? null));
|
|
765
|
+
return;
|
|
766
|
+
}
|
|
767
|
+
if (!record) {
|
|
768
|
+
applyMeta();
|
|
769
|
+
done({ outcome: "absent" });
|
|
770
|
+
return;
|
|
771
|
+
}
|
|
772
|
+
if (expected !== undefined && expected !== record.version) {
|
|
773
|
+
fail(new VersionConflict(id, expected, record.version));
|
|
774
|
+
return;
|
|
775
|
+
}
|
|
776
|
+
const removal = documents.delete(id);
|
|
777
|
+
removal.onerror = () => fail(requestError(removal, `IndexedDB delete failed for '${id}'`));
|
|
778
|
+
applyMeta();
|
|
779
|
+
done({ outcome: "deleted" });
|
|
780
|
+
});
|
|
781
|
+
};
|
|
782
|
+
if (options.resolveIntents) {
|
|
783
|
+
request(intents.getAll(), "resolution snapshot", (rows) => {
|
|
784
|
+
assertJournalSnapshot(id, options.resolveIntents.expected, rows);
|
|
785
|
+
for (const row of options.resolveIntents.expected) {
|
|
786
|
+
const removal = intents.delete(row.requestId);
|
|
787
|
+
removal.onerror = () => fail(requestError(removal, "IndexedDB resolution delete failed"));
|
|
788
|
+
}
|
|
789
|
+
deleteDocument();
|
|
790
|
+
});
|
|
791
|
+
return;
|
|
792
|
+
}
|
|
793
|
+
if (!requireSettled) {
|
|
794
|
+
deleteDocument();
|
|
795
|
+
return;
|
|
796
|
+
}
|
|
797
|
+
// The same in-transaction hold scan as `writeJournaled`: the answer holds for the deletion
|
|
798
|
+
// that follows, and for the `onHeld` rows put in its place.
|
|
799
|
+
request(intents.getAll(), "intent scan", (rows) => {
|
|
800
|
+
const holder = rows.find((row) => row.target === id && row.state !== "acknowledged");
|
|
801
|
+
if (holder) {
|
|
802
|
+
if (!onHeld) {
|
|
803
|
+
fail(new IntentHoldConflict(id, holder.requestId, holder.state));
|
|
804
|
+
return;
|
|
805
|
+
}
|
|
806
|
+
putMeta(onHeld.meta);
|
|
807
|
+
done({ outcome: "held", requestId: holder.requestId, state: holder.state });
|
|
650
808
|
return;
|
|
651
809
|
}
|
|
652
|
-
const removal = documents.delete(id);
|
|
653
|
-
removal.onerror = () => fail(requestError(removal, `IndexedDB delete failed for '${id}'`));
|
|
654
|
-
applyMeta();
|
|
655
|
-
done({ outcome: "deleted" });
|
|
656
|
-
});
|
|
657
|
-
};
|
|
658
|
-
if (options.resolveIntents) {
|
|
659
|
-
request(intents.getAll(), "resolution snapshot", (rows) => {
|
|
660
|
-
assertJournalSnapshot(id, options.resolveIntents.expected, rows);
|
|
661
|
-
for (const row of options.resolveIntents.expected) {
|
|
662
|
-
const removal = intents.delete(row.requestId);
|
|
663
|
-
removal.onerror = () => fail(requestError(removal, "IndexedDB resolution delete failed"));
|
|
664
|
-
}
|
|
665
810
|
deleteDocument();
|
|
666
811
|
});
|
|
667
|
-
|
|
668
|
-
}
|
|
669
|
-
if (!requireSettled) {
|
|
670
|
-
deleteDocument();
|
|
671
|
-
return;
|
|
672
|
-
}
|
|
673
|
-
// The same in-transaction hold scan as `writeJournaled`: the answer holds for the deletion
|
|
674
|
-
// that follows, and for the `onHeld` rows put in its place.
|
|
675
|
-
request(intents.getAll(), "intent scan", (rows) => {
|
|
676
|
-
const holder = rows.find((row) => row.target === id && row.state !== "acknowledged");
|
|
677
|
-
if (holder) {
|
|
678
|
-
if (!onHeld) {
|
|
679
|
-
fail(new IntentHoldConflict(id, holder.requestId, holder.state));
|
|
680
|
-
return;
|
|
681
|
-
}
|
|
682
|
-
putMeta(onHeld.meta);
|
|
683
|
-
done({ outcome: "held", requestId: holder.requestId, state: holder.state });
|
|
684
|
-
return;
|
|
685
|
-
}
|
|
686
|
-
deleteDocument();
|
|
687
|
-
});
|
|
812
|
+
}, fail, guard);
|
|
688
813
|
});
|
|
689
814
|
}
|
|
690
815
|
/**
|
|
@@ -740,6 +865,132 @@ export class IndexedDbBackend {
|
|
|
740
865
|
const { frontmatter, body } = parseMarkdown(record.raw, pathFromConceptId(id), { okfVersion: editionOf(index) });
|
|
741
866
|
return { document: { doc: { id, frontmatter, body }, version: record.version }, raw: record.raw, intents: rows, meta };
|
|
742
867
|
}
|
|
868
|
+
/**
|
|
869
|
+
* Every document's head with its intents and the meta rows named for it, from ONE readonly
|
|
870
|
+
* transaction over documents, reserved, intents, and meta: {@link readWithJournal} over the
|
|
871
|
+
* whole store. The root index, the journal (grouped by target, since it has no index by
|
|
872
|
+
* target) and the `shared` keys are read once, then the documents are walked by cursor, each
|
|
873
|
+
* row's own keys read with it, so a caller's `project` sees one record at a time and the
|
|
874
|
+
* listing holds only what it keeps plus the journal it read up front, intents' content
|
|
875
|
+
* included. The frontmatter is the one `readWithJournal` would parse
|
|
876
|
+
* for the record, under the edition the root index declared at this moment; the body is
|
|
877
|
+
* never parsed. Rows come back in `list` order.
|
|
878
|
+
*/
|
|
879
|
+
async readHeads(options = {}) {
|
|
880
|
+
const keysOf = options.meta ?? (() => []);
|
|
881
|
+
const sharedKeys = [...new Set(options.shared ?? [])];
|
|
882
|
+
const project = options.project ?? ((head) => head);
|
|
883
|
+
const rows = await this.#transact([DOCUMENTS, RESERVED, INTENTS, META], "readonly", (tx, done, fail, guard) => {
|
|
884
|
+
const out = [];
|
|
885
|
+
let edition;
|
|
886
|
+
const byTarget = new Map();
|
|
887
|
+
const shared = new Map();
|
|
888
|
+
// Rows still being assembled, plus one for the cursor walk itself; the listing is done
|
|
889
|
+
// when the walk has ended and every row's meta reads have landed.
|
|
890
|
+
let outstanding = 1;
|
|
891
|
+
const finish = () => {
|
|
892
|
+
if (--outstanding === 0)
|
|
893
|
+
done(out);
|
|
894
|
+
};
|
|
895
|
+
// A parse or projection failure aborts the transaction through `fail`; after it no
|
|
896
|
+
// request is placed and no request's own error is reported, so the caller's error is
|
|
897
|
+
// the rejection, not the aborted transaction's refusal of the requests still in flight.
|
|
898
|
+
let stopped = false;
|
|
899
|
+
const stop = (error) => {
|
|
900
|
+
if (stopped)
|
|
901
|
+
return;
|
|
902
|
+
stopped = true;
|
|
903
|
+
fail(error);
|
|
904
|
+
};
|
|
905
|
+
const metaStore = tx.objectStore(META);
|
|
906
|
+
const readMeta = (key, into, then) => {
|
|
907
|
+
const request = metaStore.get(key);
|
|
908
|
+
request.onerror = () => stop(requestError(request, `IndexedDB meta read failed for '${key}'`));
|
|
909
|
+
request.onsuccess = guard(() => {
|
|
910
|
+
const row = request.result;
|
|
911
|
+
if (row !== undefined)
|
|
912
|
+
into.set(key, row.value);
|
|
913
|
+
then();
|
|
914
|
+
});
|
|
915
|
+
};
|
|
916
|
+
const walk = () => {
|
|
917
|
+
const cursor = tx.objectStore(DOCUMENTS).openCursor();
|
|
918
|
+
cursor.onerror = () => stop(requestError(cursor, "IndexedDB document scan failed"));
|
|
919
|
+
cursor.onsuccess = guard(() => {
|
|
920
|
+
const current = cursor.result;
|
|
921
|
+
if (!current) {
|
|
922
|
+
finish();
|
|
923
|
+
return;
|
|
924
|
+
}
|
|
925
|
+
const record = current.value;
|
|
926
|
+
const intents = byTarget.get(record.id) ?? [];
|
|
927
|
+
const keys = [...new Set(keysOf(record.id, intents))].filter((key) => !shared.has(key) && !sharedKeys.includes(key));
|
|
928
|
+
const meta = new Map(shared);
|
|
929
|
+
let pending = keys.length;
|
|
930
|
+
outstanding += 1;
|
|
931
|
+
const complete = () => {
|
|
932
|
+
try {
|
|
933
|
+
out.push({ id: record.id, row: project(headOf(record, intents, meta, edition)) });
|
|
934
|
+
}
|
|
935
|
+
catch (error) {
|
|
936
|
+
stop(asError(error));
|
|
937
|
+
return;
|
|
938
|
+
}
|
|
939
|
+
finish();
|
|
940
|
+
};
|
|
941
|
+
if (pending === 0)
|
|
942
|
+
complete();
|
|
943
|
+
else {
|
|
944
|
+
for (const key of keys) {
|
|
945
|
+
readMeta(key, meta, () => {
|
|
946
|
+
if (--pending === 0)
|
|
947
|
+
complete();
|
|
948
|
+
});
|
|
949
|
+
}
|
|
950
|
+
}
|
|
951
|
+
if (!stopped)
|
|
952
|
+
current.continue();
|
|
953
|
+
});
|
|
954
|
+
};
|
|
955
|
+
const readShared = () => {
|
|
956
|
+
let pending = sharedKeys.length;
|
|
957
|
+
if (pending === 0) {
|
|
958
|
+
walk();
|
|
959
|
+
return;
|
|
960
|
+
}
|
|
961
|
+
for (const key of sharedKeys) {
|
|
962
|
+
readMeta(key, shared, () => {
|
|
963
|
+
if (--pending === 0)
|
|
964
|
+
walk();
|
|
965
|
+
});
|
|
966
|
+
}
|
|
967
|
+
};
|
|
968
|
+
// The index, the journal and the shared rows land before the walk starts, so every row
|
|
969
|
+
// is built under one edition and sees its whole journal, whatever order the host
|
|
970
|
+
// answers requests in.
|
|
971
|
+
const rootIndex = tx.objectStore(RESERVED).get(reservedKey("", "index.md"));
|
|
972
|
+
rootIndex.onerror = () => stop(requestError(rootIndex, "IndexedDB root index read failed"));
|
|
973
|
+
rootIndex.onsuccess = guard(() => {
|
|
974
|
+
edition = editionOf(rootIndex.result);
|
|
975
|
+
const scan = tx.objectStore(INTENTS).getAll();
|
|
976
|
+
scan.onerror = () => stop(requestError(scan, "IndexedDB journal scan failed"));
|
|
977
|
+
scan.onsuccess = guard(() => {
|
|
978
|
+
for (const row of scan.result) {
|
|
979
|
+
const list = byTarget.get(row.target);
|
|
980
|
+
if (list)
|
|
981
|
+
list.push(row);
|
|
982
|
+
else
|
|
983
|
+
byTarget.set(row.target, [row]);
|
|
984
|
+
}
|
|
985
|
+
for (const list of byTarget.values())
|
|
986
|
+
list.sort((a, b) => a.sequence - b.sequence);
|
|
987
|
+
readShared();
|
|
988
|
+
});
|
|
989
|
+
});
|
|
990
|
+
});
|
|
991
|
+
rows.sort((a, b) => compareStorageKeys(a.id, b.id));
|
|
992
|
+
return rows.map((entry) => entry.row);
|
|
993
|
+
}
|
|
743
994
|
/** Intents in local commit order, optionally restricted to one or more states. */
|
|
744
995
|
async listIntents(state) {
|
|
745
996
|
const wanted = state === undefined ? null : new Set(typeof state === "string" ? [state] : state);
|
|
@@ -760,38 +1011,67 @@ export class IndexedDbBackend {
|
|
|
760
1011
|
* missing record rejects with {@link IntentStateConflict} and writes nothing.
|
|
761
1012
|
*/
|
|
762
1013
|
async updateIntent(requestId, expectedState, patch, options = {}) {
|
|
1014
|
+
({ patch, options } = captureIntentUpdate(patch, options));
|
|
763
1015
|
const now = new Date().toISOString();
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
const
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
const
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
1016
|
+
const raw = options.document ? stringifyDoc(options.document.frontmatter, options.document.body ?? "") : undefined;
|
|
1017
|
+
const replacement = raw === undefined ? undefined : { id: options.document.id, raw, version: versionOfBytes(raw), updatedBy: defaultActor(), updatedAt: now };
|
|
1018
|
+
return this.#transact([DOCUMENTS, INTENTS, META], "readwrite", (tx, done, fail, guard) => {
|
|
1019
|
+
this.#checkJournalGuard(tx, options.guard, () => {
|
|
1020
|
+
const intents = tx.objectStore(INTENTS);
|
|
1021
|
+
const read = intents.get(requestId);
|
|
1022
|
+
read.onerror = () => fail(requestError(read, `IndexedDB intent read failed for '${requestId}'`));
|
|
1023
|
+
read.onsuccess = guard(() => {
|
|
1024
|
+
const current = read.result;
|
|
1025
|
+
if (!current || current.state !== expectedState) {
|
|
1026
|
+
fail(new IntentStateConflict(requestId, expectedState, current?.state ?? null));
|
|
1027
|
+
return;
|
|
1028
|
+
}
|
|
1029
|
+
if (options.guard && current.target !== options.guard.target)
|
|
1030
|
+
throw new JournalGuardConflict(current.target);
|
|
1031
|
+
const next = { ...current, ...patch, requestId, sequence: current.sequence, updatedAt: now };
|
|
1032
|
+
if (replacement) {
|
|
1033
|
+
const write = tx.objectStore(DOCUMENTS).put(replacement);
|
|
1034
|
+
write.onerror = () => fail(requestError(write, "IndexedDB replacement write failed"));
|
|
1035
|
+
}
|
|
1036
|
+
const put = intents.put(next);
|
|
1037
|
+
put.onerror = () => fail(requestError(put, `IndexedDB intent write failed for '${requestId}'`));
|
|
1038
|
+
const metaStore = tx.objectStore(META);
|
|
1039
|
+
for (const row of options.meta ?? []) {
|
|
1040
|
+
const metaPut = metaStore.put(row);
|
|
1041
|
+
metaPut.onerror = () => fail(requestError(metaPut, `IndexedDB meta write failed for '${row.key}'`));
|
|
1042
|
+
}
|
|
1043
|
+
done(next);
|
|
1044
|
+
});
|
|
1045
|
+
}, fail, guard);
|
|
784
1046
|
});
|
|
785
1047
|
}
|
|
786
1048
|
async readMeta(key) {
|
|
787
1049
|
const row = await this.#getOne(META, key);
|
|
788
1050
|
return row === undefined ? undefined : row.value;
|
|
789
1051
|
}
|
|
790
|
-
async writeMeta(key, value) {
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
1052
|
+
async writeMeta(key, value, options = {}) {
|
|
1053
|
+
options = captureMetaWrite(options);
|
|
1054
|
+
value = options.expected !== undefined || options.requireEmptyJournal ? captureJournalValue(value) : structuredClone(value);
|
|
1055
|
+
await this.#transact([INTENTS, META], "readwrite", (tx, done, fail, guard) => {
|
|
1056
|
+
const meta = tx.objectStore(META);
|
|
1057
|
+
const read = meta.get(key);
|
|
1058
|
+
read.onerror = () => fail(requestError(read, "IndexedDB admission read failed"));
|
|
1059
|
+
read.onsuccess = guard(() => {
|
|
1060
|
+
const row = read.result;
|
|
1061
|
+
const finish = (intents) => {
|
|
1062
|
+
assertMetaWrite(key, options, row === undefined ? { present: false } : { present: true, value: row.value }, intents);
|
|
1063
|
+
const put = meta.put({ key, value });
|
|
1064
|
+
put.onerror = () => fail(requestError(put, `IndexedDB meta write failed for '${key}'`));
|
|
1065
|
+
done(undefined);
|
|
1066
|
+
};
|
|
1067
|
+
if (!options.requireEmptyJournal) {
|
|
1068
|
+
finish([]);
|
|
1069
|
+
return;
|
|
1070
|
+
}
|
|
1071
|
+
const scan = tx.objectStore(INTENTS).getAll();
|
|
1072
|
+
scan.onerror = () => fail(requestError(scan, "IndexedDB admission journal scan failed"));
|
|
1073
|
+
scan.onsuccess = guard(() => finish(scan.result));
|
|
1074
|
+
});
|
|
795
1075
|
});
|
|
796
1076
|
}
|
|
797
1077
|
// ── blobs ──────────────────────────────────────────────────────────────────────────────
|