MCPcopy Create free account
hub / github.com/AmazingAng/PolyWorld / checkRateLimit

Function checkRateLimit

src/middleware.ts:78–105  ·  view source on GitHub ↗
(
  ip: string,
  tier: keyof typeof rateMaps
)

Source from the content-addressed store, hash-verified

76}
77
78function checkRateLimit(
79 ip: string,
80 tier: keyof typeof rateMaps
81): { allowed: boolean; retryAfter: number; limit: number; remaining: number; resetAt: number } {
82 cleanup();
83
84 const { max, windowMs } = RATE_LIMITS[tier];
85 const map = rateMaps[tier];
86 const now = Date.now();
87
88 let bucket = map.get(ip);
89 if (!bucket || now >= bucket.resetAt) {
90 bucket = { count: 0, resetAt: now + windowMs };
91 map.set(ip, bucket);
92 }
93
94 bucket.count++;
95 const remaining = Math.max(0, max - bucket.count);
96 const retryAfter = Math.ceil((bucket.resetAt - now) / 1000);
97
98 return {
99 allowed: bucket.count <= max,
100 retryAfter,
101 limit: max,
102 remaining,
103 resetAt: bucket.resetAt,
104 };
105}
106
107// ---------------------------------------------------------------------------
108// Security Headers

Callers 1

middlewareFunction · 0.85

Calls 3

cleanupFunction · 0.70
getMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected