UNPKG

3.2 kBTypeScriptView Raw
1/// <reference types="node" />
2
3export interface SubnetInfo {
4 networkAddress: string;
5 firstAddress: string;
6 lastAddress: string;
7 broadcastAddress: string;
8 subnetMask: string;
9 subnetMaskLength: number;
10 numHosts: number;
11 length: number;
12 contains(ip: string): boolean;
13}
14
15/**
16 * Check two IP address are the same.
17 */
18export function isEqual(ip1: string, ip2: string): boolean;
19
20/**
21 * Convert an IP string into a buffer.
22 */
23export function toBuffer(ip: string, buffer?: Buffer, offset?: number): Buffer;
24
25/**
26 * Convert an IP buffer into a string.
27 */
28export function toString(ip: Buffer, offset?: number, length?: number): string;
29
30/**
31 * Get the subnet mask from a CIDR prefix length.
32 *
33 * @param family The IP family is infered from the prefixLength, but can be explicity specified as either "ipv4" or "ipv6".
34 */
35export function fromPrefixLen(prefixLength: number, family?: "ipv4" | "ipv6"): string;
36
37/**
38 * Get the network ID IP address from an IP address and its subnet mask.
39 */
40export function mask(ip: string, mask: string): string;
41
42/**
43 * Get the network ID IP address from an IP address in CIDR notation.
44 */
45export function cidr(cidr: string): string;
46
47/**
48 * Get the bitwise inverse (NOT every octet) of an IP address or subnet mask.
49 */
50export function not(ip: string): string;
51
52/**
53 * Get the bitwise OR of two IP addresses (usually an IP address and a subnet mask).
54 */
55export function or(ip: string, mask: string): string;
56
57/**
58 * Check whether an IP is within a private IP address range.
59 */
60export function isPrivate(ip: string): boolean;
61
62/**
63 * Check whether an IP is within a public IP address range.
64 */
65export function isPublic(ip: string): boolean;
66
67/**
68 * Check whether an IP is a loopback address.
69 */
70export function isLoopback(ip: string): boolean;
71
72/**
73 * Check whether an IP is a IPv4 address.
74 */
75export function isV4Format(ip: string): boolean;
76
77/**
78 * Check whether an IP is a IPv6 address.
79 */
80export function isV6Format(ip: string): boolean;
81
82/**
83 * Get the loopback address for an IP family.
84 *
85 * @param family The family can be either "ipv4" or "ipv6". Default: "ipv4".
86 */
87export function loopback(family?: "ipv4" | "ipv6"): string;
88
89/**
90 * Get the address for the network interface on the current system with the specified 'name'.
91 * If no interface name is specified, the first IPv4 address or loopback address is returned.
92 *
93 * @param name The name can be any named interface, or 'public' or 'private'.
94 * @param family The family can be either "ipv4" or "ipv6". Default: "ipv4".
95 */
96export function address(name?: "public" | "private" | string, family?: "ipv4" | "ipv6"): string;
97
98/**
99 * Convert a string IPv4 IP address to the equivalent long numeric value.
100 */
101export function toLong(ip: string): number;
102
103/**
104 * Convert an IPv4 IP address from its the long numeric value to a string.
105 */
106export function fromLong(ip: number): string;
107
108/**
109 * Get the subnet information.
110 * @param ip IP address.
111 * @param subnet Subnet address.
112 */
113export function subnet(ip: string, subnet: string): SubnetInfo;
114
115/**
116 * Get the subnet information.
117 * @param cidr CIDR address.
118 */
119export function cidrSubnet(cidr: string): SubnetInfo;