ip-utilties 3.3.0 → 3.5.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,11 +1,11 @@
1
1
  {
2
2
  "name": "ip-utilties",
3
- "version": "3.3.0",
3
+ "version": "3.5.0",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "provenance": true
7
7
  },
8
- "packageManager": "npm@11.16.0",
8
+ "packageManager": "npm@11.18.0",
9
9
  "exports": {
10
10
  ".": {
11
11
  "default": "./src/ip.mjs"
@@ -41,7 +41,7 @@
41
41
  },
42
42
  "devDependencies": {
43
43
  "ava": "^8.0.1",
44
- "browser-ava": "^2.3.64",
44
+ "browser-ava": "^2.3.65",
45
45
  "c8": "^11.0.0",
46
46
  "documentation": "^14.0.3",
47
47
  "semantic-release": "^25.0.5",
package/src/ip.mjs CHANGED
@@ -20,7 +20,9 @@ const ipv4 = {
20
20
  base: 10,
21
21
  linkLocalPrefix: new Uint8Array([169, 254]),
22
22
  linkLocalPrefixLength: 16,
23
- localHostPrefixLenth: 8
23
+ localHost: new Uint8Array([127, 0, 0, 1]),
24
+ localHostPrefixLenth: 8,
25
+ wellKnownClasses: [[new Uint8Array([192, 168]), 8]]
24
26
  };
25
27
 
26
28
  const ipv6 = {
@@ -43,6 +45,7 @@ const ipv6 = {
43
45
  base: 16,
44
46
  linkLocalPrefix: new Uint16Array([0xfe80]),
45
47
  linkLocalPrefixLength: 64,
48
+ localHost: new Uint16Array([0, 0, 0, 0, 0, 0, 0, 1]),
46
49
  localHostPrefixLenth: 128
47
50
  };
48
51
 
@@ -264,7 +267,7 @@ export function normalizeCIDR(address) {
264
267
  const n = _prefix(family, address, prefixLength);
265
268
  prefix = _decode(family, n, prefixLength);
266
269
 
267
- if (family == ipv6) {
270
+ if (family === ipv6) {
268
271
  if (!prefix.endsWith("::")) {
269
272
  // TODO
270
273
  prefix += prefix.endsWith(":") ? ":" : "::";
@@ -329,16 +332,22 @@ export function reverseArpa(address) {
329
332
  return address.split(".").reverse().join(".") + ".in-addr.arpa";
330
333
  }
331
334
 
332
- export function isLocalhost(address) {
333
- const eaddr = encodeIP(address);
334
-
335
- if (!eaddr) {
336
- return false;
335
+ function _equal(a, b) {
336
+ let i = 0;
337
+ for (const slot of a) {
338
+ if (slot !== b[i++]) {
339
+ return false;
340
+ }
337
341
  }
342
+ return true;
343
+ }
338
344
 
339
- const str = eaddr.toString();
340
-
341
- return str === IPV4_LOCALHOST.toString() || str === IPV6_LOCALHOST.toString();
345
+ export function isLocalhost(address) {
346
+ const family = _family(address);
347
+ if (family) {
348
+ return _equal(family.localHost, _encode(family, address));
349
+ }
350
+ return false;
342
351
  }
343
352
 
344
353
  export function isLinkLocal(address) {
@@ -350,14 +359,7 @@ export function isLinkLocal(address) {
350
359
  }
351
360
 
352
361
  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;
362
+ return _equal(family.linkLocalPrefix, _encode(family, address));
361
363
  }
362
364
 
363
365
  export function isUniqueLocal(address) {
@@ -365,8 +367,39 @@ export function isUniqueLocal(address) {
365
367
  return eaddr?.[0] >> 9 === 126 ? true : false;
366
368
  }
367
369
 
370
+ export function _isUniqueLocal(eaddr) {
371
+ return eaddr?.[0] >> 9 === 126 ? true : false;
372
+ }
373
+
368
374
  export function hasWellKnownSubnet(address) {
369
- return isLocalhost(address) || isLinkLocal(address) || isUniqueLocal(address);
375
+ return wellKnownSubnet(address) !== undefined;
376
+ }
377
+
378
+ export function wellKnownSubnet(address) {
379
+ const family = _family(address);
380
+ if (family) {
381
+ const encoded = _encode(family, address);
382
+
383
+ if (_equal(family.localHost, encoded)) {
384
+ return [family.localHost, family.localHostPrefixLenth];
385
+ }
386
+ if (_equal(family.linkLocalPrefix, encoded)) {
387
+ return [family.linkLocalPrefix, family.linkLocalPrefixLength];
388
+ }
389
+ if (_isUniqueLocal(encoded)) {
390
+ return [encoded[0], 64];
391
+ }
392
+
393
+ if (family === ipv4) {
394
+ for (const c of family.wellKnownClasses) {
395
+ if (_equal(c[0], encoded)) {
396
+ return c;
397
+ }
398
+ }
399
+ }
400
+ }
401
+
402
+ return undefined;
370
403
  }
371
404
 
372
405
  /*
@@ -386,7 +419,6 @@ export function hasWellKnownSubnet(address) {
386
419
  ::1 128. local host
387
420
  127 8 local host
388
421
  172
389
-
390
422
  */
391
423
 
392
424
  /*
@@ -399,5 +431,5 @@ export const IPV6_LINK_LOCAL_ALL_ROUTERS = _encode(ipv6, "ff02::2");
399
431
  export const IPV6_SITE_LOCAL_ALL_ROUTERS = _encode(ipv6, "ff05::2");
400
432
  export const IPV6_SITE_LOCAL_ALL_DHCP_SERVERS = _encode(ipv6, "ff05::1:3");
401
433
 
402
- export const IPV4_LOCALHOST = _encode(ipv4, "127.0.0.1");
403
- export const IPV6_LOCALHOST = _encode(ipv6, "::1");
434
+ export const IPV4_LOCALHOST = ipv4.localHost;
435
+ export const IPV6_LOCALHOST = ipv6.localHost;
package/types/ip.d.mts CHANGED
@@ -44,7 +44,9 @@ export function isLocalhost(address: any): boolean;
44
44
  export function isLinkLocal(address: any): boolean;
45
45
  export function _isLinkLocal(family: any, address: any): boolean;
46
46
  export function isUniqueLocal(address: any): boolean;
47
+ export function _isUniqueLocal(eaddr: any): boolean;
47
48
  export function hasWellKnownSubnet(address: any): boolean;
49
+ export function wellKnownSubnet(address: any): any[] | undefined;
48
50
  /**
49
51
  * Address familiy IPv4
50
52
  */
@@ -59,5 +61,5 @@ export const IPV6_LINK_LOCAL_ALL_NODES: any;
59
61
  export const IPV6_LINK_LOCAL_ALL_ROUTERS: any;
60
62
  export const IPV6_SITE_LOCAL_ALL_ROUTERS: any;
61
63
  export const IPV6_SITE_LOCAL_ALL_DHCP_SERVERS: any;
62
- export const IPV4_LOCALHOST: any;
63
- export const IPV6_LOCALHOST: any;
64
+ export const IPV4_LOCALHOST: Uint8Array<ArrayBuffer>;
65
+ export const IPV6_LOCALHOST: Uint16Array<ArrayBuffer>;