Networks.(constructor)

Builds a set from CIDR records of either family, or from previously packed buffers.

Signature:

constructor(networks: string[] | Buffer, networks6?: Buffer);

Parameters

Parameter

Type

Description

networks

string[] | Buffer

a mixed array of CIDR records, each with an explicit /prefix; or a packed IPv4 buffer, in which case IPv6 must come via networks6

networks6

Buffer

(Optional) a packed IPv6 buffer. Usable alongside either form of networks.

Exceptions

TypeError if networks6 is given but is not a Buffer, if any CIDR record's address is invalid, or if a buffer is empty or not a whole number of records.

RangeError if a CIDR record has no /prefix — a bare address is rejected, so a single host is 10.0.0.1/32 or 2001:db8::1/128.

Remarks

The array form is the usual one, and it does the sorting for you: records are split by family, so the order you list them in does not matter. The buffer form is for restoring a set you packed earlier and reads networks as IPv4 only — an IPv6 buffer passed as the first argument would be misread, so it belongs in networks6.

An empty array yields a set with neither family populated, which answers false to everything rather than throwing.

Example

// From records, mixed families in any order
const a = new Networks(['2001:db8::/32', '10.0.0.0/8']);

// Restored from packed buffers
const b = new Networks(a.ipv4.networks, a.ipv6.networks);

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