path-to-regexp.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. import {
  2. init_define_APP_INFO
  3. } from "./chunk-XY75H3MP.js";
  4. // dep:path-to-regexp
  5. init_define_APP_INFO();
  6. // node_modules/path-to-regexp/dist.es2015/index.js
  7. init_define_APP_INFO();
  8. function lexer(str) {
  9. var tokens = [];
  10. var i = 0;
  11. while (i < str.length) {
  12. var char = str[i];
  13. if (char === "*" || char === "+" || char === "?") {
  14. tokens.push({ type: "MODIFIER", index: i, value: str[i++] });
  15. continue;
  16. }
  17. if (char === "\\") {
  18. tokens.push({ type: "ESCAPED_CHAR", index: i++, value: str[i++] });
  19. continue;
  20. }
  21. if (char === "{") {
  22. tokens.push({ type: "OPEN", index: i, value: str[i++] });
  23. continue;
  24. }
  25. if (char === "}") {
  26. tokens.push({ type: "CLOSE", index: i, value: str[i++] });
  27. continue;
  28. }
  29. if (char === ":") {
  30. var name = "";
  31. var j = i + 1;
  32. while (j < str.length) {
  33. var code = str.charCodeAt(j);
  34. if (code >= 48 && code <= 57 || code >= 65 && code <= 90 || code >= 97 && code <= 122 || code === 95) {
  35. name += str[j++];
  36. continue;
  37. }
  38. break;
  39. }
  40. if (!name)
  41. throw new TypeError("Missing parameter name at ".concat(i));
  42. tokens.push({ type: "NAME", index: i, value: name });
  43. i = j;
  44. continue;
  45. }
  46. if (char === "(") {
  47. var count = 1;
  48. var pattern = "";
  49. var j = i + 1;
  50. if (str[j] === "?") {
  51. throw new TypeError('Pattern cannot start with "?" at '.concat(j));
  52. }
  53. while (j < str.length) {
  54. if (str[j] === "\\") {
  55. pattern += str[j++] + str[j++];
  56. continue;
  57. }
  58. if (str[j] === ")") {
  59. count--;
  60. if (count === 0) {
  61. j++;
  62. break;
  63. }
  64. } else if (str[j] === "(") {
  65. count++;
  66. if (str[j + 1] !== "?") {
  67. throw new TypeError("Capturing groups are not allowed at ".concat(j));
  68. }
  69. }
  70. pattern += str[j++];
  71. }
  72. if (count)
  73. throw new TypeError("Unbalanced pattern at ".concat(i));
  74. if (!pattern)
  75. throw new TypeError("Missing pattern at ".concat(i));
  76. tokens.push({ type: "PATTERN", index: i, value: pattern });
  77. i = j;
  78. continue;
  79. }
  80. tokens.push({ type: "CHAR", index: i, value: str[i++] });
  81. }
  82. tokens.push({ type: "END", index: i, value: "" });
  83. return tokens;
  84. }
  85. function parse(str, options) {
  86. if (options === void 0) {
  87. options = {};
  88. }
  89. var tokens = lexer(str);
  90. var _a = options.prefixes, prefixes = _a === void 0 ? "./" : _a;
  91. var defaultPattern = "[^".concat(escapeString(options.delimiter || "/#?"), "]+?");
  92. var result = [];
  93. var key = 0;
  94. var i = 0;
  95. var path = "";
  96. var tryConsume = function(type) {
  97. if (i < tokens.length && tokens[i].type === type)
  98. return tokens[i++].value;
  99. };
  100. var mustConsume = function(type) {
  101. var value2 = tryConsume(type);
  102. if (value2 !== void 0)
  103. return value2;
  104. var _a2 = tokens[i], nextType = _a2.type, index = _a2.index;
  105. throw new TypeError("Unexpected ".concat(nextType, " at ").concat(index, ", expected ").concat(type));
  106. };
  107. var consumeText = function() {
  108. var result2 = "";
  109. var value2;
  110. while (value2 = tryConsume("CHAR") || tryConsume("ESCAPED_CHAR")) {
  111. result2 += value2;
  112. }
  113. return result2;
  114. };
  115. while (i < tokens.length) {
  116. var char = tryConsume("CHAR");
  117. var name = tryConsume("NAME");
  118. var pattern = tryConsume("PATTERN");
  119. if (name || pattern) {
  120. var prefix = char || "";
  121. if (prefixes.indexOf(prefix) === -1) {
  122. path += prefix;
  123. prefix = "";
  124. }
  125. if (path) {
  126. result.push(path);
  127. path = "";
  128. }
  129. result.push({
  130. name: name || key++,
  131. prefix,
  132. suffix: "",
  133. pattern: pattern || defaultPattern,
  134. modifier: tryConsume("MODIFIER") || ""
  135. });
  136. continue;
  137. }
  138. var value = char || tryConsume("ESCAPED_CHAR");
  139. if (value) {
  140. path += value;
  141. continue;
  142. }
  143. if (path) {
  144. result.push(path);
  145. path = "";
  146. }
  147. var open = tryConsume("OPEN");
  148. if (open) {
  149. var prefix = consumeText();
  150. var name_1 = tryConsume("NAME") || "";
  151. var pattern_1 = tryConsume("PATTERN") || "";
  152. var suffix = consumeText();
  153. mustConsume("CLOSE");
  154. result.push({
  155. name: name_1 || (pattern_1 ? key++ : ""),
  156. pattern: name_1 && !pattern_1 ? defaultPattern : pattern_1,
  157. prefix,
  158. suffix,
  159. modifier: tryConsume("MODIFIER") || ""
  160. });
  161. continue;
  162. }
  163. mustConsume("END");
  164. }
  165. return result;
  166. }
  167. function compile(str, options) {
  168. return tokensToFunction(parse(str, options), options);
  169. }
  170. function tokensToFunction(tokens, options) {
  171. if (options === void 0) {
  172. options = {};
  173. }
  174. var reFlags = flags(options);
  175. var _a = options.encode, encode = _a === void 0 ? function(x) {
  176. return x;
  177. } : _a, _b = options.validate, validate = _b === void 0 ? true : _b;
  178. var matches = tokens.map(function(token) {
  179. if (typeof token === "object") {
  180. return new RegExp("^(?:".concat(token.pattern, ")$"), reFlags);
  181. }
  182. });
  183. return function(data) {
  184. var path = "";
  185. for (var i = 0; i < tokens.length; i++) {
  186. var token = tokens[i];
  187. if (typeof token === "string") {
  188. path += token;
  189. continue;
  190. }
  191. var value = data ? data[token.name] : void 0;
  192. var optional = token.modifier === "?" || token.modifier === "*";
  193. var repeat = token.modifier === "*" || token.modifier === "+";
  194. if (Array.isArray(value)) {
  195. if (!repeat) {
  196. throw new TypeError('Expected "'.concat(token.name, '" to not repeat, but got an array'));
  197. }
  198. if (value.length === 0) {
  199. if (optional)
  200. continue;
  201. throw new TypeError('Expected "'.concat(token.name, '" to not be empty'));
  202. }
  203. for (var j = 0; j < value.length; j++) {
  204. var segment = encode(value[j], token);
  205. if (validate && !matches[i].test(segment)) {
  206. throw new TypeError('Expected all "'.concat(token.name, '" to match "').concat(token.pattern, '", but got "').concat(segment, '"'));
  207. }
  208. path += token.prefix + segment + token.suffix;
  209. }
  210. continue;
  211. }
  212. if (typeof value === "string" || typeof value === "number") {
  213. var segment = encode(String(value), token);
  214. if (validate && !matches[i].test(segment)) {
  215. throw new TypeError('Expected "'.concat(token.name, '" to match "').concat(token.pattern, '", but got "').concat(segment, '"'));
  216. }
  217. path += token.prefix + segment + token.suffix;
  218. continue;
  219. }
  220. if (optional)
  221. continue;
  222. var typeOfMessage = repeat ? "an array" : "a string";
  223. throw new TypeError('Expected "'.concat(token.name, '" to be ').concat(typeOfMessage));
  224. }
  225. return path;
  226. };
  227. }
  228. function match(str, options) {
  229. var keys = [];
  230. var re = pathToRegexp(str, keys, options);
  231. return regexpToFunction(re, keys, options);
  232. }
  233. function regexpToFunction(re, keys, options) {
  234. if (options === void 0) {
  235. options = {};
  236. }
  237. var _a = options.decode, decode = _a === void 0 ? function(x) {
  238. return x;
  239. } : _a;
  240. return function(pathname) {
  241. var m = re.exec(pathname);
  242. if (!m)
  243. return false;
  244. var path = m[0], index = m.index;
  245. var params = /* @__PURE__ */ Object.create(null);
  246. var _loop_1 = function(i2) {
  247. if (m[i2] === void 0)
  248. return "continue";
  249. var key = keys[i2 - 1];
  250. if (key.modifier === "*" || key.modifier === "+") {
  251. params[key.name] = m[i2].split(key.prefix + key.suffix).map(function(value) {
  252. return decode(value, key);
  253. });
  254. } else {
  255. params[key.name] = decode(m[i2], key);
  256. }
  257. };
  258. for (var i = 1; i < m.length; i++) {
  259. _loop_1(i);
  260. }
  261. return { path, index, params };
  262. };
  263. }
  264. function escapeString(str) {
  265. return str.replace(/([.+*?=^!:${}()[\]|/\\])/g, "\\$1");
  266. }
  267. function flags(options) {
  268. return options && options.sensitive ? "" : "i";
  269. }
  270. function regexpToRegexp(path, keys) {
  271. if (!keys)
  272. return path;
  273. var groupsRegex = /\((?:\?<(.*?)>)?(?!\?)/g;
  274. var index = 0;
  275. var execResult = groupsRegex.exec(path.source);
  276. while (execResult) {
  277. keys.push({
  278. name: execResult[1] || index++,
  279. prefix: "",
  280. suffix: "",
  281. modifier: "",
  282. pattern: ""
  283. });
  284. execResult = groupsRegex.exec(path.source);
  285. }
  286. return path;
  287. }
  288. function arrayToRegexp(paths, keys, options) {
  289. var parts = paths.map(function(path) {
  290. return pathToRegexp(path, keys, options).source;
  291. });
  292. return new RegExp("(?:".concat(parts.join("|"), ")"), flags(options));
  293. }
  294. function stringToRegexp(path, keys, options) {
  295. return tokensToRegexp(parse(path, options), keys, options);
  296. }
  297. function tokensToRegexp(tokens, keys, options) {
  298. if (options === void 0) {
  299. options = {};
  300. }
  301. var _a = options.strict, strict = _a === void 0 ? false : _a, _b = options.start, start = _b === void 0 ? true : _b, _c = options.end, end = _c === void 0 ? true : _c, _d = options.encode, encode = _d === void 0 ? function(x) {
  302. return x;
  303. } : _d, _e = options.delimiter, delimiter = _e === void 0 ? "/#?" : _e, _f = options.endsWith, endsWith = _f === void 0 ? "" : _f;
  304. var endsWithRe = "[".concat(escapeString(endsWith), "]|$");
  305. var delimiterRe = "[".concat(escapeString(delimiter), "]");
  306. var route = start ? "^" : "";
  307. for (var _i = 0, tokens_1 = tokens; _i < tokens_1.length; _i++) {
  308. var token = tokens_1[_i];
  309. if (typeof token === "string") {
  310. route += escapeString(encode(token));
  311. } else {
  312. var prefix = escapeString(encode(token.prefix));
  313. var suffix = escapeString(encode(token.suffix));
  314. if (token.pattern) {
  315. if (keys)
  316. keys.push(token);
  317. if (prefix || suffix) {
  318. if (token.modifier === "+" || token.modifier === "*") {
  319. var mod = token.modifier === "*" ? "?" : "";
  320. route += "(?:".concat(prefix, "((?:").concat(token.pattern, ")(?:").concat(suffix).concat(prefix, "(?:").concat(token.pattern, "))*)").concat(suffix, ")").concat(mod);
  321. } else {
  322. route += "(?:".concat(prefix, "(").concat(token.pattern, ")").concat(suffix, ")").concat(token.modifier);
  323. }
  324. } else {
  325. if (token.modifier === "+" || token.modifier === "*") {
  326. route += "((?:".concat(token.pattern, ")").concat(token.modifier, ")");
  327. } else {
  328. route += "(".concat(token.pattern, ")").concat(token.modifier);
  329. }
  330. }
  331. } else {
  332. route += "(?:".concat(prefix).concat(suffix, ")").concat(token.modifier);
  333. }
  334. }
  335. }
  336. if (end) {
  337. if (!strict)
  338. route += "".concat(delimiterRe, "?");
  339. route += !options.endsWith ? "$" : "(?=".concat(endsWithRe, ")");
  340. } else {
  341. var endToken = tokens[tokens.length - 1];
  342. var isEndDelimited = typeof endToken === "string" ? delimiterRe.indexOf(endToken[endToken.length - 1]) > -1 : endToken === void 0;
  343. if (!strict) {
  344. route += "(?:".concat(delimiterRe, "(?=").concat(endsWithRe, "))?");
  345. }
  346. if (!isEndDelimited) {
  347. route += "(?=".concat(delimiterRe, "|").concat(endsWithRe, ")");
  348. }
  349. }
  350. return new RegExp(route, flags(options));
  351. }
  352. function pathToRegexp(path, keys, options) {
  353. if (path instanceof RegExp)
  354. return regexpToRegexp(path, keys);
  355. if (Array.isArray(path))
  356. return arrayToRegexp(path, keys, options);
  357. return stringToRegexp(path, keys, options);
  358. }
  359. export {
  360. compile,
  361. match,
  362. parse,
  363. pathToRegexp,
  364. regexpToFunction,
  365. tokensToFunction,
  366. tokensToRegexp
  367. };
  368. //# sourceMappingURL=path-to-regexp.js.map