rate-limiter-custom
Version:
A fast, customizable, and efficient rate-limiting middleware for Express in TypeScript.
20 lines (19 loc) • 757 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.fixedWindowRateLimiter = fixedWindowRateLimiter;
const requestCounts = new Map();
function fixedWindowRateLimiter(options) {
setInterval(() => {
requestCounts.clear(); // Reset every windowMs
}, options.windowMs);
return (req, res, next) => {
var _a;
const ip = (_a = req.ip) !== null && _a !== void 0 ? _a : "unknown";
const currentCount = requestCounts.get(ip) || 0;
if (currentCount >= options.maxRequests) {
return res.status(429).json({ message: options.errorMessage || "Too many requests, please try again later." });
}
requestCounts.set(ip, currentCount + 1);
next();
};
}