MCPcopy Index your code
hub / github.com/CodeGraphContext/CodeGraphContext / checkRateLimit

Function checkRateLimit

website/api/lib/security.js:66–90  ·  view source on GitHub ↗
(ip, now = Date.now())

Source from the content-addressed store, hash-verified

64}
65
66function checkRateLimit(ip, now = Date.now()) {
67 cleanupExpiredEntries(now);
68 const { windowMs, maxRequests } = getRateLimitConfig();
69
70 const existing = ipRateLimits.get(ip);
71 if (!existing) {
72 ipRateLimits.set(ip, { count: 1, windowStart: now });
73 return { allowed: true, remaining: maxRequests - 1, retryAfterSeconds: 0 };
74 }
75
76 if (now - existing.windowStart >= windowMs) {
77 ipRateLimits.set(ip, { count: 1, windowStart: now });
78 return { allowed: true, remaining: maxRequests - 1, retryAfterSeconds: 0 };
79 }
80
81 if (existing.count >= maxRequests) {
82 const retryAfterMs = windowMs - (now - existing.windowStart);
83 const retryAfterSeconds = Math.max(1, Math.ceil(retryAfterMs / 1000));
84 return { allowed: false, remaining: 0, retryAfterSeconds };
85 }
86
87 existing.count += 1;
88 ipRateLimits.set(ip, existing);
89 return { allowed: true, remaining: maxRequests - existing.count, retryAfterSeconds: 0 };
90}
91
92function checkRepoCooldown(repoKey, now = Date.now()) {
93 cleanupExpiredEntries(now);

Callers 1

handlerFunction · 0.90

Calls 3

cleanupExpiredEntriesFunction · 0.85
getRateLimitConfigFunction · 0.85
getMethod · 0.65

Tested by

no test coverage detected