scanpack
Version:
Dependency scanner to detect unknown or malicious packages in Node.js and Bun projects
31 lines • 1.19 kB
JavaScript
export class RateLimiterAdapter {
requests = [];
maxRequests;
windowMs;
constructor(maxRequestsPerSecond = 10) {
this.maxRequests = maxRequestsPerSecond;
this.windowMs = 1000; // 1 second window
}
async wait() {
const now = Date.now();
// Remove requests outside the time window
this.requests = this.requests.filter(timestamp => now - timestamp < this.windowMs);
// If we're at the limit, wait until the oldest request expires
if (this.requests.length >= this.maxRequests) {
const oldestRequest = this.requests[0];
const waitTime = this.windowMs - (now - oldestRequest) + 10; // +10ms buffer
if (waitTime > 0) {
await new Promise(resolve => setTimeout(resolve, waitTime));
}
// Clean up again after waiting
const newNow = Date.now();
this.requests = this.requests.filter(timestamp => newNow - timestamp < this.windowMs);
}
// Record this request
this.requests.push(Date.now());
}
reset() {
this.requests = [];
}
}
//# sourceMappingURL=rate-limiter.adapter.js.map