node.ts
This file
Path
src/router/reg-exp-router/node.ts
170 lines · 4.9 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.
perf(router): share null object creation (#5267)Taku Amanobefore adoption4b44abe
lines 1–2why, one step back1import { createNullObject } from '../utils'2fix(reg-exp-router): associate wildcard middleware with matching routes (#5266)Taku Amanobefore adoptiona10592f
lines 3–5why, one step back3export const LABEL_REG_EXP_STR = '[^/]+'4export const ONLY_WILDCARD_REG_EXP_STR = '.*'5export const TAIL_WILDCARD_REG_EXP_STR = '(?:|/.*)'Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 6–29why, one step back6export const PATH_ERROR = Symbol()78export type ParamAssocArray = [string, number][]9export interface Context {10 varIndex: number11}1213const regExpMetaChars = new Set('.\\+*[^]$()')1415/**16 * Sort order:17 * 1. literal18 * 2. special pattern (e.g. :label{[0-9]+})19 * 3. common label pattern (e.g. :label)20 * 4. wildcard21 */22function compareKey(a: string, b: string): number {23 if (a.length === 1) {24 return b.length === 1 ? (a < b ? -1 : 1) : -125 }26 if (b.length === 1) {27 return 128 }29Merge pull request #5154 from honojs/nextYusuke Wadabefore adoptionb0c2d90
line 30why, one step back30 // wildcard: the only wildcard (.*) precedes the tail wildcardMerge commit from forkYusuke Wadabefore adoptionf0b094d
line 31why, one step back31 if (a === ONLY_WILDCARD_REG_EXP_STR || a === TAIL_WILDCARD_REG_EXP_STR) {Merge pull request #5154 from honojs/nextYusuke Wadabefore adoptionb0c2d90
line 32why, one step back32 return b === TAIL_WILDCARD_REG_EXP_STR ? -1 : 1Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 33–47why, one step back33 } else if (b === ONLY_WILDCARD_REG_EXP_STR || b === TAIL_WILDCARD_REG_EXP_STR) {34 return -135 }3637 // label38 if (a === LABEL_REG_EXP_STR) {39 return 140 } else if (b === LABEL_REG_EXP_STR) {41 return -142 }4344 return a.length === b.length ? (a < b ? -1 : 1) : b.length - a.length45}4647export class Node {Merge pull request #5154 from honojs/nextYusuke Wadabefore adoptionb0c2d90
line 48why, one step back48 // handler index of a dynamic path, or -1 for a static path terminalMerge commit from forkYusuke Wadabefore adoptionf0b094d
lines 49–50why, one step back49 #index?: number50 #varIndex?: numberperf(router): share null object creation (#5267)Taku Amanobefore adoption4b44abe
line 51why, one step back51 #children: Record<string, Node> = createNullObject()Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 52–57why, one step back5253 insert(54 tokens: readonly string[],55 index: number,56 paramMap: ParamAssocArray,57 context: Context,Merge pull request #5154 from honojs/nextYusuke Wadabefore adoptionb0c2d90
line 58why, one step back58 isStatic: booleanMerge commit from forkYusuke Wadabefore adoptionf0b094d
line 59why, one step back59 ): void {Merge pull request #5154 from honojs/nextYusuke Wadabefore adoptionb0c2d90
lines 60–73why, one step back60 // eslint-disable-next-line @typescript-eslint/no-this-alias61 let node: Node = this62 for (let i = 0, len = tokens.length; i < len; i++) {63 const token = tokens[i]64 const pattern =65 token.length === 166 ? token === '*'67 ? i === len - 168 ? ['', '', ONLY_WILDCARD_REG_EXP_STR] // '*' matches to all the trailing paths69 : ['', '', LABEL_REG_EXP_STR]70 : null71 : token === '/*'72 ? ['', '', TAIL_WILDCARD_REG_EXP_STR] // '/path/to/*' is /\/path\/to(?:|/.*)$73 : token.match(/^\:([^\{\}]+)(?:\{(.+)\})?$/)Merge commit from forkYusuke Wadabefore adoptionf0b094d
line 74why, one step back74Merge pull request #5154 from honojs/nextYusuke Wadabefore adoptionb0c2d90
lines 75–91why, one step back75 let nextNode: Node76 if (pattern) {77 const name = pattern[1]78 let regexpStr = pattern[2] || LABEL_REG_EXP_STR79 if (name && pattern[2]) {80 if (regexpStr === '.*') {81 throw PATH_ERROR82 }83 regexpStr = regexpStr.replace(/^\((?!\?:)(?=[^)]+\)$)/, '(?:') // (a|b) => (?:a|b)84 if (/\((?!\?:)/.test(regexpStr)) {85 // prefix(?:a|b) is allowed, but prefix(a|b) is not86 throw PATH_ERROR87 }88 if (regexpStr.length === 1 && regExpMetaChars.has(regexpStr)) {89 // a single-char pattern like :x{.} is ambiguous with a literal character90 throw PATH_ERROR91 }Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 92–93why, one step back92 }93Merge pull request #5154 from honojs/nextYusuke Wadabefore adoptionb0c2d90
lines 94–108why, one step back94 nextNode = node.#children[regexpStr]95 if (!nextNode) {96 if (regexpStr !== ONLY_WILDCARD_REG_EXP_STR && regexpStr !== TAIL_WILDCARD_REG_EXP_STR) {97 for (const k in node.#children) {98 if (99 // a single-char pattern coexists with single-char literals as a literal does100 (regexpStr.length > 1 || k.length > 1) &&101 k !== ONLY_WILDCARD_REG_EXP_STR &&102 k !== TAIL_WILDCARD_REG_EXP_STR103 ) {104 throw PATH_ERROR105 }106 }107 }108 nextNode = node.#children[regexpStr] = new Node()Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 109–110why, one step back109 }110 if (name !== '') {Merge pull request #5154 from honojs/nextYusuke Wadabefore adoptionb0c2d90
lines 111–112why, one step back111 nextNode.#varIndex ??= context.varIndex++112 paramMap.push([name, nextNode.#varIndex])Merge commit from forkYusuke Wadabefore adoptionf0b094d
line 113why, one step back113 }Merge pull request #5154 from honojs/nextYusuke Wadabefore adoptionb0c2d90
lines 114–126why, one step back114 } else {115 nextNode = node.#children[token]116 if (!nextNode) {117 for (const k in node.#children) {118 if (119 k.length > 1 &&120 k !== ONLY_WILDCARD_REG_EXP_STR &&121 k !== TAIL_WILDCARD_REG_EXP_STR122 ) {123 throw PATH_ERROR124 }125 }126 nextNode = node.#children[token] = new Node()Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 127–128why, one step back127 }128 }Merge pull request #5154 from honojs/nextYusuke Wadabefore adoptionb0c2d90
lines 129–130why, one step back129130 node = nextNodeMerge commit from forkYusuke Wadabefore adoptionf0b094d
lines 131–132why, one step back131 }132Merge pull request #5154 from honojs/nextYusuke Wadabefore adoptionb0c2d90
lines 133–136why, one step back133 if (node.#index !== undefined) {134 throw PATH_ERROR135 }136 node.#index = isStatic ? -1 : indexMerge commit from forkYusuke Wadabefore adoptionf0b094d
lines 137–141why, one step back137 }138139 buildRegExpStr(): string {140 const childKeys = Object.keys(this.#children).sort(compareKey)141Merge pull request #5154 from honojs/nextYusuke Wadabefore adoptionb0c2d90
lines 142–155why, one step back142 const strList = childKeys143 .map((k) => {144 const c = this.#children[k]145 const childStr = c.buildRegExpStr()146 // an empty childStr means a static-only branch, which is handled by staticMap147 return childStr === ''148 ? ''149 : (typeof c.#varIndex === 'number'150 ? `(${k})@${c.#varIndex}`151 : regExpMetaChars.has(k)152 ? `\\${k}`153 : k) + childStr154 })155 .filter(Boolean)Merge commit from forkYusuke Wadabefore adoptionf0b094d
line 156why, one step back156Merge pull request #5154 from honojs/nextYusuke Wadabefore adoptionb0c2d90
line 157why, one step back157 if (typeof this.#index === 'number' && this.#index !== -1) {Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 158–170why, one step back158 strList.unshift(`#${this.#index}`)159 }160161 if (strList.length === 0) {162 return ''163 }164 if (strList.length === 1) {165 return strList[0]166 }167168 return '(?:' + strList.join('|') + ')'169 }170}