Skip to content

Commit 164505e

Browse files
authored
feat: add type define (#3)
copy from https://www.npmjs.com/package/@types/ip <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Added comprehensive IP address manipulation utilities with TypeScript type definitions - Introduced type checking for IP address functions - **Documentation** - Added Contributors section to README - **Chores** - Updated ESLint configuration to include Node environment - Modified package scripts and dependencies - Added TypeScript type definitions for the package - **Tests** - Added type assertion tests for IP address functions <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent fc37341 commit 164505e

File tree

6 files changed

+167
-32
lines changed

6 files changed

+167
-32
lines changed

.eslintrc.js

+28-27
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
11
module.exports = {
2-
'env': {
3-
'browser': true,
4-
'commonjs': true,
5-
'es2021': true
6-
},
7-
'extends': 'eslint:recommended',
8-
'parserOptions': {
9-
'ecmaVersion': 'latest'
10-
},
11-
'rules': {
12-
'indent': [
13-
'error',
14-
2
15-
],
16-
'linebreak-style': [
17-
'error',
18-
'unix'
19-
],
20-
'quotes': [
21-
'error',
22-
'single'
23-
],
24-
'semi': [
25-
'error',
26-
'always'
27-
]
28-
}
2+
'env': {
3+
'node': true,
4+
'browser': true,
5+
'commonjs': true,
6+
'es2021': true
7+
},
8+
'extends': 'eslint:recommended',
9+
'parserOptions': {
10+
'ecmaVersion': 'latest'
11+
},
12+
'rules': {
13+
'indent': [
14+
'error',
15+
2
16+
],
17+
'linebreak-style': [
18+
'error',
19+
'unix'
20+
],
21+
'quotes': [
22+
'error',
23+
'single'
24+
],
25+
'semi': [
26+
'error',
27+
'always'
28+
]
29+
}
2930
};

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ var normalized = ip.normalizeLax('0x7f.1'); // 127.0.0.1
8484
ip.isPrivate(normalized); // true
8585
```
8686

87+
## Contributors
88+
89+
[![Contributors](https://contrib.rocks/image?repo=eggjs/node-ip)](https://github.com/eggjs/node-ip/graphs/contributors)
90+
91+
Made with [contributors-img](https://contrib.rocks).
92+
8793
### License
8894

8995
```txt

index.d.ts

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/// <reference types="node" />
2+
3+
export 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+
*/
18+
export function isEqual(ip1: string, ip2: string): boolean;
19+
20+
/**
21+
* Convert an IP string into a buffer.
22+
*/
23+
export function toBuffer(ip: string, buffer?: Buffer, offset?: number): Buffer;
24+
25+
/**
26+
* Convert an IP buffer into a string.
27+
*/
28+
export 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+
*/
35+
export 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+
*/
40+
export function mask(ip: string, mask: string): string;
41+
42+
/**
43+
* Get the network ID IP address from an IP address in CIDR notation.
44+
*/
45+
export function cidr(cidr: string): string;
46+
47+
/**
48+
* Get the bitwise inverse (NOT every octet) of an IP address or subnet mask.
49+
*/
50+
export 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+
*/
55+
export function or(ip: string, mask: string): string;
56+
57+
/**
58+
* Check whether an IP is within a private IP address range.
59+
*/
60+
export function isPrivate(ip: string): boolean;
61+
62+
/**
63+
* Check whether an IP is within a public IP address range.
64+
*/
65+
export function isPublic(ip: string): boolean;
66+
67+
/**
68+
* Check whether an IP is a loopback address.
69+
*/
70+
export function isLoopback(ip: string): boolean;
71+
72+
/**
73+
* Check whether an IP is a IPv4 address.
74+
*/
75+
export function isV4Format(ip: string): boolean;
76+
77+
/**
78+
* Check whether an IP is a IPv6 address.
79+
*/
80+
export 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+
*/
87+
export 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+
*/
96+
export 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+
*/
101+
export function toLong(ip: string): number;
102+
103+
/**
104+
* Convert an IPv4 IP address from its the long numeric value to a string.
105+
*/
106+
export function fromLong(ip: number): string;
107+
108+
/**
109+
* Get the subnet information.
110+
* @param ip IP address.
111+
* @param subnet Subnet address.
112+
*/
113+
export function subnet(ip: string, subnet: string): SubnetInfo;
114+
115+
/**
116+
* Get the subnet information.
117+
* @param cidr CIDR address.
118+
*/
119+
export function cidrSubnet(cidr: string): SubnetInfo;

index.test-d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { expectType } from 'tsd';
2+
import ip, { isV4Format } from '.';
3+
4+
expectType<string>(ip.address());
5+
expectType<boolean>(isV4Format('127.0.0.1'));

lib/ip.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const ip = exports;
2-
const { Buffer } = require('buffer');
32
const os = require('os');
43
const net = require('net');
54

package.json

+9-4
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,23 @@
1515
},
1616
"files": [
1717
"lib",
18+
"index.d.ts",
1819
"README.md"
1920
],
2021
"main": "lib/ip",
22+
"types": "index.d.ts",
2123
"devDependencies": {
24+
"@types/node": "^22.10.5",
2225
"egg-bin": "^6.10.0",
23-
"eslint": "^8.15.0"
26+
"eslint": "^8.15.0",
27+
"tsd": "^0.31.2"
2428
},
2529
"scripts": {
2630
"lint": "eslint lib test",
27-
"test": "npm run lint && egg-bin test --ts false",
28-
"fix": "npm run lint -- --fix",
29-
"ci": "npm run lint && egg-bin cov --ts false"
31+
"pretest": "npm run lint -- --fix && tsd",
32+
"test": "egg-bin test --ts false",
33+
"preci": "npm run lint && tsd",
34+
"ci": "egg-bin cov --ts false"
3035
},
3136
"license": "MIT"
3237
}

0 commit comments

Comments
 (0)