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(iterable: AsyncIterable): Promise { 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 = {} 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(name: string, fn: () => T): T { this.calls[name] = (this.calls[name] ?? 0) + 1 return fn() } }