UNPKG

@akomalabs/kemono

Version:

A production-grade TypeScript API wrapper for the Kemono/Coomer platforms

47 lines 1.35 kB
import { RateLimitError } from '../errors/api-error'; /** * In-memory rate limiter implementation using sliding window */ export class MemoryRateLimiter { requests; maxRequests; windowMs; logger; constructor(config, logger) { this.maxRequests = config.maxRequests; this.windowMs = config.windowMs * 1000; this.requests = []; this.logger = logger; } cleanup() { const now = Date.now(); this.requests = this.requests.filter(timestamp => now - timestamp < this.windowMs); } async checkLimit() { this.cleanup(); if (this.requests.length >= this.maxRequests) { this.logger.warn('Rate limit exceeded'); throw new RateLimitError(); } } async incrementCount() { this.cleanup(); this.requests.push(Date.now()); } } /** * Creates a rate limiter instance based on the configuration * @param config Rate limit configuration from KemonoConfig * @param logger Logger instance * @returns RateLimiter instance */ export function createRateLimiter(config, logger) { if (!config) { return { checkLimit: async () => { }, incrementCount: async () => { }, }; } return new MemoryRateLimiter(config, logger); } //# sourceMappingURL=rate-limiter.js.map