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

Function ipKeyGenerator

source/ip-key-generator.ts:19–39  ·  view source on GitHub ↗
(ip: string, ipv6Subnet: number | false = 56)

Source from the content-addressed store, hash-verified

17 * @public
18 */
19export function ipKeyGenerator(ip: string, ipv6Subnet: number | false = 56) {
20 if (isIPv6(ip)) {
21 const address = new Address6(ip)
22
23 // First, check if the address is IPv4 mapped to IPv6 (e.g., ::ffff:x.y.z.w),
24 // as is common on servers with dual-stack networks (both IPv4 and IPv6). If
25 // this is the case, we extract and return the IPv4 address. Otherwise, the
26 // default subnet value of 56 (or any 32 to 80 subnet) ignores the unique IP
27 // address in the last two octets completely.
28 if (address.is4()) return address.to4().correctForm()
29
30 // For IPv6, return the network address of the subnet in CIDR format
31 if (ipv6Subnet) {
32 const subnet = new Address6(`${ip}/${ipv6Subnet}`)
33 return subnet.networkForm()
34 }
35 }
36
37 // For IPv4, just return the IP address itself
38 return ip
39}

Callers 3

keyGeneratorFunction · 0.85
validation-test.tsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…