Skip to content
Myr

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 file

Take 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 back
1import 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 back
5import { createNullObject } from '../utils'

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 6–16why, one step back
6
7type HandlerSet<T> = {8 handler: T9 possibleKeys: string[]10 score: number11}1213type HandlerParamsSet<T> = HandlerSet<T> & {14 params: Record<string, string>15}16

perf(router): share null object creation (#5267)Taku Amanobefore adoption4b44abe

line 17why, one step back
17const emptyParams = createNullObject()

fix(trie-router): match suffix wildcard routes (#5236)Taku Amanobefore adoption63bbcf5

line 18why, one step back
18let order = 0

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 19–20why, one step back
19
20export class Node<T> {

fix(trie-router): match suffix wildcard routes (#5236)Taku Amanobefore adoption63bbcf5

line 21why, one step back
21 #methods: Record<string, HandlerSet<T>>[] = []

Merge commit from forkYusuke Wadabefore adoptionf0b094d

line 22why, one step back
22

perf(router): share null object creation (#5267)Taku Amanobefore adoption4b44abe

line 23why, one step back
23 #children: Record<string, Node<T>> = createNullObject()

fix(trie-router): match suffix wildcard routes (#5236)Taku Amanobefore adoption63bbcf5

lines 24–25why, one step back
24 #patterns: Node<T>[] = []
25 #pattern?: Pattern | string

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 26–27why, one step back
26 #params: Record<string, string> = emptyParams
27

fix(trie-router): match suffix wildcard routes (#5236)Taku Amanobefore adoption63bbcf5

line 28why, one step back
28 insert(method: string, path: string, handler: T): void {

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 29–32why, one step back
29 // eslint-disable-next-line @typescript-eslint/no-this-alias
30 let curNode: Node<T> = this31 const parts = splitRoutingPath(path)32

fix(trie-router): match suffix wildcard routes (#5236)Taku Amanobefore adoption63bbcf5

line 33why, one step back
33 const possibleKeys = new Set<string>()

Merge commit from forkYusuke Wadabefore adoptionf0b094d

line 34why, one step back
34

fix(trie-router): match suffix wildcard routes (#5236)Taku Amanobefore adoption63bbcf5

lines 35–42why, one step back
35 let i = 0
36 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 || p

Merge commit from forkYusuke Wadabefore adoptionf0b094d

line 43why, one step back
43

fix(trie-router): match suffix wildcard routes (#5236)Taku Amanobefore adoption63bbcf5

lines 44–47why, one step back
44 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 back
48 }

fix(trie-router): match suffix wildcard routes (#5236)Taku Amanobefore adoption63bbcf5

lines 49–51why, one step back
49 curNode = child
50 if (isParam) {51 possibleKeys.add(pattern[1])

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 52–57why, one step back
52 }
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 back
58 possibleKeys: [...possibleKeys],
59 score: ++order,

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 60–73why, one step back
60 },
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 back
74 if (handlerSet) {

perf(router): share null object creation (#5267)Taku Amanobefore adoption4b44abe

line 75why, one step back
75 handlerSet.params = createNullObject()

Merge commit from forkYusuke Wadabefore adoptionf0b094d

line 76why, one step back
76 handlerSets.push(handlerSet)

fix(trie-router): match suffix wildcard routes (#5236)Taku Amanobefore adoption63bbcf5

lines 77–80why, one step back
77 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 back
81 }
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 }120

fix(trie-router): match suffix wildcard routes (#5236)Taku Amanobefore adoption63bbcf5

lines 121–122why, one step back
121 for (const child of node.#patterns) {
122 const pattern = child.#pattern!

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 123–126why, one step back
123 const params = node.#params === emptyParams ? {} : { ...node.#params }
124125 // Wildcard126 // '/hello/*/foo' => match /hello/bar/foo

fix(trie-router): match suffix wildcard routes (#5236)Taku Amanobefore adoption63bbcf5

lines 127–133why, one step back
127 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 back
134 }
135 continue136 }137

fix(trie-router): match suffix wildcard routes (#5236)Taku Amanobefore adoption63bbcf5

line 138why, one step back
138 const [, name, matcher] = pattern

Merge commit from forkYusuke Wadabefore adoptionf0b094d

line 139why, one step back
139

fix(trie-router): match suffix wildcard routes (#5236)Taku Amanobefore adoption63bbcf5

line 140why, one step back
140 if (!part && matcher === true) {

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 141–144why, one step back
141 continue
142 }143144 // `/js/:filename{[a-z]+.js}` => match /js/chunk/123.js

fix(trie-router): match suffix wildcard routes (#5236)Taku Amanobefore adoption63bbcf5

lines 145–147why, one step back
145 if (matcher !== true) {
146 if (!partOffsets) {147 partOffsets = []

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 148–153why, one step back
148 let offset = path[0] === '/' ? 1 : 0
149 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 back
154 const restPathString = path.slice(partOffsets[i])

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 155–160why, one step back
155
156 const m = matcher.exec(restPathString)157 if (m) {158 params[name] = m[0]159 this.#pushHandlerSets(handlerSets, child, method, node.#params, params)160

fix(trie-router): match empty wildcard remainder after regexp param (#5102)Taku Amanobefore adoption0fc7ffc

lines 161–171why, one step back
161 // '/: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 }171

fix(trie-router): match suffix wildcard routes (#5236)Taku Amanobefore adoption63bbcf5

line 172why, one step back
172 for (const _ in child.#children) {

Merge commit from forkYusuke Wadabefore adoptionf0b094d

line 173why, one step back
173 child.#params = params

fix(trie-router): count every slash a pattern consumes (#5189)Jayesh Bhadebefore adoption569b419

line 174why, one step back
174 const componentCount = m[0].match(/\//g)?.length ?? 0

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 175–176why, one step back
175 const targetCurNodes = (curNodesQueue[componentCount] ||= [])
176 targetCurNodes.push(child)

fix(trie-router): match suffix wildcard routes (#5236)Taku Amanobefore adoption63bbcf5

line 177why, one step back
177 break

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 178–208why, one step back
178 }
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 }208

fix(trie-router): match suffix wildcard routes (#5236)Taku Amanobefore adoption63bbcf5

line 209why, one step back
209 if (handlerSets[1]) {

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 210–217why, one step back
210 handlerSets.sort((a, b) => {
211 return a.score - b.score212 })213 }214215 return [handlerSets.map(({ handler, params }) => [handler, params] as [T, Params])]216 }217}