ip-utilties 3.7.4 → 3.8.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 CHANGED
@@ -11,9 +11,9 @@
11
11
 
12
12
  ip-v4/v6 utility functions
13
13
 
14
- - encoding / decoding
15
- - well known subnets
16
-
14
+ * encoding / decoding
15
+ * well known subnets
16
+
17
17
  ## Three different representations
18
18
 
19
19
  ```javascript
@@ -27,6 +27,81 @@ 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
+ * [IPV6\_NODE\_LOCAL\_ALL\_NODES](#ipv6_node_local_all_nodes)
45
+
46
+ ## FAMILY\_IPV4
47
+
48
+ Address familiy IPv4
49
+
50
+ Type: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)
51
+
52
+ ## FAMILY\_IPV6
53
+
54
+ Address familiy IPv6
55
+
56
+ Type: [string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)
57
+
58
+ ## encodeIP
59
+
60
+ Encode ipv4 or ipv6 address into number array.
61
+
62
+ ### Parameters
63
+
64
+ * `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)>)**&#x20;
65
+
66
+ Returns **any** number\[]
67
+
68
+ ## encodeIPv6
69
+
70
+ Encode ipv6 address into number array.
71
+
72
+ ### Parameters
73
+
74
+ * `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)**&#x20;
75
+
76
+ Returns **any** number\[]
77
+
78
+ ## encodeIPv4
79
+
80
+ Encode ipv4 address into number array.
81
+
82
+ ### Parameters
83
+
84
+ * `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)**&#x20;
85
+
86
+ Returns **any** number\[]
87
+
88
+ ## familyIP
89
+
90
+ IP address family for a given address.
91
+
92
+ ### Parameters
93
+
94
+ * `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))**&#x20;
95
+
96
+ 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))**&#x20;
97
+
98
+ ## IPV6\_NODE\_LOCAL\_ALL\_NODES
99
+
100
+ prefix global subnet interface
101
+ ff01:: ff02:: 1
102
+ ff05:: 2 3
103
+ <https://www.iana.org/assignments/ipv6-multicast-addresses/ipv6-multicast-addresses.xhtml>
104
+
30
105
  # install
31
106
 
32
107
  With [npm](http://npmjs.org) do:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ip-utilties",
3
- "version": "3.7.4",
3
+ "version": "3.8.0",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "provenance": true
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
- // mask length, prefix, link local,
26
- [new Uint8Array([169, 254, 0, 0]), /* */ 16, 16, true], // Link-local address (Autoconfiguration)
27
-
28
- [new Uint8Array([0, 0, 0, 0]), /* */ 8, 8], // This network
29
- [new Uint8Array([10, 0, 0, 0]), /* */ 8, 8], // Private network (RFC 1918)
30
- [new Uint8Array([100, 64, 0, 0]), /* */ 10, 10], // Carrier-grade NAT / Shared address space (CGN)
31
- [new Uint8Array([127, 0, 0, 0]), /* */ 8, 8], // Loopback
32
- [new Uint8Array([172, 16, 0, 0]), /* */ 12, 12], // Private network (RFC 1918)
33
- [new Uint8Array([192, 0, 0, 0]), /* */ 24, 24], // IETF protocol assignments
34
- [new Uint8Array([192, 0, 2, 0]), /* */ 24, 24], // TEST-NET-1
35
- [new Uint8Array([192, 168, 0, 0]), /* */ 16, 16], // Private network (RFC 1918)
36
- [new Uint8Array([198, 18, 0, 0]), /* */ 15, 15], // Network benchmark testing
37
- [new Uint8Array([198, 51, 100, 0]), /* */ 24, 24], // TEST-NET-2
38
- [new Uint8Array([203, 0, 113, 0]), /* */ 24, 24], // Reserved address space used for documentation
39
- [new Uint8Array([240, 0, 0, 0]), /* */ 4, 4], // Reserved for future use or experimental purposes
40
-
41
- [new Uint8Array([127, 0, 53, 53]), /* */ 0, 0], // Name collision occurrence
42
- [new Uint8Array([255, 255, 255, 255]), /**/ 0, 0] // Limited Broadcast address
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, true],
67
- [new Uint16Array([0xfd00, 0, 0, 0, 0, 0, 0, 0]), 8, 64, false, true],
68
- [new Uint16Array([0xfc00, 0, 0, 0, 0, 0, 0, 0]), 7, 64, false, true],
69
- [new Uint16Array([0, 0, 0, 0, 0, 0, 0, 1]), 128, 128, false, true]
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
- if (wns) {
300
- prefixLength = prefixLength === undefined ? wns[2] : parseInt(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,20 @@ export function isLinkLocal(address) {
387
390
  }
388
391
 
389
392
  export function _isLinkLocal(family, address) {
390
- return _wellKnownSubnet(family, address) === family.wellKnownAddresses[0];
393
+ return _wellKnownSubnet(family, address)?.[3] === 1;
394
+ }
395
+
396
+
397
+ export function isLoopback(address) {
398
+ const family = _family(address);
399
+ if (family) {
400
+ return _isLoopback(family, _encode(family, address));
401
+ }
402
+ return false;
403
+ }
404
+
405
+ export function _isLoopback(family, address) {
406
+ return _wellKnownSubnet(family, address)?.[3] === 2;
391
407
  }
392
408
 
393
409
  export function isUniqueLocal(address) {
@@ -414,38 +430,15 @@ export function _wellKnownSubnet(family, encoded) {
414
430
  for (const c of family.wellKnownAddresses) {
415
431
  const pl = c[1];
416
432
  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
433
  return c;
426
434
  }
427
435
  }
428
436
  }
429
437
 
430
- /*
438
+ /**
431
439
  prefix global subnet interface
432
440
  ff01:: ff02:: 1
433
441
  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
442
  * https://www.iana.org/assignments/ipv6-multicast-addresses/ipv6-multicast-addresses.xhtml
450
443
  */
451
444
  export const IPV6_NODE_LOCAL_ALL_NODES = _encode(ipv6, "ff01::1");
package/types/ip.d.mts CHANGED
@@ -54,11 +54,19 @@ export declare function _isLocalhost(family: any, encoded: any): boolean;
54
54
  export declare function isLocalhost(address: any): boolean;
55
55
  export declare function isLinkLocal(address: any): boolean;
56
56
  export declare function _isLinkLocal(family: any, address: any): boolean;
57
+ export declare function isLoopback(address: any): boolean;
58
+ export declare function _isLoopback(family: any, address: any): boolean;
57
59
  export declare function isUniqueLocal(address: any): boolean;
58
60
  export declare function _isUniqueLocal(eaddr: any): boolean;
59
61
  export declare function hasWellKnownSubnet(address: any): boolean;
60
62
  export declare function wellKnownSubnet(address: any): any;
61
63
  export declare function _wellKnownSubnet(family: any, encoded: any): any;
64
+ /**
65
+ prefix global subnet interface
66
+ ff01:: ff02:: 1
67
+ ff05:: 2 3
68
+ * https://www.iana.org/assignments/ipv6-multicast-addresses/ipv6-multicast-addresses.xhtml
69
+ */
62
70
  export declare const IPV6_NODE_LOCAL_ALL_NODES: any;
63
71
  export declare const IPV6_NODE_LOCAL_ALL_ROUTERS: any;
64
72
  export declare const IPV6_LINK_LOCAL_ALL_NODES: any;