limiter.js
Version:
limiter.js is a Node.js/TypeScript library that provides simple Rate limiter protection for Express applications. It tracks requests per IP address and enforces rate limits within a sliding time window. If an IP exceeds the allowed requests, limiter.js ca
34 lines (33 loc) • 1.24 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RateLimiter = void 0;
class RateLimiter {
constructor(windowMs, maxHits) {
this.windowMs = windowMs;
this.maxHits = maxHits;
}
shouldBlock(ipTracker) {
const hitCount = ipTracker.getHitCountInWindow(this.windowMs);
return hitCount >= this.maxHits;
}
shouldBanPermanently(ipTracker, permanentBanThreshold) {
if (permanentBanThreshold === undefined) {
return false; // no permanent ban threshold set
}
// check if IP is already permanently banned
if (ipTracker.getIsPermanentlyBanned()) {
return false; // already permanently banned, no need to ban again
}
const hitCount = ipTracker.getHitCountInWindow(this.windowMs);
const banCount = ipTracker.getBanCount();
// Permanent ban if current hits exceed threshold OR if IP has been banned multiple times
return hitCount >= permanentBanThreshold || banCount >= 3; // Ban permanently after 3 temporary bans
}
getWindowMs() {
return this.windowMs;
}
getMaxHits() {
return this.maxHits;
}
}
exports.RateLimiter = RateLimiter;