var Url = function() { this.protocol = null, this.slashes = null, this.auth = null, this.host = null, this.port = null, this.hostname = null, this.hash = null, this.search = null, this.query = null, this.pathname = null, this.path = null, this.href = null; }, urlParse = function(url, parseQueryString, slashesDenoteHost) { if (url && typeof url === "object" && url instanceof Url) return url; var u = new Url; return u.parse(url, parseQueryString, slashesDenoteHost), u; }, urlFormat = function(obj) { if (typeof obj === "string") obj = urlParse(obj); if (!(obj instanceof Url)) return Url.prototype.format.call(obj); return obj.format(); }, urlResolve = function(source, relative) { return urlParse(source, !1, !0).resolve(relative); }, urlResolveObject = function(source, relative) { if (!source) return relative; return urlParse(source, !1, !0).resolveObject(relative); }, urlToHttpOptions = function(url) { const options = { protocol: url.protocol, hostname: typeof url.hostname === "string" && url.hostname.startsWith("[") ? url.hostname.slice(1, -1) : url.hostname, hash: url.hash, search: url.search, pathname: url.pathname, path: `${url.pathname || ""}${url.search || ""}`, href: url.href }; if (url.port !== "") options.port = Number(url.port); if (url.username || url.password) options.auth = `${decodeURIComponent(url.username)}:${decodeURIComponent(url.password)}`; return options; }, { URL, URLSearchParams } = globalThis, protocolPattern = /^([a-z0-9.+-]+:)/i, portPattern = /:[0-9]*$/, simplePathPattern = /^(\/\/?(?!\/)[^?\s]*)(\?[^\s]*)?$/, delims = ["<", ">", '"', "`", " ", "\r", "\n", "\t"], unwise = ["{", "}", "|", "\\", "^", "`"].concat(delims), autoEscape = ["'"].concat(unwise), nonHostChars = ["%", "/", "?", ";", "#"].concat(autoEscape), hostEndingChars = ["/", "?", "#"], hostnameMaxLen = 255, hostnamePartPattern = /^[+a-z0-9A-Z_-]{0,63}$/, hostnamePartStart = /^([+a-z0-9A-Z_-]{0,63})(.*)$/, unsafeProtocol = { javascript: !0, "javascript:": !0 }, hostlessProtocol = { javascript: !0, "javascript:": !0 }, slashedProtocol = { http: !0, https: !0, ftp: !0, gopher: !0, file: !0, "http:": !0, "https:": !0, "ftp:": !0, "gopher:": !0, "file:": !0 }; Url.prototype.parse = function(url, parseQueryString, slashesDenoteHost) { if (typeof url !== "string") throw new TypeError("Parameter 'url' must be a string, not " + typeof url); var queryIndex = url.indexOf("?"), splitter = queryIndex !== -1 && queryIndex < url.indexOf("#") ? "?" : "#", uSplit = url.split(splitter), slashRegex = /\\/g; uSplit[0] = uSplit[0].replace(slashRegex, "/"), url = uSplit.join(splitter); var rest = url; if (rest = rest.trim(), !slashesDenoteHost && url.split("#").length === 1) { var simplePath = simplePathPattern.exec(rest); if (simplePath) { if (this.path = rest, this.href = rest, this.pathname = simplePath[1], simplePath[2]) if (this.search = simplePath[2], parseQueryString) this.query = new URLSearchParams(this.search.substr(1)).toJSON(); else this.query = this.search.substr(1); else if (parseQueryString) this.search = "", this.query = {}; return this; } } var proto = protocolPattern.exec(rest); if (proto) { proto = proto[0]; var lowerProto = proto.toLowerCase(); this.protocol = lowerProto, rest = rest.substr(proto.length); } if (slashesDenoteHost || proto || rest.match(/^\/\/[^@/]+@[^@/]+/)) { var slashes = rest.substr(0, 2) === "//"; if (slashes && !(proto && hostlessProtocol[proto])) rest = rest.substr(2), this.slashes = !0; } if (!hostlessProtocol[proto] && (slashes || proto && !slashedProtocol[proto])) { var hostEnd = -1; for (var i = 0;i < hostEndingChars.length; i++) { var hec = rest.indexOf(hostEndingChars[i]); if (hec !== -1 && (hostEnd === -1 || hec < hostEnd)) hostEnd = hec; } var auth, atSign; if (hostEnd === -1) atSign = rest.lastIndexOf("@"); else atSign = rest.lastIndexOf("@", hostEnd); if (atSign !== -1) auth = rest.slice(0, atSign), rest = rest.slice(atSign + 1), this.auth = decodeURIComponent(auth); hostEnd = -1; for (var i = 0;i < nonHostChars.length; i++) { var hec = rest.indexOf(nonHostChars[i]); if (hec !== -1 && (hostEnd === -1 || hec < hostEnd)) hostEnd = hec; } if (hostEnd === -1) hostEnd = rest.length; this.host = rest.slice(0, hostEnd), rest = rest.slice(hostEnd), this.parseHost(), this.hostname = this.hostname || ""; var ipv6Hostname = this.hostname[0] === "[" && this.hostname[this.hostname.length - 1] === "]"; if (!ipv6Hostname) { var hostparts = this.hostname.split(/\./); for (var i = 0, l = hostparts.length;i < l; i++) { var part = hostparts[i]; if (!part) continue; if (!part.match(hostnamePartPattern)) { var newpart = ""; for (var j = 0, k = part.length;j < k; j++) if (part.charCodeAt(j) > 127) newpart += "x"; else newpart += part[j]; if (!newpart.match(hostnamePartPattern)) { var validParts = hostparts.slice(0, i), notHost = hostparts.slice(i + 1), bit = part.match(hostnamePartStart); if (bit) validParts.push(bit[1]), notHost.unshift(bit[2]); if (notHost.length) rest = "/" + notHost.join(".") + rest; this.hostname = validParts.join("."); break; } } } } if (this.hostname.length > hostnameMaxLen) this.hostname = ""; else this.hostname = this.hostname.toLowerCase(); if (!ipv6Hostname) this.hostname = new URL("http://" + this.hostname).hostname; var p = this.port ? ":" + this.port : "", h = this.hostname || ""; if (this.host = h + p, this.href += this.host, ipv6Hostname) { if (this.hostname = this.hostname.substr(1, this.hostname.length - 2), rest[0] !== "/") rest = "/" + rest; } } if (!unsafeProtocol[lowerProto]) for (var i = 0, l = autoEscape.length;i < l; i++) { var ae = autoEscape[i]; if (rest.indexOf(ae) === -1) continue; var esc = encodeURIComponent(ae); if (esc === ae) esc = escape(ae); rest = rest.split(ae).join(esc); } var hash = rest.indexOf("#"); if (hash !== -1) this.hash = rest.substr(hash), rest = rest.slice(0, hash); var qm = rest.indexOf("?"); if (qm !== -1) { if (this.search = rest.substr(qm), this.query = rest.substr(qm + 1), parseQueryString) this.query = new URLSearchParams(this.query); rest = rest.slice(0, qm); } else if (parseQueryString) this.search = "", this.query = {}; if (rest) this.pathname = rest; if (slashedProtocol[lowerProto] && this.hostname && !this.pathname) this.pathname = "/"; if (this.pathname || this.search) { var p = this.pathname || "", s = this.search || ""; this.path = p + s; } return this.href = this.format(), this; }; Url.prototype.format = function() { var auth = this.auth || ""; if (auth) auth = encodeURIComponent(auth), auth = auth.replace(/%3A/i, ":"), auth += "@"; var protocol = this.protocol || "", pathname = this.pathname || "", hash = this.hash || "", host = !1, query = ""; if (this.host) host = auth + this.host; else if (this.hostname) { if (host = auth + (this.hostname.indexOf(":") === -1 ? this.hostname : "[" + this.hostname + "]"), this.port) host += ":" + this.port; } if (this.query && typeof this.query === "object" && Object.keys(this.query).length) query = new URLSearchParams(this.query).toString(); var search = this.search || query && "?" + query || ""; if (protocol && protocol.substr(-1) !== ":") protocol += ":"; if (this.slashes || (!protocol || slashedProtocol[protocol]) && host !== !1) { if (host = "//" + (host || ""), pathname && pathname.charAt(0) !== "/") pathname = "/" + pathname; } else if (!host) host = ""; if (hash && hash.charAt(0) !== "#") hash = "#" + hash; if (search && search.charAt(0) !== "?") search = "?" + search; return pathname = pathname.replace(/[?#]/g, function(match) { return encodeURIComponent(match); }), search = search.replace("#", "%23"), protocol + host + pathname + search + hash; }; Url.prototype.resolve = function(relative) { return this.resolveObject(urlParse(relative, !1, !0)).format(); }; Url.prototype.resolveObject = function(relative) { if (typeof relative === "string") { var rel = new Url; rel.parse(relative, !1, !0), relative = rel; } var result = new Url, tkeys = Object.keys(this); for (var tk = 0;tk < tkeys.length; tk++) { var tkey = tkeys[tk]; result[tkey] = this[tkey]; } if (result.hash = relative.hash, relative.href === "") return result.href = result.format(), result; if (relative.slashes && !relative.protocol) { var rkeys = Object.keys(relative); for (var rk = 0;rk < rkeys.length; rk++) { var rkey = rkeys[rk]; if (rkey !== "protocol") result[rkey] = relative[rkey]; } if (slashedProtocol[result.protocol] && result.hostname && !result.pathname) result.pathname = "/", result.path = result.pathname; return result.href = result.format(), result; } if (relative.protocol && relative.protocol !== result.protocol) { if (!slashedProtocol[relative.protocol]) { var keys = Object.keys(relative); for (var v = 0;v < keys.length; v++) { var k = keys[v]; result[k] = relative[k]; } return result.href = result.format(), result; } if (result.protocol = relative.protocol, !relative.host && !hostlessProtocol[relative.protocol]) { var relPath = (relative.pathname || "").split("/"); while (relPath.length && !(relative.host = relPath.shift())) ; if (!relative.host) relative.host = ""; if (!relative.hostname) relative.hostname = ""; if (relPath[0] !== "") relPath.unshift(""); if (relPath.length < 2) relPath.unshift(""); result.pathname = relPath.join("/"); } else result.pathname = relative.pathname; if (result.search = relative.search, result.query = relative.query, result.host = relative.host || "", result.auth = relative.auth, result.hostname = relative.hostname || relative.host, result.port = relative.port, result.pathname || result.search) { var p = result.pathname || "", s = result.search || ""; result.path = p + s; } return result.slashes = result.slashes || relative.slashes, result.href = result.format(), result; } var isSourceAbs = result.pathname && result.pathname.charAt(0) === "/", isRelAbs = relative.host || relative.pathname && relative.pathname.charAt(0) === "/", mustEndAbs = isRelAbs || isSourceAbs || result.host && relative.pathname, removeAllDots = mustEndAbs, srcPath = result.pathname && result.pathname.split("/") || [], relPath = relative.pathname && relative.pathname.split("/") || [], psychotic = result.protocol && !slashedProtocol[result.protocol]; if (psychotic) { if (result.hostname = "", result.port = null, result.host) if (srcPath[0] === "") srcPath[0] = result.host; else srcPath.unshift(result.host); if (result.host = "", relative.protocol) { if (relative.hostname = null, relative.port = null, relative.host) if (relPath[0] === "") relPath[0] = relative.host; else relPath.unshift(relative.host); relative.host = null; } mustEndAbs = mustEndAbs && (relPath[0] === "" || srcPath[0] === ""); } if (isRelAbs) result.host = relative.host || relative.host === "" ? relative.host : result.host, result.hostname = relative.hostname || relative.hostname === "" ? relative.hostname : result.hostname, result.search = relative.search, result.query = relative.query, srcPath = relPath; else if (relPath.length) { if (!srcPath) srcPath = []; srcPath.pop(), srcPath = srcPath.concat(relPath), result.search = relative.search, result.query = relative.query; } else if (relative.search != null) { if (psychotic) { result.host = srcPath.shift(), result.hostname = result.host; var authInHost = result.host && result.host.indexOf("@") > 0 ? result.host.split("@") : !1; if (authInHost) result.auth = authInHost.shift(), result.hostname = authInHost.shift(), result.host = result.hostname; } if (result.search = relative.search, result.query = relative.query, result.pathname !== null || result.search !== null) result.path = (result.pathname ? result.pathname : "") + (result.search ? result.search : ""); return result.href = result.format(), result; } if (!srcPath.length) { if (result.pathname = null, result.search) result.path = "/" + result.search; else result.path = null; return result.href = result.format(), result; } var last = srcPath.slice(-1)[0], hasTrailingSlash = (result.host || relative.host || srcPath.length > 1) && (last === "." || last === "..") || last === "", up = 0; for (var i = srcPath.length;i >= 0; i--) if (last = srcPath[i], last === ".") srcPath.splice(i, 1); else if (last === "..") srcPath.splice(i, 1), up++; else if (up) srcPath.splice(i, 1), up--; if (!mustEndAbs && !removeAllDots) for (;up--; up) srcPath.unshift(".."); if (mustEndAbs && srcPath[0] !== "" && (!srcPath[0] || srcPath[0].charAt(0) !== "/")) srcPath.unshift(""); if (hasTrailingSlash && srcPath.join("/").substr(-1) !== "/") srcPath.push(""); var isAbsolute = srcPath[0] === "" || srcPath[0] && srcPath[0].charAt(0) === "/"; if (psychotic) { result.hostname = isAbsolute ? "" : srcPath.length ? srcPath.shift() : "", result.host = result.hostname; var authInHost = result.host && result.host.indexOf("@") > 0 ? result.host.split("@") : !1; if (authInHost) result.auth = authInHost.shift(), result.hostname = authInHost.shift(), result.host = result.hostname; } if (mustEndAbs = mustEndAbs || result.host && srcPath.length, mustEndAbs && !isAbsolute) srcPath.unshift(""); if (srcPath.length > 0) result.pathname = srcPath.join("/"); else result.pathname = null, result.path = null; if (result.pathname !== null || result.search !== null) result.path = (result.pathname ? result.pathname : "") + (result.search ? result.search : ""); return result.auth = relative.auth || result.auth, result.slashes = result.slashes || relative.slashes, result.href = result.format(), result; }; Url.prototype.parseHost = function() { var host = this.host, port = portPattern.exec(host); if (port) { if (port = port[0], port !== ":") this.port = port.substr(1); host = host.substr(0, host.length - port.length); } if (host) this.hostname = host; }; var lazy = globalThis[Symbol.for("Bun.lazy")], pathToFileURL = lazy("pathToFileURL"), fileURLToPath = lazy("fileURLToPath"), defaultObject = { parse: urlParse, resolve: urlResolve, resolveObject: urlResolveObject, format: urlFormat, Url, URLSearchParams, URL, pathToFileURL, fileURLToPath, urlToHttpOptions }; export { urlToHttpOptions, urlResolveObject as resolveObject, urlResolve as resolve, pathToFileURL, urlParse as parse, urlFormat as format, fileURLToPath, defaultObject as default, Url, URLSearchParams, URL };