@dbx-tools/shared-core 0.1.9 → 0.1.11

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/net.ts +25 -4
package/package.json CHANGED
@@ -16,7 +16,7 @@
16
16
  },
17
17
  "main": "index.ts",
18
18
  "license": "UNLICENSED",
19
- "version": "0.1.9",
19
+ "version": "0.1.11",
20
20
  "types": "index.ts",
21
21
  "type": "module",
22
22
  "exports": {
package/src/net.ts CHANGED
@@ -178,9 +178,9 @@ export function urlBuilder(): UrlBuilder;
178
178
  * urlBuilder({ url: "http://y" }); // http://y/
179
179
  * urlBuilder(); // http://localhost/
180
180
  */
181
- export function urlBuilder(input?: UrlLike): UrlBuilder | null;
181
+ export function urlBuilder(input?: UrlLike): UrlBuilder | undefined;
182
182
 
183
- export function urlBuilder(input?: UrlLike): UrlBuilder | null {
183
+ export function urlBuilder(input?: UrlLike): UrlBuilder | undefined {
184
184
  if (input instanceof URL) {
185
185
  return new UrlBuilderImpl(input);
186
186
  }
@@ -210,7 +210,7 @@ export function urlBuilder(input?: UrlLike): UrlBuilder | null {
210
210
  return urlBuilder(defaultUrl());
211
211
  }
212
212
  const url = parseUrl(input);
213
- return url ? urlBuilder(url) : null;
213
+ return url ? urlBuilder(url) : undefined;
214
214
  }
215
215
 
216
216
  /**
@@ -255,7 +255,7 @@ function parseUrl(input: string): URL | null {
255
255
  if (input && input.includes(URL_SCHEME_SEPARATOR)) {
256
256
  try {
257
257
  return new URL(input);
258
- } catch {}
258
+ } catch { }
259
259
  }
260
260
  return null;
261
261
  }
@@ -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.