@optimystic/db-p2p-storage-web 1.14.0 → 1.16.0

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.
@@ -1,58 +1,84 @@
1
1
  import type { ActionId, BlockId } from '@optimystic/db-core';
2
- import { KvRawStorage, type RawStoreDriver, type StoreIdentity } from '@optimystic/db-p2p';
3
- import type { OptimysticWebDBHandle } from './db.js';
2
+ import { KvRawStorage, type JournalBatchBackendOptions, type JournalContents, type JournalRecord, type JournalStore, type StoreDurability, type StoreIdentity, type StoreKey, type WriteGroupOptions } from '@optimystic/db-p2p';
3
+ import { type OptimysticWebDBHandle } from './db.js';
4
+ import { type IndexedDBDurabilityOptions } from './durability.js';
5
+ export type IndexedDBJournalStoreOptions = IndexedDBDurabilityOptions;
4
6
  /**
5
- * IndexedDB {@link RawStoreDriver}: the six logical block-storage stores mapped
6
- * to the six IndexedDB object stores (`metadata`, `revisions`, `pending`,
7
- * `transactions`, `proofs`, `materialized`) with their compound array keys.
7
+ * IndexedDB {@link JournalStore}: the six logical block-storage stores mapped to the six IndexedDB
8
+ * object stores (`metadata`, `revisions`, `pending`, `transactions`, `proofs`, `materialized`)
9
+ * with their compound array keys, read exactly as the per-write driver before it read them, so
10
+ * data an earlier build wrote reads unchanged; and the journal in the `journal` object store,
11
+ * which `JournalBatchBackend` drives.
8
12
  *
9
- * `KvRawStorage` now owns all JSON serialization, so this driver only ever
10
- * reads/writes `Uint8Array` values — IndexedDB stores a typed array natively via
11
- * structured clone and returns a `Uint8Array`, so no codec lives here. Everything
12
- * IndexedDB-specific stays: the range/key cursors (drained snapshot-first before
13
- * yielding), and the single `readwrite` transaction that makes `promote` atomic.
13
+ * `KvRawStorage` owns all JSON serialization, so this store only ever reads/writes `Uint8Array`
14
+ * values — IndexedDB stores a typed array natively via structured clone and returns a `Uint8Array`,
15
+ * so no codec lives here; a journal record is stored by structured clone too. Everything
16
+ * IndexedDB-specific stays: the range/key cursors (drained snapshot-first before yielding), and the
17
+ * one `readwrite` transaction each journal write is.
18
+ *
19
+ * ### Why a group survives a power cut, and in order
20
+ *
21
+ * IndexedDB makes one transaction durable when it is asked to (`durability: 'strict'`: the
22
+ * transaction completes only once the browser has put it on the disk) but promises nothing about
23
+ * the order relaxed transactions reach the disk, which is why the backend journals (see
24
+ * `JournalBatchBackend`). Here an append is one relaxed transaction on `journal` alone, and an
25
+ * apply is one strict transaction over the six stores and `journal`: every record's entries in
26
+ * order, the applied-through mark, and the delete of each record applied or discarded. The apply
27
+ * carries every byte it applies, so its durability depends on no earlier transaction, and the
28
+ * backend syncs each apply before the next starts — so what a power cut leaves is a prefix of the
29
+ * groups, and `durability` is `'power-loss'` wherever the browser honours the option
30
+ * (`supportsStrictDurability`). Every request of a transaction is queued synchronously and the
31
+ * transaction awaited once, because IndexedDB commits a transaction that is left idle.
32
+ *
33
+ * The handle is shared with `IndexedDBKVStore` and `loadOrCreateBrowserPeerKey`, so there is no
34
+ * `close` here: the host closes the handle after `storage.close()` has resolved.
14
35
  */
