IP address utilities for node.js providing IPv4/IPv6 address manipulation, validation, and network operations
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Comprehensive subnet calculation, CIDR operations, and network masking functionality for network analysis and IP address manipulation with support for both IPv4 and IPv6 networks.
Creates a subnet mask from a CIDR prefix length, automatically detecting or accepting explicit address family specification.
/**
* Creates subnet mask from prefix length
* @param {number} prefixlen - CIDR prefix length (0-32 for IPv4, 0-128 for IPv6)
* @param {string|number} [family] - 'ipv4', 'ipv6', 4, 6, or auto-detect if omitted
* @returns {string} String representation of subnet mask
*/
function fromPrefixLen(prefixlen, family);Usage Examples:
const ip = require('ip');
// IPv4 subnet masks (auto-detected for prefixes <= 32)
console.log(ip.fromPrefixLen(24)); // '255.255.255.0'
console.log(ip.fromPrefixLen(16)); // '255.255.0.0'
console.log(ip.fromPrefixLen(8)); // '255.0.0.0'
// IPv6 subnet masks (auto-detected for prefixes > 32)
console.log(ip.fromPrefixLen(64)); // 'ffff:ffff:ffff:ffff::'
console.log(ip.fromPrefixLen(48)); // 'ffff:ffff:ffff::'
// Explicit family specification
console.log(ip.fromPrefixLen(24, 'ipv6')); // 'ffff:ff00::'
console.log(ip.fromPrefixLen(24, 6)); // 'ffff:ff00::'
console.log(ip.fromPrefixLen(24, 'IPV4')); // '255.255.255.0'Applies a subnet mask to an IP address to extract the network address, supporting IPv4/IPv4, IPv6/IPv6, and mixed operations.
/**
* Applies subnet mask to IP address
* @param {string} addr - IP address to mask
* @param {string} mask - Subnet mask to apply
* @returns {string} Masked IP address (network address)
*/
function mask(addr, mask);Usage Examples:
const ip = require('ip');
// IPv4 masking
console.log(ip.mask('192.168.1.134', '255.255.255.0')); // '192.168.1.0'
console.log(ip.mask('10.5.10.25', '255.255.0.0')); // '10.5.0.0'
// Mixed IPv4 address with IPv6 mask
console.log(ip.mask('192.168.1.134', '::ffff:ff00')); // '::ffff:c0a8:100'
// IPv6 masking
console.log(ip.mask('2001:db8:85a3::8a2e:370:7334', 'ffff:ffff:ffff::'));
// '2001:db8:85a3::'Extracts the network address from CIDR notation by applying the appropriate subnet mask.
/**
* Extracts network address from CIDR notation
* @param {string} cidrString - CIDR notation (e.g., '192.168.1.134/26')
* @returns {string} Network address string
* @throws {Error} Throws error for invalid CIDR format
*/
function cidr(cidrString);Usage Examples:
const ip = require('ip');
// IPv4 CIDR
console.log(ip.cidr('192.168.1.134/26')); // '192.168.1.128'
console.log(ip.cidr('10.0.5.100/8')); // '10.0.0.0'
console.log(ip.cidr('172.16.250.1/12')); // '172.16.0.0'
// IPv6 CIDR
console.log(ip.cidr('2607:f0d0:1002:51::4/56')); // '2607:f0d0:1002::'
// Invalid CIDR throws error
try {
ip.cidr('192.168.1.134'); // Missing /prefix
} catch (error) {
console.log(error.message); // 'invalid CIDR subnet: 192.168.1.134'
}Calculates comprehensive subnet information including network boundaries, host counts, and contains function for range checking.
/**
* Calculates detailed subnet information
* @param {string} addr - IP address within subnet
* @param {string} mask - Subnet mask
* @returns {SubnetInfo} Object with detailed subnet information
*/
function subnet(addr, mask);interface SubnetInfo {
networkAddress: string; // Network base address
firstAddress: string; // First usable host address
lastAddress: string; // Last usable host address
broadcastAddress: string; // Broadcast address
subnetMask: string; // Subnet mask
subnetMaskLength: number; // CIDR prefix length
numHosts: number; // Number of usable host addresses
length: number; // Total number of addresses in subnet
contains: (addr: string) => boolean; // Function to check if address is in subnet
}Usage Examples:
const ip = require('ip');
const subnet = ip.subnet('192.168.1.134', '255.255.255.192');
console.log(subnet.networkAddress); // '192.168.1.128'
console.log(subnet.firstAddress); // '192.168.1.129'
console.log(subnet.lastAddress); // '192.168.1.190'
console.log(subnet.broadcastAddress); // '192.168.1.191'
console.log(subnet.subnetMask); // '255.255.255.192'
console.log(subnet.subnetMaskLength); // 26
console.log(subnet.numHosts); // 62
console.log(subnet.length); // 64
// Check if addresses are in subnet
console.log(subnet.contains('192.168.1.150')); // true
console.log(subnet.contains('192.168.1.200')); // false
// Special cases for /31 and /32 networks
const pointToPoint = ip.subnet('192.168.1.134', '255.255.255.254'); // /31
console.log(pointToPoint.numHosts); // 2
console.log(pointToPoint.firstAddress); // '192.168.1.134'
console.log(pointToPoint.lastAddress); // '192.168.1.135'
const hostRoute = ip.subnet('192.168.1.134', '255.255.255.255'); // /32
console.log(hostRoute.numHosts); // 1
console.log(hostRoute.firstAddress); // '192.168.1.134'
console.log(hostRoute.lastAddress); // '192.168.1.134'Calculates subnet information from CIDR notation, providing the same detailed analysis as the subnet function.
/**
* Calculates subnet information from CIDR notation
* @param {string} cidrString - CIDR notation (e.g., '192.168.1.134/26')
* @returns {SubnetInfo} Same object as subnet() function
* @throws {Error} Throws error for invalid CIDR format
*/
function cidrSubnet(cidrString);Usage Examples:
const ip = require('ip');
const subnet = ip.cidrSubnet('192.168.1.134/26');
console.log(subnet.networkAddress); // '192.168.1.128'
console.log(subnet.broadcastAddress); // '192.168.1.191'
console.log(subnet.numHosts); // 62
console.log(subnet.subnetMaskLength); // 26
// Range checking with CIDR subnet
console.log(subnet.contains('192.168.1.180')); // true
console.log(subnet.contains('192.168.1.195')); // false
// Common network sizes
const classC = ip.cidrSubnet('192.168.1.0/24');
console.log(classC.numHosts); // 254
const classB = ip.cidrSubnet('172.16.0.0/16');
console.log(classB.numHosts); // 65534
const classA = ip.cidrSubnet('10.0.0.0/8');
console.log(classA.numHosts); // 16777214