Skip to content
/ ip Public
generated from neoncitylights/jsr

utility package for IP addresses and socket addresses, ported from Rust

License

Notifications You must be signed in to change notification settings

nc-js/ip

Repository files navigation

@nc/ip

A package of network addresses as types, including IPv4/IPv6 addresses, IP address iterators, and socket addresses. This TypeScript implementation mostly mirrors as a port of Rust's std::net module.

Usage

IPv4 addresses

See Ipv4Addr for more details.

It is also possible to iterate through ranges of IPv4 addresses with Ipv4AddrIterator.

import { assertEquals } from '@std/assert'
import { Ipv4Addr } from '@nc/ip/v4'

const ip0 = Ipv4Addr.parse('127.0.0.1')
const ip1 = Ipv4Addr.tryNew(127, 0, 0, 1)
const ip2 = Ipv4Addr.tryFromArray([127, 0, 0, 1])
const ip3 = Ipv4Addr.tryFromUint32(2_130_706_433)
const ip4 = Ipv4Addr.tryFromUint8Array(new Uint8Array([127, 0, 0, 1]))

assertEquals(ip0, ip1)
assertEquals(ip0, ip2)
assertEquals(ip0, ip3)
assertEquals(ip0, ip4)

IPv6 addresses

See Ipv6Addr for more details.

It is also possible to iterate through ranges of IPv6 addresses with Ipv6AddrIterator.

import { assert, assertEquals } from '@std/assert'
import { Ipv6Addr } from '@nc/ip/v6'

// parse() method is not implemented yet
const ip0 = Ipv6Addr.LOCALHOST
const ip1 = Ipv6Addr.tryNew(0, 0, 0, 0, 0, 0, 0, 1)
const ip2 = Ipv6Addr.tryFromUint128(1n)
const ip3 = Ipv6Addr.tryFromArray([0, 0, 0, 0, 0, 0, 0, 1])
const ip4 = Ipv6Addr.tryFromUint16Array(new Uint16Array([0, 0, 0, 0, 0, 0, 0, 1]))

assertEquals(ip0, ip1)
assertEquals(ip0, ip2)
assertEquals(ip0, ip3)
assertEquals(ip0, ip4)

IPv4 socket addresses

See SocketAddrV4 for more details.

import { assertEquals } from '@std/assert'
import { Ipv4Addr } from '@nc/ip/v4'
import { Port, SocketAddrV4 } from '@nc/ip/socket'

const socket1 = new SocketAddrV4(Ipv4Addr.LOCALHOST, new Port(3000))
const socket2 = SocketAddrV4.parse("127.0.0.1:3000")

assertEquals(socket1, socket2)
assertEquals(socket1.addr, Ipv4Addr.LOCALHOST)
assertEquals(socket1.port.value, 3000)
assertEquals(socket1.toString(), "127.0.0.1:3000")
assertEquals(socket2?.toString(), "127.0.0.1:3000")

IPv6 socket addresses

See SocketAddrV6 for more details.

import { assertEquals } from '@std/assert'
import { Ipv6Addr } from '@nc/ip/v6'
import { Port, SocketAddrV6 } from '@nc/ip/socket'

// callers must ensure that the flow info number and scope ID number
// are both valid unsigned 32-bit integers
const socket = new SocketAddrV6(
    Ipv6Addr.LOCALHOST,
    new Port(3000),
    0, // the flow info
    0, // the scope ID
)

assertEquals(socket.addr, Ipv6Addr.LOCALHOST)
assertEquals(socket.port.value, 3000)
assertEquals(socket.flowInfo, 0)
assertEquals(socket.scopeId, 0)
assertEquals(socket.toString(), '[::1]:3000')

License

This software is licensed under the MIT license (LICENSE or https://opensource.org/license/mit/).

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the MIT license, shall be licensed as above, without any additional terms or conditions.

About

utility package for IP addresses and socket addresses, ported from Rust

Resources

License

Stars

Watchers

Forks

Contributors 2

  •  
  •