export class InMemoryRateLimit {
    options;
    buckets = new Map();
    now;
    constructor(options) {
        this.options = options;
        this.now = options.now ?? Date.now;
    }
    consume(key) {
        const now = this.now();
        const existing = this.buckets.get(key);
        const bucket = existing && existing.resetAt > now ? existing : { count: 0, resetAt: now + this.options.windowMs };
        if (bucket.count >= this.options.limit) {
            this.buckets.set(key, bucket);
            return { allowed: false, retryAfterMs: Math.max(0, bucket.resetAt - now) };
        }
        bucket.count += 1;
        this.buckets.set(key, bucket);
        return { allowed: true, retryAfterMs: 0 };
    }
    reset(key) {
        this.buckets.delete(key);
    }
}
//# sourceMappingURL=rateLimit.js.map