class RateLimiter { constructor() { this.records = new Map(); // key -> lastTs } checkLimit(key, intervalMs) { const now = Date.now(); const last = this.records.get(key) || 0; const diff = now - last; if (diff < intervalMs) { return { allowed: false, waitMs: intervalMs - diff }; } this.records.set(key, now); return { allowed: true, waitMs: 0 }; } getStats() { const result = []; for (const [key, ts] of this.records.entries()) { result.push({ key, lastAt: ts }); } return result; } clear() { this.records.clear(); } } module.exports = RateLimiter;