MCPcopy
hub / github.com/express-rate-limit/express-rate-limit / keyGenerator

Function keyGenerator

source/rate-limit.ts:233–261  ·  view source on GitHub ↗
(request: Request, response: Response)

Source from the content-addressed store, hash-verified

231 response.statusCode < 400,
232 skip: (_request: Request, _response: Response): boolean => false,
233 async keyGenerator(request: Request, response: Response): Promise<string> {
234 // By default, use the IP address (for IPv4) or subnet (for IPv6) to rate limit users.
235
236 // Run the validation checks on the IP and headers to make sure everything
237 // is working as intended.
238 validations.ip(request.ip)
239 validations.trustProxy(request)
240 validations.xForwardedForHeader(request)
241 validations.forwardedHeader(request)
242
243 // Note: eslint thinks the ! is unnecessary but dts-bundle-generator disagrees
244 // biome-ignore lint/style/noNonNullAssertion: validations.ip is called above
245 const ip: string = request.ip!
246 let subnet: number | false = 56
247
248 if (isIPv6(ip)) {
249 // Apply subnet to ignore the bits that he end-user controls and rate-limit on only the bits their ISP controls
250 subnet =
251 typeof config.ipv6Subnet === 'function'
252 ? await config.ipv6Subnet(request, response)
253 : config.ipv6Subnet
254
255 // If it was a function, check the output now (otherwise it got checked earlier)
256 if (typeof config.ipv6Subnet === 'function')
257 validations.ipv6Subnet(subnet)
258 }
259
260 return ipKeyGenerator(ip, subnet)
261 },
262 ipv6Subnet: 56,
263 async handler(
264 request: Request,

Callers

nothing calls this directly

Calls 1

ipKeyGeneratorFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…