diff --git a/src/router/trie-router/node.ts b/src/router/trie-router/node.ts index 3130433b..2d0ae6c3 100644 --- a/src/router/trie-router/node.ts +++ b/src/router/trie-router/node.ts @@ -63,10 +63,6 @@ export class Node { curNode = curNode.children[p] } - if (!curNode.methods.length) { - curNode.methods = [] - } - const m: Record> = Object.create(null) const handlerSet: HandlerSet = { @@ -82,7 +78,7 @@ export class Node { } // getHandlerSets - #gHSets( + #getHandlerSets( node: Node, method: string, nodeParams: Record, @@ -92,7 +88,7 @@ export class Node { for (let i = 0, len = node.methods.length; i < len; i++) { const m = node.methods[i] const handlerSet = (m[method] || m[METHOD_NAME_ALL]) as HandlerParamsSet - const processedSet: Record = Object.create(null) + const processedSet: Record = {} if (handlerSet !== undefined) { handlerSet.params = Object.create(null) for (let i = 0, len = handlerSet.possibleKeys.length; i < len; i++) { @@ -133,10 +129,17 @@ export class Node { // '/hello/*' => match '/hello' if (nextNode.children['*']) { handlerSets.push( - ...this.#gHSets(nextNode.children['*'], method, node.params, Object.create(null)) + ...this.#getHandlerSets( + nextNode.children['*'], + method, + node.params, + Object.create(null) + ) ) } - handlerSets.push(...this.#gHSets(nextNode, method, node.params, Object.create(null))) + handlerSets.push( + ...this.#getHandlerSets(nextNode, method, node.params, Object.create(null)) + ) } else { tempNodes.push(nextNode) } @@ -152,7 +155,9 @@ export class Node { if (pattern === '*') { const astNode = node.children['*'] if (astNode) { - handlerSets.push(...this.#gHSets(astNode, method, node.params, Object.create(null))) + handlerSets.push( + ...this.#getHandlerSets(astNode, method, node.params, Object.create(null)) + ) tempNodes.push(astNode) } continue @@ -170,24 +175,22 @@ export class Node { const restPathString = parts.slice(i).join('/') if (matcher instanceof RegExp && matcher.test(restPathString)) { params[name] = restPathString - handlerSets.push(...this.#gHSets(child, method, node.params, params)) + handlerSets.push(...this.#getHandlerSets(child, method, node.params, params)) continue } if (matcher === true || matcher.test(part)) { - if (typeof key === 'string') { - params[name] = part - if (isLast) { - handlerSets.push(...this.#gHSets(child, method, params, node.params)) - if (child.children['*']) { - handlerSets.push( - ...this.#gHSets(child.children['*'], method, params, node.params) - ) - } - } else { - child.params = params - tempNodes.push(child) + params[name] = part + if (isLast) { + handlerSets.push(...this.#getHandlerSets(child, method, params, node.params)) + if (child.children['*']) { + handlerSets.push( + ...this.#getHandlerSets(child.children['*'], method, params, node.params) + ) } + } else { + child.params = params + tempNodes.push(child) } } }