Small TypeScript module to serialize and deserialize IPv4 and IPv6 addresses.
Features:
- Parsing IPv4 and IPv6 Addresses with or without CIDR
- Serializing IPv4 and IPv6 (compressed and uncompressed)
- Converting address to and from bigints to perform bit-operations
- Checking whether an address or an subnet is covered/included in another one
let address = V4Address.fromString('1.2.3.4');
If an invalid address is given, undefined
is returned.
let address = V6Address.fromString('::1');
If an invalid address is given, undefined
is returned.
let net = V4Address.fromString('10.0.0.0/8');
let address = V4Address.fromString('5.0.0.0');
net.covers(address); // => false
The covers
method also accepts a string.
If the given string is not a valid address, false
is returned.
let net = V4Address.fromString('10.0.0.0/8');
net.covers('5.0.0.0'); // => false
net.covers('10.2.3.4'); // => true
net.covers('10.0.0.300'); // => false
function fromString(address: string, addressFamily?: number | undefined): V4Address | V6Address | undefined
Parses string to an IPv4 or IPv6 address. Returnsundefined
for invalid data or unknown address family.- address: Address string to be parsed
- addressFamily?: Optional parameter. Possible values:
4
,6
,0
,undefined
. When0
orundefined
, the address is first tried to be parsed as V4Address and then as V6Address Default:undefined
function fromBigInt(address: bigint, addressFamily: number, subnetSize?: number): V4Address | V6Address | undefined
Reads an IPv4 address or net from a bigint. Returnsundefined
for invalid data.- address: bigint address to be read
- addressFamily: Possible values:
4
,6
. Unlike in thefromString
method, this addressFamily parameter is not optional as it's way too risky to guess the address type of a bigint - subnetSize?: Optional parameter. When not set, the default subnet size of the address family is used
Base class representing an address or net.
addressType: int
Address type/version (4 or 6)size: int
Address size in bits (32 or 128)bytes: int[]
Address data as 8-bit unsignedsubnetSize: int
Size of the subnet in bits
covers(Address | string): boolean
Checks whether the current net covers/includes the given address or nettoString({ appendCIDR = undefined, compress = true }): string
Formats the address to a string- appendCIDR: When undefined, the CIDR is appended only when the address object defines a subnet
- compress: When true, the address is compressed as much as possible. Takes effect only on V6Address objects
toBigInt(): bigint
Transforms address to a bigintclone(): Address
Creates clone of this address object
IPv4 address or net
addressType: int
=>4
size: int
=>32
bytes: int[4]
Address data as 8-bit unsigned intssubnetSize: int
Size of the subnet mask in bits
covers(Address | string): boolean
Checks whether the current net covers/includes the given addresstoString({ appendCIDR: boolean = undefined }): string
Formats the address to a string- appendCIDR: When appendCIDR is undefined, the CIDR is appended only when the address object defines a subnet
clone(): V4Address
Creates a clone of this address objectstatic fromString(string): V4Address | undefined
Parses an IPv4 address or net. Returnsundefined
for invalid data.static fromBigInt(address: bigint, subnetSize?: number): V4Address | undefined
Reads an IPv4 address or net from a bigint. Returnsundefined
for invalid data.- address: bigint address to be read
- subnetSize?: Optional parameter. When not set, the default subnet size
32
is used
IPv6 address or net
addressType: int
=>6
size: int
=>128
bytes: int[16]
Address data as 8-bit unsigned intsblocks: int[8]
Address data as 16-bit unsigned intssubnetSize: int
Size of the subnet mask in bits
covers(Address | string): boolean
Checks whether the current net covers/includes the given addresstoString({ appendCIDR = undefined, compress = true }): string
Formats the address to a string- appendCIDR: When undefined, the CIDR is appended only when the address object defines a subnet
- compress: When true, the address is compressed as much as possible
clone(): V6Address
Creates clone of this address objectstatic fromString(string): V6Address | undefined
Parses an IPv6 address or net. Returnsundefined
for invalid data.static fromBigInt(address: bigint, subnetSize?: number): V6Address | undefined
Reads an IPv6 address or net from a bigint. Returnsundefined
for invalid data.- address: bigint address to be read
- subnetSize?: Optional parameter. When not set, the default subnet size
128
is used
let address = V4Address.fromString('1.2.3.4');
/* address => V4Address
- addressType: 4
- size: 32
- bytes: [1, 2, 3, 4]
- subnetSize: 32
*/
let address = V4Address.fromString('5.6.7.8/12');
/* address => V4Address
- addressType: 4
- size: 32
- bytes: [5, 6, 7, 8]
- subnetSize: 12
*/
let address = V4Address.fromString('1.2.288.4'); //Invalid third value
/* address => undefined */
let address = V4Address.fromString('1.2.3.4/42'); //Invalid CIDR value
/* address => undefined */
let address = V6Address.fromString('::1');
/* address => V6Address
- addressType: 6
- size: 128
- bytes: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
- subnetSize: 128
*/
address.toString({ uncompressed: true }); // => 0000:0000:0000:0000:0000:0000:0000:0001
address.toString(); // => ::1
address.toString({ appendCIDR: true }); // => ::1/128
let address = V6Address.fromString('::1:0:0:0:0:0/100');
/* address => V6Address
- addressType: 6
- size: 128
- bytes: [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
- subnetSize: 128
*/
address.toString({ uncompressed: true }); // => 0000:0000:0001:0000:0000:0000:0000:0000/100
address.toString(); // => 0:0:1::/100
address.toString({ appendCIDR: false }); // => 0:0:1::