ip-utilties 3.2.0 → 3.3.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ip-utilties",
3
- "version": "3.2.0",
3
+ "version": "3.3.0",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "provenance": true
package/src/ip.mjs CHANGED
@@ -19,7 +19,8 @@ const ipv4 = {
19
19
  segmentMask: 0xffn,
20
20
  base: 10,
21
21
  linkLocalPrefix: new Uint8Array([169, 254]),
22
- linkLocalPrefixLength: 16
22
+ linkLocalPrefixLength: 16,
23
+ localHostPrefixLenth: 8
23
24
  };
24
25
 
25
26
  const ipv6 = {
@@ -41,9 +42,12 @@ const ipv6 = {
41
42
  segmentMask: 0xffffn,
42
43
  base: 16,
43
44
  linkLocalPrefix: new Uint16Array([0xfe80]),
44
- linkLocalPrefixLength: 64
45
+ linkLocalPrefixLength: 64,
46
+ localHostPrefixLenth: 128
45
47
  };
46
48
 
49
+ const families = [ipv4, ipv6];
50
+
47
51
  /**
48
52
  * Encode ipv4 or ipv6 address into number array.
49
53
  * @param {string|number[]} address
@@ -64,7 +68,7 @@ export function encodeIPv6(address) {
64
68
  }
65
69
 
66
70
  /**
67
- * Encode ipv6 address into number array.
71
+ * Encode ipv4 address into number array.
68
72
  * @param {string|number[]|bigint} address
69
73
  * @returns number[]
70
74
  */
@@ -177,7 +181,7 @@ export function familyIP(address) {
177
181
  }
178
182
 
179
183
  function _family(address) {
180
- return [ipv4, ipv6].find(d => _is(d, address));
184
+ return families.find(d => _is(d, address));
181
185
  }
182
186
 
183
187
  function _is(family, address) {
@@ -278,7 +282,7 @@ export function normalizeCIDR(address) {
278
282
  n = _encode(family, prefix);
279
283
 
280
284
  if (isLocalhost(n)) {
281
- prefixLength = family === ipv6 ? 128 : 8;
285
+ prefixLength = family.localHostPrefixLenth;
282
286
  }
283
287
  }
284
288
  prefix = _decode(family, n, prefixLength);
@@ -339,10 +343,10 @@ export function isLocalhost(address) {
339
343
 
340
344
  export function isLinkLocal(address) {
341
345
  const family = _family(address);
342
- if (!family) {
343
- return false;
346
+ if (family) {
347
+ return _isLinkLocal(family, address);
344
348
  }
345
- return _isLinkLocal(family, address);
349
+ return false;
346
350
  }
347
351
 
348
352
  export function _isLinkLocal(family, address) {
package/types/ip.d.mts CHANGED
@@ -11,7 +11,7 @@ export function encodeIP(address: string | number[]): any;
11
11
  */
12
12
  export function encodeIPv6(address: string | number[] | bigint): any;
13
13
  /**
14
- * Encode ipv6 address into number array.
14
+ * Encode ipv4 address into number array.
15
15
  * @param {string|number[]|bigint} address
16
16
  * @returns number[]
17
17
  */