Files
PotentialStorage/test/helpers.ts
T
Claude 631c75428b
CI / Node 22 (pull_request) Successful in 12s
CI / Node 26 (pull_request) Successful in 13s
CI / Node 24 (pull_request) Successful in 14s
Plain operations: last write wins, hash-keyed events, simpler composite
- 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
2026-10-23 13:33:07 +00:00

41 lines
1.2 KiB
TypeScript

import { BaseKVStorage, type KVChange, 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 three primitives.
* Writes are counted as `writeMany`.
*/
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 write(changes: KVChange[]) {
return this.count('writeMany', () => this.inner.writeMany(changes))
}
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()
}
}