- 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
47 lines
1.5 KiB
TypeScript
47 lines
1.5 KiB
TypeScript
import { BaseKVStorage, type ScanOptions } from '../src/kv-storage/base-kv-storage.js'
|
|
import { KVStorageInMemory } from '../src/kv-storage/kv-storage-in-memory.js'
|
|
|
|
export async function collect<T>(iterable: AsyncIterable<T>): Promise<T[]> {
|
|
const out: T[] = []
|
|
for await (const item of iterable) out.push(item)
|
|
return out
|
|
}
|
|
|
|
/**
|
|
* Wraps another KV storage and counts primitive calls. Also shows the wrapper pattern
|
|
* (e.g. an encryption layer): extend BaseKVStorage and delegate the five primitives.
|
|
* Counts are reported under the public method names (writeMany, removeMany, writeIfAbsent).
|
|
*/
|
|
export class KVStorageCounting extends BaseKVStorage {
|
|
calls: Record<string, number> = {}
|
|
|
|
constructor(readonly inner: BaseKVStorage = new KVStorageInMemory()) {
|
|
super()
|
|
}
|
|
|
|
reset(): void {
|
|
this.calls = {}
|
|
}
|
|
|
|
readMany(keys: string[]) {
|
|
return this.count('readMany', () => this.inner.readMany(keys))
|
|
}
|
|
protected applyWrite(entries: [string, unknown][]) {
|
|
return this.count('writeMany', () => this.inner.writeMany(entries))
|
|
}
|
|
protected applyRemove(keys: string[]) {
|
|
return this.count('removeMany', () => this.inner.removeMany(keys))
|
|
}
|
|
protected applyWriteIfAbsent(entries: [string, unknown][]) {
|
|
return this.count('writeIfAbsent', () => this.inner.writeIfAbsent(entries))
|
|
}
|
|
scan(options?: ScanOptions) {
|
|
return this.count('scan', () => this.inner.scan(options))
|
|
}
|
|
|
|
private count<T>(name: string, fn: () => T): T {
|
|
this.calls[name] = (this.calls[name] ?? 0) + 1
|
|
return fn()
|
|
}
|
|
}
|