Plain operations: last write wins, hash-keyed events, simpler composite
CI / Node 22 (pull_request) Successful in 12s
CI / Node 26 (pull_request) Successful in 13s
CI / Node 24 (pull_request) Successful in 14s

- KV: one write primitive, write(changes) with { key, value? }; writeIfAbsent removed.
  The server executes exactly what clients send and never reads values, so they can be
  encrypted end to end. Adds setFields/getFields on the base class.
- HTTP: no server history or event ids; SSE relays every change (own writes included) and
  the client emits resync when a dropped stream reconnects.
- Composite: writes apply locally, then go to the source (undone locally if refused);
  pushed changes are applied and announced only where they differ from local values;
  offline writes go to a queue store (any KV storage); sync() = replay, diff, replay.
  Removes KVReplicaSet, KVOutbox, KeyCounter, SerialQueue and RetryTimer.
- Events: id = <time>_<sha256(time + canonical data)>, so concurrent appends never
  collide and retries can't duplicate; no versions or ConcurrencyError.
- Event-sourced documents: read, resolve, append, apply; catchUp folds in foreign events;
  getAt takes an event id.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017Qxbkp4tA7G5SCWjzWuxmE
This commit is contained in:
Claude
2026-10-23 13:33:07 +00:00
parent 1f82ec9110
commit 631c75428b
34 changed files with 814 additions and 1387 deletions
+30 -32
View File
@@ -7,49 +7,47 @@ let network: TestNetwork
afterEach(() => network?.close())
describe('KVStorageHttp.subscribe (SSE)', () => {
it('receives changes made by other clients as remote change events', async () => {
it('relays every change under the path as remote change events, its own writes included', async () => {
network = new TestNetwork()
const watcher = network.client()
await watcher.subscribe('')
const received = watcher.waitFor('change', (e) => e.origin === 'remote')
await watcher.child('users').subscribe()
const received: KVChangeEvent[] = []
watcher.on('change', (e) => e.origin === 'remote' && received.push(e))
const other = watcher.waitFor('change', (e) => e.origin === 'remote')
await network.client().child('users').set('1', { name: 'Ada', n: 1n })
const event = await received
expect(event.changes).toEqual([{ key: 'users/1', value: { name: 'Ada', n: 1n } }])
expect(event.id).toMatch(/^\w+-1$/)
expect(watcher.lastEventId).toBe(event.id)
await other
const own = watcher.waitFor('change', (e) => e.origin === 'remote')
await network.client().set('elsewhere', 1) // not subscribed
await watcher.child('users').delete('1')
await own
expect(received.map((e) => e.changes)).toEqual([[{ key: 'users/1', value: { name: 'Ada', n: 1n } }], [{ key: 'users/1' }]])
})
it('resumes from lastEventId and replays missed changes', async () => {
it('emits resync when a dropped stream reconnects', async () => {
network = new TestNetwork()
const writer = network.client()
const watcher = network.client()
await watcher.subscribe('')
const first = watcher.waitFor('change', (e) => e.origin === 'remote')
await writer.writeMany([['a', 1]])
await first
await watcher.unsubscribe('')
await writer.writeMany([['b', 2]])
await writer.writeMany([['c', 3]])
const replayed: KVChangeEvent[] = []
watcher.on('change', (e) => replayed.push(e))
await watcher.subscribe('') // resolves once caught up, so the missed changes have been emitted
expect(replayed.map((e) => e.changes)).toEqual([[{ key: 'b', value: 2 }], [{ key: 'c', value: 3 }]])
const resync = watcher.waitFor('resync', () => true)
network.dropStreams()
await resync
const after = watcher.waitFor('change', () => true)
await network.client().set('a', 1)
expect((await after).changes).toEqual([{ key: 'a', value: 1 }])
})
it('asks for a resync when the missed changes are no longer in the history', async () => {
network = new TestNetwork({ historySize: 1 })
const writer = network.client()
it('stops relaying after unsubscribe', async () => {
network = new TestNetwork()
const watcher = network.client()
await watcher.subscribe('')
const seen = watcher.waitFor('change', () => true)
await writer.writeMany([['a', 1]])
await seen
await watcher.unsubscribe('')
await writer.writeMany([['b', 2]])
await writer.writeMany([['c', 3]])
const resync = watcher.waitFor('resync', () => true)
await watcher.subscribe('')
expect(await resync).toEqual({ id: expect.stringMatching(/-3$/) })
await watcher.subscribe('a')
await watcher.subscribe('b')
await watcher.unsubscribe('a')
const received: KVChangeEvent[] = []
watcher.on('change', (e) => received.push(e))
const arrived = watcher.waitFor('change', () => true)
await network.client().set('a', 1)
await network.client().set('b', 2)
await arrived
expect(received.flatMap((e) => e.changes)).toEqual([{ key: 'b', value: 2 }])
})
it('reports an unreachable server as StorageUnavailableError', async () => {