Add KVStorageComposite, IndexedDB storage and SSE change streams
- Every KV storage emits change events (base class owns writeMany/removeMany/ writeIfAbsent; implementations provide protected apply* methods) - KVStorageHttpServer streams changes over SSE (GET /events) with a resumable history; KVStorageHttp.watch() subscribes and resumes via Last-Event-ID - KVStorageIndexedDB for local persistence - KVStorageComposite keeps replicas of a source in sync: local reads, resume instead of full sync, offline outbox replayed in order, echo tracking - Values may contain bigint/Uint8Array (Extended JSON over the network) - Vendor sse-session, event-emitter and extended-json (no runtime deps) - Freeze child nodes so shadowed state on children fails loudly - CI on Node 22, 24 and 26 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017Qxbkp4tA7G5SCWjzWuxmE
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
import { afterEach, describe, expect, it } from 'vitest'
|
||||
import type { KVChangeEvent } from '../../src/kv-storage/base-kv-storage.js'
|
||||
import { StorageUnavailableError } from '../../src/utils/errors.js'
|
||||
import { TestNetwork } from './sync-helpers.js'
|
||||
|
||||
let network: TestNetwork
|
||||
afterEach(() => network?.close())
|
||||
|
||||
describe('KVStorageHttp.watch (SSE)', () => {
|
||||
it('receives changes made by other clients as remote change events', async () => {
|
||||
network = new TestNetwork()
|
||||
const watcher = network.client()
|
||||
await watcher.watch()
|
||||
const received = 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)
|
||||
})
|
||||
|
||||
it('resumes from lastEventId and replays missed changes', async () => {
|
||||
network = new TestNetwork()
|
||||
const writer = network.client()
|
||||
const watcher = network.client()
|
||||
await watcher.watch()
|
||||
const first = watcher.waitFor('change', (e) => e.origin === 'remote')
|
||||
await writer.writeMany([['a', 1]])
|
||||
await first
|
||||
await watcher.unwatch()
|
||||
await writer.writeMany([['b', 2]])
|
||||
await writer.writeMany([['c', 3]])
|
||||
const replayed: KVChangeEvent[] = []
|
||||
watcher.on('change', (e) => replayed.push(e))
|
||||
await watcher.watch() // 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 }]])
|
||||
})
|
||||
|
||||
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()
|
||||
const watcher = network.client()
|
||||
await watcher.watch()
|
||||
const seen = watcher.waitFor('change', () => true)
|
||||
await writer.writeMany([['a', 1]])
|
||||
await seen
|
||||
await watcher.unwatch()
|
||||
await writer.writeMany([['b', 2]])
|
||||
await writer.writeMany([['c', 3]])
|
||||
const resync = watcher.waitFor('resync', () => true)
|
||||
await watcher.watch()
|
||||
expect(await resync).toEqual({ id: expect.stringMatching(/-3$/) })
|
||||
})
|
||||
|
||||
it('reports an unreachable server as StorageUnavailableError', async () => {
|
||||
network = new TestNetwork()
|
||||
const client = network.client()
|
||||
network.online = false
|
||||
await expect(client.readMany(['x'])).rejects.toBeInstanceOf(StorageUnavailableError)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user