UNPKG

1.14 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.fromUrl = exports.NO_HOSTNAME = void 0;
4const urlPattern = /^[a-z][*+.a-z-]+:\/\//i;
5exports.NO_HOSTNAME = Symbol("NO_HOSTNAME");
6const fromUrl = (urlLike) => {
7 /* istanbul ignore next */
8 if (typeof URL !== "function") {
9 throw new Error("Looks like the new URL() constructor is not globally available in your environment. Please make sure to use a polyfill.");
10 }
11 // Extra check for non-TypeScript users
12 if (typeof urlLike !== "string") {
13 return exports.NO_HOSTNAME;
14 }
15 // URLs that start with // are protocol relative
16 const url = urlLike.startsWith("//")
17 ? `http:${urlLike}`
18 : // URLs that start with / do not have a hostname section
19 urlLike.startsWith("/")
20 ? urlLike
21 : urlPattern.test(urlLike)
22 ? urlLike
23 : `http://${urlLike}`;
24 try {
25 return new URL(url).hostname;
26 }
27 catch (_a) {
28 return exports.NO_HOSTNAME;
29 }
30};
31exports.fromUrl = fromUrl;
32//# sourceMappingURL=from-url.js.map
\No newline at end of file