@dbx-tools/shared-core 0.1.10 → 0.1.12
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 +1 -1
- package/src/net.ts +21 -0
package/package.json
CHANGED
package/src/net.ts
CHANGED
|
@@ -449,6 +449,27 @@ export function findContainingCidr<T extends Cidr>(
|
|
|
449
449
|
return null;
|
|
450
450
|
}
|
|
451
451
|
|
|
452
|
+
/**
|
|
453
|
+
* Whether `input`'s host is a loopback address - `localhost`, an IPv4
|
|
454
|
+
* `127.0.0.0/8` address, or IPv6 `::1` - i.e. a service running on this
|
|
455
|
+
* machine (e.g. a local registry). Accepts anything {@link urlBuilder}
|
|
456
|
+
* coerces (a URL string, `URL`, or `{ url }`); returns `false` when the
|
|
457
|
+
* input has no resolvable host.
|
|
458
|
+
*
|
|
459
|
+
* @example
|
|
460
|
+
* isLoopbackHost("http://localhost:4873"); // true
|
|
461
|
+
* isLoopbackHost("http://127.0.0.1"); // true
|
|
462
|
+
* isLoopbackHost("https://registry.npmjs.org"); // false
|
|
463
|
+
*/
|
|
464
|
+
export function isLoopbackHost(input: UrlLike): boolean {
|
|
465
|
+
const host = urlBuilder(input)?.hostname;
|
|
466
|
+
if (!host) return false;
|
|
467
|
+
if (host === "localhost") return true;
|
|
468
|
+
// `URL` brackets an IPv6 host (`[::1]`); strip them before parsing.
|
|
469
|
+
const ip = host.replace(/^\[|\]$/g, "");
|
|
470
|
+
return ipInCidr(ip, "127.0.0.0/8") || ipInCidr(ip, "::1/128");
|
|
471
|
+
}
|
|
472
|
+
|
|
452
473
|
/**
|
|
453
474
|
* Network mask for `prefix` leading bits of a `bits`-wide address as a
|
|
454
475
|
* `bigint`: the top `prefix` bits set, the low host bits cleared.
|