diff --git a/src/utils/rateLimiting.ts b/src/utils/rateLimiting.ts index f1a77b7..7ce2248 100644 --- a/src/utils/rateLimiting.ts +++ b/src/utils/rateLimiting.ts @@ -1,3 +1,6 @@ +/** + * Rate limit entry tracking request count and timestamp + */ interface RateLimitEntry { count: number; timestamp: number; @@ -6,6 +9,10 @@ interface RateLimitEntry { const WINDOW_SIZE_MS = 3600000; // 1 hour const MAX_REQUESTS = 5; // Maximum requests per hour +/** + * Rate Limiter for API request throttling + * Manages rate limiting based on IP addresses with sliding window + */ class RateLimiter { private cache: Map; @@ -13,6 +20,9 @@ class RateLimiter { this.cache = new Map(); } + /** + * Check if an IP address is currently rate limited + */ isRateLimited(ip: string): boolean { const now = Date.now(); const entry = this.cache.get(ip); @@ -35,6 +45,9 @@ class RateLimiter { return false; } + /** + * Get remaining attempts for an IP address + */ getRemainingAttempts(ip: string): number { const entry = this.cache.get(ip); if (!entry) { @@ -43,6 +56,9 @@ class RateLimiter { return Math.max(0, MAX_REQUESTS - entry.count); } + /** + * Get time in milliseconds until rate limit resets for an IP + */ getTimeToReset(ip: string): number { const entry = this.cache.get(ip); if (!entry) {