@cyanheads/toolkit-mcp-server
Version:
MCP server providing system, network, geolocation, generator, datetime, and security tools
71 lines (70 loc) • 1.77 kB
JavaScript
export class Cache {
cache;
ttl;
constructor(ttlMs = 5 * 60 * 1000) {
this.cache = new Map();
this.ttl = ttlMs;
}
set(key, value) {
this.cache.set(key, {
data: value,
timestamp: Date.now()
});
}
get(key) {
const entry = this.cache.get(key);
if (!entry)
return null;
const now = Date.now();
if (now - entry.timestamp > this.ttl) {
this.cache.delete(key);
return null;
}
return entry.data;
}
clear() {
this.cache.clear();
}
prune() {
const now = Date.now();
for (const [key, entry] of this.cache.entries()) {
if (now - entry.timestamp > this.ttl) {
this.cache.delete(key);
}
}
}
}
export class RateLimiter {
windowMs;
maxRequests;
requests;
resetTime;
constructor(maxRequests = 45, windowMs = 60000) {
this.windowMs = windowMs;
this.maxRequests = maxRequests;
this.requests = 0;
this.resetTime = Date.now() + windowMs;
}
canMakeRequest() {
const now = Date.now();
if (now >= this.resetTime) {
this.requests = 0;
this.resetTime = now + this.windowMs;
}
return this.requests < this.maxRequests;
}
incrementRequests() {
this.requests++;
}
getRemainingRequests() {
const now = Date.now();
if (now >= this.resetTime) {
this.requests = 0;
this.resetTime = now + this.windowMs;
}
return Math.max(0, this.maxRequests - this.requests);
}
getTimeToReset() {
return Math.max(0, this.resetTime - Date.now());
}
}