netparser
Parse and manipulate IPv4 and IPv6 network addresses

Installation
npm install netparser
Examples
import * as netparser from 'netparser';
netparser.baseAddress('b011:a2c2:7328:cc01:4ee7:e2ec:6269:babf/73');
netparser.broadcastAddress('192.168.0.50/24');
netparser.findUnusedSubnets('192.168.0.0/22', ['192.168.1.0/24', '192.168.2.32/30']);
netparser.ip(' [2001:db8:122:344:0:0:0::0:0:0:1] ');
netparser.network(' 192.168.000.000/24 ');
netparser.networkComesBefore('192.168.0.0/24', '10.0.0.0/8');
netparser.networkContainsSubnet('192.168.0.0/16', '192.168.0.0/24');
netparser.networksIntersect('192.168.0.0/23', '192.168.1.0/24');
netparser.nextAddress('192.168.0.0');
netparser.nextNetwork('192.168.0.0/24');
netparser.rangeOfNetworks('192.168.1.2', '192.168.2.2');
netparser.sort(['255.255.255.255', '192.168.0.0/16', '192.168.2.3/31']);
netparser.summarize(['192.168.1.1', '192.168.0.0/16', '192.168.2.3/31']);
var matcher = new netparser.Matcher(['192.168.0.0/24', '192.168.2.0/23', '192.168.4.0/24']);
matcher.has('192.168.3.0');
FYI
- For simplicity, all functions will only ever return
String, String[], boolean, or null.
- By default the library will fail silently and
null is returned when errors are encountered. To override this setting set the optional throwErrors parameter to True.
- By default the library will conveniently mask out provided
network values to their base address when such an operation makes sense. To override this setting set the optional strict parameter to True where applicable.
Benchmarks
npm run bench
'index.bench.ts' output:
baseAddress (netparser) x 1,881,378 ops/sec ±0.66% (95 runs sampled)
baseAddress (ip-address) x 1,355,975 ops/sec ±0.64% (88 runs sampled)
baseAddress (ipaddr.js) x 509,825 ops/sec ±2.07% (89 runs sampled)
baseAddress (netmask) x 326,042 ops/sec ±3.84% (82 runs sampled)
contains (netparser) x 883,418 ops/sec ±1.53% (84 runs sampled)
contains (ip-address) x 901,704 ops/sec ±1.44% (90 runs sampled)
contains (ipaddr.js) x 59,005 ops/sec ±13.38% (65 runs sampled)
contains (netmask) x 304,785 ops/sec ±1.77% (88 runs sampled)
'match.bench.ts' output:
create (netparser) x 11.91 ops/sec ±5.55% (34 runs sampled)
create (cidr-matcher) x 5.13 ops/sec ±5.43% (17 runs sampled)
create (ipaddr.js) x 28.78 ops/sec ±4.83% (50 runs sampled)
query (netparser) x 145,604 ops/sec ±1.25% (91 runs sampled)
query (cidr-matcher) x 1,035 ops/sec ±3.74% (83 runs sampled)
query (ipaddr.js) x 16.22 ops/sec ±1.76% (44 runs sampled)
API
Docs generated using docts
BaseAddress returns the base address for a given subnet address
Source code: <>
baseAddress( ) â null | string <>
ââȘ networkAddress string - A network address like 192.168.0.4/24
ââ« throwErrors? undefined | true | false - Stop the library from failing silently
BroadcastAddress returns the broadcast address for an IPv4 address.
Please note that IPv6 does not have broadcast addresses.
Source code: <>
broadcastAddress( ) â null | string <>
ââȘ network string - A network like 192.168.0.0/24
ââ« throwErrors? undefined | true | false - Stop the library from failing silently
FindUnusedSubnets returns array of unused subnets given the aggregate and sibling subnets
Source code: <>
findUnusedSubnets( ) â null | string[] <>
ââȘ aggregate string - An aggregate network like 192.168.0.0/24
ââȘ subnets string[] - Array of subnetworks like ["192.168.0.0/24", "192.168.0.128/26"]
ââ« strict? undefined | true | false - Do not automatically mask addresses to baseAddresses
ââ« throwErrors? undefined | true | false - Stop the library from failing silently
Function ip
Parse an IP address
Source code: <>
ip( ) â null | string <>
ââȘ address string - Either an address like 192.168.0.0 or subnet 192.168.0.0/24
ââ« throwErrors? undefined | true | false - Stop the library from failing silently
Parse a network address
Source code: <>
network( ) â null | string <>
ââȘ networkAddress string - A network like 192.168.0.0/24
ââ« throwErrors? undefined | true | false - Stop the library from failing silently
NetworkComesBefore returns a bool with regards to numerical network order.
Please note that IPv4 comes before IPv6 and larger networks come before smaller ones.
Source code: <>
networkComesBefore( ) â null | true | false <>
ââȘ network string - A network like 192.168.0.0/24
ââȘ otherNetwork string - A network like 192.168.1.0/24
ââ« strict? undefined | true | false - Do not automatically mask addresses to baseAddresses
ââ« throwErrors? undefined | true | false - Stop the library from failing silently
NetworkContainsAddress validates that the address is inside the network
Source code: <>
networkContainsAddress( ) â null | true | false <>
ââȘ network string - A network like 192.168.0.0/24
ââȘ address string - A network like 192.168.0.100
ââ« strict? undefined | true | false - Do not automatically mask addresses to baseAddresses
ââ« throwErrors? undefined | true | false - Stop the library from failing silently
NetworkContainsSubnet validates that the network is a valid supernet
Source code: <>
networkContainsSubnet( ) â null | true | false <>
ââȘ network string - A network like 192.168.0.0/16
ââȘ subnet string - A network like 192.168.0.0/24
ââ« strict? undefined | true | false - Do not automatically mask addresses to baseAddresses
ââ« throwErrors? undefined | true | false - Stop the library from failing silently
NetworksIntersect returns a bool showing if the networks overlap
Source code: <>
networksIntersect( ) â null | true | false <>
ââȘ network string - A network like 192.168.0.0/23
ââȘ otherNetwork string - A network like 192.168.1.0/24
ââ« strict? undefined | true | false - Do not automatically mask addresses to baseAddresses
ââ« throwErrors? undefined | true | false - Stop the library from failing silently
NextAddress returns the next address
Source code: <>
nextAddress( ) â null | string <>
ââȘ address string - An address like 192.168.0.0
ââ« throwErrors? undefined | true | false - Stop the library from failing silently
NextNetwork returns the next network of the same size.
Source code: <>
nextNetwork( ) â null | string <>
ââȘ network string - A network like 192.168.0.0/24
ââ« strict? undefined | true | false - Do not automatically mask addresses to baseAddresses
ââ« throwErrors? undefined | true | false - Stop the library from failing silently
RangeOfNetworks returns an array of networks given a range of addresses
Source code: <>
rangeOfNetworks( ) â null | string[] <>
ââȘ startAddress string - An address like 192.168.1.2
ââȘ stopAddress string - An address like 192.168.1.5
ââ« throwErrors? undefined | true | false - Stop the library from failing silently
Function sort
Sort returns an array of sorted networks
Source code: <>
sort( ) â null | string[] <>
ââȘ networkAddresses string[] - An array of addresses or subnets
ââ« throwErrors? undefined | true | false - Stop the library from failing silently
Summarize returns an array of aggregates given a list of networks
Source code: <>
summarize( ) â null | string[] <>
ââȘ networks string[] - An array of addresses or subnets
ââ« strict? undefined | true | false - Do not automatically mask addresses to baseAddresses
ââ« throwErrors? undefined | true | false - Stop the library from failing silently