toBinaryList() function

Packs CIDR records into the sorted binary form that lookups binary-search over.

Signature:

export declare function toBinaryList(networks: string[], type?: NetworkType): Buffer;

Parameters

Parameter

Type

Description

networks

string[]

CIDR records, each needing an explicit /prefix

type

NetworkType

(Optional) the family, if known; omit to have it detected from the first record

Returns:

Buffer

A buffer of [start, end] address pairs, little-endian, ascending by start address and pairwise disjoint. Two addresses of sizeOf() bytes per record.

Exceptions

TypeError if a record's address is invalid or belongs to another family than the first one.

RangeError if a record has no /prefix — see cidrToRangeInt().

Remarks

Three things happen here that the caller can observe. Ranges are sorted by start address, which is the precondition NetworkList relies on for binary search. Overlapping ranges are then coalesced into a single record spanning both — note that this compares ranges, not text, so 10.0.0.0/8 and 10.0.0.5/8 collapse into one record, and so do a network and a subnet of it, such as 10.0.0.0/8 and 10.1.0.0/16, which come back as just the supernet. And the family is taken from networks[0], so an empty array reaches getType() as undefined and throws.

Coalescing is what makes the result searchable, not merely smaller: binary search over ranges is only sound when the ranges are disjoint. Left overlapping, a probe that lands on a subnet nested inside an earlier supernet and finds the target above that subnet's end moves right, and never revisits the supernet sitting at a lower index — so an address the list does cover answers false.

Only overlap is merged, never mere adjacency: 11.0.0.0/8 and 12.0.0.0/8 abut but stay two records. That is deliberate. The union of two adjacent prefixes need not be a prefix itself, and NetworkList rejects a buffer holding a range that does not re-expand to one record, so merging adjacency would break the buffer round trip for a space saving that correctness does not need.

The result therefore holds at most as many records as it was given, and fewer whenever the input overlaps.

Read this page as plain markdown — no HTML, no navigation. For pasting into an LLM, or for an agent to fetch.