ip-utilties 3.1.4 → 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.1.4",
3
+ "version": "3.3.0",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "provenance": true
package/src/ip.mjs CHANGED
@@ -18,7 +18,9 @@ const ipv4 = {
18
18
  segmentLength: 8,
19
19
  segmentMask: 0xffn,
20
20
  base: 10,
21
- linkLocalPrefixLength: 16
21
+ linkLocalPrefix: new Uint8Array([169, 254]),
22
+ linkLocalPrefixLength: 16,
23
+ localHostPrefixLenth: 8
22
24
  };
23
25
 
24
26
  const ipv6 = {
@@ -39,9 +41,13 @@ const ipv6 = {
39
41
  segmentLength: 16,
40
42
  segmentMask: 0xffffn,
41
43
  base: 16,
42
- linkLocalPrefixLength: 64
44
+ linkLocalPrefix: new Uint16Array([0xfe80]),
45
+ linkLocalPrefixLength: 64,
46
+ localHostPrefixLenth: 128
43
47
  };
44
48
 
49
+ const families = [ipv4, ipv6];
50
+
45
51
  /**
46
52
  * Encode ipv4 or ipv6 address into number array.
47
53
  * @param {string|number[]} address
@@ -62,7 +68,7 @@ export function encodeIPv6(address) {
62
68
  }
63
69
 
64
70
  /**
65
- * Encode ipv6 address into number array.
71
+ * Encode ipv4 address into number array.
66
72
  * @param {string|number[]|bigint} address
67
73
  * @returns number[]
68
74
  */
@@ -175,7 +181,7 @@ export function familyIP(address) {
175
181
  }
176
182
 
177
183
  function _family(address) {
178
- return [ipv4, ipv6].find(d => _is(d, address));
184
+ return families.find(d => _is(d, address));
179
185
  }
180
186
 
181
187
  function _is(family, address) {
@@ -253,7 +259,7 @@ export function normalizeCIDR(address) {
253
259
 
254
260
  const family = isIPv6(prefix) ? ipv6 : ipv4;
255
261
 
256
- if (isUniqueLocal(address) || isLinkLocal(address)) {
262
+ if (isUniqueLocal(address) || _isLinkLocal(family, address)) {
257
263
  prefixLength = family.linkLocalPrefixLength;
258
264
  const n = _prefix(family, address, prefixLength);
259
265
  prefix = _decode(family, n, prefixLength);
@@ -276,7 +282,7 @@ export function normalizeCIDR(address) {
276
282
  n = _encode(family, prefix);
277
283
 
278
284
  if (isLocalhost(n)) {
279
- prefixLength = family === ipv6 ? 128 : 8;
285
+ prefixLength = family.localHostPrefixLenth;
280
286
  }
281
287
  }
282
288
  prefix = _decode(family, n, prefixLength);
@@ -336,14 +342,24 @@ export function isLocalhost(address) {
336
342
  }
337
343
 
338
344
  export function isLinkLocal(address) {
339
- const eaddr = encodeIP(address);
340
- if (eaddr) {
341
- return eaddr[0] === 0xfe80 || (eaddr[0] === 169 && eaddr[1] === 254);
345
+ const family = _family(address);
346
+ if (family) {
347
+ return _isLinkLocal(family, address);
342
348
  }
343
-
344
349
  return false;
345
350
  }
346
351
 
352
+ export function _isLinkLocal(family, address) {
353
+ const eaddr = _encode(family, address);
354
+ let i = 0;
355
+ for (const slot of family.linkLocalPrefix) {
356
+ if (slot !== eaddr[i++]) {
357
+ return false;
358
+ }
359
+ }
360
+ return true;
361
+ }
362
+
347
363
  export function isUniqueLocal(address) {
348
364
  const eaddr = encodeIP(address);
349
365
  return eaddr?.[0] >> 9 === 126 ? true : false;
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
  */
@@ -42,6 +42,7 @@ export function normalizeIP(address: any): string | undefined;
42
42
  export function reverseArpa(address: any): string;
43
43
  export function isLocalhost(address: any): boolean;
44
44
  export function isLinkLocal(address: any): boolean;
45
+ export function _isLinkLocal(family: any, address: any): boolean;
45
46
  export function isUniqueLocal(address: any): boolean;
46
47
  export function hasWellKnownSubnet(address: any): boolean;
47
48
  /**