node.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 node_exports = {};
  19. __export(node_exports, {
  20. Node: () => Node
  21. });
  22. module.exports = __toCommonJS(node_exports);
  23. var import_router = require("../../router");
  24. var import_url = require("../../utils/url");
  25. const emptyParams = /* @__PURE__ */ Object.create(null);
  26. const hasChildren = (children) => {
  27. for (const _ in children) {
  28. return true;
  29. }
  30. return false;
  31. };
  32. class Node {
  33. #methods;
  34. #children;
  35. #patterns;
  36. #order = 0;
  37. #params = emptyParams;
  38. constructor(method, handler, children) {
  39. this.#children = children || /* @__PURE__ */ Object.create(null);
  40. this.#methods = [];
  41. if (method && handler) {
  42. const m = /* @__PURE__ */ Object.create(null);
  43. m[method] = { handler, possibleKeys: [], score: 0 };
  44. this.#methods = [m];
  45. }
  46. this.#patterns = [];
  47. }
  48. insert(method, path, handler) {
  49. this.#order = ++this.#order;
  50. let curNode = this;
  51. const parts = (0, import_url.splitRoutingPath)(path);
  52. const possibleKeys = [];
  53. for (let i = 0, len = parts.length; i < len; i++) {
  54. const p = parts[i];
  55. const nextP = parts[i + 1];
  56. const pattern = (0, import_url.getPattern)(p, nextP);
  57. const key = Array.isArray(pattern) ? pattern[0] : p;
  58. if (key in curNode.#children) {
  59. curNode = curNode.#children[key];
  60. if (pattern) {
  61. possibleKeys.push(pattern[1]);
  62. }
  63. continue;
  64. }
  65. curNode.#children[key] = new Node();
  66. if (pattern) {
  67. curNode.#patterns.push(pattern);
  68. possibleKeys.push(pattern[1]);
  69. }
  70. curNode = curNode.#children[key];
  71. }
  72. curNode.#methods.push({
  73. [method]: {
  74. handler,
  75. possibleKeys: possibleKeys.filter((v, i, a) => a.indexOf(v) === i),
  76. score: this.#order
  77. }
  78. });
  79. return curNode;
  80. }
  81. #pushHandlerSets(handlerSets, node, method, nodeParams, params) {
  82. for (let i = 0, len = node.#methods.length; i < len; i++) {
  83. const m = node.#methods[i];
  84. const handlerSet = m[method] || m[import_router.METHOD_NAME_ALL];
  85. const processedSet = {};
  86. if (handlerSet !== void 0) {
  87. handlerSet.params = /* @__PURE__ */ Object.create(null);
  88. handlerSets.push(handlerSet);
  89. if (nodeParams !== emptyParams || params && params !== emptyParams) {
  90. for (let i2 = 0, len2 = handlerSet.possibleKeys.length; i2 < len2; i2++) {
  91. const key = handlerSet.possibleKeys[i2];
  92. const processed = processedSet[handlerSet.score];
  93. handlerSet.params[key] = params?.[key] && !processed ? params[key] : nodeParams[key] ?? params?.[key];
  94. processedSet[handlerSet.score] = true;
  95. }
  96. }
  97. }
  98. }
  99. }
  100. search(method, path) {
  101. const handlerSets = [];
  102. this.#params = emptyParams;
  103. const curNode = this;
  104. let curNodes = [curNode];
  105. const parts = (0, import_url.splitPath)(path);
  106. const curNodesQueue = [];
  107. const len = parts.length;
  108. let partOffsets = null;
  109. for (let i = 0; i < len; i++) {
  110. const part = parts[i];
  111. const isLast = i === len - 1;
  112. const tempNodes = [];
  113. for (let j = 0, len2 = curNodes.length; j < len2; j++) {
  114. const node = curNodes[j];
  115. const nextNode = node.#children[part];
  116. if (nextNode) {
  117. nextNode.#params = node.#params;
  118. if (isLast) {
  119. if (nextNode.#children["*"]) {
  120. this.#pushHandlerSets(handlerSets, nextNode.#children["*"], method, node.#params);
  121. }
  122. this.#pushHandlerSets(handlerSets, nextNode, method, node.#params);
  123. } else {
  124. tempNodes.push(nextNode);
  125. }
  126. }
  127. for (let k = 0, len3 = node.#patterns.length; k < len3; k++) {
  128. const pattern = node.#patterns[k];
  129. const params = node.#params === emptyParams ? {} : { ...node.#params };
  130. if (pattern === "*") {
  131. const astNode = node.#children["*"];
  132. if (astNode) {
  133. this.#pushHandlerSets(handlerSets, astNode, method, node.#params);
  134. astNode.#params = params;
  135. tempNodes.push(astNode);
  136. }
  137. continue;
  138. }
  139. const [key, name, matcher] = pattern;
  140. if (!part && !(matcher instanceof RegExp)) {
  141. continue;
  142. }
  143. const child = node.#children[key];
  144. if (matcher instanceof RegExp) {
  145. if (partOffsets === null) {
  146. partOffsets = new Array(len);
  147. let offset = path[0] === "/" ? 1 : 0;
  148. for (let p = 0; p < len; p++) {
  149. partOffsets[p] = offset;
  150. offset += parts[p].length + 1;
  151. }
  152. }
  153. const restPathString = path.substring(partOffsets[i]);
  154. const m = matcher.exec(restPathString);
  155. if (m) {
  156. params[name] = m[0];
  157. this.#pushHandlerSets(handlerSets, child, method, node.#params, params);
  158. if (hasChildren(child.#children)) {
  159. child.#params = params;
  160. const componentCount = m[0].match(/\//)?.length ?? 0;
  161. const targetCurNodes = curNodesQueue[componentCount] ||= [];
  162. targetCurNodes.push(child);
  163. }
  164. continue;
  165. }
  166. }
  167. if (matcher === true || matcher.test(part)) {
  168. params[name] = part;
  169. if (isLast) {
  170. this.#pushHandlerSets(handlerSets, child, method, params, node.#params);
  171. if (child.#children["*"]) {
  172. this.#pushHandlerSets(
  173. handlerSets,
  174. child.#children["*"],
  175. method,
  176. params,
  177. node.#params
  178. );
  179. }
  180. } else {
  181. child.#params = params;
  182. tempNodes.push(child);
  183. }
  184. }
  185. }
  186. }
  187. const shifted = curNodesQueue.shift();
  188. curNodes = shifted ? tempNodes.concat(shifted) : tempNodes;
  189. }
  190. if (handlerSets.length > 1) {
  191. handlerSets.sort((a, b) => {
  192. return a.score - b.score;
  193. });
  194. }
  195. return [handlerSets.map(({ handler, params }) => [handler, params])];
  196. }
  197. }
  198. // Annotate the CommonJS export names for ESM import in node:
  199. 0 && (module.exports = {
  200. Node
  201. });