UNPKG

sveltekit-rate-limiter

Version:

A modular rate limiter for SvelteKit. Use in password resets, account registration, etc.

22 lines (21 loc) 573 B
import TTLCache from '@isaacs/ttlcache'; export class RetryAfterStore { cache; constructor(maxItems = Infinity) { this.cache = new TTLCache({ max: maxItems, noUpdateTTL: true }); } async clear() { return this.cache.clear(); } async add(hash, ttl) { const currentRate = this.cache.get(hash); if (currentRate) return this.cache.get(hash) ?? 0; const retryAfter = Date.now() + ttl; this.cache.set(hash, retryAfter, { ttl }); return retryAfter; } }