UNPKG

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