index.mjs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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. return input;
  268. }
  269. return joinURL(_base, input);
  270. }
  271. function withoutBase(input, base) {
  272. if (isEmptyURL(base)) {
  273. return input;
  274. }
  275. const _base = withoutTrailingSlash(base);
  276. if (!input.startsWith(_base)) {
  277. return input;
  278. }
  279. const trimmed = input.slice(_base.length);
  280. return trimmed[0] === "/" ? trimmed : "/" + trimmed;
  281. }
  282. function withQuery(input, query) {
  283. const parsed = parseURL(input);
  284. const mergedQuery = { ...parseQuery(parsed.search), ...query };
  285. parsed.search = stringifyQuery(mergedQuery);
  286. return stringifyParsedURL(parsed);
  287. }
  288. function filterQuery(input, predicate) {
  289. if (!input.includes("?")) {
  290. return input;
  291. }
  292. const parsed = parseURL(input);
  293. const query = parseQuery(parsed.search);
  294. const filteredQuery = Object.fromEntries(
  295. Object.entries(query).filter(([key, value]) => predicate(key, value))
  296. );
  297. parsed.search = stringifyQuery(filteredQuery);
  298. return stringifyParsedURL(parsed);
  299. }
  300. function getQuery(input) {
  301. return parseQuery(parseURL(input).search);
  302. }
  303. function isEmptyURL(url) {
  304. return !url || url === "/";
  305. }
  306. function isNonEmptyURL(url) {
  307. return url && url !== "/";
  308. }
  309. function joinURL(base, ...input) {
  310. let url = base || "";
  311. for (const segment of input.filter((url2) => isNonEmptyURL(url2))) {
  312. if (url) {
  313. const _segment = segment.replace(JOIN_LEADING_SLASH_RE, "");
  314. url = withTrailingSlash(url) + _segment;
  315. } else {
  316. url = segment;
  317. }
  318. }
  319. return url;
  320. }
  321. function joinRelativeURL(..._input) {
  322. const JOIN_SEGMENT_SPLIT_RE = /\/(?!\/)/;
  323. const input = _input.filter(Boolean);
  324. const segments = [];
  325. let segmentsDepth = 0;
  326. for (const i of input) {
  327. if (!i || i === "/") {
  328. continue;
  329. }
  330. for (const [sindex, s] of i.split(JOIN_SEGMENT_SPLIT_RE).entries()) {
  331. if (!s || s === ".") {
  332. continue;
  333. }
  334. if (s === "..") {
  335. if (segments.length === 1 && hasProtocol(segments[0])) {
  336. continue;
  337. }
  338. segments.pop();
  339. segmentsDepth--;
  340. continue;
  341. }
  342. if (sindex === 1 && segments[segments.length - 1]?.endsWith(":/")) {
  343. segments[segments.length - 1] += "/" + s;
  344. continue;
  345. }
  346. segments.push(s);
  347. segmentsDepth++;
  348. }
  349. }
  350. let url = segments.join("/");
  351. if (segmentsDepth >= 0) {
  352. if (input[0]?.startsWith("/") && !url.startsWith("/")) {
  353. url = "/" + url;
  354. } else if (input[0]?.startsWith("./") && !url.startsWith("./")) {
  355. url = "./" + url;
  356. }
  357. } else {
  358. url = "../".repeat(-1 * segmentsDepth) + url;
  359. }
  360. if (input[input.length - 1]?.endsWith("/") && !url.endsWith("/")) {
  361. url += "/";
  362. }
  363. return url;
  364. }
  365. function withHttp(input) {
  366. return withProtocol(input, "http://");
  367. }
  368. function withHttps(input) {
  369. return withProtocol(input, "https://");
  370. }
  371. function withoutProtocol(input) {
  372. return withProtocol(input, "");
  373. }
  374. function withProtocol(input, protocol) {
  375. let match = input.match(PROTOCOL_REGEX);
  376. if (!match) {
  377. match = input.match(/^\/{2,}/);
  378. }
  379. if (!match) {
  380. return protocol + input;
  381. }
  382. return protocol + input.slice(match[0].length);
  383. }
  384. function normalizeURL(input) {
  385. const parsed = parseURL(input);
  386. parsed.pathname = encodePath(decodePath(parsed.pathname));
  387. parsed.hash = encodeHash(decode(parsed.hash));
  388. parsed.host = encodeHost(decode(parsed.host));
  389. parsed.search = stringifyQuery(parseQuery(parsed.search));
  390. return stringifyParsedURL(parsed);
  391. }
  392. function resolveURL(base = "", ...inputs) {
  393. if (typeof base !== "string") {
  394. throw new TypeError(
  395. `URL input should be string received ${typeof base} (${base})`
  396. );
  397. }
  398. const filteredInputs = inputs.filter((input) => isNonEmptyURL(input));
  399. if (filteredInputs.length === 0) {
  400. return base;
  401. }
  402. const url = parseURL(base);
  403. for (const inputSegment of filteredInputs) {
  404. const urlSegment = parseURL(inputSegment);
  405. if (urlSegment.pathname) {
  406. url.pathname = withTrailingSlash(url.pathname) + withoutLeadingSlash(urlSegment.pathname);
  407. }
  408. if (urlSegment.hash && urlSegment.hash !== "#") {
  409. url.hash = urlSegment.hash;
  410. }
  411. if (urlSegment.search && urlSegment.search !== "?") {
  412. if (url.search && url.search !== "?") {
  413. const queryString = stringifyQuery({
  414. ...parseQuery(url.search),
  415. ...parseQuery(urlSegment.search)
  416. });
  417. url.search = queryString.length > 0 ? "?" + queryString : "";
  418. } else {
  419. url.search = urlSegment.search;
  420. }
  421. }
  422. }
  423. return stringifyParsedURL(url);
  424. }
  425. function isSamePath(p1, p2) {
  426. return decode(withoutTrailingSlash(p1)) === decode(withoutTrailingSlash(p2));
  427. }
  428. function isEqual(a, b, options = {}) {
  429. if (!options.trailingSlash) {
  430. a = withTrailingSlash(a);
  431. b = withTrailingSlash(b);
  432. }
  433. if (!options.leadingSlash) {
  434. a = withLeadingSlash(a);
  435. b = withLeadingSlash(b);
  436. }
  437. if (!options.encoding) {
  438. a = decode(a);
  439. b = decode(b);
  440. }
  441. return a === b;
  442. }
  443. function withFragment(input, hash) {
  444. if (!hash || hash === "#") {
  445. return input;
  446. }
  447. const parsed = parseURL(input);
  448. parsed.hash = hash === "" ? "" : "#" + encodeHash(hash);
  449. return stringifyParsedURL(parsed);
  450. }
  451. function withoutFragment(input) {
  452. return stringifyParsedURL({ ...parseURL(input), hash: "" });
  453. }
  454. function withoutHost(input) {
  455. const parsed = parseURL(input);
  456. return (parsed.pathname || "/") + parsed.search + parsed.hash;
  457. }
  458. const protocolRelative = Symbol.for("ufo:protocolRelative");
  459. function parseURL(input = "", defaultProto) {
  460. const _specialProtoMatch = input.match(
  461. /^[\s\0]*(blob:|data:|javascript:|vbscript:)(.*)/i
  462. );
  463. if (_specialProtoMatch) {
  464. const [, _proto, _pathname = ""] = _specialProtoMatch;
  465. return {
  466. protocol: _proto.toLowerCase(),
  467. pathname: _pathname,
  468. href: _proto + _pathname,
  469. auth: "",
  470. host: "",
  471. search: "",
  472. hash: ""
  473. };
  474. }
  475. if (!hasProtocol(input, { acceptRelative: true })) {
  476. return defaultProto ? parseURL(defaultProto + input) : parsePath(input);
  477. }
  478. const [, protocol = "", auth, hostAndPath = ""] = input.replace(/\\/g, "/").match(/^[\s\0]*([\w+.-]{2,}:)?\/\/([^/@]+@)?(.*)/) || [];
  479. let [, host = "", path = ""] = hostAndPath.match(/([^#/?]*)(.*)?/) || [];
  480. if (protocol === "file:") {
  481. path = path.replace(/\/(?=[A-Za-z]:)/, "");
  482. }
  483. const { pathname, search, hash } = parsePath(path);
  484. return {
  485. protocol: protocol.toLowerCase(),
  486. auth: auth ? auth.slice(0, Math.max(0, auth.length - 1)) : "",
  487. host,
  488. pathname,
  489. search,
  490. hash,
  491. [protocolRelative]: !protocol
  492. };
  493. }
  494. function parsePath(input = "") {
  495. const [pathname = "", search = "", hash = ""] = (input.match(/([^#?]*)(\?[^#]*)?(#.*)?/) || []).splice(1);
  496. return {
  497. pathname,
  498. search,
  499. hash
  500. };
  501. }
  502. function parseAuth(input = "") {
  503. const [username, password] = input.split(":");
  504. return {
  505. username: decode(username),
  506. password: decode(password)
  507. };
  508. }
  509. function parseHost(input = "") {
  510. const [hostname, port] = (input.match(/([^/:]*):?(\d+)?/) || []).splice(1);
  511. return {
  512. hostname: decode(hostname),
  513. port
  514. };
  515. }
  516. function stringifyParsedURL(parsed) {
  517. const pathname = parsed.pathname || "";
  518. const search = parsed.search ? (parsed.search.startsWith("?") ? "" : "?") + parsed.search : "";
  519. const hash = parsed.hash || "";
  520. const auth = parsed.auth ? parsed.auth + "@" : "";
  521. const host = parsed.host || "";
  522. const proto = parsed.protocol || parsed[protocolRelative] ? (parsed.protocol || "") + "//" : "";
  523. return proto + auth + host + pathname + search + hash;
  524. }
  525. const FILENAME_STRICT_REGEX = /\/([^/]+\.[^/]+)$/;
  526. const FILENAME_REGEX = /\/([^/]+)$/;
  527. function parseFilename(input = "", opts) {
  528. const { pathname } = parseURL(input);
  529. const matches = opts?.strict ? pathname.match(FILENAME_STRICT_REGEX) : pathname.match(FILENAME_REGEX);
  530. return matches ? matches[1] : void 0;
  531. }
  532. class $URL {
  533. protocol;
  534. host;
  535. auth;
  536. pathname;
  537. query = {};
  538. hash;
  539. constructor(input = "") {
  540. if (typeof input !== "string") {
  541. throw new TypeError(
  542. `URL input should be string received ${typeof input} (${input})`
  543. );
  544. }
  545. const parsed = parseURL(input);
  546. this.protocol = decode(parsed.protocol);
  547. this.host = decode(parsed.host);
  548. this.auth = decode(parsed.auth);
  549. this.pathname = decodePath(parsed.pathname);
  550. this.query = parseQuery(parsed.search);
  551. this.hash = decode(parsed.hash);
  552. }
  553. get hostname() {
  554. return parseHost(this.host).hostname;
  555. }
  556. get port() {
  557. return parseHost(this.host).port || "";
  558. }
  559. get username() {
  560. return parseAuth(this.auth).username;
  561. }
  562. get password() {
  563. return parseAuth(this.auth).password || "";
  564. }
  565. get hasProtocol() {
  566. return this.protocol.length;
  567. }
  568. get isAbsolute() {
  569. return this.hasProtocol || this.pathname[0] === "/";
  570. }
  571. get search() {
  572. const q = stringifyQuery(this.query);
  573. return q.length > 0 ? "?" + q : "";
  574. }
  575. get searchParams() {
  576. const p = new URLSearchParams();
  577. for (const name in this.query) {
  578. const value = this.query[name];
  579. if (Array.isArray(value)) {
  580. for (const v of value) {
  581. p.append(name, v);
  582. }
  583. } else {
  584. p.append(
  585. name,
  586. typeof value === "string" ? value : JSON.stringify(value)
  587. );
  588. }
  589. }
  590. return p;
  591. }
  592. get origin() {
  593. return (this.protocol ? this.protocol + "//" : "") + encodeHost(this.host);
  594. }
  595. get fullpath() {
  596. return encodePath(this.pathname) + this.search + encodeHash(this.hash);
  597. }
  598. get encodedAuth() {
  599. if (!this.auth) {
  600. return "";
  601. }
  602. const { username, password } = parseAuth(this.auth);
  603. return encodeURIComponent(username) + (password ? ":" + encodeURIComponent(password) : "");
  604. }
  605. get href() {
  606. const auth = this.encodedAuth;
  607. const originWithAuth = (this.protocol ? this.protocol + "//" : "") + (auth ? auth + "@" : "") + encodeHost(this.host);
  608. return this.hasProtocol && this.isAbsolute ? originWithAuth + this.fullpath : this.fullpath;
  609. }
  610. append(url) {
  611. if (url.hasProtocol) {
  612. throw new Error("Cannot append a URL with protocol");
  613. }
  614. Object.assign(this.query, url.query);
  615. if (url.pathname) {
  616. this.pathname = withTrailingSlash(this.pathname) + withoutLeadingSlash(url.pathname);
  617. }
  618. if (url.hash) {
  619. this.hash = url.hash;
  620. }
  621. }
  622. toJSON() {
  623. return this.href;
  624. }
  625. toString() {
  626. return this.href;
  627. }
  628. }
  629. function createURL(input) {
  630. return new $URL(input);
  631. }
  632. 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 };