ip-utilties 3.6.1 → 3.7.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,6 +11,9 @@
11
11
 
12
12
  ip-v4/v6 utility functions
13
13
 
14
+ - encoding / decoding
15
+ - well known subnets
16
+
14
17
  ## Three different representations
15
18
 
16
19
  ```javascript
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ip-utilties",
3
- "version": "3.6.1",
3
+ "version": "3.7.0",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "provenance": true
@@ -13,6 +13,7 @@
13
13
  },
14
14
  "description": "ip v4/v6 utility functions",
15
15
  "keywords": [
16
+ "address",
16
17
  "cidr",
17
18
  "ip",
18
19
  "ipv4",
@@ -41,10 +42,10 @@
41
42
  },
42
43
  "devDependencies": {
43
44
  "ava": "^8.0.1",
44
- "browser-ava": "^2.3.66",
45
+ "browser-ava": "^2.3.67",
45
46
  "c8": "^11.0.0",
46
47
  "documentation": "^14.0.3",
47
- "semantic-release": "^25.0.6",
48
+ "semantic-release": "^25.0.7",
48
49
  "typescript": "^7.0.2"
49
50
  },
50
51
  "overrides": {
package/src/ip.mjs CHANGED
@@ -23,22 +23,24 @@ const ipv4 = {
23
23
 
24
24
  // https://www.geeksforgeeks.org/computer-networks/subnet-cheat-sheet/
25
25
  wellKnownAddresses: [
26
- [new Uint8Array([169, 254, 0, 0]), 16], // Link-local address (Autoconfiguration)
27
-
28
- [new Uint8Array([0, 0, 0, 0]), 8], // This network
29
- [new Uint8Array([10, 0, 0, 0]), 8], // Private network (RFC 1918)
30
- [new Uint8Array([100, 64, 0, 0]), 10], // Carrier-grade NAT / Shared address space (CGN)
31
- [new Uint8Array([127, 0, 0, 0]), 8], // Loopback
32
- [new Uint8Array([127, 0, 53, 53]), 0], // Name collision occurrence
33
- [new Uint8Array([172, 16, 0, 0]), 12], // Private network (RFC 1918)
34
- [new Uint8Array([192, 0, 0, 0]), 24], // IETF protocol assignments
35
- [new Uint8Array([192, 0, 2, 0]), 24], // TEST-NET-1
36
- [new Uint8Array([192, 168, 0, 0]), 16], // Private network (RFC 1918)
37
- [new Uint8Array([198, 18, 0, 0]), 15], // Network benchmark testing
38
- [new Uint8Array([198, 51, 100, 0]), 24], // TEST-NET-2
39
- [new Uint8Array([203, 0, 113, 0]), 24], // Reserved address space used for documentation
40
- [new Uint8Array([240, 0, 0, 0]), 4], // Reserved for future use or experimental purposes
41
- [new Uint8Array([255, 255, 255, 255]), 0] // Limited Broadcast address
26
+ // mask length, prefix, link local,
27
+ [new Uint8Array([169, 254, 0, 0]), /* */ 16, 16, true], // Link-local address (Autoconfiguration)
28
+
29
+ [new Uint8Array([0, 0, 0, 0]), /* */ 8, 8], // This network
30
+ [new Uint8Array([10, 0, 0, 0]), /* */ 8, 8], // Private network (RFC 1918)
31
+ [new Uint8Array([100, 64, 0, 0]), /* */ 10, 10], // Carrier-grade NAT / Shared address space (CGN)
32
+ [new Uint8Array([127, 0, 0, 0]), /* */ 8, 8], // Loopback
33
+ [new Uint8Array([172, 16, 0, 0]), /* */ 12, 12], // Private network (RFC 1918)
34
+ [new Uint8Array([192, 0, 0, 0]), /* */ 24, 24], // IETF protocol assignments
35
+ [new Uint8Array([192, 0, 2, 0]), /* */ 24, 24], // TEST-NET-1
36
+ [new Uint8Array([192, 168, 0, 0]), /* */ 16, 16], // Private network (RFC 1918)
37
+ [new Uint8Array([198, 18, 0, 0]), /* */ 15, 15], // Network benchmark testing
38
+ [new Uint8Array([198, 51, 100, 0]), /* */ 24, 24], // TEST-NET-2
39
+ [new Uint8Array([203, 0, 113, 0]), /* */ 24, 24], // Reserved address space used for documentation
40
+ [new Uint8Array([240, 0, 0, 0]), /* */ 4, 4], // Reserved for future use or experimental purposes
41
+
42
+ [new Uint8Array([127, 0, 53, 53]), /* */ 0, 0], // Name collision occurrence
43
+ [new Uint8Array([255, 255, 255, 255]), /**/ 0, 0] // Limited Broadcast address
42
44
  ]
43
45
  };
44
46
 
@@ -62,7 +64,12 @@ const ipv6 = {
62
64
  base: 16,
63
65
  localHost: new Uint16Array([0, 0, 0, 0, 0, 0, 0, 1]),
64
66
  localHostPrefixLenth: 128,
65
- wellKnownAddresses: [[new Uint16Array([0xfe80, 0, 0, 0, 0, 0, 0, 0]), 64]]
67
+ wellKnownAddresses: [
68
+ [new Uint16Array([0xfe80, 0, 0, 0, 0, 0, 0, 0]), 64, 64, true],
69
+ [new Uint16Array([0xfd00, 0, 0, 0, 0, 0, 0, 0]), 8, 64, false, true],
70
+ [new Uint16Array([0xfc00, 0, 0, 0, 0, 0, 0, 0]), 7, 64, false, true],
71
+ [new Uint16Array([0, 0, 0, 0, 0, 0, 0, 1]), 128, 128, false, true]
72
+ ]
66
73
  };
67
74
 
68
75
  const families = [ipv4, ipv6];
@@ -282,18 +289,19 @@ export function normalizeCIDR(address) {
282
289
  let longPrefix;
283
290
 
284
291
  const family = isIPv6(prefix) ? ipv6 : ipv4;
292
+ let encoded = _encode(family, prefix);
293
+ const wns = _wellKnownSubnet(family, encoded);
285
294
 
286
- const wns = _wellKnownSubnet(family, address);
287
- if (family === ipv4 && wns && !prefixLength) {
288
- prefixLength = wns[1];
295
+ if (wns && !prefixLength) {
296
+ prefixLength = wns[2];
289
297
  prefix = _decode(
290
298
  family,
291
- _prefix(family, wns[0], prefixLength),
299
+ _prefix(family, encoded, prefixLength),
292
300
  prefixLength
293
301
  );
294
- longPrefix = wns[0];
302
+ longPrefix = _decode(family, wns[0]);
295
303
  } else {
296
- if (isUniqueLocal(address) || _isLinkLocal(family, address)) {
304
+ if (_isUniqueLocal(encoded) || _isLinkLocal(family, encoded)) {
297
305
  prefixLength = family.wellKnownAddresses[0][1]; // TODO
298
306
  const n = _prefix(family, address, prefixLength);
299
307
  prefix = _decode(family, n, prefixLength);
@@ -308,19 +316,15 @@ export function normalizeCIDR(address) {
308
316
  } else {
309
317
  prefixLength = prefixLength === undefined ? 0 : parseInt(prefixLength);
310
318
 
311
- let n;
312
-
313
319
  if (prefixLength) {
314
- n = _prefix(family, prefix, prefixLength);
320
+ encoded = _prefix(family, prefix, prefixLength);
315
321
  } else {
316
- n = _encode(family, prefix);
317
-
318
- if (isLocalhost(n)) {
322
+ if (_isLocalhost(family, encoded)) {
319
323
  prefixLength = family.localHostPrefixLenth;
320
324
  }
321
325
  }
322
- prefix = _decode(family, n, prefixLength);
323
- longPrefix = _decode(family, n);
326
+ prefix = _decode(family, encoded, prefixLength);
327
+ longPrefix = _decode(family, encoded);
324
328
  }
325
329
  }
326
330
 
@@ -374,10 +378,14 @@ function _equal(a, b) {
374
378
  return true;
375
379
  }
376
380
 
381
+ export function _isLocalhost(family, encoded) {
382
+ return _equal(family.localHost, encoded);
383
+ }
384
+
377
385
  export function isLocalhost(address) {
378
386
  const family = _family(address);
379
387
  if (family) {
380
- return _equal(family.localHost, _encode(family, address));
388
+ return _isLocalhost(family, _encode(family, address));
381
389
  }
382
390
  return false;
383
391
  }
@@ -385,7 +393,7 @@ export function isLocalhost(address) {
385
393
  export function isLinkLocal(address) {
386
394
  const family = _family(address);
387
395
  if (family) {
388
- return _isLinkLocal(family, address);
396
+ return _isLinkLocal(family, _encode(family, address));
389
397
  }
390
398
  return false;
391
399
  }
@@ -410,21 +418,11 @@ export function hasWellKnownSubnet(address) {
410
418
  export function wellKnownSubnet(address) {
411
419
  const family = _family(address);
412
420
  if (family) {
413
- return _wellKnownSubnet(family, address);
421
+ return _wellKnownSubnet(family, _encode(family, address));
414
422
  }
415
423
  }
416
424
 
417
- export function _wellKnownSubnet(family, address) {
418
- const encoded = _encode(family, address);
419
-
420
- if (_equal(family.localHost, encoded)) {
421
- return [family.localHost, family.localHostPrefixLenth];
422
- }
423
-
424
- if (_isUniqueLocal(encoded)) {
425
- return [encoded[0], 64];
426
- }
427
-
425
+ export function _wellKnownSubnet(family, encoded) {
428
426
  for (const c of family.wellKnownAddresses) {
429
427
  const pl = c[1];
430
428
  if (pl > 0 && _prefix(family, c[0], pl) === _prefix(family, encoded, pl)) {
@@ -443,7 +441,6 @@ export function _wellKnownSubnet(family, address) {
443
441
 
444
442
  /*
445
443
  prefix global subnet interface
446
-
447
444
  ff01:: ff02:: 1
448
445
  ff05:: 2 3
449
446
 
package/types/ip.d.mts CHANGED
@@ -49,6 +49,7 @@ export declare function normalizeCIDR(address: any): {
49
49
  export declare function formatCIDR(address: any, prefixLength: any): any;
50
50
  export declare function normalizeIP(address: any): string | undefined;
51
51
  export declare function reverseArpa(address: any): string;
52
+ export declare function _isLocalhost(family: any, encoded: any): boolean;
52
53
  export declare function isLocalhost(address: any): boolean;
53
54
  export declare function isLinkLocal(address: any): boolean;
54
55
  export declare function _isLinkLocal(family: any, address: any): boolean;
@@ -56,7 +57,7 @@ export declare function isUniqueLocal(address: any): boolean;
56
57
  export declare function _isUniqueLocal(eaddr: any): boolean;
57
58
  export declare function hasWellKnownSubnet(address: any): boolean;
58
59
  export declare function wellKnownSubnet(address: any): any;
59
- export declare function _wellKnownSubnet(family: any, address: any): any;
60
+ export declare function _wellKnownSubnet(family: any, encoded: any): any;
60
61
  export declare const IPV6_NODE_LOCAL_ALL_NODES: any;
61
62
  export declare const IPV6_NODE_LOCAL_ALL_ROUTERS: any;
62
63
  export declare const IPV6_LINK_LOCAL_ALL_NODES: any;