net package
Fast CIDR membership testing for IPv4 and IPv6, using sorted binary ranges rather than per-network comparison.
Build a Networks from a list of CIDR records and ask it whether an address is in any of them — that is the whole API for most callers, and it is what @imqueue/http-protect uses for its allow-list. NetworkList is the single-family layer underneath, and the cidrToRange() and ipToInt() helpers are exported for building something else on the same primitives.
Remarks
Each network is stored as a start/end address pair in a Buffer, sorted, and matched by binary search, so lookup is logarithmic in the number of networks instead of linear. Addresses are compared as bigint, which is what makes one code path cover both a 4-byte and a 16-byte address.
The two families never share a buffer, because an IPv6 record is 32 bytes against IPv4's 8 — Networks holds one NetworkList per NetworkType and dispatches on the address it is given.
Every CIDR record needs an explicit prefix length. A bare address is rejected, so a single host is 203.0.113.7/32 or 2001:db8::1/128; passing 203.0.113.7 throws while parsing. Anything that is not a valid record throws rather than being skipped, so one bad entry fails the whole list — validate with isValid() first if the input is untrusted.
Example 1
import { Networks } from '@imqueue/net';
const allowed = new Networks(['10.0.0.0/8', '192.168.0.0/16', '2001:db8::/32']);
allowed.includes('10.1.2.3'); // true
allowed.includes('8.8.8.8'); // false
allowed.includes('2001:db8::1'); // true
Example 2
import { isValid, Networks } from '@imqueue/net';
// Untrusted input: check before constructing, because a bad record throws.
const records = input.filter(r => isValid(r.split('/')[0]));
const networks = new Networks(records);
Classes
|
Class |
Description |
|---|---|
|
A single-family list of networks, stored as sorted binary ranges and searched in O(log n). | |
|
A CIDR membership set covering both address families — the entry point for most callers. |
Enumerations
|
Enumeration |
Description |
|---|---|
|
Which address family a value belongs to. |
Functions
|
Function |
Description |
|---|---|
|
Reads a string of | |
|
Expands a CIDR record into the first and last address it covers, as text. | |
|
Expands a CIDR record into the first and last address it covers, as integers. | |
|
Determines an address's family, or verifies the one you claim it has. | |
|
Covers an integer address range with the fewest CIDR records that fit it exactly. | |
|
Renders an integer address back to text. | |
|
Converts an address to the unsigned | |
|
Compresses an IPv6 address: drops leading zeros from each group and collapses the longest run of zero groups to | |
|
Expands an IPv6 address to its full eight-group, four-digit form. | |
|
Whether a string is a valid IPv4 or IPv6 address. | |
|
Whether a string is a valid IPv4 address. | |
|
Whether a string is a valid IPv6 address. | |
|
The mask table for a family, indexable by prefix length. | |
|
Covers an address range with the fewest CIDR records that fit it exactly. | |
|
How many bytes one address of the given family occupies. | |
|
Reads a little-endian byte sequence as an unsigned | |
|
Packs CIDR records into the sorted binary form that lookups binary-search over. | |
|
Writes an unsigned | |
|
Unpacks a binary list back into integer address ranges. | |
|
Unpacks a binary list back into CIDR text. | |
|
Asserts that a string is a valid address of either family. | |
|
Asserts that a string is a valid IPv4 address. | |
|
Asserts that a string is a valid IPv6 address. |
Interfaces
|
Interface |
Description |
|---|---|
|
Integer address ranges grouped by family, as returned by Networks.toIntRanges(). |
Variables
|
Variable |
Description |
|---|---|
|
Bytes in a binary IPv4 address: 4. | |
|
The 33 IPv4 network masks, indexed by prefix length — | |
|
Longest an IPv4 address can be as text: 15 characters, from four three-digit octets plus three dots. | |
|
Bytes in a binary IPv6 address: 16. | |
|
The 129 IPv6 network masks, indexed by prefix length — | |
|
Longest an IPv6 address can be as text: 39 characters, from eight groups of four hex digits plus seven colons. | |
|
The literal |
Read this page as plain markdown — no HTML, no navigation. For pasting into an LLM, or for an agent to fetch.