node.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // src/router/reg-exp-router/node.ts
  2. var LABEL_REG_EXP_STR = "[^/]+";
  3. var ONLY_WILDCARD_REG_EXP_STR = ".*";
  4. var TAIL_WILDCARD_REG_EXP_STR = "(?:|/.*)";
  5. var PATH_ERROR = /* @__PURE__ */ Symbol();
  6. var regExpMetaChars = new Set(".\\+*[^]$()");
  7. function compareKey(a, b) {
  8. if (a.length === 1) {
  9. return b.length === 1 ? a < b ? -1 : 1 : -1;
  10. }
  11. if (b.length === 1) {
  12. return 1;
  13. }
  14. if (a === ONLY_WILDCARD_REG_EXP_STR || a === TAIL_WILDCARD_REG_EXP_STR) {
  15. return 1;
  16. } else if (b === ONLY_WILDCARD_REG_EXP_STR || b === TAIL_WILDCARD_REG_EXP_STR) {
  17. return -1;
  18. }
  19. if (a === LABEL_REG_EXP_STR) {
  20. return 1;
  21. } else if (b === LABEL_REG_EXP_STR) {
  22. return -1;
  23. }
  24. return a.length === b.length ? a < b ? -1 : 1 : b.length - a.length;
  25. }
  26. var Node = class _Node {
  27. #index;
  28. #varIndex;
  29. #children = /* @__PURE__ */ Object.create(null);
  30. insert(tokens, index, paramMap, context, pathErrorCheckOnly) {
  31. if (tokens.length === 0) {
  32. if (this.#index !== void 0) {
  33. throw PATH_ERROR;
  34. }
  35. if (pathErrorCheckOnly) {
  36. return;
  37. }
  38. this.#index = index;
  39. return;
  40. }
  41. const [token, ...restTokens] = tokens;
  42. const pattern = token === "*" ? restTokens.length === 0 ? ["", "", ONLY_WILDCARD_REG_EXP_STR] : ["", "", LABEL_REG_EXP_STR] : token === "/*" ? ["", "", TAIL_WILDCARD_REG_EXP_STR] : token.match(/^\:([^\{\}]+)(?:\{(.+)\})?$/);
  43. let node;
  44. if (pattern) {
  45. const name = pattern[1];
  46. let regexpStr = pattern[2] || LABEL_REG_EXP_STR;
  47. if (name && pattern[2]) {
  48. if (regexpStr === ".*") {
  49. throw PATH_ERROR;
  50. }
  51. regexpStr = regexpStr.replace(/^\((?!\?:)(?=[^)]+\)$)/, "(?:");
  52. if (/\((?!\?:)/.test(regexpStr)) {
  53. throw PATH_ERROR;
  54. }
  55. }
  56. node = this.#children[regexpStr];
  57. if (!node) {
  58. if (Object.keys(this.#children).some(
  59. (k) => k !== ONLY_WILDCARD_REG_EXP_STR && k !== TAIL_WILDCARD_REG_EXP_STR
  60. )) {
  61. throw PATH_ERROR;
  62. }
  63. if (pathErrorCheckOnly) {
  64. return;
  65. }
  66. node = this.#children[regexpStr] = new _Node();
  67. if (name !== "") {
  68. node.#varIndex = context.varIndex++;
  69. }
  70. }
  71. if (!pathErrorCheckOnly && name !== "") {
  72. paramMap.push([name, node.#varIndex]);
  73. }
  74. } else {
  75. node = this.#children[token];
  76. if (!node) {
  77. if (Object.keys(this.#children).some(
  78. (k) => k.length > 1 && k !== ONLY_WILDCARD_REG_EXP_STR && k !== TAIL_WILDCARD_REG_EXP_STR
  79. )) {
  80. throw PATH_ERROR;
  81. }
  82. if (pathErrorCheckOnly) {
  83. return;
  84. }
  85. node = this.#children[token] = new _Node();
  86. }
  87. }
  88. node.insert(restTokens, index, paramMap, context, pathErrorCheckOnly);
  89. }
  90. buildRegExpStr() {
  91. const childKeys = Object.keys(this.#children).sort(compareKey);
  92. const strList = childKeys.map((k) => {
  93. const c = this.#children[k];
  94. return (typeof c.#varIndex === "number" ? `(${k})@${c.#varIndex}` : regExpMetaChars.has(k) ? `\\${k}` : k) + c.buildRegExpStr();
  95. });
  96. if (typeof this.#index === "number") {
  97. strList.unshift(`#${this.#index}`);
  98. }
  99. if (strList.length === 0) {
  100. return "";
  101. }
  102. if (strList.length === 1) {
  103. return strList[0];
  104. }
  105. return "(?:" + strList.join("|") + ")";
  106. }
  107. };
  108. export {
  109. Node,
  110. PATH_ERROR
  111. };