15
- export declare class IndexedDBStoreDriver implements RawStoreDriver {
36
+ export declare class IndexedDBJournalStore implements JournalStore {
16
37
  private readonly db;
38
+ readonly durability: StoreDurability;
17
39
  private readonly identity;
18
- constructor(db: OptimysticWebDBHandle);
40
+ private readonly strict;
41
+ constructor(db: OptimysticWebDBHandle, options?: IndexedDBJournalStoreOptions);
19
42
  storeIdentity(): StoreIdentity;
20
- getMetadata(blockId: BlockId): Promise<Uint8Array | undefined>;
21
- putMetadata(blockId: BlockId, value: Uint8Array): Promise<void>;
22
- getRevision(blockId: BlockId, rev: number): Promise<Uint8Array | undefined>;
23
- putRevision(blockId: BlockId, rev: number, value: Uint8Array): Promise<void>;
24
- rangeRevisions(blockId: BlockId, lo: number, hi: number, reverse: boolean): AsyncIterable<[number, Uint8Array]>;
25
- getPending(blockId: BlockId, actionId: ActionId): Promise<Uint8Array | undefined>;
26
- putPending(blockId: BlockId, actionId: ActionId, value: Uint8Array): Promise<void>;
27
- deletePending(blockId: BlockId, actionId: ActionId): Promise<void>;
28
- listPendingActionIds(blockId: BlockId): AsyncIterable<ActionId>;
29
- getTransaction(blockId: BlockId, actionId: ActionId): Promise<Uint8Array | undefined>;
30
- putTransaction(blockId: BlockId, actionId: ActionId, value: Uint8Array): Promise<void>;
31
- getProof(blockId: BlockId, rev: number): Promise<Uint8Array | undefined>;
32
- putProof(blockId: BlockId, rev: number, value: Uint8Array): Promise<void>;
33
- getMaterialized(blockId: BlockId, actionId: ActionId): Promise<Uint8Array | undefined>;
34
- putMaterialized(blockId: BlockId, actionId: ActionId, value: Uint8Array): Promise<void>;
35
- deleteMaterialized(blockId: BlockId, actionId: ActionId): Promise<void>;
36
- promote(blockId: BlockId, actionId: ActionId): Promise<void>;
43
+ get(key: StoreKey): Promise<Uint8Array | undefined>;
44
+ rangeRevisions(blockId: BlockId, lo: number, hi: number): Promise<[number, Uint8Array][]>;
45
+ listPendingActionIds(blockId: BlockId): Promise<ActionId[]>;
37
46
  listBlockIds(): AsyncIterable<BlockId>;
38
47
  approximateBytesUsed(): Promise<number>;
48
+ /** One relaxed transaction on `journal`: the record under its `seq`. `JournalStore.appendJournal`'s contract. */
49
+ appendJournal(record: JournalRecord): Promise<void>;
50
+ /**
51
+ * One transaction over the six stores and `journal`, strict where the browser supports it: each
52
+ * record's entries in order, then the mark, then the delete of every numbered record. A number
53
+ * with no record present (a strict group's, never appended) deletes nothing.
54
+ * `JournalStore.applyJournal`'s contract.
55
+ */
56
+ applyJournal(apply: readonly JournalRecord[], appliedThrough: number, discard: readonly number[]): Promise<void>;
57
+ /** One readonly transaction: every numbered record and the mark. `JournalStore.readJournal`'s contract. */
58
+ readJournal(): Promise<JournalContents>;
39
59
  }
60
+ /** The write group's and the journal backend's options (`idleFlushMs`, `maxUnsyncedBytes`, the test-only `setTimer` and `now`), and whether to ask for strict transactions. */
61
+ export type IndexedDBRawStorageOptions = WriteGroupOptions & JournalBatchBackendOptions & IndexedDBDurabilityOptions;
40
62
  /**
41
- * IndexedDB-backed {@link IRawStorage} for browser peers, now a thin shell over
42
- * the shared {@link KvRawStorage} kernel driven by an {@link IndexedDBStoreDriver}.
43
- * The public name/constructor (`new IndexedDBRawStorage(handle)`) is unchanged so
44
- * existing imports keep resolving; the kernel supplies the `IRawStorage` surface
45
- * and the driver supplies IndexedDB behavior.
63
+ * IndexedDB-backed {@link IRawStorage} for browser peers: the shared {@link KvRawStorage} kernel
64
+ * over a {@link WriteGroupDriver} over a {@link JournalBatchBackend} over an
65
+ * {@link IndexedDBJournalStore}. Every write resolves into the driver's in-memory overlay;
66
+ * `flush()` hands the group to the backend, which applies it as one strict transaction before the
67
+ * flush resolves, or — a flush with a window — appends it as one relaxed journal record and applies
68
+ * it by the deadline. The public name/constructor (`new IndexedDBRawStorage(handle)`) is unchanged
69
+ * so existing imports keep resolving.
46
70
  *
47
- * `listBlockIds`/`getApproximateBytesUsed` are re-declared here as always-present
48
- * (the IndexedDB driver always implements them, so the kernel constructor always
49
- * wires them) — the base declares them optional, but every web consumer relies
50
- * on them.
71
+ * `listBlockIds`/`getApproximateBytesUsed`/`getStoreIdentity`/`close` are re-declared here as
72
+ * always-present (the store always implements the first three, so the backend, driver and kernel
73
+ * always wire them, and the driver always has a `close`) — the base declares them optional, but
74
+ * every web consumer relies on them. `close()` applies what is still held and disarms the timers; it
75
+ * does not close the shared handle, which the host closes afterwards.
51
76
  */
52
77
  export declare class IndexedDBRawStorage extends KvRawStorage {
53
78
  listBlockIds: () => AsyncIterable<BlockId>;
54
79
  getApproximateBytesUsed: () => Promise<number>;
55
80
  getStoreIdentity: () => StoreIdentity;
56
- constructor(db: OptimysticWebDBHandle);
81
+ close: () => Promise<void>;
82
+ constructor(db: OptimysticWebDBHandle, options?: IndexedDBRawStorageOptions);
57
83
  }
58
84
  //# sourceMappingURL=indexeddb-storage.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"indexeddb-storage.d.ts","sourceRoot":"","sources":["../../src/indexeddb-storage.ts"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O,GAAG,OAAO,CAAC,QAAQ,EAAE,CAAC;IAmB1D,YAAY,IAAI,aAAa,CAAC,OAAO,CAAC;IAWvC,oBAAoB,IAAI,OAAO,CAAC,MAAM,CAAC;IAY7C,iHAAiH;IAC3G,aAAa,CAAC,MAAM,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC;IAKzD;;;;;OAKG;IACG,YAAY,CAAC,KAAK,EAAE,SAAS,aAAa,EAAE,EAAE,cAAc,EAAE,MAAM,EAAE,OAAO,EAAE,SAAS,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAgBtH,2GAA2G;IACrG,WAAW,IAAI,OAAO,CAAC,eAAe,CAAC;CAqB7C;AAED,+KAA+K;AAC/K,MAAM,MAAM,0BAA0B,GAAG,iBAAiB,GAAG,0BAA0B,GAAG,0BAA0B,CAAC;AAErH;;;;;;;;;;;;;;GAcG;AACH,qBAAa,mBAAoB,SAAQ,YAAY;IAC5C,YAAY,EAAE,MAAM,aAAa,CAAC,OAAO,CAAC,CAAC;IAC3C,uBAAuB,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,CAAC;IAC/C,gBAAgB,EAAE,MAAM,aAAa,CAAC;IACtC,KAAK,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;gBAEvB,EAAE,EAAE,qBAAqB,EAAE,OAAO,GAAE,0BAA+B;CAG/E"}
@@ -1,75 +1,140 @@
1
- import { KvRawStorage, identityForHandle } from '@optimystic/db-p2p';
1
+ import { JournalBatchBackend, KvRawStorage, WriteGroupDriver, identityForHandle, } from '@optimystic/db-p2p';
2
+ import { JOURNAL_MARK_KEY } from './db.js';
3
+ import { resolveStrictDurability, transactionDurability } from './durability.js';
2
4
  import { createLogger } from './logger.js';
3
5
  const log = createLogger('storage:indexeddb');
6
+ /** The object stores an apply writes: the six block-storage stores and the journal itself. */
7
+ const APPLY_STORES = ['metadata', 'revisions', 'pending', 'transactions', 'proofs', 'materialized', 'journal'];
8
+ function idbKeyOf(key) {
9
+ switch (key.store) {
10
+ case 'metadata':
11
+ return key.blockId;
12
+ case 'revisions':
13
+ case 'proofs':
14
+ return [key.blockId, key.rev];
15
+ case 'pending':
16
+ case 'transactions':
17
+ case 'materialized':
18
+ return [key.blockId, key.actionId];
19
+ }
20
+ }
21
+ /**
22
+ * The stored value as a record this build can read, or `undefined`. A record is stored by
23
+ * structured clone, so this is the check the byte codec's decode makes for LevelDB: a record it
24
+ * fails is left out of `readJournal`, and recovery treats its number as missing.
25
+ */
26
+ function asJournalRecord(value) {
27
+ if (typeof value !== 'object' || value === null)
28
+ return undefined;
29
+ const { seq, entries } = value;
30
+ if (typeof seq !== 'number' || !Number.isSafeInteger(seq) || seq < 1 || !Array.isArray(entries))
31
+ return undefined;
32
+ return entries.every(isBatchEntry) ? { seq, entries } : undefined;
33
+ }
34
+ function isBatchEntry(entry) {
35
+ if (typeof entry !== 'object' || entry === null)
36
+ return false;
37
+ const { key, value } = entry;
38
+ return isStoreKey(key) && (value === undefined || value instanceof Uint8Array);
39
+ }
40
+ function isStoreKey(key) {
41
+ if (typeof key !== 'object' || key === null)
42
+ return false;
43
+ const { store, blockId, rev, actionId } = key;
44
+ if (typeof blockId !== 'string')
45
+ return false;
46
+ switch (store) {
47
+ case 'metadata':
48
+ return true;
49
+ case 'revisions':
50
+ case 'proofs':
51
+ return Number.isInteger(rev);
52
+ case 'pending':
53
+ case 'transactions':
54
+ case 'materialized':
55
+ return typeof actionId === 'string';
56
+ default:
57
+ return false;
58
+ }
59
+ }
4
60
  /**
5
- * IndexedDB {@link RawStoreDriver}: the six logical block-storage stores mapped
6
- * to the six IndexedDB object stores (`metadata`, `revisions`, `pending`,
7
- * `transactions`, `proofs`, `materialized`) with their compound array keys.
61
+ * IndexedDB {@link JournalStore}: the six logical block-storage stores mapped to the six IndexedDB
62
+ * object stores (`metadata`, `revisions`, `pending`, `transactions`, `proofs`, `materialized`)
63
+ * with their compound array keys, read exactly as the per-write driver before it read them, so
64
+ * data an earlier build wrote reads unchanged; and the journal in the `journal` object store,
65
+ * which `JournalBatchBackend` drives.
8
66
  *
9
- * `KvRawStorage` now owns all JSON serialization, so this driver only ever
10
- * reads/writes `Uint8Array` values — IndexedDB stores a typed array natively via
11
- * structured clone and returns a `Uint8Array`, so no codec lives here. Everything
12
- * IndexedDB-specific stays: the range/key cursors (drained snapshot-first before
13
- * yielding), and the single `readwrite` transaction that makes `promote` atomic.
67
+ * `KvRawStorage` owns all JSON serialization, so this store only ever reads/writes `Uint8Array`
68
+ * values — IndexedDB stores a typed array natively via structured clone and returns a `Uint8Array`,
69
+ * so no codec lives here; a journal record is stored by structured clone too. Everything
70
+ * IndexedDB-specific stays: the range/key cursors (drained snapshot-first before yielding), and the
71
+ * one `readwrite` transaction each journal write is.
72
+ *
73
+ * ### Why a group survives a power cut, and in order
74
+ *
75
+ * IndexedDB makes one transaction durable when it is asked to (`durability: 'strict'`: the
76
+ * transaction completes only once the browser has put it on the disk) but promises nothing about
77
+ * the order relaxed transactions reach the disk, which is why the backend journals (see
78
+ * `JournalBatchBackend`). Here an append is one relaxed transaction on `journal` alone, and an
79
+ * apply is one strict transaction over the six stores and `journal`: every record's entries in
80
+ * order, the applied-through mark, and the delete of each record applied or discarded. The apply
81
+ * carries every byte it applies, so its durability depends on no earlier transaction, and the
82
+ * backend syncs each apply before the next starts — so what a power cut leaves is a prefix of the
83
+ * groups, and `durability` is `'power-loss'` wherever the browser honours the option
84
+ * (`supportsStrictDurability`). Every request of a transaction is queued synchronously and the
85
+ * transaction awaited once, because IndexedDB commits a transaction that is left idle.
86
+ *
87
+ * The handle is shared with `IndexedDBKVStore` and `loadOrCreateBrowserPeerKey`, so there is no
88
+ * `close` here: the host closes the handle after `storage.close()` has resolved.
14
89
  */
15
- export class IndexedDBStoreDriver {
90
+ export class IndexedDBJournalStore {
16
91
  db;
92
+ // NOTE: in a browser that ignores `durability` this is `'process-crash'`, and there the backend
93
+ // journals nothing and applies every group at once as one relaxed transaction: a group lands
94
+ // whole, but browsers do not promise that what survives an operating-system crash or power cut is
95
+ // a prefix of relaxed transactions (flush contract rule 3). With strict transactions both hold,
96
+ // as the class doc says.
97
+ durability;
17
98
  // NOTE: identity is the HANDLE object, so two handles opened over the same IndexedDB database
18
99
  // NAME read as two identities. Resolving that would mean naming the underlying database,
19
100
  // which `OptimysticWebDBHandle` does not expose. The package opener (`db.ts`) hands out one
20
101
  // handle per database name in practice, so this is the reachable case, not the complete one.
102
+ // Two tabs over one database run two nodes over one store, which Invariant 5 of
103
+ // `packages/db-p2p/docs/storage.md` forbids and nothing here enforces: the second tab's open
104
+ // applies the journal records the first tab still holds, and each then numbers its records from
105
+ // its own count.
21
106
  identity;
22
- constructor(db) {
107
+ strict;
108
+ constructor(db, options = {}) {
23
109
  this.db = db;
110
+ this.strict = resolveStrictDurability(options);
111
+ this.durability = this.strict ? 'power-loss' : 'process-crash';
24
112
  this.identity = identityForHandle('idb-handle', db);
25
113
  }
26
114
  storeIdentity() {
27
115
  return this.identity;
28
116
  }
29
- // --- metadata ---
30
- async getMetadata(blockId) {
31
- return this.db.get('metadata', blockId);
32
- }
33
- async putMetadata(blockId, value) {
34
- await this.db.put('metadata', value, blockId);
117
+ // --- reads: the applied stores ---
118
+ async get(key) {
119
+ return this.db.get(key.store, idbKeyOf(key));
35
120
  }
36
- // --- revisions ---
37
- async getRevision(blockId, rev) {
38
- return this.db.get('revisions', [blockId, rev]);
39
- }
40
- async putRevision(blockId, rev, value) {
41
- await this.db.put('revisions', value, [blockId, rev]);
42
- }
43
- async *rangeRevisions(blockId, lo, hi, reverse) {
121
+ async rangeRevisions(blockId, lo, hi) {
44
122
  const range = IDBKeyRange.bound([blockId, lo], [blockId, hi]);
45
123
  const tx = this.db.transaction('revisions', 'readonly');
46
124
  const store = tx.objectStore('revisions');
47
- // Snapshot first so we don't hold the transaction open across yields —
48
- // IndexedDB auto-commits idle transactions, which would invalidate the
49
- // cursor between the consumer's awaits (the kernel's drain-before-yield
50
- // contract). Do NOT switch to lazy yielding mid-transaction.
125
+ // Snapshot first so the transaction is not held open across the caller's awaits —
126
+ // IndexedDB auto-commits idle transactions, which would invalidate the cursor
127
+ // (the kernel's drain-before-yield contract). Do NOT switch to lazy yielding mid-transaction.
51
128
  const results = [];
52
- let cursor = await store.openCursor(range, reverse ? 'prev' : 'next');
129
+ let cursor = await store.openCursor(range);
53
130
  while (cursor) {
54
131
  results.push([cursor.key[1], cursor.value]);
55
132
  cursor = await cursor.continue();
56
133
  }
57
134
  await tx.done;
58
- for (const result of results) {
59
- yield result;
60
- }
61
- }
62
- // --- pending ---
63
- async getPending(blockId, actionId) {
64
- return this.db.get('pending', [blockId, actionId]);
135
+ return results;
65
136
  }
66
- async putPending(blockId, actionId, value) {
67
- await this.db.put('pending', value, [blockId, actionId]);
68
- }
69
- async deletePending(blockId, actionId) {
70
- await this.db.delete('pending', [blockId, actionId]);
71
- }
72
- async *listPendingActionIds(blockId) {
137
+ async listPendingActionIds(blockId) {
73
138
  // IndexedDB key ordering: array keys compare element-by-element, and a
74
139
  // shorter prefix-equal array is less than a longer one; arrays sort
75
140
  // above all primitive types. So `[blockId]` < `[blockId, anyActionId]`
@@ -85,56 +150,8 @@ export class IndexedDBStoreDriver {
85
150
  cursor = await cursor.continue();
86
151
  }
87
152
  await tx.done;
88
- for (const actionId of results) {
89
- yield actionId;
90
- }
91
- }
92
- // --- transactions ---
93
- async getTransaction(blockId, actionId) {
94
- return this.db.get('transactions', [blockId, actionId]);
95
- }
96
- async putTransaction(blockId, actionId, value) {
97
- await this.db.put('transactions', value, [blockId, actionId]);
98
- }
99
- // --- proofs (keyed [blockId, rev] like revisions; no delete — see RawStoreDriver.getProof) ---
100
- async getProof(blockId, rev) {
101
- return this.db.get('proofs', [blockId, rev]);
102
- }
103
- async putProof(blockId, rev, value) {
104
- await this.db.put('proofs', value, [blockId, rev]);
105
- }
106
- // --- materialized ---
107
- async getMaterialized(blockId, actionId) {
108
- return this.db.get('materialized', [blockId, actionId]);
109
- }
110
- async putMaterialized(blockId, actionId, value) {
111
- await this.db.put('materialized', value, [blockId, actionId]);
112
- }
113
- // The kernel owns the put-or-delete branch of `saveMaterializedBlock`, so the
114
- // driver exposes delete as a separate op.
115
- async deleteMaterialized(blockId, actionId) {
116
- await this.db.delete('materialized', [blockId, actionId]);
117
- }
118
- // --- promote (the only cross-key atomic op) ---
119
- async promote(blockId, actionId) {
120
- const key = [blockId, actionId];
121
- // Single readwrite transaction over both stores IS the atomic move: a crash
122
- // leaves either the pending or the committed entry, never both/neither.
123
- const tx = this.db.transaction(['pending', 'transactions'], 'readwrite');
124
- const pendingStore = tx.objectStore('pending');
125
- const transactionsStore = tx.objectStore('transactions');
126
- const value = await pendingStore.get(key);
127
- if (!value) {
128
- // Settle the transaction before throwing so the failed promote does not
129
- // leak an open transaction — keep this ordering.
130
- await tx.done.catch(() => undefined);
131
- throw new Error(`Pending action ${actionId} not found for block ${blockId}`);
132
- }
133
- await transactionsStore.put(value, key);
134
- await pendingStore.delete(key);
135
- await tx.done;
153
+ return results;
136
154
  }
137
- // --- optional passthroughs ---
138
155
  async *listBlockIds() {
139
156
  // The `metadata` store is keyed by blockId directly, so its keys ARE the
140
157
  // distinct block ids. getAllKeys reads them under an implicit readonly
@@ -155,22 +172,77 @@ export class IndexedDBStoreDriver {
155
172
  return 0;
156
173
  }
157
174
  }
175
+ // --- the journal ---
176
+ /** One relaxed transaction on `journal`: the record under its `seq`. `JournalStore.appendJournal`'s contract. */
177
+ async appendJournal(record) {
178
+ const tx = this.db.transaction('journal', 'readwrite', { durability: 'relaxed' });
179
+ await Promise.all([tx.store.put(record, record.seq), tx.done]);
180
+ }
181
+ /**
182
+ * One transaction over the six stores and `journal`, strict where the browser supports it: each
183
+ * record's entries in order, then the mark, then the delete of every numbered record. A number
184
+ * with no record present (a strict group's, never appended) deletes nothing.
185
+ * `JournalStore.applyJournal`'s contract.
186
+ */
187
+ async applyJournal(apply, appliedThrough, discard) {
188
+ const tx = this.db.transaction(APPLY_STORES, 'readwrite', { durability: transactionDurability(this.strict) });
189
+ const requests = [];
190
+ for (const record of apply) {
191
+ for (const { key, value } of record.entries) {
192
+ const store = tx.objectStore(key.store);
193
+ requests.push(value === undefined ? store.delete(idbKeyOf(key)) : store.put(value, idbKeyOf(key)));
194
+ }
195
+ }
196
+ const journal = tx.objectStore('journal');
197
+ requests.push(journal.put(appliedThrough, JOURNAL_MARK_KEY));
198
+ for (const { seq } of apply)
199
+ requests.push(journal.delete(seq));
200
+ for (const seq of discard)
201
+ requests.push(journal.delete(seq));
202
+ await Promise.all([...requests, tx.done]);
203
+ }
204
+ /** One readonly transaction: every numbered record and the mark. `JournalStore.readJournal`'s contract. */
205
+ async readJournal() {
206
+ const tx = this.db.transaction('journal', 'readonly');
207
+ // Every record's key is a positive safe integer; the mark's string key sorts above every number.
208
+ const [values, mark] = await Promise.all([
209
+ tx.store.getAll(IDBKeyRange.bound(1, Number.MAX_SAFE_INTEGER)),
210
+ tx.store.get(JOURNAL_MARK_KEY),
211
+ tx.done,
212
+ ]);
213
+ const records = [];
214
+ for (const value of values) {
215
+ const record = asJournalRecord(value);
216
+ if (record) {
217
+ records.push(record);
218
+ }
219
+ else {
220
+ log('read-journal: a journal record of %s is not one this build can read and is left out; recovery treats its number as missing', this.identity);
221
+ }
222
+ }
223
+ records.sort((a, b) => a.seq - b.seq);
224
+ const appliedThrough = typeof mark === 'number' && Number.isSafeInteger(mark) && mark >= 0 ? mark : 0;
225
+ return { appliedThrough, records };
226
+ }
158
227
  }
159
228
  /**
160
- * IndexedDB-backed {@link IRawStorage} for browser peers, now a thin shell over
161
- * the shared {@link KvRawStorage} kernel driven by an {@link IndexedDBStoreDriver}.
162
- * The public name/constructor (`new IndexedDBRawStorage(handle)`) is unchanged so
163
- * existing imports keep resolving; the kernel supplies the `IRawStorage` surface
164
- * and the driver supplies IndexedDB behavior.
229
+ * IndexedDB-backed {@link IRawStorage} for browser peers: the shared {@link KvRawStorage} kernel
230
+ * over a {@link WriteGroupDriver} over a {@link JournalBatchBackend} over an
231
+ * {@link IndexedDBJournalStore}. Every write resolves into the driver's in-memory overlay;
232
+ * `flush()` hands the group to the backend, which applies it as one strict transaction before the
233
+ * flush resolves, or — a flush with a window — appends it as one relaxed journal record and applies
234
+ * it by the deadline. The public name/constructor (`new IndexedDBRawStorage(handle)`) is unchanged
235
+ * so existing imports keep resolving.
165
236
  *
166
- * `listBlockIds`/`getApproximateBytesUsed` are re-declared here as always-present
167
- * (the IndexedDB driver always implements them, so the kernel constructor always
168
- * wires them) — the base declares them optional, but every web consumer relies
169
- * on them.
237
+ * `listBlockIds`/`getApproximateBytesUsed`/`getStoreIdentity`/`close` are re-declared here as
238
+ * always-present (the store always implements the first three, so the backend, driver and kernel
239
+ * always wire them, and the driver always has a `close`) — the base declares them optional, but
240
+ * every web consumer relies on them. `close()` applies what is still held and disarms the timers; it
241
+ * does not close the shared handle, which the host closes afterwards.
170
242
  */
171
243
  export class IndexedDBRawStorage extends KvRawStorage {
172
- constructor(db) {
173
- super(new IndexedDBStoreDriver(db));
244
+ constructor(db, options = {}) {
245
+ super(new WriteGroupDriver(new JournalBatchBackend(new IndexedDBJournalStore(db, options), options), options));
174
246
  }
175
247
  }
176
248
  //# sourceMappingURL=indexeddb-storage.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"indexeddb-storage.js","sourceRoot":"","sources":["../../src/indexeddb-storage.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,iBAAiB,EAA2C,MAAM,oBAAoB,CAAC;AAE9G,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAE3C,MAAM,GAAG,GAAG,YAAY,CAAC,mBAAmB,CAAC,CAAC;AAE9C;;;;;;;;;;GAUG;AACH,MAAM,OAAO,oBAAoB;IAOH;IAN7B,8FAA8F;IAC9F,yFAAyF;IACzF,4FAA4F;IAC5F,6FAA6F;IAC5E,QAAQ,CAAgB;IAEzC,YAA6B,EAAyB;QAAzB,OAAE,GAAF,EAAE,CAAuB;Q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GAAG,eAAe,CAAC,KAAK,CAAC,CAAC;YACtC,IAAI,MAAM,EAAE,CAAC;gBACZ,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;YACtB,CAAC;iBAAM,CAAC;gBACP,GAAG,CAAC,4HAA4H,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC;YAClJ,CAAC;QACF,CAAC;QACD,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC;QACtC,MAAM,cAAc,GAAG,OAAO,IAAI,KAAK,QAAQ,IAAI,MAAM,CAAC,aAAa,CAAC,IAAI,CAAC,IAAI,IAAI,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;QACtG,OAAO,EAAE,cAAc,EAAE,OAAO,EAAE,CAAC;IACpC,CAAC;CACD;AAKD;;;;;;;;;;;;;;GAcG;AACH,MAAM,OAAO,mBAAoB,SAAQ,YAAY;IAMpD,YAAY,EAAyB,EAAE,UAAsC,EAAE;QAC9E,KAAK,CAAC,IAAI,gBAAgB,CAAC,IAAI,mBAAmB,CAAC,IAAI,qBAAqB,CAAC,EAAE,EAAE,OAAO,CAAC,EAAE,OAAO,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC;IAChH,CAAC;CACD"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@optimystic/db-p2p-storage-web",
3
- "version": "1.14.0",
3
+ "version": "1.16.0",
4
4
  "type": "module",
5
5
  "description": "Browser IndexedDB storage backend for @optimystic/db-p2p",
6
6
  "main": "dist/src/index.js",
@@ -54,8 +54,8 @@
54
54
  "typescript": "^5.9.3"
55
55
  },
56
56
  "dependencies": {
57
- "@optimystic/db-core": "^1.14.0",
58
- "@optimystic/db-p2p": "^1.14.0",
57
+ "@optimystic/db-core": "^1.16.0",
58
+ "@optimystic/db-p2p": "^1.16.0",
59
59
  "debug": "^4.4.3",
60
60
  "idb": "^8.0.3"
61
61
  },
package/src/db.ts CHANGED
@@ -1,12 +1,20 @@
1
1
  import { openDB, type DBSchema, type IDBPDatabase } from 'idb';
2
2
  import type { ActionId, BlockId } from '@optimystic/db-core';
3
+ import type { JournalRecord } from '@optimystic/db-p2p';
4
+
5
+ /**
6
+ * The key of the applied-through mark in the `journal` store. A string, where every record's key
7
+ * is a number: IndexedDB orders numbers before strings, so a key range over the numbers never
8
+ * reaches the mark.
9
+ */
10
+ export const JOURNAL_MARK_KEY = 'applied-through';
3
11
 
4
12
  /**
5
13
  * IndexedDB schema for the Optimystic browser storage backend.
6
14
  *
7
15
  * The six block-storage stores keep their original object stores and compound
8
- * array keys, but their values are now opaque `Uint8Array` blobs: the shared
9
- * `KvRawStorage` kernel owns JSON (de)serialization, so `IndexedDBStoreDriver`
16
+ * array keys, but their values are opaque `Uint8Array` blobs: the shared
17
+ * `KvRawStorage` kernel owns JSON (de)serialization, so `IndexedDBJournalStore`
10
18
  * only ever hands IndexedDB kernel-encoded bytes. IndexedDB stores a
11
19
  * `Uint8Array` natively via structured clone and returns a `Uint8Array`, so the
12
20
  * bytes round-trip without a codec here.
@@ -21,7 +29,11 @@ import type { ActionId, BlockId } from '@optimystic/db-core';
21
29
  * as `revisions`, deliberately NOT the action keyspace (an action id is caller-chosen,
22
30
  * so no reserved id could be relied on).
23
31
  * - `materialized`: materialized block bytes keyed by `[blockId, actionId]`. Deleting
24
- * the row is a driver `delete`, not a stored `undefined`.
32
+ * the row is an entry of a write group with no value, not a stored `undefined`.
33
+ * - `journal`: the write groups appended and not yet applied to the six stores above, each a
34
+ * `JournalRecord` stored by structured clone under its `seq` (no codec: the record's keys are
35
+ * plain objects and its values `Uint8Array`s), and under {@link JOURNAL_MARK_KEY} the number of
36
+ * the last record applied. `IndexedDBJournalStore` owns both; nothing else touches the store.
25
37
  * - `kv`: a generic string keyspace shared by `IndexedDBKVStore` and the
26
38
  * identity helper. Identity keys are stored as raw `Uint8Array` blobs (under
27
39
  * a separate logical store from string-only `IKVStore` data, but the same
@@ -52,6 +64,10 @@ export interface OptimysticWebDB extends DBSchema {
52
64
  key: [BlockId, ActionId];
53
65
  value: Uint8Array;
54
66
  };
67
+ journal: {
68
+ key: number | typeof JOURNAL_MARK_KEY;
69
+ value: JournalRecord | number;
70
+ };
55
71
  kv: {
56
72
  key: string;
57
73
  value: string | Uint8Array;
@@ -59,9 +75,11 @@ export interface OptimysticWebDB extends DBSchema {
59
75
  }
60
76
 
61
77
  export const DEFAULT_DB_NAME = 'optimystic';
62
- // Bumped to 2 when the `proofs` object store was added. The `upgrade` hook is guarded
63
- // by `objectStoreNames.contains`, so an existing v1 database gains only the new store.
64
- export const DEFAULT_DB_VERSION = 2;
78
+ // Bumped to 2 when the `proofs` object store was added and to 3 for `journal`. The `upgrade` hook
79
+ // is guarded by `objectStoreNames.contains`, so an existing database gains only the stores it
80
+ // lacks (backlog `debt-indexeddb-version-upgrade-is-untested-and-unhandled` owns testing that, and
81
+ // the open that another tab's older connection blocks).
82
+ export const DEFAULT_DB_VERSION = 3;
65
83
 
66
84
  export type OptimysticWebDBHandle = IDBPDatabase<OptimysticWebDB>;
67
85
 
@@ -96,6 +114,9 @@ export async function openOptimysticWebDb(
96
114
  if (!db.objectStoreNames.contains('materialized')) {
97
115
  db.createObjectStore('materialized');
98
116
  }
117
+ if (!db.objectStoreNames.contains('journal')) {
118
+ db.createObjectStore('journal');
119
+ }
99
120
  if (!db.objectStoreNames.contains('kv')) {
100
121
  db.createObjectStore('kv');
101
122
  }