@j03fr0st/pubg-ts
Version:
A comprehensive TypeScript wrapper for the PUBG API
37 lines • 1.36 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RateLimiter = void 0;
class RateLimiter {
constructor(maxRequests = 10, windowMs = 60000) {
this.requests = [];
this.maxRequests = maxRequests;
this.windowMs = windowMs;
}
async waitForSlot() {
const now = Date.now();
this.requests = this.requests.filter((time) => now - time < this.windowMs);
if (this.requests.length >= this.maxRequests) {
const oldestRequest = Math.min(...this.requests);
const waitTime = this.windowMs - (now - oldestRequest);
if (waitTime > 0) {
await new Promise((resolve) => setTimeout(resolve, waitTime));
return this.waitForSlot();
}
}
this.requests.push(now);
}
getRemainingRequests() {
const now = Date.now();
this.requests = this.requests.filter((time) => now - time < this.windowMs);
return Math.max(0, this.maxRequests - this.requests.length);
}
getResetTime() {
if (this.requests.length === 0)
return 0;
const _now = Date.now();
const oldestRequest = Math.min(...this.requests);
return oldestRequest + this.windowMs;
}
}
exports.RateLimiter = RateLimiter;
//# sourceMappingURL=rate-limiter.js.map