(req, config)
| 84 | |
| 85 | // Mock rate limit function |
| 86 | const checkRateLimit = (req, config) => { |
| 87 | const reqId = req.sessionID || req.ip; |
| 88 | const currentTime = Math.floor(Date.now() / 1000); |
| 89 | |
| 90 | if (!rateStore.has(reqId)) { |
| 91 | rateStore.set(reqId, { |
| 92 | limit: config.limit, |
| 93 | remaining: config.limit - 1, |
| 94 | reset: currentTime + config.reset |
| 95 | }); |
| 96 | |
| 97 | return { allowed: true, remaining: config.limit - 1, reset: currentTime + config.reset }; |
| 98 | } |
| 99 | |
| 100 | const state = rateStore.get(reqId); |
| 101 | |
| 102 | if (currentTime >= state.reset) { |
| 103 | // Reset the window |
| 104 | state.remaining = config.limit - 1; |
| 105 | state.reset = currentTime + config.reset; |
| 106 | |
| 107 | return { allowed: true, remaining: state.remaining, reset: state.reset }; |
| 108 | } |
| 109 | |
| 110 | if (state.remaining > 0) { |
| 111 | state.remaining--; |
| 112 | |
| 113 | return { allowed: true, remaining: state.remaining, reset: state.reset }; |
| 114 | } |
| 115 | |
| 116 | return { allowed: false, remaining: 0, reset: state.reset }; |
| 117 | }; |
| 118 | |
| 119 | const config = { limit: 100, reset: 900 }; |
| 120 |
no outgoing calls
no test coverage detected