diff --git a/src/router/trie-router/node.ts b/src/router/trie-router/node.ts index 94ab4de19..b19f8cff3 100644 --- a/src/router/trie-router/node.ts +++ b/src/router/trie-router/node.ts @@ -13,13 +13,15 @@ type HandlerParamsSet = HandlerSet & { params: Record } +const emptyParams = Object.create(null) + export class Node { #methods: Record>[] #children: Record> #patterns: Pattern[] #order: number = 0 - #params: Record = Object.create(null) + #params: Record = emptyParams constructor(method?: string, handler?: T, children?: Record>) { this.#children = children || Object.create(null) @@ -82,7 +84,7 @@ export class Node { node: Node, method: string, nodeParams: Record, - params: Record + params?: Record ): HandlerParamsSet[] { const handlerSets: HandlerParamsSet[] = [] for (let i = 0, len = node.#methods.length; i < len; i++) { @@ -91,15 +93,16 @@ export class Node { const processedSet: Record = {} if (handlerSet !== undefined) { handlerSet.params = Object.create(null) - for (let i = 0, len = handlerSet.possibleKeys.length; i < len; i++) { - const key = handlerSet.possibleKeys[i] - const processed = processedSet[handlerSet.score] - handlerSet.params[key] = - params[key] && !processed ? params[key] : nodeParams[key] ?? params[key] - processedSet[handlerSet.score] = true - } - handlerSets.push(handlerSet) + if (nodeParams !== emptyParams || (params && params !== emptyParams)) { + for (let i = 0, len = handlerSet.possibleKeys.length; i < len; i++) { + const key = handlerSet.possibleKeys[i] + const processed = processedSet[handlerSet.score] + handlerSet.params[key] = + params?.[key] && !processed ? params[key] : nodeParams[key] ?? params?.[key] + processedSet[handlerSet.score] = true + } + } } } return handlerSets @@ -107,7 +110,7 @@ export class Node { search(method: string, path: string): [[T, Params][]] { const handlerSets: HandlerParamsSet[] = [] - this.#params = Object.create(null) + this.#params = emptyParams // eslint-disable-next-line @typescript-eslint/no-this-alias const curNode: Node = this @@ -129,17 +132,10 @@ export class Node { // '/hello/*' => match '/hello' if (nextNode.#children['*']) { handlerSets.push( - ...this.#getHandlerSets( - nextNode.#children['*'], - method, - node.#params, - Object.create(null) - ) + ...this.#getHandlerSets(nextNode.#children['*'], method, node.#params) ) } - handlerSets.push( - ...this.#getHandlerSets(nextNode, method, node.#params, Object.create(null)) - ) + handlerSets.push(...this.#getHandlerSets(nextNode, method, node.#params)) } else { tempNodes.push(nextNode) } @@ -147,17 +143,14 @@ export class Node { for (let k = 0, len3 = node.#patterns.length; k < len3; k++) { const pattern = node.#patterns[k] - - const params = { ...node.#params } + const params = node.#params === emptyParams ? {} : { ...node.#params } // Wildcard // '/hello/*/foo' => match /hello/bar/foo if (pattern === '*') { const astNode = node.#children['*'] if (astNode) { - handlerSets.push( - ...this.#getHandlerSets(astNode, method, node.#params, Object.create(null)) - ) + handlerSets.push(...this.#getHandlerSets(astNode, method, node.#params)) tempNodes.push(astNode) } continue