index.mjs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. const n = /[^\0-\x7E]/;
  2. const t = /[\x2E\u3002\uFF0E\uFF61]/g;
  3. const o = {
  4. overflow: "Overflow Error",
  5. "not-basic": "Illegal Input",
  6. "invalid-input": "Invalid Input"
  7. };
  8. const e = Math.floor;
  9. const r = String.fromCharCode;
  10. function s(n2) {
  11. throw new RangeError(o[n2]);
  12. }
  13. const c = function(n2, t2) {
  14. return n2 + 22 + 75 * (n2 < 26) - ((t2 != 0) << 5);
  15. };
  16. const u = function(n2, t2, o2) {
  17. let r2 = 0;
  18. for (n2 = o2 ? e(n2 / 700) : n2 >> 1, n2 += e(n2 / t2); n2 > 455; r2 += 36) {
  19. n2 = e(n2 / 35);
  20. }
  21. return e(r2 + 36 * n2 / (n2 + 38));
  22. };
  23. function toASCII(o2) {
  24. return (function(n2, o3) {
  25. const e2 = n2.split("@");
  26. let r2 = "";
  27. e2.length > 1 && (r2 = e2[0] + "@", n2 = e2[1]);
  28. const s2 = (function(n3, t2) {
  29. const o4 = [];
  30. let e3 = n3.length;
  31. for (; e3--; ) {
  32. o4[e3] = t2(n3[e3]);
  33. }
  34. return o4;
  35. })((n2 = n2.replace(t, ".")).split("."), o3).join(".");
  36. return r2 + s2;
  37. })(o2, function(t2) {
  38. return n.test(t2) ? "xn--" + (function(n2) {
  39. const t3 = [];
  40. const o3 = (n2 = (function(n3) {
  41. const t4 = [];
  42. let o4 = 0;
  43. const e2 = n3.length;
  44. for (; o4 < e2; ) {
  45. const r2 = n3.charCodeAt(o4++);
  46. if (r2 >= 55296 && r2 <= 56319 && o4 < e2) {
  47. const e3 = n3.charCodeAt(o4++);
  48. (64512 & e3) == 56320 ? t4.push(((1023 & r2) << 10) + (1023 & e3) + 65536) : (t4.push(r2), o4--);
  49. } else {
  50. t4.push(r2);
  51. }
  52. }
  53. return t4;
  54. })(n2)).length;
  55. let f = 128;
  56. let i = 0;
  57. let l = 72;
  58. for (const o4 of n2) {
  59. o4 < 128 && t3.push(r(o4));
  60. }
  61. const h = t3.length;
  62. let p = h;
  63. for (h && t3.push("-"); p < o3; ) {
  64. let o4 = 2147483647;
  65. for (const t4 of n2) {
  66. t4 >= f && t4 < o4 && (o4 = t4);
  67. }
  68. const a = p + 1;
  69. o4 - f > e((2147483647 - i) / a) && s("overflow"), i += (o4 - f) * a, f = o4;
  70. for (const o5 of n2) {
  71. if (o5 < f && ++i > 2147483647 && s("overflow"), o5 == f) {
  72. let n3 = i;
  73. for (let o6 = 36; ; o6 += 36) {
  74. const s2 = o6 <= l ? 1 : o6 >= l + 26 ? 26 : o6 - l;
  75. if (n3 < s2) {
  76. break;
  77. }
  78. const u2 = n3 - s2;
  79. const f2 = 36 - s2;
  80. t3.push(r(c(s2 + u2 % f2, 0))), n3 = e(u2 / f2);
  81. }
  82. t3.push(r(c(n3, 0))), l = u(i, a, p == h), i = 0, ++p;
  83. }
  84. }
  85. ++i, ++f;
  86. }
  87. return t3.join("");
  88. })(t2) : t2;
  89. });
  90. }
  91. const HASH_RE = /#/g;
  92. const AMPERSAND_RE = /&/g;
  93. const SLASH_RE = /\//g;
  94. const EQUAL_RE = /=/g;
  95. const IM_RE = /\?/g;
  96. const PLUS_RE = /\+/g;
  97. const ENC_CARET_RE = /%5e/gi;
  98. const ENC_BACKTICK_RE = /%60/gi;
  99. const ENC_CURLY_OPEN_RE = /%7b/gi;
  100. const ENC_PIPE_RE = /%7c/gi;
  101. const ENC_CURLY_CLOSE_RE = /%7d/gi;
  102. const ENC_SPACE_RE = /%20/gi;
  103. const ENC_SLASH_RE = /%2f/gi;
  104. const ENC_ENC_SLASH_RE = /%252f/gi;
  105. function encode(text) {
  106. return encodeURI("" + text).replace(ENC_PIPE_RE, "|");
  107. }
  108. function encodeHash(text) {
  109. return encode(text).replace(ENC_CURLY_OPEN_RE, "{").replace(ENC_CURLY_CLOSE_RE, "}").replace(ENC_CARET_RE, "^");
  110. }
  111. function encodeQueryValue(input) {
  112. return encode(typeof input === "string" ? input : JSON.stringify(input)).replace(PLUS_RE, "%2B").replace(ENC_SPACE_RE, "+").replace(HASH_RE, "%23").replace(AMPERSAND_RE, "%26").replace(ENC_BACKTICK_RE, "`").replace(ENC_CARET_RE, "^").replace(SLASH_RE, "%2F");
  113. }
  114. function encodeQueryKey(text) {
  115. return encodeQueryValue(text).replace(EQUAL_RE, "%3D");
  116. }
  117. function encodePath(text) {
  118. return encode(text).replace(HASH_RE, "%23").replace(IM_RE, "%3F").replace(ENC_ENC_SLASH_RE, "%2F").replace(AMPERSAND_RE, "%26").replace(PLUS_RE, "%2B");
  119. }
  120. function encodeParam(text) {
  121. return encodePath(text).replace(SLASH_RE, "%2F");
  122. }
  123. function decode(text = "") {
  124. try {
  125. return decodeURIComponent("" + text);
  126. } catch {
  127. return "" + text;
  128. }
  129. }
  130. function decodePath(text) {
  131. return decode(text.replace(ENC_SLASH_RE, "%252F"));
  132. }
  133. function decodeQueryKey(text) {
  134. return decode(text.replace(PLUS_RE, " "));
  135. }
  136. function decodeQueryValue(text) {
  137. return decode(text.replace(PLUS_RE, " "));
  138. }
  139. function encodeHost(name = "") {
  140. return toASCII(name);
  141. }
  142. function parseQuery(parametersString = "") {
  143. const object = /* @__PURE__ */ Object.create(null);
  144. if (parametersString[0] === "?") {
  145. parametersString = parametersString.slice(1);
  146. }
  147. for (const parameter of parametersString.split("&")) {
  148. const s = parameter.match(/([^=]+)=?(.*)/) || [];
  149. if (s.length < 2) {
  150. continue;
  151. }
  152. const key = decodeQueryKey(s[1]);
  153. if (key === "__proto__" || key === "constructor") {
  154. continue;
  155. }
  156. const value = decodeQueryValue(s[2] || "");
  157. if (object[key] === void 0) {
  158. object[key] = value;
  159. } else if (Array.isArray(object[key])) {
  160. object[key].push(value);
  161. } else {
  162. object[key] = [object[key], value];
  163. }
  164. }
  165. return object;
  166. }
  167. function encodeQueryItem(key, value) {
  168. if (typeof value === "number" || typeof value === "boolean") {
  169. value = String(value);
  170. }
  171. if (!value) {
  172. return encodeQueryKey(key);
  173. }
  174. if (Array.isArray(value)) {
  175. return value.map(
  176. (_value) => `${encodeQueryKey(key)}=${encodeQueryValue(_value)}`
  177. ).join("&");
  178. }
  179. return `${encodeQueryKey(key)}=${encodeQueryValue(value)}`;
  180. }
  181. function stringifyQuery(query) {
  182. return Object.keys(query).filter((k) => query[k] !== void 0).map((k) => encodeQueryItem(k, query[k])).filter(Boolean).join("&");
  183. }
  184. const PROTOCOL_STRICT_REGEX = /^[\s\w\0+.-]{2,}:([/\\]{1,2})/;
  185. const PROTOCOL_REGEX = /^[\s\w\0+.-]{2,}:([/\\]{2})?/;
  186. const PROTOCOL_RELATIVE_REGEX = /^([/\\]\s*){2,}[^/\\]/;
  187. const PROTOCOL_SCRIPT_RE = /^[\s\0]*(blob|data|javascript|vbscript):$/i;
  188. const TRAILING_SLASH_RE = /\/$|\/\?|\/#/;
  189. const JOIN_LEADING_SLASH_RE = /^\.?\//;
  190. function isRelative(inputString) {
  191. return ["./", "../"].some((string_) => inputString.startsWith(string_));
  192. }
  193. function hasProtocol(inputString, opts = {}) {
  194. if (typeof opts === "boolean") {
  195. opts = { acceptRelative: opts };
  196. }
  197. if (opts.strict) {
  198. return PROTOCOL_STRICT_REGEX.test(inputString);
  199. }
  200. return PROTOCOL_REGEX.test(inputString) || (opts.acceptRelative ? PROTOCOL_RELATIVE_REGEX.test(inputString) : false);
  201. }
  202. function isScriptProtocol(protocol) {
  203. return !!protocol && PROTOCOL_SCRIPT_RE.test(protocol);
  204. }
  205. function hasTrailingSlash(input = "", respectQueryAndFragment) {
  206. if (!respectQueryAndFragment) {
  207. return input.endsWith("/");
  208. }
  209. return TRAILING_SLASH_RE.test(input);
  210. }
  211. function withoutTrailingSlash(input = "", respectQueryAndFragment) {
  212. if (!respectQueryAndFragment) {
  213. return (hasTrailingSlash(input) ? input.slice(0, -1) : input) || "/";
  214. }
  215. if (!hasTrailingSlash(input, true)) {
  216. return input || "/";
  217. }
  218. let path = input;
  219. let fragment = "";
  220. const fragmentIndex = input.indexOf("#");
  221. if (fragmentIndex !== -1) {
  222. path = input.slice(0, fragmentIndex);
  223. fragment = input.slice(fragmentIndex);
  224. }
  225. const [s0, ...s] = path.split("?");
  226. const cleanPath = s0.endsWith("/") ? s0.slice(0, -1) : s0;
  227. return (cleanPath || "/") + (s.length > 0 ? `?${s.join("?")}` : "") + fragment;
  228. }
  229. function withTrailingSlash(input = "", respectQueryAndFragment) {
  230. if (!respectQueryAndFragment) {
  231. return input.endsWith("/") ? input : input + "/";
  232. }
  233. if (hasTrailingSlash(input, true)) {
  234. return input || "/";
  235. }
  236. let path = input;
  237. let fragment = "";
  238. const fragmentIndex = input.indexOf("#");
  239. if (fragmentIndex !== -1) {
  240. path = input.slice(0, fragmentIndex);
  241. fragment = input.slice(fragmentIndex);
  242. if (!path) {
  243. return fragment;
  244. }
  245. }
  246. const [s0, ...s] = path.split("?");
  247. return s0 + "/" + (s.length > 0 ? `?${s.join("?")}` : "") + fragment;
  248. }
  249. function hasLeadingSlash(input = "") {
  250. return input.startsWith("/");
  251. }
  252. function withoutLeadingSlash(input = "") {
  253. return (hasLeadingSlash(input) ? input.slice(1) : input) || "/";
  254. }
  255. function withLeadingSlash(input = "") {
  256. return hasLeadingSlash(input) ? input : "/" + input;
  257. }
  258. function cleanDoubleSlashes(input = "") {
  259. return input.split("://").map((string_) => string_.replace(/\/{2,}/g, "/")).join("://");
  260. }
  261. function withBase(input, base) {
  262. if (isEmptyURL(base) || hasProtocol(input)) {
  263. return input;
  264. }
  265. const _base = withoutTrailingSlash(base);
  266. if (input.startsWith(_base)) {
  267. const nextChar = input[_base.length];
  268. if (!nextChar || nextChar === "/" || nextChar === "?") {
  269. return input;
  270. }
  271. }
  272. return joinURL(_base, input);
  273. }
  274. function withoutBase(input, base) {
  275. if (isEmptyURL(base)) {
  276. return input;
  277. }
  278. const _base = withoutTrailingSlash(base);
  279. if (!input.startsWith(_base)) {
  280. return input;
  281. }
  282. const nextChar = input[_base.length];
  283. if (nextChar && nextChar !== "/" && nextChar !== "?") {
  284. return input;
  285. }
  286. const trimmed = input.slice(_base.length).replace(/^\/+/, "");
  287. return "/" + trimmed;
  288. }
  289. function withQuery(input, query) {
  290. const parsed = parseURL(input);
  291. const mergedQuery = { ...parseQuery(parsed.search), ...query };
  292. parsed.search = stringifyQuery(mergedQuery);
  293. return stringifyParsedURL(parsed);
  294. }
  295. function filterQuery(input, predicate) {
  296. if (!input.includes("?")) {
  297. return input;
  298. }
  299. const parsed = parseURL(input);
  300. const query = parseQuery(parsed.search);
  301. const filteredQuery = Object.fromEntries(
  302. Object.entries(query).filter(([key, value]) => predicate(key, value))
  303. );
  304. parsed.search = stringifyQuery(filteredQuery);
  305. return stringifyParsedURL(parsed);
  306. }
  307. function getQuery(input) {
  308. return parseQuery(parseURL(input).search);
  309. }
  310. function isEmptyURL(url) {
  311. return !url || url === "/";
  312. }
  313. function isNonEmptyURL(url) {
  314. return url && url !== "/";
  315. }
  316. function joinURL(base, ...input) {
  317. let url = base || "";
  318. for (const segment of input.filter((url2) => isNonEmptyURL(url2))) {
  319. if (url) {
  320. const _segment = segment.replace(JOIN_LEADING_SLASH_RE, "");
  321. url = withTrailingSlash(url) + _segment;
  322. } else {
  323. url = segment;
  324. }
  325. }
  326. return url;
  327. }
  328. function joinRelativeURL(..._input) {
  329. const JOIN_SEGMENT_SPLIT_RE = /\/(?!\/)/;
  330. const input = _input.filter(Boolean);
  331. const segments = [];
  332. let segmentsDepth = 0;
  333. for (const i of input) {
  334. if (!i || i === "/") {
  335. continue;
  336. }
  337. for (const [sindex, s] of i.split(JOIN_SEGMENT_SPLIT_RE).entries()) {
  338. if (!s || s === ".") {
  339. continue;
  340. }
  341. if (s === "..") {
  342. if (segments.length === 1 && hasProtocol(segments[0])) {
  343. continue;
  344. }
  345. segments.pop();
  346. segmentsDepth--;
  347. continue;
  348. }
  349. if (sindex === 1 && segments[segments.length - 1]?.endsWith(":/")) {
  350. segments[segments.length - 1] += "/" + s;
  351. continue;
  352. }
  353. segments.push(s);
  354. segmentsDepth++;
  355. }
  356. }
  357. let url = segments.join("/");
  358. if (segmentsDepth >= 0) {
  359. if (input[0]?.startsWith("/") && !url.startsWith("/")) {
  360. url = "/" + url;
  361. } else if (input[0]?.startsWith("./") && !url.startsWith("./")) {
  362. url = "./" + url;
  363. }
  364. } else {
  365. url = "../".repeat(-1 * segmentsDepth) + url;
  366. }
  367. if (input[input.length - 1]?.endsWith("/") && !url.endsWith("/")) {
  368. url += "/";
  369. }
  370. return url;
  371. }
  372. function withHttp(input) {
  373. return withProtocol(input, "http://");
  374. }
  375. function withHttps(input) {
  376. return withProtocol(input, "https://");
  377. }
  378. function withoutProtocol(input) {
  379. return withProtocol(input, "");
  380. }
  381. function withProtocol(input, protocol) {
  382. let match = input.match(PROTOCOL_REGEX);
  383. if (!match) {
  384. match = input.match(/^\/{2,}/);
  385. }
  386. if (!match) {
  387. return protocol + input;
  388. }
  389. return protocol + input.slice(match[0].length);
  390. }
  391. function normalizeURL(input) {
  392. const parsed = parseURL(input);
  393. parsed.pathname = encodePath(decodePath(parsed.pathname));
  394. parsed.hash = encodeHash(decode(parsed.hash));
  395. parsed.host = encodeHost(decode(parsed.host));
  396. parsed.search = stringifyQuery(parseQuery(parsed.search));
  397. return stringifyParsedURL(parsed);
  398. }
  399. function resolveURL(base = "", ...inputs) {
  400. if (typeof base !== "string") {
  401. throw new TypeError(
  402. `URL input should be string received ${typeof base} (${base})`
  403. );
  404. }
  405. const filteredInputs = inputs.filter((input) => isNonEmptyURL(input));
  406. if (filteredInputs.length === 0) {
  407. return base;
  408. }
  409. const url = parseURL(base);
  410. for (const inputSegment of filteredInputs) {
  411. const urlSegment = parseURL(inputSegment);
  412. if (urlSegment.pathname) {
  413. url.pathname = withTrailingSlash(url.pathname) + withoutLeadingSlash(urlSegment.pathname);
  414. }
  415. if (urlSegment.hash && urlSegment.hash !== "#") {
  416. url.hash = urlSegment.hash;
  417. }
  418. if (urlSegment.search && urlSegment.search !== "?") {
  419. if (url.search && url.search !== "?") {
  420. const queryString = stringifyQuery({
  421. ...parseQuery(url.search),
  422. ...parseQuery(urlSegment.search)
  423. });
  424. url.search = queryString.length > 0 ? "?" + queryString : "";
  425. } else {
  426. url.search = urlSegment.search;
  427. }
  428. }
  429. }
  430. return stringifyParsedURL(url);
  431. }
  432. function isSamePath(p1, p2) {
  433. return decode(withoutTrailingSlash(p1)) === decode(withoutTrailingSlash(p2));
  434. }
  435. function isEqual(a, b, options = {}) {
  436. if (!options.trailingSlash) {
  437. a = withTrailingSlash(a);
  438. b = withTrailingSlash(b);
  439. }
  440. if (!options.leadingSlash) {
  441. a = withLeadingSlash(a);
  442. b = withLeadingSlash(b);
  443. }
  444. if (!options.encoding) {
  445. a = decode(a);
  446. b = decode(b);
  447. }
  448. return a === b;
  449. }
  450. function withFragment(input, hash) {
  451. if (!hash || hash === "#") {
  452. return input;
  453. }
  454. const parsed = parseURL(input);
  455. parsed.hash = hash === "" ? "" : "#" + encodeHash(hash);
  456. return stringifyParsedURL(parsed);
  457. }
  458. function withoutFragment(input) {
  459. return stringifyParsedURL({ ...parseURL(input), hash: "" });
  460. }
  461. function withoutHost(input) {
  462. const parsed = parseURL(input);
  463. return (parsed.pathname || "/") + parsed.search + parsed.hash;
  464. }
  465. const protocolRelative = Symbol.for("ufo:protocolRelative");
  466. function parseURL(input = "", defaultProto) {
  467. const _specialProtoMatch = input.match(
  468. /^[\s\0]*(blob:|data:|javascript:|vbscript:)(.*)/i
  469. );
  470. if (_specialProtoMatch) {
  471. const [, _proto, _pathname = ""] = _specialProtoMatch;
  472. return {
  473. protocol: _proto.toLowerCase(),
  474. pathname: _pathname,
  475. href: _proto + _pathname,
  476. auth: "",
  477. host: "",
  478. search: "",
  479. hash: ""
  480. };
  481. }
  482. if (!hasProtocol(input, { acceptRelative: true })) {
  483. return defaultProto ? parseURL(defaultProto + input) : parsePath(input);
  484. }
  485. const [, protocol = "", auth, hostAndPath = ""] = input.replace(/\\/g, "/").match(/^[\s\0]*([\w+.-]{2,}:)?\/\/([^/@]+@)?(.*)/) || [];
  486. let [, host = "", path = ""] = hostAndPath.match(/([^#/?]*)(.*)?/) || [];
  487. if (protocol === "file:") {
  488. path = path.replace(/\/(?=[A-Za-z]:)/, "");
  489. }
  490. const { pathname, search, hash } = parsePath(path);
  491. return {
  492. protocol: protocol.toLowerCase(),
  493. auth: auth ? auth.slice(0, Math.max(0, auth.length - 1)) : "",
  494. host,
  495. pathname,
  496. search,
  497. hash,
  498. [protocolRelative]: !protocol
  499. };
  500. }
  501. function parsePath(input = "") {
  502. const [pathname = "", search = "", hash = ""] = (input.match(/([^#?]*)(\?[^#]*)?(#.*)?/) || []).splice(1);
  503. return {
  504. pathname,
  505. search,
  506. hash
  507. };
  508. }
  509. function parseAuth(input = "") {
  510. const [username, password] = input.split(":");
  511. return {
  512. username: decode(username),
  513. password: decode(password)
  514. };
  515. }
  516. function parseHost(input = "") {
  517. const [hostname, port] = (input.match(/([^/:]*):?(\d+)?/) || []).splice(1);
  518. return {
  519. hostname: decode(hostname),
  520. port
  521. };
  522. }
  523. function stringifyParsedURL(parsed) {
  524. const pathname = parsed.pathname || "";
  525. const search = parsed.search ? (parsed.search.startsWith("?") ? "" : "?") + parsed.search : "";
  526. const hash = parsed.hash || "";
  527. const auth = parsed.auth ? parsed.auth + "@" : "";
  528. const host = parsed.host || "";
  529. const proto = parsed.protocol || parsed[protocolRelative] ? (parsed.protocol || "") + "//" : "";
  530. return proto + auth + host + pathname + search + hash;
  531. }
  532. const FILENAME_STRICT_REGEX = /\/([^/]+\.[^/]+)$/;
  533. const FILENAME_REGEX = /\/([^/]+)$/;
  534. function parseFilename(input = "", opts) {
  535. const { pathname } = parseURL(input);
  536. const matches = opts?.strict ? pathname.match(FILENAME_STRICT_REGEX) : pathname.match(FILENAME_REGEX);
  537. return matches ? matches[1] : void 0;
  538. }
  539. class $URL {
  540. protocol;
  541. host;
  542. auth;
  543. pathname;
  544. query = {};
  545. hash;
  546. constructor(input = "") {
  547. if (typeof input !== "string") {
  548. throw new TypeError(
  549. `URL input should be string received ${typeof input} (${input})`
  550. );
  551. }
  552. const parsed = parseURL(input);
  553. this.protocol = decode(parsed.protocol);
  554. this.host = decode(parsed.host);
  555. this.auth = decode(parsed.auth);
  556. this.pathname = decodePath(parsed.pathname);
  557. this.query = parseQuery(parsed.search);
  558. this.hash = decode(parsed.hash);
  559. }
  560. get hostname() {
  561. return parseHost(this.host).hostname;
  562. }
  563. get port() {
  564. return parseHost(this.host).port || "";
  565. }
  566. get username() {
  567. return parseAuth(this.auth).username;
  568. }
  569. get password() {
  570. return parseAuth(this.auth).password || "";
  571. }
  572. get hasProtocol() {
  573. return this.protocol.length;
  574. }
  575. get isAbsolute() {
  576. return this.hasProtocol || this.pathname[0] === "/";
  577. }
  578. get search() {
  579. const q = stringifyQuery(this.query);
  580. return q.length > 0 ? "?" + q : "";
  581. }
  582. get searchParams() {
  583. const p = new URLSearchParams();
  584. for (const name in this.query) {
  585. const value = this.query[name];
  586. if (Array.isArray(value)) {
  587. for (const v of value) {
  588. p.append(name, v);
  589. }
  590. } else {
  591. p.append(
  592. name,
  593. typeof value === "string" ? value : JSON.stringify(value)
  594. );
  595. }
  596. }
  597. return p;
  598. }
  599. get origin() {
  600. return (this.protocol ? this.protocol + "//" : "") + encodeHost(this.host);
  601. }
  602. get fullpath() {
  603. return encodePath(this.pathname) + this.search + encodeHash(this.hash);
  604. }
  605. get encodedAuth() {
  606. if (!this.auth) {
  607. return "";
  608. }
  609. const { username, password } = parseAuth(this.auth);
  610. return encodeURIComponent(username) + (password ? ":" + encodeURIComponent(password) : "");
  611. }
  612. get href() {
  613. const auth = this.encodedAuth;
  614. const originWithAuth = (this.protocol ? this.protocol + "//" : "") + (auth ? auth + "@" : "") + encodeHost(this.host);
  615. return this.hasProtocol && this.isAbsolute ? originWithAuth + this.fullpath : this.fullpath;
  616. }
  617. append(url) {
  618. if (url.hasProtocol) {
  619. throw new Error("Cannot append a URL with protocol");
  620. }
  621. Object.assign(this.query, url.query);
  622. if (url.pathname) {
  623. this.pathname = withTrailingSlash(this.pathname) + withoutLeadingSlash(url.pathname);
  624. }
  625. if (url.hash) {
  626. this.hash = url.hash;
  627. }
  628. }
  629. toJSON() {
  630. return this.href;
  631. }
  632. toString() {
  633. return this.href;
  634. }
  635. }
  636. function createURL(input) {
  637. return new $URL(input);
  638. }
  639. export { $URL, cleanDoubleSlashes, createURL, decode, decodePath, decodeQueryKey, decodeQueryValue, encode, encodeHash, encodeHost, encodeParam, encodePath, encodeQueryItem, encodeQueryKey, encodeQueryValue, filterQuery, getQuery, hasLeadingSlash, hasProtocol, hasTrailingSlash, isEmptyURL, isEqual, isNonEmptyURL, isRelative, isSamePath, isScriptProtocol, joinRelativeURL, joinURL, normalizeURL, parseAuth, parseFilename, parseHost, parsePath, parseQuery, parseURL, resolveURL, stringifyParsedURL, stringifyQuery, withBase, withFragment, withHttp, withHttps, withLeadingSlash, withProtocol, withQuery, withTrailingSlash, withoutBase, withoutFragment, withoutHost, withoutLeadingSlash, withoutProtocol, withoutTrailingSlash };