node-iputils
v0.1.0
Published
A very small and simple library to handle IPv4- & IPv6-addresses.
Downloads
3
Readme
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)
| Param | Type | Description | | --- | --- | --- | | ip | string | The IP address which is invalid. | | net | string | The network mask which is invalid. |
InvalidConversionError
Kind: global class
IPAddress
Kind: global class
- IPAddress
- new IPAddress(ip, net)
- instance
- .minimize() ⇒ string
- .maximize() ⇒ string
- .asNumber() ⇒ bigint
- .maskAsNumber() ⇒ bigint
- .applyMask() ⇒ IPAddress
- .increment(by) ⇒ IPAddress
- .decrement(by) ⇒ IPAddress
- .getSubnet() ⇒ IPSubnet
- static
new IPAddress(ip, net)
| Param | Type | Description | | --- | --- | --- | | ip | string | The IP-address to store. | | net | string | The 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.
| Param | Type | Description | | --- | --- | --- | | by | number | The number to increment the IP address by. |
ipAddress.decrement(by) ⇒ IPAddress
Kind: instance method of IPAddress
Returns: IPAddress - The decremented IP address.
| Param | Type | Description | | --- | --- | --- | | by | number | The 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.
| Param | Type | Description | | --- | --- | --- | | number | bigint | The number to convert. | | type | number | The type of the IP address. 4 for IPv4 and 6 for IPv6. |
IPSubnet
Kind: global class
- IPSubnet
- new IPSubnet(parent)
- .isMatchingType(address)
- .isInSubnet(address) ⇒ boolean
- .getBiggestIPAsNumber() ⇒ bigint
- .getSmallestIPAsNumber() ⇒ bigint
new IPSubnet(parent)
| Param | Type | Description | | --- | --- | --- | | parent | IPAddress | The parent IP address from which this subnet is generated. |
ipSubnet.isMatchingType(address)
Kind: instance method of IPSubnet
Throws:
- InvalidIPError If the address is of a different type.
| Param | Type | Description | | --- | --- | --- | | address | IPAddress | The address to check. |
ipSubnet.isInSubnet(address) ⇒ boolean
Kind: instance method of IPSubnet
Returns: boolean - True if the address is in the subnet, false otherwise.
| Param | Type | Description | | --- | --- | --- | | address | IPAddress | The 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