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. 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 deduplicated, so two records covering the same range yield one entry — note that this compares ranges, not text, so 10.0.0.0/8 and 10.0.0.5/8 count as duplicates. They are then sorted, which is the precondition NetworkList relies on for binary search. And the family is taken from networks[0], so an empty array reaches getType() as undefined and throws.

Overlapping ranges are kept as they are rather than merged, so a list can hold a network and a subnet of it. That costs a little space but never affects the answer.

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