export interface SessionRecord { value: Record; expires: number; } export interface SessionStore { get(key: string): Promise; set(key: string, record: SessionRecord): Promise; delete(key: string): Promise; /** Atomically removes and returns a transaction, preventing callback replay. */ consume(key: string): Promise; } export class MemorySessionStore implements SessionStore { private readonly records = new Map(); async get(key: string): Promise { const record = this.records.get(key); if (!record || record.expires <= Date.now()) { this.records.delete(key); return undefined; } return structuredClone(record); } async set(key: string, record: SessionRecord): Promise { this.records.set(key, structuredClone(record)); } async delete(key: string): Promise { this.records.delete(key); } async consume(key: string): Promise { const record = this.records.get(key); this.records.delete(key); return record && record.expires > Date.now() ? structuredClone(record) : undefined; } }