(ip: string, ipv6Subnet: number | false = 56)
| 17 | * @public |
| 18 | */ |
| 19 | export 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 | } |
no outgoing calls
no test coverage detected
searching dependent graphs…