0.1.0 • Published 12 months ago

node-iputils v0.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
12 months ago

node-iputils

A quite small and simple IP-library with only a few classes. Tested & linted.

Features:

  • Handle IPv4 & IPv6 addresses and subnets with the same class.
  • Simply pass addresses into constructors, the library will decide what it is and how to handle it.
  • Calculate the first and last IP of a subnet.
  • Apply a mask to an IP address.
  • Find out if an IP is inside a subnet.
  • Increment or decrement IPs to get e.g. the next one.
  • Convert addresses to numbers and back.

Usage

Install using npm:

npm install node-iputils

Now use it in your code:

const { IPAddress } = require("node-iputils");

const ipSubnet = new IPAddress("192.168.0.0", 24);
const ipv4 = new IPAddress("192.168.0.5", 32);
ipSubnet.getSubnet().isInSubnet(ipv4) // => true

const ipv6Subnet = new IPAddress("fde8:894a:40c:ee20::", 64);
const ipv6 = new IPAddress("fde8:894a:40c:ee20:0::05:5", 128);
ipv6Subnet.getSubnet().isInSubnet(ipv6) // => true

/* The following functions also work for IPv4-addresses */
ipv6.maximize() // => fde8:894a:040c:ee20:0000:0000:0005:0005
ipv6.minimize() // => fde8:894a:040c:ee20::5:5
ipv6.asNumber() // => 337502080359017093851530813254247186437n

const ipv6FromNumber = IPAddress.fromNumber(337502080359017093851530813254247186437n, 6); // => new IPAddress instance.

ipv4.maskAsNumber() // => 4294967295n

API documentation

Classes

InvalidIPError

Kind: global class

new InvalidIPError(ip, net)

ParamTypeDescription
ipstringThe IP address which is invalid.
netstringThe network mask which is invalid.

InvalidConversionError

Kind: global class

IPAddress

Kind: global class

new IPAddress(ip, net)

ParamTypeDescription
ipstringThe IP-address to store.
netstringThe network mask to store.

ipAddress.minimize() ⇒ string

Kind: instance method of IPAddress
Returns: string - The minimized IP-address.

ipAddress.maximize() ⇒ string

Kind: instance method of IPAddress
Returns: string - The maximized IP-address.

ipAddress.asNumber() ⇒ bigint

Kind: instance method of IPAddress
Returns: bigint - The IP-address as a number.

ipAddress.maskAsNumber() ⇒ bigint

Kind: instance method of IPAddress
Returns: bigint - The network mask as a number.

ipAddress.applyMask() ⇒ IPAddress

Kind: instance method of IPAddress
Returns: IPAddress - The masked IP-address.

ipAddress.increment(by) ⇒ IPAddress

Kind: instance method of IPAddress
Returns: IPAddress - The incremented IP address.

ParamTypeDescription
bynumberThe number to increment the IP address by.

ipAddress.decrement(by) ⇒ IPAddress

Kind: instance method of IPAddress
Returns: IPAddress - The decremented IP address.

ParamTypeDescription
bynumberThe number to decrement the IP address by.

ipAddress.getSubnet() ⇒ IPSubnet

Kind: instance method of IPAddress
Returns: IPSubnet - The subnet of the IP-address.

IPAddress.fromNumber(number, type) ⇒ IPAddress

Kind: static method of IPAddress
Returns: IPAddress - The IP address.

ParamTypeDescription
numberbigintThe number to convert.
typenumberThe type of the IP address. 4 for IPv4 and 6 for IPv6.

IPSubnet

Kind: global class

new IPSubnet(parent)

ParamTypeDescription
parentIPAddressThe parent IP address from which this subnet is generated.

ipSubnet.isMatchingType(address)

Kind: instance method of IPSubnet
Throws:

ParamTypeDescription
addressIPAddressThe address to check.

ipSubnet.isInSubnet(address) ⇒ boolean

Kind: instance method of IPSubnet
Returns: boolean - True if the address is in the subnet, false otherwise.

ParamTypeDescription
addressIPAddressThe address to check.

ipSubnet.getBiggestIPAsNumber() ⇒ bigint

Kind: instance method of IPSubnet
Returns: bigint - The biggest IP address in the subnet as a number.

ipSubnet.getSmallestIPAsNumber() ⇒ bigint

Kind: instance method of IPSubnet
Returns: bigint - The smallest IP address in the subnet as a number.


© /dev/paul

0.1.0

12 months ago