A library for parsing IPv4 and IPv6 IP addresses in node and the browser.
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Conversion between different address representations including hexadecimal, binary, decimal, canonical, and reverse DNS formats for both IPv4 and IPv6 addresses.
Convert IPv4 addresses between various numeric and string representations.
/**
* Convert IPv4 address to hexadecimal string with colon separators
* Available on Address4 instances
* @returns Colon-separated hex string (e.g., "c0:a8:01:01")
*/
toHex(): string;
/**
* Convert IPv4 address to array of integers
* Available on Address4 instances
* @returns Array of 4 integers (0-255) representing each octet
*/
toArray(): number[];
/**
* Convert IPv4 address to BigInt representation
* Available on Address4 instances
* @returns BigInt value of the 32-bit address
*/
bigInt(): bigint;
/**
* Convert IPv4 address to zero-padded binary string
* Available on Address4 instances
* @returns 32-character binary string
*/
binaryZeroPad(): string;
/**
* Convert IPv4 address to IPv6 group format for embedding
* Available on Address4 instances
* @returns Hex string formatted for IPv6 embedding
*/
toGroup6(): string;
/**
* Generate reverse DNS (in-addr.arpa) representation
* Available on Address4 instances
* @param options - Options for reverse form generation
* @returns Reverse DNS string
*/
reverseForm(options?: ReverseFormOptions): string;IPv4 Conversion Examples:
import { Address4 } from "ip-address";
const addr = new Address4("192.168.1.100");
// Hexadecimal conversion
console.log(addr.toHex()); // "c0:a8:01:64"
// Array conversion
console.log(addr.toArray()); // [192, 168, 1, 100]
// BigInt conversion
console.log(addr.bigInt()); // 3232235876n
// Binary conversion
console.log(addr.binaryZeroPad()); // "11000000101010000000000101100100"
// IPv6 group format
console.log(addr.toGroup6()); // "c0a8:0164"
// Reverse DNS
console.log(addr.reverseForm()); // "100.1.168.192.in-addr.arpa."
console.log(addr.reverseForm({ omitSuffix: true })); // "100.1.168.192"Convert IPv6 addresses between various representations including compressed, canonical, and specialized formats.
/**
* Convert IPv6 address to correct compressed form
* Available on Address6 instances
* @returns Properly compressed IPv6 string (e.g., "2001:db8::1")
*/
correctForm(): string;
/**
* Convert IPv6 address to canonical (uncompressed) form
* Available on Address6 instances
* @returns Full 32-character hex with colons (e.g., "2001:0db8:0000:0000:0000:0000:0000:0001")
*/
canonicalForm(): string;
/**
* Convert IPv6 address to decimal representation
* Available on Address6 instances
* @returns Colon-separated decimal groups
*/
decimal(): string;
/**
* Convert IPv6 address to BigInt representation
* Available on Address6 instances
* @returns 128-bit BigInt value
*/
bigInt(): bigint;
/**
* Convert IPv6 address to zero-padded binary string
* Available on Address6 instances
* @returns 128-character binary string
*/
binaryZeroPad(): string;
/**
* Convert IPv6 address to byte array
* Available on Address6 instances
* @returns Array of bytes representing the address
*/
toByteArray(): number[];
/**
* Convert IPv6 address to unsigned byte array
* Available on Address6 instances
* @returns Array of unsigned bytes (0-255)
*/
toUnsignedByteArray(): number[];
/**
* Generate reverse DNS (ip6.arpa) representation
* Available on Address6 instances
* @param options - Options for reverse form generation
* @returns Reverse DNS string
*/
reverseForm(options?: ReverseFormOptions): string;
/**
* Generate Microsoft UNC literal representation
* Available on Address6 instances
* @returns Microsoft-compatible IPv6 literal format
*/
microsoftTranscription(): string;IPv6 Conversion Examples:
import { Address6 } from "ip-address";
const addr = new Address6("2001:0db8:0000:0000:0000:0000:0000:0001");
// Form conversions
console.log(addr.correctForm()); // "2001:db8::1" (compressed)
console.log(addr.canonicalForm()); // "2001:0db8:0000:0000:0000:0000:0000:0001" (full)
console.log(addr.decimal()); // "08193:03512:00000:00000:00000:00000:00000:00001"
// Numeric conversions
console.log(addr.bigInt()); // 42540766411282592856903984951653826561n
console.log(addr.binaryZeroPad()); // 128-character binary string
// Byte array conversions
console.log(addr.toByteArray()); // [32, 1, 13, 184, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
console.log(addr.toUnsignedByteArray()); // Same as toByteArray for IPv6
// Reverse DNS
console.log(addr.reverseForm());
// "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa."
console.log(addr.reverseForm({ omitSuffix: true }));
// "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2"
// Microsoft UNC format
console.log(addr.microsoftTranscription()); // "2001-db8-0-0-0-0-0-1.ipv6-literal.net"Special conversions for IPv6 addresses containing IPv4 components.
/**
* Extract IPv4 address from the last 32 bits
* Available on Address6 instances
* @returns Address4 instance from last 32 bits
*/
to4(): Address4;
/**
* Convert to IPv4-in-IPv6 representation
* Available on Address6 instances
* @returns String with IPv4 notation in the last group
*/
to4in6(): string;
/**
* Convert to 6to4 address format if applicable
* Available on Address6 instances
* @returns Address6 instance in 6to4 format, or null if not v4-in-v6
*/
to6to4(): Address6 | null;IPv4-in-IPv6 Examples:
// IPv4-mapped IPv6 address
const mapped = new Address6("::ffff:192.168.1.100");
console.log(mapped.to4().correctForm()); // "192.168.1.100"
console.log(mapped.to4in6()); // "::ffff:192.168.1.100"
// Any IPv6 can extract last 32 bits as IPv4
const regular = new Address6("2001:db8:1234:5678::abcd");
console.log(regular.to4().correctForm()); // "0.0.171.205" (from ::abcd)
// 6to4 conversion (only works for v4-in-v6 addresses)
const v4inv6 = new Address6("::ffff:203.0.113.1");
const sixToFour = v4inv6.to6to4();
if (sixToFour) {
console.log(sixToFour.correctForm()); // "2002:cb00:7101::/16"
}
// Regular addresses return null for 6to4
const regularAddr = new Address6("2001:db8::1");
console.log(regularAddr.to6to4()); // nullCreate addresses from various input formats using static methods.
// IPv4 static creation methods
static fromHex(hex: string): Address4;
static fromInteger(integer: number): Address4;
static fromBigInt(bigInt: bigint): Address4;
static fromArpa(arpaFormAddress: string): Address4;
// IPv6 static creation methods
static fromBigInt(bigInt: bigint): Address6;
static fromByteArray(bytes: Array<any>): Address6;
static fromUnsignedByteArray(bytes: Array<any>): Address6;
static fromArpa(arpaFormAddress: string): Address6;
static fromAddress4(address: string): Address6;Cross-Format Creation Examples:
// IPv4 from various formats
const fromHex = Address4.fromHex("c0a80164");
const fromInt = Address4.fromInteger(3232235876);
const fromBigInt = Address4.fromBigInt(3232235876n);
const fromArpa = Address4.fromArpa("100.1.168.192.in-addr.arpa.");
console.log(fromHex.correctForm()); // "192.168.1.100"
console.log(fromInt.correctForm()); // "192.168.1.100"
console.log(fromBigInt.correctForm()); // "192.168.1.100"
console.log(fromArpa.correctForm()); // "192.168.1.100"
// IPv6 from various formats
const bigIntV6 = Address6.fromBigInt(BigInt("42540766411282592856903984951653826561"));
console.log(bigIntV6.correctForm()); // "2001:db8::1"
const fromBytes = Address6.fromByteArray([32, 1, 13, 184, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]);
console.log(fromBytes.correctForm()); // "2001:db8::1"
const fromV4 = Address6.fromAddress4("192.168.1.100");
console.log(fromV4.correctForm()); // "::ffff:c0a8:164"
const fromArpaV6 = Address6.fromArpa("1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa.");
console.log(fromArpaV6.correctForm()); // "2001:db8::1"Check if addresses are in their canonical or correct forms.
/**
* Check if address is in correct form
* Available on both Address4 and Address6 instances
* @returns true if address matches its canonical representation
*/
isCorrect(): boolean;
/**
* Check if IPv6 address is in canonical form (IPv6 only)
* Available on Address6 instances only
* @returns true if address is in full canonical form
*/
isCanonical(): boolean;Form Validation Examples:
// IPv4 form validation
const v4Correct = new Address4("192.168.1.1");
const v4Incorrect = new Address4("192.168.001.001");
console.log(v4Correct.isCorrect()); // true
console.log(v4Incorrect.isCorrect()); // false (leading zeros)
console.log(v4Incorrect.correctForm()); // "192.168.1.1"
// IPv6 form validation
const v6Compressed = new Address6("2001:db8::1");
const v6Canonical = new Address6("2001:0db8:0000:0000:0000:0000:0000:0001");
const v6Incorrect = new Address6("2001:0db8::0001");
console.log(v6Compressed.isCorrect()); // true (properly compressed)
console.log(v6Compressed.isCanonical()); // false (not full form)
console.log(v6Canonical.isCorrect()); // false (should be compressed)
console.log(v6Canonical.isCanonical()); // true (full canonical form)
console.log(v6Incorrect.isCorrect()); // false (unnecessary leading zeros)
console.log(v6Incorrect.correctForm()); // "2001:db8::1"Common use cases for format conversions in networking applications.
Database Storage:
// Store addresses in different formats for different use cases
function storeAddress(addrStr: string) {
try {
const addr = new Address6(addrStr);
return {
canonical: addr.canonicalForm(), // For exact matching
compressed: addr.correctForm(), // For display
binary: addr.binaryZeroPad(), // For bitwise operations
bigint: addr.bigInt().toString(), // For numeric operations
bytes: addr.toByteArray(), // For binary protocols
reverse: addr.reverseForm() // For PTR records
};
} catch (e) {
const addr = new Address4(addrStr);
return {
canonical: addr.correctForm(),
binary: addr.binaryZeroPad(),
bigint: addr.bigInt().toString(),
bytes: addr.toArray(),
reverse: addr.reverseForm()
};
}
}Network Configuration:
// Generate configuration formats
function generateConfig(networkStr: string) {
const network = new Address4(networkStr);
return {
cisco: `ip route ${network.correctForm()} ${network.subnetMask}`,
binary: network.binaryZeroPad(),
hex: network.toHex(),
reverseZone: network.reverseForm({ omitSuffix: true })
};
}
const config = generateConfig("192.168.1.0/24");
console.log(config);
// {
// cisco: "ip route 192.168.1.0 24",
// binary: "11000000101010000000000100000000",
// hex: "c0:a8:01:00",
// reverseZone: "1.168.192"
// }interface ReverseFormOptions {
omitSuffix?: boolean;
}