hono
Version:
Web framework built on Web Standards
163 lines (162 loc) • 5.69 kB
JavaScript
// src/router/trie-router/node.ts
import { METHOD_NAME_ALL } from "../../router.js";
import { getPattern, splitPath, splitRoutingPath } from "../../utils/url.js";
var emptyParams = /* @__PURE__ */ Object.create(null);
var Node = class {
constructor(method, handler, children) {
this.
this.
if (method && handler) {
const m = /* @__PURE__ */ Object.create(null);
m[method] = { handler, possibleKeys: [], score: 0 };
this.
}
this.
}
insert(method, path, handler) {
this.
let curNode = this;
const parts = splitRoutingPath(path);
const possibleKeys = [];
for (let i = 0, len = parts.length; i < len; i++) {
const p = parts[i];
const nextP = parts[i + 1];
const pattern = getPattern(p, nextP);
const key = Array.isArray(pattern) ? pattern[0] : p;
if (key in curNode.
curNode = curNode.
if (pattern) {
possibleKeys.push(pattern[1]);
}
continue;
}
curNode.
if (pattern) {
curNode.
possibleKeys.push(pattern[1]);
}
curNode = curNode.
}
curNode.
[ ]: {
handler,
possibleKeys: possibleKeys.filter((v, i, a) => a.indexOf(v) === i),
score: this.
}
});
return curNode;
}
const handlerSets = [];
for (let i = 0, len = node.
const m = node.
const handlerSet = m[method] || m[METHOD_NAME_ALL];
const processedSet = {};
if (handlerSet !== void 0) {
handlerSet.params = /* @__PURE__ */ Object.create(null);
handlerSets.push(handlerSet);
if (nodeParams !== emptyParams || params && params !== emptyParams) {
for (let i2 = 0, len2 = handlerSet.possibleKeys.length; i2 < len2; i2++) {
const key = handlerSet.possibleKeys[i2];
const processed = processedSet[handlerSet.score];
handlerSet.params[key] = params?.[key] && !processed ? params[key] : nodeParams[key] ?? params?.[key];
processedSet[handlerSet.score] = true;
}
}
}
}
return handlerSets;
}
search(method, path) {
const handlerSets = [];
this.
const curNode = this;
let curNodes = [curNode];
const parts = splitPath(path);
const curNodesQueue = [];
for (let i = 0, len = parts.length; i < len; i++) {
const part = parts[i];
const isLast = i === len - 1;
const tempNodes = [];
for (let j = 0, len2 = curNodes.length; j < len2; j++) {
const node = curNodes[j];
const nextNode = node.
if (nextNode) {
nextNode.
if (isLast) {
if (nextNode.
handlerSets.push(
...this.
);
}
handlerSets.push(...this.
} else {
tempNodes.push(nextNode);
}
}
for (let k = 0, len3 = node.
const pattern = node.
const params = node.
if (pattern === "*") {
const astNode = node.
if (astNode) {
handlerSets.push(...this.
astNode.
tempNodes.push(astNode);
}
continue;
}
const [key, name, matcher] = pattern;
if (!part && !(matcher instanceof RegExp)) {
continue;
}
const child = node.
const restPathString = parts.slice(i).join("/");
if (matcher instanceof RegExp) {
const m = matcher.exec(restPathString);
if (m) {
params[name] = m[0];
handlerSets.push(...this.
if (Object.keys(child.
child.
const componentCount = m[0].match(/\//)?.length ?? 0;
const targetCurNodes = curNodesQueue[componentCount] ||= [];
targetCurNodes.push(child);
}
continue;
}
}
if (matcher === true || matcher.test(part)) {
params[name] = part;
if (isLast) {
handlerSets.push(...this.
if (child.
handlerSets.push(
...this.
);
}
} else {
child.
tempNodes.push(child);
}
}
}
}
curNodes = tempNodes.concat(curNodesQueue.shift() ?? []);
}
if (handlerSets.length > 1) {
handlerSets.sort((a, b) => {
return a.score - b.score;
});
}
return [handlerSets.map(({ handler, params }) => [handler, params])];
}
};
export {
Node
};