url.js 7.7 KB

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