UNPKG

multibridge

Version:

A multi-database connection framework with centralized configuration

42 lines (41 loc) 1.06 kB
"use strict"; /** * Simple in-memory rate limiter * Prevents excessive connection creation attempts */ Object.defineProperty(exports, "__esModule", { value: true }); exports.RateLimiter = void 0; class RateLimiter { limits; maxRequests; windowMs; constructor(maxRequests = 10, windowMs = 60000) { this.limits = new Map(); this.maxRequests = maxRequests; this.windowMs = windowMs; } check(key) { const now = Date.now(); const entry = this.limits.get(key); if (!entry || now > entry.resetTime) { // Create new window this.limits.set(key, { count: 1, resetTime: now + this.windowMs, }); return true; } if (entry.count >= this.maxRequests) { return false; // Rate limit exceeded } entry.count++; return true; } reset(key) { this.limits.delete(key); } clear() { this.limits.clear(); } } exports.RateLimiter = RateLimiter;