ip-utilties 3.6.0 → 3.6.2

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.
Files changed (3) hide show
  1. package/README.md +3 -0
  2. package/package.json +4 -3
  3. package/src/ip.mjs +47 -49
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.0",
3
+ "version": "3.6.2",
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
@@ -18,19 +18,18 @@ const ipv4 = {
18
18
  segmentLength: 8,
19
19
  segmentMask: 0xffn,
20
20
  base: 10,
21
- linkLocalPrefix: new Uint8Array([169, 254]),
22
- linkLocalPrefixLength: 16,
23
21
  localHost: new Uint8Array([127, 0, 0, 1]),
24
22
  localHostPrefixLenth: 8,
25
23
 
26
24
  // https://www.geeksforgeeks.org/computer-networks/subnet-cheat-sheet/
27
25
  wellKnownAddresses: [
26
+ [new Uint8Array([169, 254, 0, 0]), 16, true], // Link-local address (Autoconfiguration)
27
+
28
28
  [new Uint8Array([0, 0, 0, 0]), 8], // This network
29
29
  [new Uint8Array([10, 0, 0, 0]), 8], // Private network (RFC 1918)
30
30
  [new Uint8Array([100, 64, 0, 0]), 10], // Carrier-grade NAT / Shared address space (CGN)
31
31
  [new Uint8Array([127, 0, 0, 0]), 8], // Loopback
32
32
  [new Uint8Array([127, 0, 53, 53]), 0], // Name collision occurrence
33
- [new Uint8Array([169, 254, 0, 0]), 16], // Link-local address (Autoconfiguration)
34
33
  [new Uint8Array([172, 16, 0, 0]), 12], // Private network (RFC 1918)
35
34
  [new Uint8Array([192, 0, 0, 0]), 24], // IETF protocol assignments
36
35
  [new Uint8Array([192, 0, 2, 0]), 24], // TEST-NET-1
@@ -61,10 +60,13 @@ const ipv6 = {
61
60
  segmentLength: 16,
62
61
  segmentMask: 0xffffn,
63
62
  base: 16,
64
- linkLocalPrefix: new Uint16Array([0xfe80]),
65
- linkLocalPrefixLength: 64,
66
63
  localHost: new Uint16Array([0, 0, 0, 0, 0, 0, 0, 1]),
67
- localHostPrefixLenth: 128
64
+ localHostPrefixLenth: 128,
65
+ wellKnownAddresses: [
66
+ [new Uint16Array([0xfe80, 0, 0, 0, 0, 0, 0, 0]), 64, true],
67
+ [new Uint16Array([0xfd00, 0, 0, 0, 0, 0, 0, 0]), 8, false, true],
68
+ [new Uint16Array([0, 0, 0, 0, 0, 0, 0, 1]), 128, false, true]
69
+ ]
68
70
  };
69
71
 
70
72
  const families = [ipv4, ipv6];
@@ -285,34 +287,45 @@ export function normalizeCIDR(address) {
285
287
 
286
288
  const family = isIPv6(prefix) ? ipv6 : ipv4;
287
289
 
288
- if (isUniqueLocal(address) || _isLinkLocal(family, address)) {
289
- prefixLength = family.linkLocalPrefixLength;
290
- const n = _prefix(family, address, prefixLength);
291
- prefix = _decode(family, n, prefixLength);
292
-
293
- if (family === ipv6) {
294
- if (!prefix.endsWith("::")) {
295
- // TODO
296
- prefix += prefix.endsWith(":") ? ":" : "::";
297
- }
298
- }
299
- longPrefix = prefix;
290
+ const wns = _wellKnownSubnet(family, address);
291
+ if (family === ipv4 && wns && !prefixLength) {
292
+ prefixLength = wns[1];
293
+ prefix = _decode(
294
+ family,
295
+ _prefix(family, wns[0], prefixLength),
296
+ prefixLength
297
+ );
298
+ longPrefix = wns[0];
300
299
  } else {
301
- prefixLength = prefixLength === undefined ? 0 : parseInt(prefixLength);
300
+ if (isUniqueLocal(address) || _isLinkLocal(family, address)) {
301
+ prefixLength = family.wellKnownAddresses[0][1]; // TODO
302
+ const n = _prefix(family, address, prefixLength);
303
+ prefix = _decode(family, n, prefixLength);
304
+
305
+ if (family === ipv6) {
306
+ if (!prefix.endsWith("::")) {
307
+ // TODO
308
+ prefix += prefix.endsWith(":") ? ":" : "::";
309
+ }
310
+ }
311
+ longPrefix = prefix;
312
+ } else {
313
+ prefixLength = prefixLength === undefined ? 0 : parseInt(prefixLength);
302
314
 
303
- let n;
315
+ let n;
304
316
 
305
- if (prefixLength) {
306
- n = _prefix(family, prefix, prefixLength);
307
- } else {
308
- n = _encode(family, prefix);
317
+ if (prefixLength) {
318
+ n = _prefix(family, prefix, prefixLength);
319
+ } else {
320
+ n = _encode(family, prefix);
309
321
 
310
- if (isLocalhost(n)) {
311
- prefixLength = family.localHostPrefixLenth;
322
+ if (isLocalhost(n)) {
323
+ prefixLength = family.localHostPrefixLenth;
324
+ }
312
325
  }
326
+ prefix = _decode(family, n, prefixLength);
327
+ longPrefix = _decode(family, n);
313
328
  }
314
- prefix = _decode(family, n, prefixLength);
315
- longPrefix = _decode(family, n);
316
329
  }
317
330
 
318
331
  return {
@@ -382,7 +395,7 @@ export function isLinkLocal(address) {
382
395
  }
383
396
 
384
397
  export function _isLinkLocal(family, address) {
385
- return _equal(family.linkLocalPrefix, _encode(family, address));
398
+ return _wellKnownSubnet(family, address) === family.wellKnownAddresses[0];
386
399
  }
387
400
 
388
401
  export function isUniqueLocal(address) {
@@ -408,24 +421,10 @@ export function wellKnownSubnet(address) {
408
421
  export function _wellKnownSubnet(family, address) {
409
422
  const encoded = _encode(family, address);
410
423
 
411
- if (_equal(family.localHost, encoded)) {
412
- return [family.localHost, family.localHostPrefixLenth];
413
- }
414
- if (_equal(family.linkLocalPrefix, encoded)) {
415
- return [family.linkLocalPrefix, family.linkLocalPrefixLength];
416
- }
417
- if (_isUniqueLocal(encoded)) {
418
- return [encoded[0], 64];
419
- }
420
-
421
- if (family === ipv4) {
422
- for (const c of family.wellKnownAddresses) {
423
- const pl = c[1];
424
- if (
425
- pl > 0 &&
426
- _prefix(family, c[0], pl) === _prefix(family, encoded, pl)
427
- ) {
428
- /*console.log(
424
+ for (const c of family.wellKnownAddresses) {
425
+ const pl = c[1];
426
+ if (pl > 0 && _prefix(family, c[0], pl) === _prefix(family, encoded, pl)) {
427
+ /*console.log(
429
428
  c,
430
429
  encoded,
431
430
  _prefix(family, c[0], pl),
@@ -433,8 +432,7 @@ export function _wellKnownSubnet(family, address) {
433
432
  prefixIP(c[0], pl),
434
433
  prefixIP(encoded, pl)
435
434
  );*/
436
- return c;
437
- }
435
+ return c;
438
436
  }
439
437
  }
440
438
  }