import { stableHash } from './parser.js'; // Page-memory queue. Deduplication only advances after an explicit successful ACK. export class DeliveryQueue { constructor({ capacity = 1000, historyCapacity = 4000 } = {}) { this.capacity = capacity; this.historyCapacity = historyCapacity; this.pending = new Map(); this.delivered = new Map(); this.overflow = false; } reset() { this.pending.clear(); this.delivered.clear(); this.overflow = false; } get size() { return this.pending.size; } add(record) { const signature = stableHash(JSON.stringify(record)); if (this.delivered.get(record.externalId) === signature) { this.pending.delete(record.externalId); return; } if (!this.pending.has(record.externalId) && this.pending.size >= this.capacity) { this.overflow = true; return; } this.pending.set(record.externalId, { signature, record }); } batch({ maxRecords = 150, maxBytes = 750000 } = {}) { const entries = []; let bytes = 0; for (const entry of this.pending.entries()) { const size = new TextEncoder().encode(JSON.stringify(entry[1].record)).length; if (entries.length >= maxRecords || bytes + size > maxBytes) break; entries.push(entry); bytes += size; } return entries; } acknowledge(entries) { for (const [id, item] of entries) { this.delivered.set(id, item.signature); if (this.pending.get(id)?.signature === item.signature) this.pending.delete(id); } while (this.delivered.size > this.historyCapacity) this.delivered.delete(this.delivered.keys().next().value); } }