index.mjs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. const n = /[^\0-\x7E]/;
  2. const t = /[\x2E\u3002\uFF0E\uFF61]/g;
  3. const o = { overflow: "Overflow Error", "not-basic": "Illegal Input", "invalid-input": "Invalid Input" };
  4. const e = Math.floor;
  5. const r = String.fromCharCode;
  6. function s(n2) {
  7. throw new RangeError(o[n2]);
  8. }
  9. const c = function(n2, t2) {
  10. return n2 + 22 + 75 * (n2 < 26) - ((t2 != 0) << 5);
  11. };
  12. const u = function(n2, t2, o2) {
  13. let r2 = 0;
  14. for (n2 = o2 ? e(n2 / 700) : n2 >> 1, n2 += e(n2 / t2); n2 > 455; r2 += 36) {
  15. n2 = e(n2 / 35);
  16. }
  17. return e(r2 + 36 * n2 / (n2 + 38));
  18. };
  19. function toASCII(o2) {
  20. return function(n2, o3) {
  21. const e2 = n2.split("@");
  22. let r2 = "";
  23. e2.length > 1 && (r2 = e2[0] + "@", n2 = e2[1]);
  24. const s2 = function(n3, t2) {
  25. const o4 = [];
  26. let e3 = n3.length;
  27. for (; e3--; ) {
  28. o4[e3] = t2(n3[e3]);
  29. }
  30. return o4;
  31. }((n2 = n2.replace(t, ".")).split("."), o3).join(".");
  32. return r2 + s2;
  33. }(o2, function(t2) {
  34. return n.test(t2) ? "xn--" + function(n2) {
  35. const t3 = [];
  36. const o3 = (n2 = function(n3) {
  37. const t4 = [];
  38. let o4 = 0;
  39. const e2 = n3.length;
  40. for (; o4 < e2; ) {
  41. const r2 = n3.charCodeAt(o4++);
  42. if (r2 >= 55296 && r2 <= 56319 && o4 < e2) {
  43. const e3 = n3.charCodeAt(o4++);
  44. (64512 & e3) == 56320 ? t4.push(((1023 & r2) << 10) + (1023 & e3) + 65536) : (t4.push(r2), o4--);
  45. } else {
  46. t4.push(r2);
  47. }
  48. }
  49. return t4;
  50. }(n2)).length;
  51. let f = 128;
  52. let i = 0;
  53. let l = 72;
  54. for (const o4 of n2) {
  55. o4 < 128 && t3.push(r(o4));
  56. }
  57. const h = t3.length;
  58. let p = h;
  59. for (h && t3.push("-"); p < o3; ) {
  60. let o4 = 2147483647;
  61. for (const t4 of n2) {
  62. t4 >= f && t4 < o4 && (o4 = t4);
  63. }
  64. const a = p + 1;
  65. o4 - f > e((2147483647 - i) / a) && s("overflow"), i += (o4 - f) * a, f = o4;
  66. for (const o5 of n2) {
  67. if (o5 < f && ++i > 2147483647 && s("overflow"), o5 == f) {
  68. let n3 = i;
  69. for (let o6 = 36; ; o6 += 36) {
  70. const s2 = o6 <= l ? 1 : o6 >= l + 26 ? 26 : o6 - l;
  71. if (n3 < s2) {
  72. break;
  73. }
  74. const u2 = n3 - s2;
  75. const f2 = 36 - s2;
  76. t3.push(r(c(s2 + u2 % f2, 0))), n3 = e(u2 / f2);
  77. }
  78. t3.push(r(c(n3, 0))), l = u(i, a, p == h), i = 0, ++p;
  79. }
  80. }
  81. ++i, ++f;
  82. }
  83. return t3.join("");
  84. }(t2) : t2;
  85. });
  86. }
  87. const HASH_RE = /#/g;
  88. const AMPERSAND_RE = /&/g;
  89. const SLASH_RE = /\//g;
  90. const EQUAL_RE = /=/g;
  91. const IM_RE = /\?/g;
  92. const PLUS_RE = /\+/g;
  93. const ENC_BRACKET_OPEN_RE = /%5B/gi;
  94. const ENC_BRACKET_CLOSE_RE = /%5D/gi;
  95. const ENC_CARET_RE = /%5E/gi;
  96. const ENC_BACKTICK_RE = /%60/gi;
  97. const ENC_CURLY_OPEN_RE = /%7B/gi;
  98. const ENC_PIPE_RE = /%7C/gi;
  99. const ENC_CURLY_CLOSE_RE = /%7D/gi;
  100. const ENC_SPACE_RE = /%20/gi;
  101. const ENC_SLASH_RE = /%2F/gi;
  102. const ENC_ENC_SLASH_RE = /%252F/gi;
  103. function encode(text) {
  104. return encodeURI("" + text).replace(ENC_PIPE_RE, "|").replace(ENC_BRACKET_OPEN_RE, "[").replace(ENC_BRACKET_CLOSE_RE, "]");
  105. }
  106. function encodeHash(text) {
  107. return encode(text).replace(ENC_CURLY_OPEN_RE, "{").replace(ENC_CURLY_CLOSE_RE, "}").replace(ENC_CARET_RE, "^");
  108. }
  109. function encodeQueryValue(text) {
  110. return encode(text).replace(PLUS_RE, "%2B").replace(ENC_SPACE_RE, "+").replace(HASH_RE, "%23").replace(AMPERSAND_RE, "%26").replace(ENC_BACKTICK_RE, "`").replace(ENC_CURLY_OPEN_RE, "{").replace(ENC_CURLY_CLOSE_RE, "}").replace(ENC_CARET_RE, "^");
  111. }
  112. function encodeQueryKey(text) {
  113. return encodeQueryValue(text).replace(EQUAL_RE, "%3D");
  114. }
  115. function encodePath(text) {
  116. 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");
  117. }
  118. function encodeParam(text) {
  119. return encodePath(text).replace(SLASH_RE, "%2F");
  120. }
  121. function decode(text = "") {
  122. try {
  123. return decodeURIComponent("" + text);
  124. } catch (_err) {
  125. return "" + text;
  126. }
  127. }
  128. function decodePath(text) {
  129. return decode(text.replace(ENC_SLASH_RE, "%252F"));
  130. }
  131. function decodeQueryValue(text) {
  132. return decode(text.replace(PLUS_RE, " "));
  133. }
  134. function encodeHost(name = "") {
  135. return toASCII(name);
  136. }
  137. function parseQuery(paramsStr = "") {
  138. const obj = {};
  139. if (paramsStr[0] === "?") {
  140. paramsStr = paramsStr.substr(1);
  141. }
  142. for (const param of paramsStr.split("&")) {
  143. const s = param.match(/([^=]+)=?(.*)/) || [];
  144. if (s.length < 2) {
  145. continue;
  146. }
  147. const key = decode(s[1]);
  148. if (key === "__proto__" || key === "constructor") {
  149. continue;
  150. }
  151. const value = decodeQueryValue(s[2] || "");
  152. if (obj[key]) {
  153. if (Array.isArray(obj[key])) {
  154. obj[key].push(value);
  155. } else {
  156. obj[key] = [obj[key], value];
  157. }
  158. } else {
  159. obj[key] = value;
  160. }
  161. }
  162. return obj;
  163. }
  164. function encodeQueryItem(key, val) {
  165. if (!val) {
  166. return encodeQueryKey(key);
  167. }
  168. if (Array.isArray(val)) {
  169. return val.map((_val) => `${encodeQueryKey(key)}=${encodeQueryValue(_val)}`).join("&");
  170. }
  171. return `${encodeQueryKey(key)}=${encodeQueryValue(val)}`;
  172. }
  173. function stringifyQuery(query) {
  174. return Object.keys(query).map((k) => encodeQueryItem(k, query[k])).join("&");
  175. }
  176. class $URL {
  177. constructor(input = "") {
  178. this.query = {};
  179. if (typeof input !== "string") {
  180. throw new TypeError(`URL input should be string received ${typeof input} (${input})`);
  181. }
  182. const parsed = parseURL(input);
  183. this.protocol = decode(parsed.protocol);
  184. this.host = decode(parsed.host);
  185. this.auth = decode(parsed.auth);
  186. this.pathname = decodePath(parsed.pathname);
  187. this.query = parseQuery(parsed.search);
  188. this.hash = decode(parsed.hash);
  189. }
  190. get hostname() {
  191. return parseHost(this.host).hostname;
  192. }
  193. get port() {
  194. return parseHost(this.host).port || "";
  195. }
  196. get username() {
  197. return parseAuth(this.auth).username;
  198. }
  199. get password() {
  200. return parseAuth(this.auth).password || "";
  201. }
  202. get hasProtocol() {
  203. return this.protocol.length;
  204. }
  205. get isAbsolute() {
  206. return this.hasProtocol || this.pathname[0] === "/";
  207. }
  208. get search() {
  209. const q = stringifyQuery(this.query);
  210. return q.length ? "?" + q : "";
  211. }
  212. get searchParams() {
  213. const p = new URLSearchParams();
  214. for (const name in this.query) {
  215. const value = this.query[name];
  216. if (Array.isArray(value)) {
  217. value.forEach((v) => p.append(name, v));
  218. } else {
  219. p.append(name, value || "");
  220. }
  221. }
  222. return p;
  223. }
  224. get origin() {
  225. return (this.protocol ? this.protocol + "//" : "") + encodeHost(this.host);
  226. }
  227. get fullpath() {
  228. return encodePath(this.pathname) + this.search + encodeHash(this.hash);
  229. }
  230. get encodedAuth() {
  231. if (!this.auth) {
  232. return "";
  233. }
  234. const { username, password } = parseAuth(this.auth);
  235. return encodeURIComponent(username) + (password ? ":" + encodeURIComponent(password) : "");
  236. }
  237. get href() {
  238. const auth = this.encodedAuth;
  239. const originWithAuth = (this.protocol ? this.protocol + "//" : "") + (auth ? auth + "@" : "") + encodeHost(this.host);
  240. return this.hasProtocol && this.isAbsolute ? originWithAuth + this.fullpath : this.fullpath;
  241. }
  242. append(url) {
  243. if (url.hasProtocol) {
  244. throw new Error("Cannot append a URL with protocol");
  245. }
  246. Object.assign(this.query, url.query);
  247. if (url.pathname) {
  248. this.pathname = withTrailingSlash(this.pathname) + withoutLeadingSlash(url.pathname);
  249. }
  250. if (url.hash) {
  251. this.hash = url.hash;
  252. }
  253. }
  254. toJSON() {
  255. return this.href;
  256. }
  257. toString() {
  258. return this.href;
  259. }
  260. }
  261. function isRelative(inputStr) {
  262. return ["./", "../"].some((str) => inputStr.startsWith(str));
  263. }
  264. function hasProtocol(inputStr, acceptProtocolRelative = false) {
  265. return /^\w+:\/\/.+/.test(inputStr) || acceptProtocolRelative && /^\/\/[^/]+/.test(inputStr);
  266. }
  267. const TRAILING_SLASH_RE = /\/$|\/\?/;
  268. function hasTrailingSlash(input = "", queryParams = false) {
  269. if (!queryParams) {
  270. return input.endsWith("/");
  271. }
  272. return TRAILING_SLASH_RE.test(input);
  273. }
  274. function withoutTrailingSlash(input = "", queryParams = false) {
  275. if (!queryParams) {
  276. return (hasTrailingSlash(input) ? input.slice(0, -1) : input) || "/";
  277. }
  278. if (!hasTrailingSlash(input, true)) {
  279. return input || "/";
  280. }
  281. const [s0, ...s] = input.split("?");
  282. return (s0.slice(0, -1) || "/") + (s.length ? `?${s.join("?")}` : "");
  283. }
  284. function withTrailingSlash(input = "", queryParams = false) {
  285. if (!queryParams) {
  286. return input.endsWith("/") ? input : input + "/";
  287. }
  288. if (hasTrailingSlash(input, true)) {
  289. return input || "/";
  290. }
  291. const [s0, ...s] = input.split("?");
  292. return s0 + "/" + (s.length ? `?${s.join("?")}` : "");
  293. }
  294. function hasLeadingSlash(input = "") {
  295. return input.startsWith("/");
  296. }
  297. function withoutLeadingSlash(input = "") {
  298. return (hasLeadingSlash(input) ? input.substr(1) : input) || "/";
  299. }
  300. function withLeadingSlash(input = "") {
  301. return hasLeadingSlash(input) ? input : "/" + input;
  302. }
  303. function cleanDoubleSlashes(input = "") {
  304. return input.split("://").map((str) => str.replace(/\/{2,}/g, "/")).join("://");
  305. }
  306. function withBase(input, base) {
  307. if (isEmptyURL(base)) {
  308. return input;
  309. }
  310. const _base = withoutTrailingSlash(base);
  311. if (input.startsWith(_base)) {
  312. return input;
  313. }
  314. return joinURL(_base, input);
  315. }
  316. function withoutBase(input, base) {
  317. if (isEmptyURL(base)) {
  318. return input;
  319. }
  320. const _base = withoutTrailingSlash(base);
  321. if (input.startsWith(_base)) {
  322. return input.substr(_base.length) || "/";
  323. }
  324. return input;
  325. }
  326. function withQuery(input, query) {
  327. const parsed = parseURL(input);
  328. const mergedQuery = { ...parseQuery(parsed.search), ...query };
  329. parsed.search = stringifyQuery(mergedQuery);
  330. return stringifyParsedURL(parsed);
  331. }
  332. function getQuery(input) {
  333. return parseQuery(parseURL(input).search);
  334. }
  335. function isEmptyURL(url) {
  336. return !url || url === "/";
  337. }
  338. function isNonEmptyURL(url) {
  339. return url && url !== "/";
  340. }
  341. function joinURL(base, ...input) {
  342. let url = base || "";
  343. for (const i of input.filter(isNonEmptyURL)) {
  344. url = url ? withTrailingSlash(url) + withoutLeadingSlash(i) : i;
  345. }
  346. return url;
  347. }
  348. function createURL(input) {
  349. return new $URL(input);
  350. }
  351. function normalizeURL(input) {
  352. return createURL(input).toString();
  353. }
  354. function resolveURL(base, ...input) {
  355. const url = createURL(base);
  356. for (const i of input.filter(isNonEmptyURL)) {
  357. url.append(createURL(i));
  358. }
  359. return url.toString();
  360. }
  361. function isSamePath(p1, p2) {
  362. return decode(withoutTrailingSlash(p1)) === decode(withoutTrailingSlash(p2));
  363. }
  364. function parseURL(input = "", defaultProto) {
  365. if (!hasProtocol(input, true)) {
  366. return defaultProto ? parseURL(defaultProto + input) : parsePath(input);
  367. }
  368. const [protocol = "", auth, hostAndPath] = (input.replace(/\\/g, "/").match(/([^:/]+:)?\/\/([^/@]+@)?(.*)/) || []).splice(1);
  369. const [host = "", path = ""] = (hostAndPath.match(/([^/?#]*)(.*)?/) || []).splice(1);
  370. const { pathname, search, hash } = parsePath(path);
  371. return {
  372. protocol,
  373. auth: auth ? auth.substr(0, auth.length - 1) : "",
  374. host,
  375. pathname,
  376. search,
  377. hash
  378. };
  379. }
  380. function parsePath(input = "") {
  381. const [pathname = "", search = "", hash = ""] = (input.match(/([^#?]*)(\?[^#]*)?(#.*)?/) || []).splice(1);
  382. return {
  383. pathname,
  384. search,
  385. hash
  386. };
  387. }
  388. function parseAuth(input = "") {
  389. const [username, password] = input.split(":");
  390. return {
  391. username: decode(username),
  392. password: decode(password)
  393. };
  394. }
  395. function parseHost(input = "") {
  396. const [hostname, port] = (input.match(/([^/]*)(:0-9+)?/) || []).splice(1);
  397. return {
  398. hostname: decode(hostname),
  399. port
  400. };
  401. }
  402. function stringifyParsedURL(parsed) {
  403. const fullpath = parsed.pathname + (parsed.search ? (parsed.search.startsWith("?") ? "" : "?") + parsed.search : "") + parsed.hash;
  404. if (!parsed.protocol) {
  405. return fullpath;
  406. }
  407. return parsed.protocol + "//" + (parsed.auth ? parsed.auth + "@" : "") + parsed.host + fullpath;
  408. }
  409. export { $URL, cleanDoubleSlashes, createURL, decode, decodePath, decodeQueryValue, encode, encodeHash, encodeHost, encodeParam, encodePath, encodeQueryItem, encodeQueryKey, encodeQueryValue, getQuery, hasLeadingSlash, hasProtocol, hasTrailingSlash, isEmptyURL, isNonEmptyURL, isRelative, isSamePath, joinURL, normalizeURL, parseAuth, parseHost, parsePath, parseQuery, parseURL, resolveURL, stringifyParsedURL, stringifyQuery, withBase, withLeadingSlash, withQuery, withTrailingSlash, withoutBase, withoutLeadingSlash, withoutTrailingSlash };