UNPKG

mcp-wayback-machine

Version:

MCP server and CLI tool for interacting with the Wayback Machine without API keys

62 lines 1.97 kB
/** * Rate limiting for Wayback Machine API requests. * * RateLimitBackend defines the consumer interface (just acquire()). * InMemoryRateLimiter implements it for single-process use (stdio mode). * Worker deployments use a Durable Object backend (see rate-limit-do.ts). */ /** * In-memory rate limiter using a sliding window of request timestamps. * Suitable for single-process use (stdio mode) where all requests share * the same heap. */ export class InMemoryRateLimiter { requests = []; maxRequests; windowMs; constructor(options) { this.maxRequests = options.maxRequests; this.windowMs = options.windowMs; } canMakeRequest() { this.cleanup(); return this.requests.length < this.maxRequests; } async waitForSlot() { while (!this.canMakeRequest()) { const oldestRequest = this.requests[0]; if (oldestRequest === undefined) break; const waitTime = oldestRequest + this.windowMs - Date.now(); if (waitTime > 0) { await new Promise((resolve) => setTimeout(resolve, waitTime + 100)); } this.cleanup(); } } recordRequest() { this.requests.push(Date.now()); } /** * Wait for a slot, then reserve it. waitForSlot() guarantees that * canMakeRequest() returned true immediately before it resolved, * so we can record immediately. */ async acquire() { await this.waitForSlot(); this.recordRequest(); } cleanup() { const cutoff = Date.now() - this.windowMs; this.requests = this.requests.filter((time) => time > cutoff); } } /** * Default rate limiter for stdio mode — conservative limits to be * respectful of the Internet Archive service. */ export const waybackRateLimiter = new InMemoryRateLimiter({ maxRequests: 15, windowMs: 60000, }); //# sourceMappingURL=rate-limit.js.map