ip-utilties 3.2.0 → 3.4.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.4.0",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "provenance": true
package/src/ip.mjs CHANGED
@@ -19,7 +19,9 @@ const ipv4 = {
19
19
  segmentMask: 0xffn,
20
20
  base: 10,
21
21
  linkLocalPrefix: new Uint8Array([169, 254]),
22
- linkLocalPrefixLength: 16
22
+ linkLocalPrefixLength: 16,
23
+ localHost: new Uint8Array([127, 0, 0, 1]),
24
+ localHostPrefixLenth: 8
23
25
  };
24
26
 
25
27
  const ipv6 = {
@@ -41,9 +43,13 @@ const ipv6 = {
41
43
  segmentMask: 0xffffn,
42
44
  base: 16,
43
45
  linkLocalPrefix: new Uint16Array([0xfe80]),
44
- linkLocalPrefixLength: 64
46
+ linkLocalPrefixLength: 64,
47
+ localHost: new Uint16Array([0, 0, 0, 0, 0, 0, 0, 1]),
48
+ localHostPrefixLenth: 128
45
49
  };
46
50
 
51
+ const families = [ipv4, ipv6];
52
+
47
53
  /**
48
54
  * Encode ipv4 or ipv6 address into number array.
49
55
  * @param {string|number[]} address
@@ -64,7 +70,7 @@ export function encodeIPv6(address) {
64
70
  }
65
71
 
66
72
  /**
67
- * Encode ipv6 address into number array.
73
+ * Encode ipv4 address into number array.
68
74
  * @param {string|number[]|bigint} address
69
75
  * @returns number[]
70
76
  */
@@ -177,7 +183,7 @@ export function familyIP(address) {
177
183
  }
178
184
 
179
185
  function _family(address) {
180
- return [ipv4, ipv6].find(d => _is(d, address));
186
+ return families.find(d => _is(d, address));
181
187
  }
182
188
 
183
189
  function _is(family, address) {
@@ -278,7 +284,7 @@ export function normalizeCIDR(address) {
278
284
  n = _encode(family, prefix);
279
285
 
280
286
  if (isLocalhost(n)) {
281
- prefixLength = family === ipv6 ? 128 : 8;
287
+ prefixLength = family.localHostPrefixLenth;
282
288
  }
283
289
  }
284
290
  prefix = _decode(family, n, prefixLength);
@@ -325,35 +331,35 @@ export function reverseArpa(address) {
325
331
  return address.split(".").reverse().join(".") + ".in-addr.arpa";
326
332
  }
327
333
 
328
- export function isLocalhost(address) {
329
- const eaddr = encodeIP(address);
330
-
331
- if (!eaddr) {
332
- return false;
334
+ function _equal(a, b) {
335
+ let i = 0;
336
+ for (const slot of a) {
337
+ if (slot !== b[i++]) {
338
+ return false;
339
+ }
333
340
  }
341
+ return true;
342
+ }
334
343
 
335
- const str = eaddr.toString();
344
+ export function isLocalhost(address) {
345
+ const family = _family(address);
346
+ if (family) {
347
+ return _equal(family.localHost, _encode(family, address));
348
+ }
336
349
 
337
- return str === IPV4_LOCALHOST.toString() || str === IPV6_LOCALHOST.toString();
350
+ return false;
338
351
  }
339
352
 
340
353
  export function isLinkLocal(address) {
341
354
  const family = _family(address);
342
- if (!family) {
343
- return false;
355
+ if (family) {
356
+ return _isLinkLocal(family, address);
344
357
  }
345
- return _isLinkLocal(family, address);
358
+ return false;
346
359
  }
347
360
 
348
361
  export function _isLinkLocal(family, address) {
349
- const eaddr = _encode(family, address);
350
- let i = 0;
351
- for (const slot of family.linkLocalPrefix) {
352
- if (slot !== eaddr[i++]) {
353
- return false;
354
- }
355
- }
356
- return true;
362
+ return _equal(family.linkLocalPrefix, _encode(family, address));
357
363
  }
358
364
 
359
365
  export function isUniqueLocal(address) {
@@ -382,7 +388,6 @@ export function hasWellKnownSubnet(address) {
382
388
  ::1 128. local host
383
389
  127 8 local host
384
390
  172
385
-
386
391
  */
387
392
 
388
393
  /*
@@ -395,5 +400,5 @@ export const IPV6_LINK_LOCAL_ALL_ROUTERS = _encode(ipv6, "ff02::2");
395
400
  export const IPV6_SITE_LOCAL_ALL_ROUTERS = _encode(ipv6, "ff05::2");
396
401
  export const IPV6_SITE_LOCAL_ALL_DHCP_SERVERS = _encode(ipv6, "ff05::1:3");
397
402
 
398
- export const IPV4_LOCALHOST = _encode(ipv4, "127.0.0.1");
399
- export const IPV6_LOCALHOST = _encode(ipv6, "::1");
403
+ export const IPV4_LOCALHOST = ipv4.localHost;
404
+ export const IPV6_LOCALHOST = ipv6.localHost;
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
  */
@@ -59,5 +59,5 @@ export const IPV6_LINK_LOCAL_ALL_NODES: any;
59
59
  export const IPV6_LINK_LOCAL_ALL_ROUTERS: any;
60
60
  export const IPV6_SITE_LOCAL_ALL_ROUTERS: any;
61
61
  export const IPV6_SITE_LOCAL_ALL_DHCP_SERVERS: any;
62
- export const IPV4_LOCALHOST: any;
63
- export const IPV6_LOCALHOST: any;
62
+ export const IPV4_LOCALHOST: Uint8Array<ArrayBuffer>;
63
+ export const IPV6_LOCALHOST: Uint16Array<ArrayBuffer>;