queue.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import { stableHash } from './parser.js';
  2. // Page-memory queue. Deduplication only advances after an explicit successful ACK.
  3. export class DeliveryQueue {
  4. constructor({ capacity = 1000, historyCapacity = 4000 } = {}) {
  5. this.capacity = capacity;
  6. this.historyCapacity = historyCapacity;
  7. this.pending = new Map();
  8. this.delivered = new Map();
  9. this.overflow = false;
  10. }
  11. reset() { this.pending.clear(); this.delivered.clear(); this.overflow = false; }
  12. get size() { return this.pending.size; }
  13. add(record) {
  14. const signature = stableHash(JSON.stringify(record));
  15. if (this.delivered.get(record.externalId) === signature) { this.pending.delete(record.externalId); return; }
  16. if (!this.pending.has(record.externalId) && this.pending.size >= this.capacity) { this.overflow = true; return; }
  17. this.pending.set(record.externalId, { signature, record });
  18. }
  19. batch({ maxRecords = 150, maxBytes = 750000 } = {}) {
  20. const entries = []; let bytes = 0;
  21. for (const entry of this.pending.entries()) {
  22. const size = new TextEncoder().encode(JSON.stringify(entry[1].record)).length;
  23. if (entries.length >= maxRecords || bytes + size > maxBytes) break;
  24. entries.push(entry); bytes += size;
  25. }
  26. return entries;
  27. }
  28. acknowledge(entries) {
  29. for (const [id, item] of entries) {
  30. this.delivered.set(id, item.signature);
  31. if (this.pending.get(id)?.signature === item.signature) this.pending.delete(id);
  32. }
  33. while (this.delivered.size > this.historyCapacity) this.delivered.delete(this.delivered.keys().next().value);
  34. }
  35. }