url.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // src/utils/url.ts
  2. var splitPath = (path) => {
  3. const paths = path.split("/");
  4. if (paths[0] === "") {
  5. paths.shift();
  6. }
  7. return paths;
  8. };
  9. var splitRoutingPath = (routePath) => {
  10. const { groups, path } = extractGroupsFromPath(routePath);
  11. const paths = splitPath(path);
  12. return replaceGroupMarks(paths, groups);
  13. };
  14. var extractGroupsFromPath = (path) => {
  15. const groups = [];
  16. path = path.replace(/\{[^}]+\}/g, (match, index) => {
  17. const mark = `@${index}`;
  18. groups.push([mark, match]);
  19. return mark;
  20. });
  21. return { groups, path };
  22. };
  23. var replaceGroupMarks = (paths, groups) => {
  24. for (let i = groups.length - 1; i >= 0; i--) {
  25. const [mark] = groups[i];
  26. for (let j = paths.length - 1; j >= 0; j--) {
  27. if (paths[j].includes(mark)) {
  28. paths[j] = paths[j].replace(mark, groups[i][1]);
  29. break;
  30. }
  31. }
  32. }
  33. return paths;
  34. };
  35. var patternCache = {};
  36. var getPattern = (label, next) => {
  37. if (label === "*") {
  38. return "*";
  39. }
  40. const match = label.match(/^\:([^\{\}]+)(?:\{(.+)\})?$/);
  41. if (match) {
  42. const cacheKey = `${label}#${next}`;
  43. if (!patternCache[cacheKey]) {
  44. if (match[2]) {
  45. patternCache[cacheKey] = next && next[0] !== ":" && next[0] !== "*" ? [cacheKey, match[1], new RegExp(`^${match[2]}(?=/${next})`)] : [label, match[1], new RegExp(`^${match[2]}$`)];
  46. } else {
  47. patternCache[cacheKey] = [label, match[1], true];
  48. }
  49. }
  50. return patternCache[cacheKey];
  51. }
  52. return null;
  53. };
  54. var tryDecode = (str, decoder) => {
  55. try {
  56. return decoder(str);
  57. } catch {
  58. return str.replace(/(?:%[0-9A-Fa-f]{2})+/g, (match) => {
  59. try {
  60. return decoder(match);
  61. } catch {
  62. return match;
  63. }
  64. });
  65. }
  66. };
  67. var tryDecodeURI = (str) => tryDecode(str, decodeURI);
  68. var getPath = (request) => {
  69. const url = request.url;
  70. const start = url.indexOf("/", url.indexOf(":") + 4);
  71. let i = start;
  72. for (; i < url.length; i++) {
  73. const charCode = url.charCodeAt(i);
  74. if (charCode === 37) {
  75. const queryIndex = url.indexOf("?", i);
  76. const hashIndex = url.indexOf("#", i);
  77. const end = queryIndex === -1 ? hashIndex === -1 ? void 0 : hashIndex : hashIndex === -1 ? queryIndex : Math.min(queryIndex, hashIndex);
  78. const path = url.slice(start, end);
  79. return tryDecodeURI(path.includes("%25") ? path.replace(/%25/g, "%2525") : path);
  80. } else if (charCode === 63 || charCode === 35) {
  81. break;
  82. }
  83. }
  84. return url.slice(start, i);
  85. };
  86. var getQueryStrings = (url) => {
  87. const queryIndex = url.indexOf("?", 8);
  88. return queryIndex === -1 ? "" : "?" + url.slice(queryIndex + 1);
  89. };
  90. var getPathNoStrict = (request) => {
  91. const result = getPath(request);
  92. return result.length > 1 && result.at(-1) === "/" ? result.slice(0, -1) : result;
  93. };
  94. var mergePath = (base, sub, ...rest) => {
  95. if (rest.length) {
  96. sub = mergePath(sub, ...rest);
  97. }
  98. return `${base?.[0] === "/" ? "" : "/"}${base}${sub === "/" ? "" : `${base?.at(-1) === "/" ? "" : "/"}${sub?.[0] === "/" ? sub.slice(1) : sub}`}`;
  99. };
  100. var checkOptionalParameter = (path) => {
  101. if (path.charCodeAt(path.length - 1) !== 63 || !path.includes(":")) {
  102. return null;
  103. }
  104. const segments = path.split("/");
  105. const results = [];
  106. let basePath = "";
  107. segments.forEach((segment) => {
  108. if (segment !== "" && !/\:/.test(segment)) {
  109. basePath += "/" + segment;
  110. } else if (/\:/.test(segment)) {
  111. if (/\?/.test(segment)) {
  112. if (results.length === 0 && basePath === "") {
  113. results.push("/");
  114. } else {
  115. results.push(basePath);
  116. }
  117. const optionalSegment = segment.replace("?", "");
  118. basePath += "/" + optionalSegment;
  119. results.push(basePath);
  120. } else {
  121. basePath += "/" + segment;
  122. }
  123. }
  124. });
  125. return results.filter((v, i, a) => a.indexOf(v) === i);
  126. };
  127. var _decodeURI = (value) => {
  128. if (!/[%+]/.test(value)) {
  129. return value;
  130. }
  131. if (value.indexOf("+") !== -1) {
  132. value = value.replace(/\+/g, " ");
  133. }
  134. return value.indexOf("%") !== -1 ? tryDecode(value, decodeURIComponent_) : value;
  135. };
  136. var _getQueryParam = (url, key, multiple) => {
  137. let encoded;
  138. if (!multiple && key && !/[%+]/.test(key)) {
  139. let keyIndex2 = url.indexOf("?", 8);
  140. if (keyIndex2 === -1) {
  141. return void 0;
  142. }
  143. if (!url.startsWith(key, keyIndex2 + 1)) {
  144. keyIndex2 = url.indexOf(`&${key}`, keyIndex2 + 1);
  145. }
  146. while (keyIndex2 !== -1) {
  147. const trailingKeyCode = url.charCodeAt(keyIndex2 + key.length + 1);
  148. if (trailingKeyCode === 61) {
  149. const valueIndex = keyIndex2 + key.length + 2;
  150. const endIndex = url.indexOf("&", valueIndex);
  151. return _decodeURI(url.slice(valueIndex, endIndex === -1 ? void 0 : endIndex));
  152. } else if (trailingKeyCode == 38 || isNaN(trailingKeyCode)) {
  153. return "";
  154. }
  155. keyIndex2 = url.indexOf(`&${key}`, keyIndex2 + 1);
  156. }
  157. encoded = /[%+]/.test(url);
  158. if (!encoded) {
  159. return void 0;
  160. }
  161. }
  162. const results = {};
  163. encoded ??= /[%+]/.test(url);
  164. let keyIndex = url.indexOf("?", 8);
  165. while (keyIndex !== -1) {
  166. const nextKeyIndex = url.indexOf("&", keyIndex + 1);
  167. let valueIndex = url.indexOf("=", keyIndex);
  168. if (valueIndex > nextKeyIndex && nextKeyIndex !== -1) {
  169. valueIndex = -1;
  170. }
  171. let name = url.slice(
  172. keyIndex + 1,
  173. valueIndex === -1 ? nextKeyIndex === -1 ? void 0 : nextKeyIndex : valueIndex
  174. );
  175. if (encoded) {
  176. name = _decodeURI(name);
  177. }
  178. keyIndex = nextKeyIndex;
  179. if (name === "") {
  180. continue;
  181. }
  182. let value;
  183. if (valueIndex === -1) {
  184. value = "";
  185. } else {
  186. value = url.slice(valueIndex + 1, nextKeyIndex === -1 ? void 0 : nextKeyIndex);
  187. if (encoded) {
  188. value = _decodeURI(value);
  189. }
  190. }
  191. if (multiple) {
  192. if (!(results[name] && Array.isArray(results[name]))) {
  193. results[name] = [];
  194. }
  195. ;
  196. results[name].push(value);
  197. } else {
  198. results[name] ??= value;
  199. }
  200. }
  201. return key ? results[key] : results;
  202. };
  203. var getQueryParam = _getQueryParam;
  204. var getQueryParams = (url, key) => {
  205. return _getQueryParam(url, key, true);
  206. };
  207. var decodeURIComponent_ = decodeURIComponent;
  208. export {
  209. checkOptionalParameter,
  210. decodeURIComponent_,
  211. getPath,
  212. getPathNoStrict,
  213. getPattern,
  214. getQueryParam,
  215. getQueryParams,
  216. getQueryStrings,
  217. mergePath,
  218. splitPath,
  219. splitRoutingPath,
  220. tryDecode,
  221. tryDecodeURI
  222. };