ip-utilties 3.0.3 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -4
- package/package.json +12 -7
- package/src/ip.mjs +25 -14
- package/types/ip.d.mts +8 -0
package/README.md
CHANGED
|
@@ -9,18 +9,17 @@
|
|
|
9
9
|
[](http://commitizen.github.io/cz-cli/)
|
|
10
10
|
[](https://snyk.io/test/github/arlac77/ip-utilties)
|
|
11
11
|
|
|
12
|
-
ip
|
|
12
|
+
ip-v4/v6 utility functions
|
|
13
13
|
|
|
14
14
|
## Three different representations
|
|
15
15
|
|
|
16
16
|
```javascript
|
|
17
17
|
const ip4AsString = "10.0.0.1";
|
|
18
18
|
const ip4AsArray = new Uint8Array([10,0,0,1]);
|
|
19
|
-
const ip4or6AsBigInt = 10n <<
|
|
19
|
+
const ip4or6AsBigInt = 10n << 24n | 1n;
|
|
20
20
|
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
encodeIPv4("10.0.0.1") // -> 10n << 14n | 1n;
|
|
22
|
+
encodeIPv4("10.0.0.1") // -> 10n << 24n | 1n;
|
|
24
23
|
```
|
|
25
24
|
|
|
26
25
|
# API
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ip-utilties",
|
|
3
|
-
"version": "3.0
|
|
3
|
+
"version": "3.1.0",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public",
|
|
6
6
|
"provenance": true
|
|
7
7
|
},
|
|
8
|
-
"packageManager": "npm@11.
|
|
8
|
+
"packageManager": "npm@11.16.0",
|
|
9
9
|
"exports": {
|
|
10
10
|
".": {
|
|
11
11
|
"default": "./src/ip.mjs"
|
|
@@ -40,15 +40,20 @@
|
|
|
40
40
|
"lint:typescript": "tsc --allowJs --checkJs --noEmit --resolveJsonModule --target esnext -m esnext --module nodenext --moduleResolution nodenext ./src**/*.mjs"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
|
-
"ava": "^
|
|
44
|
-
"browser-ava": "^2.3.
|
|
43
|
+
"ava": "^8.0.1",
|
|
44
|
+
"browser-ava": "^2.3.61",
|
|
45
45
|
"c8": "^11.0.0",
|
|
46
46
|
"documentation": "^14.0.3",
|
|
47
|
-
"semantic-release": "^25.0.
|
|
48
|
-
"typescript": "^6.0.
|
|
47
|
+
"semantic-release": "^25.0.5",
|
|
48
|
+
"typescript": "^6.0.3"
|
|
49
|
+
},
|
|
50
|
+
"overrides": {
|
|
51
|
+
"c8": {
|
|
52
|
+
"yargs": "^18.0.0"
|
|
53
|
+
}
|
|
49
54
|
},
|
|
50
55
|
"engines": {
|
|
51
|
-
"node": ">=
|
|
56
|
+
"node": ">=26.2.0"
|
|
52
57
|
},
|
|
53
58
|
"repository": {
|
|
54
59
|
"type": "git",
|
package/src/ip.mjs
CHANGED
|
@@ -1,9 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Address familiy IPv4
|
|
3
|
+
*/
|
|
4
|
+
export const FAMILY_IPV4 = "IPv4";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Address familiy IPv6
|
|
8
|
+
*/
|
|
9
|
+
export const FAMILY_IPV6 = "IPv6";
|
|
10
|
+
|
|
1
11
|
const ipv4 = {
|
|
2
|
-
name:
|
|
12
|
+
name: FAMILY_IPV4,
|
|
3
13
|
factory: Uint8Array,
|
|
4
|
-
normalize
|
|
5
|
-
return address;
|
|
6
|
-
},
|
|
14
|
+
normalize: address => address,
|
|
7
15
|
separator: ".",
|
|
8
16
|
bitLength: 32,
|
|
9
17
|
segments: 4,
|
|
@@ -13,7 +21,7 @@ const ipv4 = {
|
|
|
13
21
|
};
|
|
14
22
|
|
|
15
23
|
const ipv6 = {
|
|
16
|
-
name:
|
|
24
|
+
name: FAMILY_IPV6,
|
|
17
25
|
factory: Uint16Array,
|
|
18
26
|
normalize(address) {
|
|
19
27
|
const parts = address.split(":");
|
|
@@ -34,7 +42,7 @@ const ipv6 = {
|
|
|
34
42
|
|
|
35
43
|
/**
|
|
36
44
|
* Encode ipv4 or ipv6 address into number array.
|
|
37
|
-
* @param {string|number[]} address
|
|
45
|
+
* @param {string|number[]} address
|
|
38
46
|
* @returns number[]
|
|
39
47
|
*/
|
|
40
48
|
export function encodeIP(address) {
|
|
@@ -44,7 +52,7 @@ export function encodeIP(address) {
|
|
|
44
52
|
|
|
45
53
|
/**
|
|
46
54
|
* Encode ipv6 address into number array.
|
|
47
|
-
* @param {string|number[]|bigint} address
|
|
55
|
+
* @param {string|number[]|bigint} address
|
|
48
56
|
* @returns number[]
|
|
49
57
|
*/
|
|
50
58
|
export function encodeIPv6(address) {
|
|
@@ -53,7 +61,7 @@ export function encodeIPv6(address) {
|
|
|
53
61
|
|
|
54
62
|
/**
|
|
55
63
|
* Encode ipv6 address into number array.
|
|
56
|
-
* @param {string|number[]|bigint} address
|
|
64
|
+
* @param {string|number[]|bigint} address
|
|
57
65
|
* @returns number[]
|
|
58
66
|
*/
|
|
59
67
|
export function encodeIPv4(address) {
|
|
@@ -231,7 +239,10 @@ export function rangeIP(address, prefix, lowerAdd = 0, upperReduce = 0) {
|
|
|
231
239
|
|
|
232
240
|
export function matchPrefixIP(prefix, length, address) {
|
|
233
241
|
const family = _family(address);
|
|
234
|
-
return
|
|
242
|
+
return (
|
|
243
|
+
family !== undefined &&
|
|
244
|
+
_prefix(family, address, length) === _prefix(family, prefix, length)
|
|
245
|
+
);
|
|
235
246
|
}
|
|
236
247
|
|
|
237
248
|
export function normalizeCIDR(address) {
|
|
@@ -334,11 +345,11 @@ export function hasWellKnownSubnet(address) {
|
|
|
334
345
|
/*
|
|
335
346
|
* https://www.iana.org/assignments/ipv6-multicast-addresses/ipv6-multicast-addresses.xhtml
|
|
336
347
|
*/
|
|
337
|
-
export const IPV6_NODE_LOCAL_ALL_NODES
|
|
338
|
-
export const IPV6_NODE_LOCAL_ALL_ROUTERS
|
|
339
|
-
export const IPV6_LINK_LOCAL_ALL_NODES
|
|
340
|
-
export const IPV6_LINK_LOCAL_ALL_ROUTERS
|
|
341
|
-
export const IPV6_SITE_LOCAL_ALL_ROUTERS
|
|
348
|
+
export const IPV6_NODE_LOCAL_ALL_NODES = _encode(ipv6, "ff01::1");
|
|
349
|
+
export const IPV6_NODE_LOCAL_ALL_ROUTERS = _encode(ipv6, "ff01::2");
|
|
350
|
+
export const IPV6_LINK_LOCAL_ALL_NODES = _encode(ipv6, "ff02::1");
|
|
351
|
+
export const IPV6_LINK_LOCAL_ALL_ROUTERS = _encode(ipv6, "ff02::2");
|
|
352
|
+
export const IPV6_SITE_LOCAL_ALL_ROUTERS = _encode(ipv6, "ff05::2");
|
|
342
353
|
export const IPV6_SITE_LOCAL_ALL_DHCP_SERVERS = _encode(ipv6, "ff05::1:3");
|
|
343
354
|
|
|
344
355
|
export const IPV4_LOCALHOST = _encode(ipv4, "127.0.0.1");
|
package/types/ip.d.mts
CHANGED
|
@@ -44,6 +44,14 @@ export function isLocalhost(address: any): boolean;
|
|
|
44
44
|
export function isLinkLocal(address: any): boolean;
|
|
45
45
|
export function isUniqueLocal(address: any): boolean;
|
|
46
46
|
export function hasWellKnownSubnet(address: any): boolean;
|
|
47
|
+
/**
|
|
48
|
+
* Address familiy IPv4
|
|
49
|
+
*/
|
|
50
|
+
export const FAMILY_IPV4: "IPv4";
|
|
51
|
+
/**
|
|
52
|
+
* Address familiy IPv6
|
|
53
|
+
*/
|
|
54
|
+
export const FAMILY_IPV6: "IPv6";
|
|
47
55
|
export const IPV6_NODE_LOCAL_ALL_NODES: any;
|
|
48
56
|
export const IPV6_NODE_LOCAL_ALL_ROUTERS: any;
|
|
49
57
|
export const IPV6_LINK_LOCAL_ALL_NODES: any;
|