node.ts
This file
Path
src/router/trie-router/node.ts
217 lines · 6.5 KiB · TypeScript
Last changed by
perf(router): share null object creation (#5267)Taku Amanobefore adoption4b44abe
History of this fileTake it
The walk stopped after 200 commits: lines older than f0b094d point at it.
Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 1–4why, one step back1import type { Params } from '../../router'2import { METHOD_NAME_ALL } from '../../router'3import type { Pattern } from '../../utils/url'4import { getPattern, splitPath, splitRoutingPath } from '../../utils/url'perf(router): share null object creation (#5267)Taku Amanobefore adoption4b44abe
line 5why, one step back5import { createNullObject } from '../utils'Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 6–16why, one step back67type HandlerSet<T> = {8 handler: T9 possibleKeys: string[]10 score: number11}1213type HandlerParamsSet<T> = HandlerSet<T> & {14 params: Record<string, string>15}16perf(router): share null object creation (#5267)Taku Amanobefore adoption4b44abe
line 17why, one step back17const emptyParams = createNullObject()fix(trie-router): match suffix wildcard routes (#5236)Taku Amanobefore adoption63bbcf5
line 18why, one step back18let order = 0Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 19–20why, one step back1920export class Node<T> {fix(trie-router): match suffix wildcard routes (#5236)Taku Amanobefore adoption63bbcf5
line 21why, one step back21 #methods: Record<string, HandlerSet<T>>[] = []Merge commit from forkYusuke Wadabefore adoptionf0b094d
line 22why, one step back22perf(router): share null object creation (#5267)Taku Amanobefore adoption4b44abe
line 23why, one step back23 #children: Record<string, Node<T>> = createNullObject()fix(trie-router): match suffix wildcard routes (#5236)Taku Amanobefore adoption63bbcf5
lines 24–25why, one step back24 #patterns: Node<T>[] = []25 #pattern?: Pattern | stringMerge commit from forkYusuke Wadabefore adoptionf0b094d
lines 26–27why, one step back26 #params: Record<string, string> = emptyParams27fix(trie-router): match suffix wildcard routes (#5236)Taku Amanobefore adoption63bbcf5
line 28why, one step back28 insert(method: string, path: string, handler: T): void {Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 29–32why, one step back29 // eslint-disable-next-line @typescript-eslint/no-this-alias30 let curNode: Node<T> = this31 const parts = splitRoutingPath(path)32fix(trie-router): match suffix wildcard routes (#5236)Taku Amanobefore adoption63bbcf5
line 33why, one step back33 const possibleKeys = new Set<string>()Merge commit from forkYusuke Wadabefore adoptionf0b094d
line 34why, one step back34fix(trie-router): match suffix wildcard routes (#5236)Taku Amanobefore adoption63bbcf5
lines 35–42why, one step back35 let i = 036 for (const p of parts) {37 const nextP = parts[++i]38 const pattern =39 getPattern(p, nextP) ||40 (nextP === undefined && p && p.indexOf('*') === p.length - 1 ? p : null)41 const isParam = Array.isArray(pattern)42 const key = isParam ? pattern[0] : pattern || pMerge commit from forkYusuke Wadabefore adoptionf0b094d
line 43why, one step back43fix(trie-router): match suffix wildcard routes (#5236)Taku Amanobefore adoption63bbcf5
lines 44–47why, one step back44 const child = (curNode.#children[key] ||= new Node())45 if (pattern && !child.#pattern) {46 child.#pattern = pattern47 curNode.#patterns.push(child)Merge commit from forkYusuke Wadabefore adoptionf0b094d
line 48why, one step back48 }fix(trie-router): match suffix wildcard routes (#5236)Taku Amanobefore adoption63bbcf5
lines 49–51why, one step back49 curNode = child50 if (isParam) {51 possibleKeys.add(pattern[1])Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 52–57why, one step back52 }53 }5455 curNode.#methods.push({56 [method]: {57 handler,fix(trie-router): match suffix wildcard routes (#5236)Taku Amanobefore adoption63bbcf5
lines 58–59why, one step back58 possibleKeys: [...possibleKeys],59 score: ++order,Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 60–73why, one step back60 },61 })62 }6364 #pushHandlerSets(65 handlerSets: HandlerParamsSet<T>[],66 node: Node<T>,67 method: string,68 nodeParams: Record<string, string>,69 params?: Record<string, string>70 ): void {71 for (let i = 0, len = node.#methods.length; i < len; i++) {72 const m = node.#methods[i]73 const handlerSet = (m[method] || m[METHOD_NAME_ALL]) as HandlerParamsSet<T>fix(trie-router): match suffix wildcard routes (#5236)Taku Amanobefore adoption63bbcf5
line 74why, one step back74 if (handlerSet) {perf(router): share null object creation (#5267)Taku Amanobefore adoption4b44abe
line 75why, one step back75 handlerSet.params = createNullObject()Merge commit from forkYusuke Wadabefore adoptionf0b094d
line 76why, one step back76 handlerSets.push(handlerSet)fix(trie-router): match suffix wildcard routes (#5236)Taku Amanobefore adoption63bbcf5
lines 77–80why, one step back77 for (let i = 0, len = handlerSet.possibleKeys.length; i < len; i++) {78 const key = handlerSet.possibleKeys[i]79 handlerSet.params[key] =80 params?.[key] && !i ? params[key] : (nodeParams[key] ?? params?.[key])Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 81–120why, one step back81 }82 }83 }84 }8586 search(method: string, path: string): [[T, Params][]] {87 const handlerSets: HandlerParamsSet<T>[] = []88 this.#params = emptyParams8990 // eslint-disable-next-line @typescript-eslint/no-this-alias91 const curNode: Node<T> = this92 let curNodes = [curNode]93 const parts = splitPath(path)94 const curNodesQueue: Node<T>[][] = []9596 const len = parts.length97 let partOffsets: number[] | null = null9899 for (let i = 0; i < len; i++) {100 const part: string = parts[i]101 const isLast = i === len - 1102 const tempNodes: Node<T>[] = []103104 for (let j = 0, len2 = curNodes.length; j < len2; j++) {105 const node = curNodes[j]106 const nextNode = node.#children[part]107108 if (nextNode) {109 nextNode.#params = node.#params110 if (isLast) {111 // '/hello/*' => match '/hello'112 if (nextNode.#children['*']) {113 this.#pushHandlerSets(handlerSets, nextNode.#children['*'], method, node.#params)114 }115 this.#pushHandlerSets(handlerSets, nextNode, method, node.#params)116 } else {117 tempNodes.push(nextNode)118 }119 }120fix(trie-router): match suffix wildcard routes (#5236)Taku Amanobefore adoption63bbcf5
lines 121–122why, one step back121 for (const child of node.#patterns) {122 const pattern = child.#pattern!Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 123–126why, one step back123 const params = node.#params === emptyParams ? {} : { ...node.#params }124125 // Wildcard126 // '/hello/*/foo' => match /hello/bar/foofix(trie-router): match suffix wildcard routes (#5236)Taku Amanobefore adoption63bbcf5
lines 127–133why, one step back127 if (typeof pattern === 'string') {128 if (pattern === '*' || part.startsWith(pattern.slice(0, -1))) {129 this.#pushHandlerSets(handlerSets, child, method, node.#params)130 if (pattern === '*') {131 child.#params = params132 tempNodes.push(child)133 }Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 134–137why, one step back134 }135 continue136 }137fix(trie-router): match suffix wildcard routes (#5236)Taku Amanobefore adoption63bbcf5
line 138why, one step back138 const [, name, matcher] = patternMerge commit from forkYusuke Wadabefore adoptionf0b094d
line 139why, one step back139fix(trie-router): match suffix wildcard routes (#5236)Taku Amanobefore adoption63bbcf5
line 140why, one step back140 if (!part && matcher === true) {Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 141–144why, one step back141 continue142 }143144 // `/js/:filename{[a-z]+.js}` => match /js/chunk/123.jsfix(trie-router): match suffix wildcard routes (#5236)Taku Amanobefore adoption63bbcf5
lines 145–147why, one step back145 if (matcher !== true) {146 if (!partOffsets) {147 partOffsets = []Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 148–153why, one step back148 let offset = path[0] === '/' ? 1 : 0149 for (let p = 0; p < len; p++) {150 partOffsets[p] = offset151 offset += parts[p].length + 1152 }153 }fix(trie-router): match suffix wildcard routes (#5236)Taku Amanobefore adoption63bbcf5
line 154why, one step back154 const restPathString = path.slice(partOffsets[i])Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 155–160why, one step back155156 const m = matcher.exec(restPathString)157 if (m) {158 params[name] = m[0]159 this.#pushHandlerSets(handlerSets, child, method, node.#params, params)160fix(trie-router): match empty wildcard remainder after regexp param (#5102)Taku Amanobefore adoption0fc7ffc
lines 161–171why, one step back161 // '/:id{[0-9]+}/*' => match '/123'162 if (m[0].length === restPathString.length && child.#children['*']) {163 this.#pushHandlerSets(164 handlerSets,165 child.#children['*'],166 method,167 node.#params,168 params169 )170 }171fix(trie-router): match suffix wildcard routes (#5236)Taku Amanobefore adoption63bbcf5
line 172why, one step back172 for (const _ in child.#children) {Merge commit from forkYusuke Wadabefore adoptionf0b094d
line 173why, one step back173 child.#params = paramsfix(trie-router): count every slash a pattern consumes (#5189)Jayesh Bhadebefore adoption569b419
line 174why, one step back174 const componentCount = m[0].match(/\//g)?.length ?? 0Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 175–176why, one step back175 const targetCurNodes = (curNodesQueue[componentCount] ||= [])176 targetCurNodes.push(child)fix(trie-router): match suffix wildcard routes (#5236)Taku Amanobefore adoption63bbcf5
line 177why, one step back177 breakMerge commit from forkYusuke Wadabefore adoptionf0b094d
lines 178–208why, one step back178 }179180 continue181 }182 }183184 if (matcher === true || matcher.test(part)) {185 params[name] = part186 if (isLast) {187 this.#pushHandlerSets(handlerSets, child, method, params, node.#params)188 if (child.#children['*']) {189 this.#pushHandlerSets(190 handlerSets,191 child.#children['*'],192 method,193 params,194 node.#params195 )196 }197 } else {198 child.#params = params199 tempNodes.push(child)200 }201 }202 }203 }204205 const shifted = curNodesQueue.shift()206 curNodes = shifted ? tempNodes.concat(shifted) : tempNodes207 }208fix(trie-router): match suffix wildcard routes (#5236)Taku Amanobefore adoption63bbcf5
line 209why, one step back209 if (handlerSets[1]) {Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 210–217why, one step back210 handlerSets.sort((a, b) => {211 return a.score - b.score212 })213 }214215 return [handlerSets.map(({ handler, params }) => [handler, params] as [T, Params])]216 }217}