UNPKG

cidr-block

Version:

IPv4 and IPv6 address and cidr range utilities

1,523 lines (1,516 loc) 68.3 kB
/** * Represents an IPv4 address from a 'literal' value. * Examples: * - String: "10.0.0.0" * - Number: 167772160 * - Octet Array: [10, 0, 0, 0] */ type Ipv4AddressLiteral = string | number | number[]; /** * Represents an IPv4 address in string format (uses stricter typescript type). */ type Ipv4AddressString = `${number}.${number}.${number}.${number}`; /** * Represents an IPv4 address as an array of four octets. */ type Ipv4AddressOctets = [number, number, number, number]; /** * Represents an IPv4 CIDR from a 'literal' value. * Examples: * - String: "10.0.0.0/24" * - Object: { address: "10.0.0.0", range: 24 } * - Tuple: [[10, 0, 0, 0], 24] */ type Ipv4CidrLiteral = string | { address: Ipv4AddressLiteral; range: number; } | [Ipv4AddressLiteral, number]; /** * Represents an IPv4 CIDR in string format (uses stricter typescript type). */ type Ipv4CidrString = `${number}.${number}.${number}.${number}/${number}`; /** * Represents an IPv4 address with utility methods for manipulation and comparison. * * While you can instantiate this class directly, it is recommended to use the * {@link ipv4.address} shorthand method from the `ipv4` namespace instead. * * @example Creating addresses (prefer the shorthand) * ```ts * import { ipv4 } from 'cidr-block'; * * // Recommended: use the ipv4 namespace shorthand * const addr1 = ipv4.address("192.168.1.1"); * const addr2 = ipv4.address(3232235777); * const addr3 = ipv4.address([192, 168, 1, 1]); * ``` * * @example Converting between formats * ```ts * import { ipv4 } from 'cidr-block'; * * const addr = ipv4.address("192.168.1.1"); * addr.toString(); // "192.168.1.1" * addr.toNumber(); // 3232235777 * addr.octets(); // [192, 168, 1, 1] * addr.toBinaryString(); // "11000000.10101000.00000001.00000001" * ``` * * @example Comparing addresses * ```ts * import { ipv4 } from 'cidr-block'; * * const addr = ipv4.address("192.168.1.1"); * addr.equals("192.168.1.1"); // true * addr.isGreaterThan("192.168.1.0"); // true * addr.isLessThan("192.168.1.2"); // true * ``` * * @example Navigating sequential addresses * ```ts * import { ipv4 } from 'cidr-block'; * * const addr = ipv4.address("192.168.1.1"); * addr.nextAddress()?.toString(); // "192.168.1.2" * addr.previousAddress()?.toString(); // "192.168.1.0" * ``` * * @example Checking address types * ```ts * import { ipv4 } from 'cidr-block'; * * ipv4.address("127.0.0.1").isLoopbackAddress(); // true * ipv4.address("10.0.0.1").isPrivateAddress(); // true * ipv4.address("169.254.1.1").isLocalLinkAddress(); // true * ipv4.address("224.0.0.1").isMulticastAddress(); // true * ``` */ declare class Ipv4Address { #private; constructor(ip: Ipv4AddressLiteral); /** * Gets the octets of the IPv4 address. * * @example * ```ts * import { ipv4 } from 'cidr-block'; * * const addr = ipv4.address("192.168.1.1"); * addr.octets(); // [192, 168, 1, 1] * ``` * * @returns An array of four numbers representing the octets of the IPv4 address. */ octets(): Ipv4AddressOctets; /** * Returns the string representation of the IPv4 address. * * @example * ```ts * import { ipv4 } from 'cidr-block'; * * const addr = ipv4.address([10, 0, 0, 1]); * addr.toString(); // "10.0.0.1" * ``` * * @returns The IPv4 address as a string in dotted-decimal notation (example: "192.187.0.1"). */ toString(): Ipv4AddressString; /** * Returns the binary string representation of the IPv4 address. * * @example * ```ts * import { ipv4 } from 'cidr-block'; * * const addr = ipv4.address("192.168.1.1"); * addr.toBinaryString(); // "11000000.10101000.00000001.00000001" * ``` * * @returns The IPv4 address as a binary string in dotted-decimal notation (example: "11000000.10111011.00000000.00000001"). */ toBinaryString(): string; /** * Converts the IPv4 address to its numeric representation. * * @example * ```ts * import { ipv4 } from 'cidr-block'; * * const addr = ipv4.address("192.168.1.1"); * addr.toNumber(); // 3232235777 * ``` * * @returns The IPv4 address as a number. */ toNumber(): number; /** * Checks if there is a next sequential IPv4 address. * This would only return false if the current address is the maximum possible value (255.255.255.255). * * @example * ```ts * import { ipv4 } from 'cidr-block'; * * ipv4.address("192.168.1.1").hasNextAddress(); // true * ipv4.address("255.255.255.255").hasNextAddress(); // false * ``` * * @returns true if there is a next IPv4 address. */ hasNextAddress(): boolean; /** * Gets the next sequential IPv4 address. * * @example * ```ts * import { ipv4 } from 'cidr-block'; * * const addr = ipv4.address("192.168.1.1"); * addr.nextAddress()?.toString(); // "192.168.1.2" * * const maxAddr = ipv4.address("255.255.255.255"); * maxAddr.nextAddress(); // undefined * ``` * * @returns The next IPv4 address, or undefined if the current address is the maximum possible value. */ nextAddress(): Ipv4Address | undefined; /** * Checks if there is a previous sequential IPv4 address. * This would only return false if the current address is the minimum possible value (0.0.0.0). * * @example * ```ts * import { ipv4 } from 'cidr-block'; * * ipv4.address("192.168.1.1").hasPreviousAddress(); // true * ipv4.address("0.0.0.0").hasPreviousAddress(); // false * ``` * * @returns true if there is a previous IPv4 address. */ hasPreviousAddress(): boolean; /** * Gets the previous sequential IPv4 address. * * @example * ```ts * import { ipv4 } from 'cidr-block'; * * const addr = ipv4.address("192.168.1.1"); * addr.previousAddress()?.toString(); // "192.168.1.0" * * const minAddr = ipv4.address("0.0.0.0"); * minAddr.previousAddress(); // undefined * ``` * * @returns The previous IPv4 address, or undefined if the current address is the minimum possible value. */ previousAddress(): Ipv4Address | undefined; /** * Compares two IPv4 addresses for equality. * * @example * ```ts * import { ipv4 } from 'cidr-block'; * * const addr = ipv4.address("192.168.1.1"); * addr.equals("192.168.1.1"); // true * addr.equals([192, 168, 1, 1]); // true * addr.equals(ipv4.address("10.0.0.1")); // false * ``` * * @param otherAddress - The other IPv4 address to compare with, which can be an Ipv4Address instance or literal value. * @returns true if both IPv4 addresses are equal */ equals(otherAddress: Ipv4Address | Ipv4AddressLiteral): boolean; /** * Compares if this IPv4 address is greater than another IPv4 address. * * @example * ```ts * import { ipv4 } from 'cidr-block'; * * const addr = ipv4.address("192.168.1.1"); * addr.isGreaterThan("192.168.1.0"); // true * addr.isGreaterThan("192.168.1.1"); // false * addr.isGreaterThan("192.168.1.2"); // false * ``` * * @param otherAddress - The other IPv4 address to compare with, which can be an Ipv4Address instance or literal value. * @returns true if this IPv4 address is greater than the other IPv4 address */ isGreaterThan(otherAddress: Ipv4Address | Ipv4AddressLiteral): boolean; /** * Compares if this IPv4 address is greater than or equal to another IPv4 address. * * @example * ```ts * import { ipv4 } from 'cidr-block'; * * const addr = ipv4.address("192.168.1.1"); * addr.isGreaterThanOrEqual("192.168.1.0"); // true * addr.isGreaterThanOrEqual("192.168.1.1"); // true * addr.isGreaterThanOrEqual("192.168.1.2"); // false * ``` * * @param otherAddress - The other IPv4 address to compare with, which can be an Ipv4Address instance or literal value. * @returns true if this IPv4 address is greater than or equal to the other IPv4 address */ isGreaterThanOrEqual(otherAddress: Ipv4Address | Ipv4AddressLiteral): boolean; /** * Compares if this IPv4 address is less than another IPv4 address. * * @example * ```ts * import { ipv4 } from 'cidr-block'; * * const addr = ipv4.address("192.168.1.1"); * addr.isLessThan("192.168.1.2"); // true * addr.isLessThan("192.168.1.1"); // false * addr.isLessThan("192.168.1.0"); // false * ``` * * @param otherAddress - The other IPv4 address to compare with, which can be an Ipv4Address instance or literal value. * @returns true if this IPv4 address is less than the other IPv4 address */ isLessThan(otherAddress: Ipv4Address | Ipv4AddressLiteral): boolean; /** * Compares if this IPv4 address is less than or equal to another IPv4 address. * * @example * ```ts * import { ipv4 } from 'cidr-block'; * * const addr = ipv4.address("192.168.1.1"); * addr.isLessThanOrEqual("192.168.1.2"); // true * addr.isLessThanOrEqual("192.168.1.1"); // true * addr.isLessThanOrEqual("192.168.1.0"); // false * ``` * * @param otherAddress - The other IPv4 address to compare with, which can be an Ipv4Address instance or literal value. * @returns true if this IPv4 address is less than or equal to the other IPv4 address */ isLessThanOrEqual(otherAddress: Ipv4Address | Ipv4AddressLiteral): boolean; /** * Checks if the IPv4 address is a loopback address (in the CIDR of 127.0.0.0/8) * * @example * ```ts * import { ipv4 } from 'cidr-block'; * * ipv4.address("127.0.0.1").isLoopbackAddress(); // true * ipv4.address("127.255.0.1").isLoopbackAddress(); // true * ipv4.address("192.168.1.1").isLoopbackAddress(); // false * ``` * * @returns true if the IPv4 address is a loopback address (example: 127.0.0.1 is true) */ isLoopbackAddress(): boolean; /** * Checks if the IPv4 address is a private address. * * This is based on RFC 1918, which defines the following private address ranges: * - 10.0.0.0/8 * - 172.16.0.0/12 * - 192.168.0.0/16 * * @example * ```ts * import { ipv4 } from 'cidr-block'; * * ipv4.address("10.0.0.1").isPrivateAddress(); // true * ipv4.address("172.16.0.1").isPrivateAddress(); // true * ipv4.address("192.168.1.1").isPrivateAddress(); // true * ipv4.address("8.8.8.8").isPrivateAddress(); // false * ``` * * @returns true if the IPv4 address is in any of the private address ranges. */ isPrivateAddress(): boolean; /** * Checks if the IPv4 address is a local-link address (in the CIDR of 169.254.0.0/16). * * @example * ```ts * import { ipv4 } from 'cidr-block'; * * ipv4.address("169.254.1.1").isLocalLinkAddress(); // true * ipv4.address("169.254.255.255").isLocalLinkAddress(); // true * ipv4.address("192.168.1.1").isLocalLinkAddress(); // false * ``` * * @returns true if the IPv4 address is a local-link address. */ isLocalLinkAddress(): boolean; /** * Checks if the IPv4 address is a multicast address (between 224.0.0.0 to 239.255.255.255). * * @example * ```ts * import { ipv4 } from 'cidr-block'; * * ipv4.address("224.0.0.1").isMulticastAddress(); // true * ipv4.address("239.255.255.255").isMulticastAddress(); // true * ipv4.address("192.168.1.1").isMulticastAddress(); // false * ``` * * @returns true if the IPv4 address is a multicast address. */ isMulticastAddress(): boolean; } /** * Represents an IPv4 CIDR block with utility methods for subnet operations. * * While you can instantiate this class directly, it is recommended to use the * {@link ipv4.cidr} shorthand method from the `ipv4` namespace instead. * * @example Creating CIDR blocks (prefer the shorthand) * ```ts * import { ipv4 } from 'cidr-block'; * * // Recommended: use the ipv4 namespace shorthand * const cidr1 = ipv4.cidr("192.168.0.0/24"); * const cidr2 = ipv4.cidr({ address: "10.0.0.0", range: 8 }); * const cidr3 = ipv4.cidr([[172, 16, 0, 0], 12]); * ``` * * @example Getting CIDR properties * ```ts * import { ipv4 } from 'cidr-block'; * * const cidr = ipv4.cidr("192.168.0.0/24"); * cidr.baseAddress().toString(); // "192.168.0.0" * cidr.range(); // 24 * cidr.netmask().toString(); // "255.255.255.0" * cidr.network().toString(); // "192.168.0.0" * cidr.addressCount(); // 256 * ``` * * @example Working with usable addresses * ```ts * import { ipv4 } from 'cidr-block'; * * const cidr = ipv4.cidr("192.168.0.0/24"); * cidr.getFirstUsableAddress()?.toString(); // "192.168.0.1" * cidr.getLastUsableAddress()?.toString(); // "192.168.0.254" * ``` * * @example Iterating over addresses * ```ts * import { ipv4 } from 'cidr-block'; * * const cidr = ipv4.cidr("192.168.1.0/30"); * for (const addr of cidr.addresses()) { * console.log(addr.toString()); * } * // "192.168.1.0", "192.168.1.1", "192.168.1.2", "192.168.1.3" * ``` * * @example Checking containment and overlap * ```ts * import { ipv4 } from 'cidr-block'; * * const cidr = ipv4.cidr("192.168.0.0/24"); * cidr.includes(ipv4.address("192.168.0.100")); // true * cidr.includes(ipv4.address("192.168.1.1")); // false * cidr.overlaps("192.168.0.0/25"); // true * cidr.overlaps("10.0.0.0/8"); // false * ``` * * @example Splitting into subranges * ```ts * import { ipv4 } from 'cidr-block'; * * const cidr = ipv4.cidr("192.168.0.0/24"); * * // Split into equal /26 subnets * cidr.subnet(26).map(s => s.toString()); * // ["192.168.0.0/26", "192.168.0.64/26", "192.168.0.128/26", "192.168.0.192/26"] * * // Split by specific ranges * cidr.subnetBy([25, 26, 27, 27]).map(s => s.toString()); * // ["192.168.0.0/25", "192.168.0.128/26", "192.168.0.192/27", "192.168.0.224/27"] * ``` * * @example Navigating sequential CIDRs * ```ts * import { ipv4 } from 'cidr-block'; * * const cidr = ipv4.cidr("192.168.0.0/24"); * cidr.nextCIDR()?.toString(); // "192.168.1.0/24" * cidr.previousCIDR()?.toString(); // "192.167.255.0/24" * ``` */ declare class Ipv4Cidr { #private; constructor(address: Ipv4CidrLiteral); /** * Gets the base IPv4 address of the CIDR. * * @example * ```ts * import { ipv4 } from 'cidr-block'; * * const cidr = ipv4.cidr("192.168.0.0/24"); * cidr.baseAddress().toString(); // "192.168.0.0" * ``` * * @returns The base IPv4 address. */ baseAddress(): Ipv4Address; /** * Gets the CIDR range. * * @example * ```ts * import { ipv4 } from 'cidr-block'; * * const cidr = ipv4.cidr("192.168.0.0/24"); * cidr.range(); // 24 * ``` * * @returns The CIDR range as a number. */ range(): number; /** * Calculates the netmask for the CIDR range. * * @example * ```ts * import { ipv4 } from 'cidr-block'; * * ipv4.cidr("192.168.0.0/24").netmask().toString(); // "255.255.255.0" * ipv4.cidr("10.0.0.0/8").netmask().toString(); // "255.0.0.0" * ipv4.cidr("172.16.0.0/16").netmask().toString(); // "255.255.0.0" * ``` * * @returns The netmask as an Ipv4Address. */ netmask(): Ipv4Address; /** * Calculates the hostmask (inverse of the netmask) for the CIDR range. * * @example * ```ts * import { ipv4 } from 'cidr-block'; * * ipv4.cidr("192.168.0.0/24").hostmask().toString(); // "0.0.0.255" * ipv4.cidr("10.0.0.0/8").hostmask().toString(); // "0.255.255.255" * ipv4.cidr("172.16.0.0/16").hostmask().toString(); // "0.0.255.255" * ``` * * @returns The hostmask as an Ipv4Address. */ hostmask(): Ipv4Address; /** * Calculates the network address by applying the netmask to the base address. * * @example * ```ts * import { ipv4 } from 'cidr-block'; * * ipv4.cidr("192.168.1.5/24").network().toString(); // "192.168.1.0" * ipv4.cidr("10.5.10.20/8").network().toString(); // "10.0.0.0" * ipv4.cidr("172.16.5.1/16").network().toString(); // "172.16.0.0" * ``` * * @returns The network address as an Ipv4Address. */ network(): Ipv4Address; /** * Calculates the network CIDR by applying the netmask to the base address and returning a CIDR with the network address. * * @example * ```ts * import { ipv4 } from 'cidr-block'; * * ipv4.cidr("192.168.1.5/24").networkCIDR().toString(); // "192.168.1.0/24" * ipv4.cidr("10.5.10.20/8").networkCIDR().toString(); // "10.0.0.0/8" * ipv4.cidr("172.16.5.1/16").networkCIDR().toString(); // "172.16.0.0/16" * ``` * * @returns The network CIDR as an Ipv4Cidr. */ networkCIDR(): Ipv4Cidr; /** * Returns the string representation of the IPv4 CIDR. * * @example * ```ts * import { ipv4 } from 'cidr-block'; * * const cidr = ipv4.cidr({ address: "10.0.0.0", range: 8 }); * cidr.toString(); // "10.0.0.0/8" * ``` * * @returns The IPv4 CIDR as a string (example: "192.0.0.0/24"). */ toString(): Ipv4CidrString; /** * Gets the address and range parts of the IPv4 CIDR. * * @example * ```ts * import { ipv4 } from 'cidr-block'; * * const [address, range] = ipv4.cidr("192.168.0.0/24").rangeParts(); * address.toString(); // "192.168.0.0" * range; // 24 * ``` * * @returns A tuple containing the IPv4 address and the CIDR range. */ rangeParts(): [Ipv4Address, number]; /** * Calculates the total number of addresses in the CIDR range. * * @example * ```ts * import { ipv4 } from 'cidr-block'; * * ipv4.cidr("192.168.0.0/24").addressCount(); // 256 * ipv4.cidr("10.0.0.0/8").addressCount(); // 16777216 * ipv4.cidr("192.168.1.0/32").addressCount(); // 1 * ``` * * @returns The total number of addresses in the CIDR range. */ addressCount(): number; /** * Generates all IPv4 addresses within the CIDR range. * * @example * ```ts * import { ipv4 } from 'cidr-block'; * * const cidr = ipv4.cidr("192.168.1.0/30"); * for (const addr of cidr.addresses()) { * console.log(addr.toString()); * } * // Output: "192.168.1.0", "192.168.1.1", "192.168.1.2", "192.168.1.3" * ``` * * @returns A generator that yields each IPv4 address in the CIDR range. */ addresses(): Generator<Ipv4Address>; /** * Checks if this CIDR is equal to another CIDR. * * @example * ```ts * import { ipv4 } from 'cidr-block'; * * const cidr = ipv4.cidr("192.168.0.0/24"); * cidr.equals("192.168.0.0/24"); // true * cidr.equals({ address: "192.168.0.0", range: 24 }); // true * cidr.equals("10.0.0.0/8"); // false * ``` * * @param other - The other IPv4 CIDR to compare with. * @returns True if both CIDRs have the same base address and range; otherwise, false. */ equals(other: Ipv4Cidr | Ipv4CidrLiteral): boolean; /** * Checks if there is a next sequential CIDR. * * @example * ```ts * import { ipv4 } from 'cidr-block'; * * ipv4.cidr("192.168.0.0/24").hasNextCIDR(); // true * ipv4.cidr("255.255.255.0/24").hasNextCIDR(); // false * ``` * * @returns true if there is a next CIDR. */ hasNextCIDR(): boolean; /** * Gets the next sequential CIDR. * * @example * ```ts * import { ipv4 } from 'cidr-block'; * * const cidr = ipv4.cidr("192.168.0.0/24"); * cidr.nextCIDR()?.toString(); // "192.168.1.0/24" * * const lastCidr = ipv4.cidr("255.255.255.0/24"); * lastCidr.nextCIDR(); // undefined * ``` * * @returns The next CIDR, or undefined if there is no next CIDR. */ nextCIDR(): Ipv4Cidr | undefined; /** * Checks if there is a previous sequential CIDR. * * @example * ```ts * import { ipv4 } from 'cidr-block'; * * ipv4.cidr("192.168.1.0/24").hasPreviousCIDR(); // true * ipv4.cidr("0.0.0.0/24").hasPreviousCIDR(); // false * ``` * * @returns true if there is a previous CIDR. */ hasPreviousCIDR(): boolean; /** * Gets the previous sequential CIDR. * * @example * ```ts * import { ipv4 } from 'cidr-block'; * * const cidr = ipv4.cidr("192.168.1.0/24"); * cidr.previousCIDR()?.toString(); // "192.168.0.0/24" * * const firstCidr = ipv4.cidr("0.0.0.0/24"); * firstCidr.previousCIDR(); // undefined * ``` * * @returns The previous CIDR, or undefined if there is no previous CIDR. */ previousCIDR(): Ipv4Cidr | undefined; /** * Gets the first usable address in the CIDR range. * * @example * ```ts * import { ipv4 } from 'cidr-block'; * * const cidr = ipv4.cidr("192.168.0.0/24"); * cidr.getFirstUsableAddress()?.toString(); // "192.168.0.1" * * const hostCidr = ipv4.cidr("192.168.1.1/32"); * hostCidr.getFirstUsableAddress(); // undefined (no usable addresses in /32) * ``` * * @returns The first usable IPv4 address, or undefined if the range is /32. */ getFirstUsableAddress(): Ipv4Address | undefined; /** * Gets the last usable address in the CIDR range. * * @example * ```ts * import { ipv4 } from 'cidr-block'; * * const cidr = ipv4.cidr("192.168.0.0/24"); * cidr.getLastUsableAddress()?.toString(); // "192.168.0.254" * * const hostCidr = ipv4.cidr("192.168.1.1/32"); * hostCidr.getLastUsableAddress(); // undefined (no usable addresses in /32) * ``` * * @returns The last usable IPv4 address, or undefined if the range is /32. */ getLastUsableAddress(): Ipv4Address | undefined; /** * Checks if the given IPv4 address is within the CIDR range. * * @example * ```ts * import { ipv4 } from 'cidr-block'; * * const cidr = ipv4.cidr("192.168.0.0/24"); * cidr.includes(ipv4.address("192.168.0.100")); // true * cidr.includes(ipv4.address("192.168.1.1")); // false * ``` * * @param ip - The IPv4 address to check. * @returns True if the address is within the CIDR range; otherwise, false. */ includes(ip: Ipv4Address): boolean; /** * Checks if this CIDR overlaps with another CIDR. * * @example * ```ts * import { ipv4 } from 'cidr-block'; * * const cidr = ipv4.cidr("192.168.0.0/24"); * cidr.overlaps("192.168.0.0/25"); // true (subset) * cidr.overlaps("192.168.0.128/25"); // true (partial overlap) * cidr.overlaps("10.0.0.0/8"); // false (no overlap) * ``` * * @param other - The other IPv4 CIDR to check for overlap. * @returns True if the CIDRs overlap; otherwise, false. */ overlaps(other: Ipv4Cidr | Ipv4CidrLiteral): boolean; /** * Splits the CIDR into smaller subranges of the specified new range. * * @example * ```ts * import { ipv4 } from 'cidr-block'; * * const cidr = ipv4.cidr("192.168.0.0/24"); * const subnets = cidr.subnet(26); * subnets.map(s => s.toString()); * // ["192.168.0.0/26", "192.168.0.64/26", "192.168.0.128/26", "192.168.0.192/26"] * ``` * * @param newRange - The new CIDR range for the subnets. * @returns An array of Ipv4Cidr instances representing the subnets. * @throws InvalidIpv4CidrRangeError if the new range is less than the current range or greater than the maximum range. */ subnet(newRange: number): Ipv4Cidr[]; /** * Splits the CIDR into sequential subranges with the specified CIDR ranges. * Each range in the input array creates a subrange starting where the previous one ended. * * @example * ```ts * import { ipv4 } from 'cidr-block'; * * const cidr = ipv4.cidr("10.0.0.0/16"); * const subnets = cidr.subnetBy([24, 20, 24]); * subnets.map(s => s.toString()); * // ["10.0.0.0/24", "10.0.1.0/20", "10.0.17.0/24"] * ``` * * @param ranges - An array of CIDR range values (e.g., [24, 20, 24]). * @returns An array of Ipv4Cidr instances representing the subnets. * @throws InvalidIpv4CidrRangeError if any range is less than the current range or greater than 32. */ subnetBy(ranges: number[]): Ipv4Cidr[]; } declare class InvalidIpv4AddressError extends Error { name: string; constructor(invalidAddress: unknown); } declare class InvalidIpv4CidrError extends Error { name: string; constructor(invalidCidr: unknown); } declare class InvalidIpv4CidrRangeError extends Error { name: string; } declare namespace ipv4 { /** * The maximum possible value for an IPv4 address. */ const MAX_SIZE = 4294967295; /** * The minimum possible value for an IPv4 address. */ const MIN_SIZE = 0; /** * The maximum CIDR range for IPv4 addresses. */ const MAX_RANGE = 32; /** * The minimum CIDR range for IPv4 addresses. */ const MIN_RANGE = 0; /** * Creates a new Ipv4Address instance from the given literal. * Valid formats include string, number, or octet array. * * @example Creating addresses * ```ts * import { ipv4 } from 'cidr-block'; * * const addr1 = ipv4.address("192.168.1.1"); * const addr2 = ipv4.address(3232235777); * const addr3 = ipv4.address([192, 168, 1, 1]); * ``` * * @example Converting between formats * ```ts * import { ipv4 } from 'cidr-block'; * * const addr = ipv4.address("192.168.1.1"); * addr.toString(); // "192.168.1.1" * addr.toNumber(); // 3232235777 * addr.octets(); // [192, 168, 1, 1] * addr.toBinaryString(); // "11000000.10101000.00000001.00000001" * ``` * * @example Comparing addresses * ```ts * import { ipv4 } from 'cidr-block'; * * const addr = ipv4.address("192.168.1.1"); * addr.equals("192.168.1.1"); // true * addr.isGreaterThan("192.168.1.0"); // true * addr.isLessThan("192.168.1.2"); // true * ``` * * @example Navigating sequential addresses * ```ts * import { ipv4 } from 'cidr-block'; * * const addr = ipv4.address("192.168.1.1"); * addr.nextAddress()?.toString(); // "192.168.1.2" * addr.previousAddress()?.toString(); // "192.168.1.0" * ``` * * @example Checking address types * ```ts * import { ipv4 } from 'cidr-block'; * * ipv4.address("127.0.0.1").isLoopbackAddress(); // true * ipv4.address("10.0.0.1").isPrivateAddress(); // true * ipv4.address("169.254.1.1").isLocalLinkAddress(); // true * ipv4.address("224.0.0.1").isMulticastAddress(); // true * ``` * * @param ip - The IPv4 address in string, number, or octet array format. * @returns A new Ipv4Address instance. * @throws {InvalidIpv4AddressError} If the input is not a valid IPv4 address. */ function address(ip: Ipv4AddressLiteral): Ipv4Address; /** * Creates a new Ipv4Cidr instance from the given literal. * Valid formats include string, object, or tuple. * * @example Creating CIDR blocks * ```ts * import { ipv4 } from 'cidr-block'; * * const cidr1 = ipv4.cidr("192.168.0.0/24"); * const cidr2 = ipv4.cidr({ address: "10.0.0.0", range: 8 }); * const cidr3 = ipv4.cidr([[172, 16, 0, 0], 12]); * ``` * * @example Getting CIDR properties * ```ts * import { ipv4 } from 'cidr-block'; * * const cidr = ipv4.cidr("192.168.0.0/24"); * cidr.baseAddress().toString(); // "192.168.0.0" * cidr.range(); // 24 * cidr.netmask().toString(); // "255.255.255.0" * cidr.addressCount(); // 256 * ``` * * @example Working with usable addresses * ```ts * import { ipv4 } from 'cidr-block'; * * const cidr = ipv4.cidr("192.168.0.0/24"); * cidr.getFirstUsableAddress()?.toString(); // "192.168.0.1" * cidr.getLastUsableAddress()?.toString(); // "192.168.0.254" * ``` * * @example Iterating over addresses * ```ts * import { ipv4 } from 'cidr-block'; * * const cidr = ipv4.cidr("192.168.1.0/30"); * for (const addr of cidr.addresses()) { * console.log(addr.toString()); * } * // "192.168.1.0", "192.168.1.1", "192.168.1.2", "192.168.1.3" * ``` * * @example Checking containment and overlap * ```ts * import { ipv4 } from 'cidr-block'; * * const cidr = ipv4.cidr("192.168.0.0/24"); * cidr.includes(ipv4.address("192.168.0.100")); // true * cidr.includes(ipv4.address("192.168.1.1")); // false * cidr.overlaps("192.168.0.0/25"); // true * cidr.overlaps("10.0.0.0/8"); // false * ``` * * @example Splitting into subranges * ```ts * import { ipv4 } from 'cidr-block'; * * const cidr = ipv4.cidr("192.168.0.0/24"); * * // Split into equal /26 subnets * cidr.subnet(26).map(s => s.toString()); * // ["192.168.0.0/26", "192.168.0.64/26", "192.168.0.128/26", "192.168.0.192/26"] * * // Split by specific ranges * cidr.subnetBy([25, 26, 27, 27]).map(s => s.toString()); * // ["192.168.0.0/25", "192.168.0.128/26", "192.168.0.192/27", "192.168.0.224/27"] * ``` * * @example Navigating sequential CIDRs * ```ts * import { ipv4 } from 'cidr-block'; * * const cidr = ipv4.cidr("192.168.0.0/24"); * cidr.nextCIDR()?.toString(); // "192.168.1.0/24" * cidr.previousCIDR()?.toString(); // "192.167.255.0/24" * ``` * * @param cidr - The IPv4 CIDR in string, object, or tuple format. * @returns A new Ipv4Cidr instance. * @throws {InvalidIpv4CidrError} If the input is not a valid IPv4 CIDR. */ function cidr(cidr: Ipv4CidrLiteral): Ipv4Cidr; /** * Validates whether the given input is a valid IPv4 address. * * @example * ```ts * import { ipv4 } from 'cidr-block'; * * ipv4.isValidAddress("192.168.1.1"); // true * ipv4.isValidAddress("256.0.0.1"); // false (octet out of range) * ipv4.isValidAddress(3232235777); // true * ipv4.isValidAddress([192, 168, 1, 1]); // true * ipv4.isValidAddress("invalid"); // false * ``` * * @param ip - The IPv4 address to validate, which can be in string, number, or octet array format. * @returns True if the input is a valid IPv4 address; otherwise, false. */ function isValidAddress(ip: Ipv4AddressLiteral): boolean; /** * Validates whether the given input is a valid IPv4 CIDR. * * @example * ```ts * import { ipv4 } from 'cidr-block'; * * ipv4.isValidCIDR("192.168.0.0/24"); // true * ipv4.isValidCIDR("192.168.0.0/33"); // false (range out of bounds) * ipv4.isValidCIDR({ address: "10.0.0.0", range: 8 }); // true * ipv4.isValidCIDR([[172, 16, 0, 0], 12]); // true * ipv4.isValidCIDR("invalid/24"); // false * ``` * * @param cidr - The IPv4 CIDR to validate, which can be in string, object, or tuple format. * @returns True if the input is a valid IPv4 CIDR; otherwise, false. */ function isValidCIDR(cidr: Ipv4CidrLiteral): boolean; /** * Parses the given IPv4 address into its octet representation. * * @example * ```ts * import { ipv4 } from 'cidr-block'; * * ipv4.parseOctets("192.168.1.1"); // [192, 168, 1, 1] * ipv4.parseOctets(3232235777); // [192, 168, 1, 1] * ipv4.parseOctets([10, 0, 0, 1]); // [10, 0, 0, 1] * ``` * * @param ip - The IPv4 address to parse, which can be in string, number, or octet array format. * @returns {Ipv4AddressOctets} An array of four numbers representing the octets of the IPv4 address. * @throws {InvalidIpv4AddressError} If the input is not a valid IPv4 address. */ function parseOctets(ip: Ipv4AddressLiteral): Ipv4AddressOctets; } /** * Represents an IPv6 address from a 'literal' value. * Examples: * - String: "2001:db8::1" * - BigInt: 42540766411282592856903984951653826561n * - Hextets Array: [0x2001, 0xdb8, 0, 0, 0, 0, 0, 1] */ type Ipv6AddressLiteral = string | bigint | number[]; /** * Represents an IPv6 address as an array of eight 16-bit hextets. */ type Ipv6AddressHextets = [number, number, number, number, number, number, number, number]; /** * Represents an IPv6 CIDR from a 'literal' value. * Examples: * - String: "2001:db8::/32" * - Object: { address: "2001:db8::", range: 32 } * - Tuple: ["2001:db8::", 32] */ type Ipv6CidrLiteral = string | { address: Ipv6AddressLiteral; range: number; } | [Ipv6AddressLiteral, number]; /** * Represents an IPv6 address with utility methods for manipulation and comparison. * * While you can instantiate this class directly, it is recommended to use the * {@link ipv6.address} shorthand method from the `ipv6` namespace instead. * * @example Creating addresses (prefer the shorthand) * ```ts * import { ipv6 } from 'cidr-block'; * * // Recommended: use the ipv6 namespace shorthand * const addr1 = ipv6.address("2001:db8::1"); * const addr2 = ipv6.address(42540766411282592856903984951653826561n); * const addr3 = ipv6.address([0x2001, 0xdb8, 0, 0, 0, 0, 0, 1]); * ``` * * @example Converting between formats * ```ts * import { ipv6 } from 'cidr-block'; * * const addr = ipv6.address("2001:db8::1"); * addr.toString(); // "2001:db8::1" (compressed) * addr.toFullString(); // "2001:0db8:0000:0000:0000:0000:0000:0001" * addr.toBigInt(); // 42540766411282592856903984951653826561n * addr.hextets(); // [0x2001, 0xdb8, 0, 0, 0, 0, 0, 1] * ``` * * @example Comparing addresses * ```ts * import { ipv6 } from 'cidr-block'; * * const addr = ipv6.address("2001:db8::1"); * addr.equals("2001:db8::1"); // true * addr.isGreaterThan("2001:db8::0"); // true * addr.isLessThan("2001:db8::2"); // true * ``` * * @example Navigating sequential addresses * ```ts * import { ipv6 } from 'cidr-block'; * * const addr = ipv6.address("2001:db8::1"); * addr.nextAddress()?.toString(); // "2001:db8::2" * addr.previousAddress()?.toString(); // "2001:db8::" * ``` * * @example Checking address types * ```ts * import { ipv6 } from 'cidr-block'; * * ipv6.address("::1").isLoopbackAddress(); // true * ipv6.address("fc00::1").isUniqueLocalAddress(); // true * ipv6.address("fe80::1").isLinkLocalAddress(); // true * ipv6.address("ff02::1").isMulticastAddress(); // true * ``` */ declare class Ipv6Address { #private; constructor(ip: Ipv6AddressLiteral); /** * Gets the hextets of the IPv6 address. * * @example * ```ts * import { ipv6 } from 'cidr-block'; * * const addr = ipv6.address("2001:db8::1"); * addr.hextets(); // [0x2001, 0xdb8, 0, 0, 0, 0, 0, 1] * ``` * * @returns An array of eight numbers representing the hextets of the IPv6 address. */ hextets(): Ipv6AddressHextets; /** * Returns the full (expanded) string representation of the IPv6 address. * * @example * ```ts * import { ipv6 } from 'cidr-block'; * * const addr = ipv6.address("2001:db8::1"); * addr.toFullString(); // "2001:0db8:0000:0000:0000:0000:0000:0001" * ``` * * @returns The IPv6 address as a full string with all hextets expanded. */ toFullString(): string; /** * Returns the compressed string representation of the IPv6 address. * Uses :: notation for the longest run of consecutive zero hextets. * * @example * ```ts * import { ipv6 } from 'cidr-block'; * * const addr = ipv6.address("2001:0db8:0000:0000:0000:0000:0000:0001"); * addr.toString(); // "2001:db8::1" * ``` * * @returns The IPv6 address as a compressed string. */ toString(): string; /** * Returns the binary string representation of the IPv6 address. * * @example * ```ts * import { ipv6 } from 'cidr-block'; * * const addr = ipv6.address("::1"); * addr.toBinaryString(); * // "0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000000:0000000000000001" * ``` * * @returns The IPv6 address as a binary string with colons separating hextets. */ toBinaryString(): string; /** * Converts the IPv6 address to its BigInt representation. * * @example * ```ts * import { ipv6 } from 'cidr-block'; * * const addr = ipv6.address("2001:db8::1"); * addr.toBigInt(); // 42540766411282592856903984951653826561n * ``` * * @returns The IPv6 address as a BigInt. */ toBigInt(): bigint; /** * Checks if there is a next sequential IPv6 address. * This would only return false if the current address is the maximum possible value. * * @example * ```ts * import { ipv6 } from 'cidr-block'; * * ipv6.address("2001:db8::1").hasNextAddress(); // true * ipv6.address("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff").hasNextAddress(); // false * ``` * * @returns true if there is a next IPv6 address. */ hasNextAddress(): boolean; /** * Gets the next sequential IPv6 address. * * @example * ```ts * import { ipv6 } from 'cidr-block'; * * const addr = ipv6.address("2001:db8::1"); * addr.nextAddress()?.toString(); // "2001:db8::2" * * const maxAddr = ipv6.address("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); * maxAddr.nextAddress(); // undefined * ``` * * @returns The next IPv6 address, or undefined if the current address is the maximum possible value. */ nextAddress(): Ipv6Address | undefined; /** * Checks if there is a previous sequential IPv6 address. * This would only return false if the current address is the minimum possible value (::). * * @example * ```ts * import { ipv6 } from 'cidr-block'; * * ipv6.address("2001:db8::1").hasPreviousAddress(); // true * ipv6.address("::").hasPreviousAddress(); // false * ``` * * @returns true if there is a previous IPv6 address. */ hasPreviousAddress(): boolean; /** * Gets the previous sequential IPv6 address. * * @example * ```ts * import { ipv6 } from 'cidr-block'; * * const addr = ipv6.address("2001:db8::1"); * addr.previousAddress()?.toString(); // "2001:db8::" * * const minAddr = ipv6.address("::"); * minAddr.previousAddress(); // undefined * ``` * * @returns The previous IPv6 address, or undefined if the current address is the minimum possible value. */ previousAddress(): Ipv6Address | undefined; /** * Compares two IPv6 addresses for equality. * * @example * ```ts * import { ipv6 } from 'cidr-block'; * * const addr = ipv6.address("2001:db8::1"); * addr.equals("2001:db8::1"); // true * addr.equals("2001:0db8:0:0:0:0:0:1"); // true * addr.equals(ipv6.address("2001:db8::2")); // false * ``` * * @param otherAddress - The other IPv6 address to compare with, which can be an Ipv6Address instance or literal value. * @returns true if both IPv6 addresses are equal */ equals(otherAddress: Ipv6Address | Ipv6AddressLiteral): boolean; /** * Compares if this IPv6 address is greater than another IPv6 address. * * @example * ```ts * import { ipv6 } from 'cidr-block'; * * const addr = ipv6.address("2001:db8::1"); * addr.isGreaterThan("2001:db8::"); // true * addr.isGreaterThan("2001:db8::1"); // false * addr.isGreaterThan("2001:db8::2"); // false * ``` * * @param otherAddress - The other IPv6 address to compare with, which can be an Ipv6Address instance or literal value. * @returns true if this IPv6 address is greater than the other IPv6 address */ isGreaterThan(otherAddress: Ipv6Address | Ipv6AddressLiteral): boolean; /** * Compares if this IPv6 address is greater than or equal to another IPv6 address. * * @example * ```ts * import { ipv6 } from 'cidr-block'; * * const addr = ipv6.address("2001:db8::1"); * addr.isGreaterThanOrEqual("2001:db8::"); // true * addr.isGreaterThanOrEqual("2001:db8::1"); // true * addr.isGreaterThanOrEqual("2001:db8::2"); // false * ``` * * @param otherAddress - The other IPv6 address to compare with, which can be an Ipv6Address instance or literal value. * @returns true if this IPv6 address is greater than or equal to the other IPv6 address */ isGreaterThanOrEqual(otherAddress: Ipv6Address | Ipv6AddressLiteral): boolean; /** * Compares if this IPv6 address is less than another IPv6 address. * * @example * ```ts * import { ipv6 } from 'cidr-block'; * * const addr = ipv6.address("2001:db8::1"); * addr.isLessThan("2001:db8::2"); // true * addr.isLessThan("2001:db8::1"); // false * addr.isLessThan("2001:db8::"); // false * ``` * * @param otherAddress - The other IPv6 address to compare with, which can be an Ipv6Address instance or literal value. * @returns true if this IPv6 address is less than the other IPv6 address */ isLessThan(otherAddress: Ipv6Address | Ipv6AddressLiteral): boolean; /** * Compares if this IPv6 address is less than or equal to another IPv6 address. * * @example * ```ts * import { ipv6 } from 'cidr-block'; * * const addr = ipv6.address("2001:db8::1"); * addr.isLessThanOrEqual("2001:db8::2"); // true * addr.isLessThanOrEqual("2001:db8::1"); // true * addr.isLessThanOrEqual("2001:db8::"); // false * ``` * * @param otherAddress - The other IPv6 address to compare with, which can be an Ipv6Address instance or literal value. * @returns true if this IPv6 address is less than or equal to the other IPv6 address */ isLessThanOrEqual(otherAddress: Ipv6Address | Ipv6AddressLiteral): boolean; /** * Checks if the IPv6 address is the loopback address (::1) * * @example * ```ts * import { ipv6 } from 'cidr-block'; * * ipv6.address("::1").isLoopbackAddress(); // true * ipv6.address("2001:db8::1").isLoopbackAddress(); // false * ``` * * @returns true if the IPv6 address is the loopback address */ isLoopbackAddress(): boolean; /** * Checks if the IPv6 address is the unspecified address (::) * * @example * ```ts * import { ipv6 } from 'cidr-block'; * * ipv6.address("::").isUnspecifiedAddress(); // true * ipv6.address("2001:db8::1").isUnspecifiedAddress(); // false * ``` * * @returns true if the IPv6 address is the unspecified address */ isUnspecifiedAddress(): boolean; /** * Checks if the IPv6 address is a unique local address (fc00::/7). * These are the IPv6 equivalent of RFC 1918 private addresses. * * @example * ```ts * import { ipv6 } from 'cidr-block'; * * ipv6.address("fc00::1").isUniqueLocalAddress(); // true * ipv6.address("fd00::1").isUniqueLocalAddress(); // true * ipv6.address("2001:db8::1").isUniqueLocalAddress(); // false * ``` * * @returns true if the IPv6 address is a unique local address */ isUniqueLocalAddress(): boolean; /** * Checks if the IPv6 address is a link-local address (fe80::/10) * * @example * ```ts * import { ipv6 } from 'cidr-block'; * * ipv6.address("fe80::1").isLinkLocalAddress(); // true * ipv6.address("2001:db8::1").isLinkLocalAddress(); // false * ``` * * @returns true if the IPv6 address is a link-local address */ isLinkLocalAddress(): boolean; /** * Checks if the IPv6 address is a multicast address (ff00::/8) * * @example * ```ts * import { ipv6 } from 'cidr-block'; * * ipv6.address("ff02::1").isMulticastAddress(); // true * ipv6.address("2001:db8::1").isMulticastAddress(); // false * ``` * * @returns true if the IPv6 address is a multicast address */ isMulticastAddress(): boolean; /** * Checks if the IPv6 address is an IPv4-mapped IPv6 address (::ffff:0:0/96) * * @example * ```ts * import { ipv6 } from 'cidr-block'; * * ipv6.address("::ffff:192.168.1.1").isIPv4MappedAddress(); // true * ipv6.address("::ffff:c0a8:0101").isIPv4MappedAddress(); // true * ipv6.address("2001:db8::1").isIPv4MappedAddress(); // false * ``` * * @returns true if the IPv6 address is an IPv4-mapped address */ isIPv4MappedAddress(): boolean; /** * Checks if the IPv6 address is a documentation address (2001:db8::/32) * * @example * ```ts * import { ipv6 } from 'cidr-block'; * * ipv6.address("2001:db8::1").isDocumentationAddress(); // true * ipv6.address("2001:db8:1234::1").isDocumentationAddress(); // true * ipv6.address("2001:470::1").isDocumentationAddress(); // false * ``` * * @returns true if the IPv6 address is a documentation address */ isDocumentationAddress(): boolean; } /** * Represents an IPv6 CIDR block with utility methods for subnet operations. * * While you can instantiate this class directly, it is recommended to use the * {@link ipv6.cidr} shorthand method from the `ipv6` namespace instead. * * @example Creating CIDR blocks (prefer the shorthand) * ```ts * import { ipv6 } from 'cidr-block'; * * // Recommended: use the ipv6 namespace shorthand * const cidr1 = ipv6.cidr("2001:db8::/32"); * const cidr2 = ipv6.cidr({ address: "2001:db8::", range: 32 }); * const cidr3 = ipv6.cidr(["2001:db8::", 32]); * ``` * * @example Getting CIDR properties * ```ts * import { ipv6 } from 'cidr-block'; * * const cidr = ipv6.cidr("2001:db8::/32"); * cidr.baseAddress().toString(); // "2001:db8::" * cidr.range(); // 32 * cidr.netmask().toString(); // "ffff:ffff::" * cidr.network().toString(); // "2001:db8::" * cidr.addressCount(); // 79228162514264337593543950336n * ``` * * @example Working with usable addresses * ```ts * import { ipv6 } from 'cidr-block'; * * const cidr = ipv6.cidr("2001:db8::/126"); * cidr.getFirstUsableAddress()?.toString(); // "2001:db8::1" * cidr.getLastUsableAddress()?.toString(); // "2001:db8::2" * ``` * * @example Checking containment and overlap * ```ts * import { ipv6 } from 'cidr-block'; * * const cidr = ipv6.cidr("2001:db8::/32"); * cidr.includes(ipv6.address("2001:db8::1")); // true * cidr.includes(ipv6.address("2001:db9::1")); // false * cidr.overlaps("2001:db8::/48"); // true * cidr.overlaps("2001:db9::/32"); // false * ``` * * @example Splitting into subranges * ```ts * import { ipv6 } from 'cidr-block'; * * const cidr = ipv6.cidr("2001:db8::/32"); * * // Split into equal /48 subnets * cidr.subnet(34).map(s => s.toString()); * // ["2001:db8::/34", "2001:db8:4000::/34", "2001:db8:8000::/34", "2001:db8:c000::/34"] * ``` * * @example Navigating sequential CIDRs * ```ts * import { ipv6 } from 'cidr-block'; * * const cidr = ipv6.cidr("2001:db8::/32"); * cidr.nextCIDR()?.toString(); // "2001:db9::/32" * cidr.previousCIDR()?.toString(); // "2001:db7::/32" * ``` */ declare class Ipv6Cidr { #private; constructor(address: Ipv6CidrLiteral); /** * Gets the base IPv6 address of the CIDR. * * @example * ```ts * import { ipv6 } from 'cidr-block'; * * const cidr = ipv6.cidr("2001:db8::/32"); * cidr.baseAddress().toString(); // "2001:db8::" * ``` * * @returns The base IPv6 address. */ baseAddress(): Ipv6Address; /** * Gets the CIDR range. * * @example * ```ts * import { ipv6 } from 'cidr-block'; * * const cidr = ipv6.cidr("2001:db8::/32"); * cidr.range(); // 32 * ``` * * @returns The CIDR range as a number. */ range(): number; /** * Calculates the netmask for the CIDR range. * * @example * ```ts * import { ipv6 } from 'cidr-block'