UNPKG

tshttpurl

Version:

Http URL and IP address parser and normalizer

190 lines (189 loc) 7.66 kB
export declare class IPv6 { private static readonly MAX; private static readonly IPV4PREFIX; private static readonly IPV4MASK; private static readonly ZERO; private static readonly HEX4REGEX; private static readonly HEX2REGEX; private static readonly HEXREGEX; private static readonly DECREGEX; private static readonly SPACEREGEX; private static readonly COLONSREGEX; private static readonly COLONREGEX; private value; /** * * @param value The object from which to create the new instance. E.g. * ```ts * "192.168.0.1" * "::FFFF:192.168.0.1" * "fe80:0:0:0:202:b3ff:fe1e:8329" * 0xfe800000000000000202b3fffe1e8329n * 0xFFFF08080808n // "8.8.8.8" * ``` */ constructor(value: IPv6 | bigint | string); /** * Parses a hexidecimal string. * @param value String made up only of hexidecimal digits * @returns number represented by `value` * @throws RangeError if the whole string isn't parsed. */ private static parseHex; /** * Parses a decimal string. * @param value String made up only of decimal digits * @returns number represented by `value` * @throws RangeError if the whole string isn't parsed. */ private static parseDecimal; /** * Gets the number of times the specified RegExp matches a specified string. * * @param subStr Regular Expression to search for. Should have * the 'g' flag specified so multiple matches are made. * @param superStr The String in which to search. * @returns the number of times `subStr` appears in `superStr` */ private static matchCount; /** * Casts the value to an IPv6 instance. * @param value Value to cast. * @returns the supplied value if it is already an IPv6, otherwise a new * IPv6 constructed from it. * @throws RangeError if a negative or >128 bit bigint is passed, or an * Error if string that can not be parsed is passed. */ static from(value: IPv6 | bigint | string): IPv6; /** * Casts the given object to a bigint. * @param value bigint, IPv6, or IPv6 formatted string to cast to a bigint. * @returns bigint */ private static toBigInt; /** * Helper function that parses an IPv6 string to a bigint * @param value String representing an IPv6 Address * @throws Error if the string can't be parsed * @returns bigint */ private static fromString; /** * Determines if the object represents an IP v 4 address or not. * ```ts * IPv6.isIPv4("10.0.0.1") // true * IPv6.isIPv4("::ffff:a00:1") // true * IPv6.isIPv4(" 2001:db8:a::123") // false * ``` * @param ipv6 The object to test. */ static isIPv4(ipv6: IPv6 | bigint | string): boolean; /** * Determines if the object represents an IP v 4 address or not. * ```ts * (new IPv6("10.0.0.1") ).isIPv4(); // true * (new IPv6("::ffff:a00:1") ).isIPv4(); // true * (new IPv6(" 2001:db8:a::123")).isIPv4(); // false * ``` * @returns true if this is an IP v 4 address, false otherwise. */ isIPv4(): boolean; /** * Gets the [canonical](https://en.wikipedia.org/wiki/IPv6_address#Recommended_representation_as_text) * IP v 6 representation of an IP Address. I.e. Lower case, without leading zeros and the left-most * largest set of consecutive zero parts collapsed to a double colon: * ```ts * "::ffff:192.168.0.1" * "2001:db8::1:0:0:1" * ``` * @param ipv6 The object representing the IPv6 address. * @param dotDec For [IPv4-mapped](https://en.wikipedia.org/wiki/IPv6#IPv4-mapped_IPv6_addresses), * indicates whether to use the 'dot-decimal' notation for the IPv4 part. * Defaults to true. For example: * ```ts * IPv6.toIPv6String("10.0.0.1") // "::ffff:10.0.0.1" * IPv6.toIPv6String("10.0.0.1", false) // "::ffff:a00:1" * ``` * @returns The compact IP v 6 representation of this IP Address. */ static toIPv6String(ipv6: IPv6 | bigint | string, dotDec?: boolean): string; /** * Gets the [canonical](https://en.wikipedia.org/wiki/IPv6_address#Recommended_representation_as_text) * IP v 6 representation of an IP Address. I.e. Lower case, without leading zeros and the left-most * largest set of consecutive zero parts collapsed to a double colon: * ```ts * "::ffff:192.168.0.1" * "2001:db8::1:0:0:1" * ``` * @param dotDec For [IPv4-mapped](https://en.wikipedia.org/wiki/IPv6#IPv4-mapped_IPv6_addresses), * indicates whether to use the 'dot-decimal' notation for the IPv4 part. * Defaults to true. For example: * ```ts * (new IPv6("10.0.0.1")).toIPv6String() // "::ffff:10.0.0.1" * (new IPv6("10.0.0.1")).toIPv6String(false) // "::ffff:a00:1" * ``` * @returns The compact IP v 6 representation of this IP Address. */ toIPv6String(dotDec?: boolean): string; /** * Gets the IP v 4 "dot-decimal" representation of an IP address, if it * represents an IP v 4 address. E.g. * ```ts * "192.168.0.1" * ``` * @returns The "dot-decimal" representation of this IP Address. * * @throws an Error if ipv6 isn't an IP v 4 address. (if isIPv4() === false) */ static toIPv4String(ipv6: IPv6 | bigint | string): string; /** * Gets the IP v 4 "dot-decimal" representation of this IP address, if this * address represents an IP v 4 address. E.g. * ```ts * "192.168.0.1" * ``` * * @returns The "dot-decimal" representation of this IP Address. * * @throws an Error if this isn't a IP v 4 address. (if isIPv4() === false) */ toIPv4String(): string; /**` * @returns returns the IP v 4 "dot-decimal" representation if * isIPv4() == true, otherwise the IP v 6 formatted version. */ toString(): string; /** * Compares this IP Address to another for order. * @param first The first address to compare * @param second The second address to compare * @returns A negative integer, zero, or a positive integer as first * is less than, equal to, or greater than second. * @throws an Error if either parameter is a string that can not be parsed. */ static compare(first: IPv6 | bigint | string, second: IPv6 | bigint | string): number; /** * Compares this IP Address to another for order. * @param other The address to use as a comparison. * * @returns A negative integer, zero, or a positive integer as this * is less than, equal to, or greater than other. * @throws an Error if other is a string that can not be parsed. */ compare(other: IPv6 | bigint | string): number; /** * Determines whether the arguments represent the same address * @param first The first address to use as a comparison. * @param second The second address to use as a comparison. * @returns boolean */ static equals(first: IPv6 | bigint | string | null, second: IPv6 | bigint | string | null): boolean; /** * Determines whether the passed in value represents the same value as * this instance * @param other The address to use as a comparison. * null or malformed addresses result in false being returned. * @returns boolean */ equals(other: IPv6 | bigint | string | null): boolean; }