parseIPv6

import { parseIPv6 } from "jsr:@ende/net/unstable-ip";

function ยท experimental

function parseIPv6(addr: string): Uint8Array | undefined

Parses a string as an IPv6 address.

Implements the URL standard's IPv6 parser, which covers :: compression and the trailing x:x:x:x:x:x:d.d.d.d form. Zone IDs (fe80::1%eth0) are intentionally omitted by that standard and are rejected here too, unlike node:net's isIP().

addr: string

IPv6 address in a string format (e.g., "2001:db8::1").

Returns

The sixteen address bytes, or undefined if the string is not a valid IPv6 address.

Example: Parse an IPv6 address

import { parseIPv6 } from "@ende/net/unstable-ip"
import { assertEquals } from "@ende/assert"

const loopback = new Uint8Array(16)
loopback[15] = 1

assertEquals(parseIPv6("::1"), loopback)
assertEquals(parseIPv6("2001:db8:::1"), undefined)

Source