The IPAddress class represents an IP address, providing methods to validate, parse, and determine the version of the IP address (IPv4 or IPv6).

The class supports constructing from a string or another IPAddress instance, as well as extracting an IP address from a request header. It also provides utility methods for checking IP version and serializing the IP address.

const ip = IPAddress.from("192.168.1.1");
console.log(ip.isV4); // true
console.log(ip.version); // 4

Constructors

Accessors

  • get isV4(): boolean
  • Checks if the IP address is IPv4.

    Returns boolean

    true if the IP address is IPv4, otherwise false.

  • get isV6(): boolean
  • Checks if the IP address is IPv6.

    Returns boolean

    true if the IP address is IPv6, otherwise false.

  • get version(): 4 | 6
  • Gets the version of the IP address (4 for IPv4 or 6 for IPv6).

    Returns 4 | 6

    4 if the IP is IPv4, 6 if IPv6.

    If the IP address is invalid.

Methods

  • Checks if a value can be parsed as a valid IPAddress.

    Parameters

    • ip: string | IPAddress

      The IP address to check, either as a string or an IPAddress instance.

    Returns boolean

    true if the IP address can be parsed, otherwise false.

  • Extracts an IP address from a Request object by checking the CF-Connecting-IP header.

    Parameters

    • request: Request<unknown, CfProperties<unknown>>

      The incoming request containing headers.

    Returns null | IPAddress

    A new IPAddress instance if the IP is present in the headers, otherwise null.