ip-utilties 3.7.4 → 3.7.5
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 +70 -3
- package/package.json +1 -1
- package/src/ip.mjs +36 -56
- package/types/ip.d.mts +6 -0
package/README.md
CHANGED
|
@@ -11,9 +11,9 @@
|
|
|
11
11
|
|
|
12
12
|
ip-v4/v6 utility functions
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
* encoding / decoding
|
|
15
|
+
* well known subnets
|
|
16
|
+
|
|
17
17
|
## Three different representations
|
|
18
18
|
|
|
19
19
|
```javascript
|
|
@@ -27,6 +27,73 @@ encodeIPv4("10.0.0.1") // -> 10n << 24n | 1n;
|
|
|
27
27
|
|
|
28
28
|
# API
|
|
29
29
|
|
|
30
|
+
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
|
|
31
|
+
|
|
32
|
+
### Table of Contents
|
|
33
|
+
|
|
34
|
+
* [FAMILY\_IPV4](#family_ipv4)
|
|
35
|
+
* [FAMILY\_IPV6](#family_ipv6)
|
|
36
|
+
* [encodeIP](#encodeip)
|
|
37
|
+
* [Parameters](#parameters)
|
|
38
|
+
* [encodeIPv6](#encodeipv6)
|
|
39
|
+
* [Parameters](#parameters-1)
|
|
40
|
+
* [encodeIPv4](#encodeipv4)
|
|
41
|
+
* [Parameters](#parameters-2)
|
|
42
|
+
* [familyIP](#familyip)
|
|
43
|
+
* [Parameters](#parameters-3)
|
|
44
|
+
|
|
45
|
+
## FAMILY\_IPV4
|
|
46
|
+
|
|
47
|
+
Address familiy IPv4
|
|
48
|
+
|
|
49
|
+
Type: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)
|
|
50
|
+
|
|
51
|
+
## FAMILY\_IPV6
|
|
52
|
+
|
|
53
|
+
Address familiy IPv6
|
|
54
|
+
|
|
55
|
+
Type: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)
|
|
56
|
+
|
|
57
|
+
## encodeIP
|
|
58
|
+
|
|
59
|
+
Encode ipv4 or ipv6 address into number array.
|
|
60
|
+
|
|
61
|
+
### Parameters
|
|
62
|
+
|
|
63
|
+
* `address` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) | [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)>)** 
|
|
64
|
+
|
|
65
|
+
Returns **any** number\[]
|
|
66
|
+
|
|
67
|
+
## encodeIPv6
|
|
68
|
+
|
|
69
|
+
Encode ipv6 address into number array.
|
|
70
|
+
|
|
71
|
+
### Parameters
|
|
72
|
+
|
|
73
|
+
* `address` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) | [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)> | bigint)** 
|
|
74
|
+
|
|
75
|
+
Returns **any** number\[]
|
|
76
|
+
|
|
77
|
+
## encodeIPv4
|
|
78
|
+
|
|
79
|
+
Encode ipv4 address into number array.
|
|
80
|
+
|
|
81
|
+
### Parameters
|
|
82
|
+
|
|
83
|
+
* `address` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) | [Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)> | bigint)** 
|
|
84
|
+
|
|
85
|
+
Returns **any** number\[]
|
|
86
|
+
|
|
87
|
+
## familyIP
|
|
88
|
+
|
|
89
|
+
IP address family for a given address.
|
|
90
|
+
|
|
91
|
+
### Parameters
|
|
92
|
+
|
|
93
|
+
* `address` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) | [Uint8Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) | [Uint16Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Uint16Array))** 
|
|
94
|
+
|
|
95
|
+
Returns **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) | [undefined](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/undefined))** 
|
|
96
|
+
|
|
30
97
|
# install
|
|
31
98
|
|
|
32
99
|
With [npm](http://npmjs.org) do:
|
package/package.json
CHANGED
package/src/ip.mjs
CHANGED
|
@@ -22,24 +22,31 @@ const ipv4 = {
|
|
|
22
22
|
|
|
23
23
|
// https://www.geeksforgeeks.org/computer-networks/subnet-cheat-sheet/
|
|
24
24
|
wellKnownAddresses: [
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
[new Uint8Array([
|
|
34
|
-
|
|
35
|
-
[new Uint8Array([
|
|
36
|
-
[new Uint8Array([
|
|
37
|
-
[new Uint8Array([
|
|
38
|
-
[new Uint8Array([
|
|
39
|
-
[new Uint8Array([
|
|
40
|
-
|
|
41
|
-
[new Uint8Array([
|
|
42
|
-
[new Uint8Array([
|
|
25
|
+
/*
|
|
26
|
+
mask
|
|
27
|
+
identifying prefix length
|
|
28
|
+
prefix length
|
|
29
|
+
link local = 1 https://en.wikipedia.org/wiki/Link-local_address
|
|
30
|
+
loopback / localhost = 2
|
|
31
|
+
unique local = 3 https://en.wikipedia.org/wiki/Unique_local_address
|
|
32
|
+
*/
|
|
33
|
+
[new Uint8Array([169, 254, 0, 0]), /* */ 16, 16, 1], // Link-local address (Autoconfiguration)
|
|
34
|
+
|
|
35
|
+
[new Uint8Array([0, 0, 0, 0]), /* */ 8, 8, 0], // This network
|
|
36
|
+
[new Uint8Array([10, 0, 0, 0]), /* */ 8, 8, 0], // Private network (RFC 1918)
|
|
37
|
+
[new Uint8Array([100, 64, 0, 0]), /* */ 10, 10, 0], // Carrier-grade NAT / Shared address space (CGN)
|
|
38
|
+
[new Uint8Array([127, 0, 0, 0]), /* */ 8, 8, 2], // Loopback
|
|
39
|
+
[new Uint8Array([172, 16, 0, 0]), /* */ 12, 12, 0], // Private network (RFC 1918)
|
|
40
|
+
[new Uint8Array([192, 0, 0, 0]), /* */ 24, 24, 0], // IETF protocol assignments
|
|
41
|
+
[new Uint8Array([192, 0, 2, 0]), /* */ 24, 24, 0], // TEST-NET-1
|
|
42
|
+
[new Uint8Array([192, 168, 0, 0]), /* */ 16, 16, 0], // Private network (RFC 1918)
|
|
43
|
+
[new Uint8Array([198, 18, 0, 0]), /* */ 15, 15, 0], // Network benchmark testing
|
|
44
|
+
[new Uint8Array([198, 51, 100, 0]), /* */ 24, 24, 0], // TEST-NET-2
|
|
45
|
+
[new Uint8Array([203, 0, 113, 0]), /* */ 24, 24, 0], // Reserved address space used for documentation
|
|
46
|
+
[new Uint8Array([240, 0, 0, 0]), /* */ 4, 4, 0], // Reserved for future use or experimental purposes
|
|
47
|
+
|
|
48
|
+
[new Uint8Array([127, 0, 53, 53]), /* */ 0, 0, 0], // Name collision occurrence
|
|
49
|
+
[new Uint8Array([255, 255, 255, 255]), /**/ 0, 0, 0] // Limited Broadcast address
|
|
43
50
|
]
|
|
44
51
|
};
|
|
45
52
|
|
|
@@ -63,10 +70,10 @@ const ipv6 = {
|
|
|
63
70
|
base: 16,
|
|
64
71
|
localHost: new Uint16Array([0, 0, 0, 0, 0, 0, 0, 1]),
|
|
65
72
|
wellKnownAddresses: [
|
|
66
|
-
[new Uint16Array([0xfe80, 0, 0, 0, 0, 0, 0, 0]), 64, 64,
|
|
67
|
-
[new Uint16Array([0xfd00, 0, 0, 0, 0, 0, 0, 0]), 8, 64,
|
|
68
|
-
[new Uint16Array([0xfc00, 0, 0, 0, 0, 0, 0, 0]), 7, 64,
|
|
69
|
-
[new Uint16Array([0, 0, 0, 0, 0, 0, 0, 1]), 128, 128,
|
|
73
|
+
[new Uint16Array([0xfe80, 0, 0, 0, 0, 0, 0, 0]), /**/ 64, 64, 1],
|
|
74
|
+
[new Uint16Array([0xfd00, 0, 0, 0, 0, 0, 0, 0]), /* */ 8, 64, 3],
|
|
75
|
+
[new Uint16Array([0xfc00, 0, 0, 0, 0, 0, 0, 0]), /* */ 7, 64, 3],
|
|
76
|
+
[new Uint16Array([0, 0, 0, 0, 0, 0, 0, 1]), /*. */ 128, 128, 2]
|
|
70
77
|
]
|
|
71
78
|
};
|
|
72
79
|
|
|
@@ -290,33 +297,29 @@ export function matchPrefixIP(prefix, length, address) {
|
|
|
290
297
|
|
|
291
298
|
export function normalizeCIDR(address) {
|
|
292
299
|
let [prefix, prefixLength] = address.split(/\//);
|
|
293
|
-
let longPrefix;
|
|
294
300
|
|
|
295
301
|
const family = isIPv6(prefix) ? ipv6 : ipv4;
|
|
296
302
|
let encoded = _encode(family, prefix);
|
|
297
303
|
const wns = _wellKnownSubnet(family, encoded);
|
|
298
304
|
|
|
299
|
-
|
|
300
|
-
prefixLength
|
|
305
|
+
prefixLength =
|
|
306
|
+
prefixLength === undefined ? (wns?.[2] ?? 0) : parseInt(prefixLength);
|
|
301
307
|
|
|
308
|
+
if (wns) {
|
|
302
309
|
prefix = _decode(
|
|
303
310
|
family,
|
|
304
311
|
_prefix(family, encoded, prefixLength),
|
|
305
|
-
prefixLength
|
|
312
|
+
family === ipv6 ? 128 : prefixLength
|
|
306
313
|
);
|
|
307
|
-
longPrefix = _decode(family, encoded);
|
|
308
314
|
} else {
|
|
309
|
-
prefixLength = prefixLength === undefined ? 0 : parseInt(prefixLength);
|
|
310
|
-
|
|
311
315
|
if (prefixLength) {
|
|
312
316
|
encoded = _prefix(family, prefix, prefixLength);
|
|
313
317
|
}
|
|
314
318
|
prefix = _decode(family, encoded, prefixLength);
|
|
315
|
-
longPrefix = _decode(family, encoded);
|
|
316
319
|
}
|
|
317
320
|
|
|
318
321
|
return {
|
|
319
|
-
longPrefix,
|
|
322
|
+
longPrefix: _decode(family, encoded),
|
|
320
323
|
prefix,
|
|
321
324
|
prefixLength,
|
|
322
325
|
cidr: `${prefix}/${prefixLength}`,
|
|
@@ -387,7 +390,7 @@ export function isLinkLocal(address) {
|
|
|
387
390
|
}
|
|
388
391
|
|
|
389
392
|
export function _isLinkLocal(family, address) {
|
|
390
|
-
return _wellKnownSubnet(family, address) ===
|
|
393
|
+
return _wellKnownSubnet(family, address)?.[3] === 1;
|
|
391
394
|
}
|
|
392
395
|
|
|
393
396
|
export function isUniqueLocal(address) {
|
|
@@ -414,38 +417,15 @@ export function _wellKnownSubnet(family, encoded) {
|
|
|
414
417
|
for (const c of family.wellKnownAddresses) {
|
|
415
418
|
const pl = c[1];
|
|
416
419
|
if (pl > 0 && _prefix(family, c[0], pl) === _prefix(family, encoded, pl)) {
|
|
417
|
-
/*console.log(
|
|
418
|
-
c,
|
|
419
|
-
encoded,
|
|
420
|
-
_prefix(family, c[0], pl),
|
|
421
|
-
_prefix(family, encoded, pl),
|
|
422
|
-
prefixIP(c[0], pl),
|
|
423
|
-
prefixIP(encoded, pl)
|
|
424
|
-
);*/
|
|
425
420
|
return c;
|
|
426
421
|
}
|
|
427
422
|
}
|
|
428
423
|
}
|
|
429
424
|
|
|
430
|
-
|
|
425
|
+
/**
|
|
431
426
|
prefix global subnet interface
|
|
432
427
|
ff01:: ff02:: 1
|
|
433
428
|
ff05:: 2 3
|
|
434
|
-
|
|
435
|
-
https://en.wikipedia.org/wiki/Link-local_address
|
|
436
|
-
fe80:: 64 link local
|
|
437
|
-
169.254. /16 link local
|
|
438
|
-
|
|
439
|
-
https://en.wikipedia.org/wiki/Unique_local_address
|
|
440
|
-
fc00:: 64 unique local
|
|
441
|
-
fd00:: 64 unique local
|
|
442
|
-
|
|
443
|
-
::1 128. local host
|
|
444
|
-
127 8 local host
|
|
445
|
-
172
|
|
446
|
-
*/
|
|
447
|
-
|
|
448
|
-
/*
|
|
449
429
|
* https://www.iana.org/assignments/ipv6-multicast-addresses/ipv6-multicast-addresses.xhtml
|
|
450
430
|
*/
|
|
451
431
|
export const IPV6_NODE_LOCAL_ALL_NODES = _encode(ipv6, "ff01::1");
|
package/types/ip.d.mts
CHANGED
|
@@ -59,6 +59,12 @@ export declare function _isUniqueLocal(eaddr: any): boolean;
|
|
|
59
59
|
export declare function hasWellKnownSubnet(address: any): boolean;
|
|
60
60
|
export declare function wellKnownSubnet(address: any): any;
|
|
61
61
|
export declare function _wellKnownSubnet(family: any, encoded: any): any;
|
|
62
|
+
/**
|
|
63
|
+
prefix global subnet interface
|
|
64
|
+
ff01:: ff02:: 1
|
|
65
|
+
ff05:: 2 3
|
|
66
|
+
* https://www.iana.org/assignments/ipv6-multicast-addresses/ipv6-multicast-addresses.xhtml
|
|
67
|
+
*/
|
|
62
68
|
export declare const IPV6_NODE_LOCAL_ALL_NODES: any;
|
|
63
69
|
export declare const IPV6_NODE_LOCAL_ALL_ROUTERS: any;
|
|
64
70
|
export declare const IPV6_LINK_LOCAL_ALL_NODES: any;
|