ultimate-mcp-server
Version:
The definitive all-in-one Model Context Protocol server for AI-assisted coding across 30+ platforms
37 lines • 1.14 kB
JavaScript
export class RateLimiter {
requests;
window; // in milliseconds
timestamps = [];
constructor(requests, windowInSeconds) {
this.requests = requests;
this.window = windowInSeconds * 1000;
}
allow() {
const now = Date.now();
const cutoff = now - this.window;
// Remove old timestamps
this.timestamps = this.timestamps.filter(t => t > cutoff);
// Check if we can allow this request
if (this.timestamps.length < this.requests) {
this.timestamps.push(now);
return true;
}
return false;
}
reset() {
this.timestamps = [];
}
getRemaining() {
const now = Date.now();
const cutoff = now - this.window;
this.timestamps = this.timestamps.filter(t => t > cutoff);
return Math.max(0, this.requests - this.timestamps.length);
}
getResetTime() {
if (this.timestamps.length === 0)
return 0;
const oldestTimestamp = Math.min(...this.timestamps);
return oldestTimestamp + this.window;
}
}
//# sourceMappingURL=rate-limiter.js.map