fix(reg-exp-router): use matchers[METHOD_NAME_ALL] as fallback for unknown method (#2255)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* fix(reg-exp-router): use `matchers[METHOD_NAME_ALL]` as fallback for unknown method` * refactor(reg-exp-router): build matchers for only registered methods It is no longer necessary to create a matcher for every method, since the matcher will fallback if it is not found. * chore: denoify * test: add test and use more describable test name * test: add expect
authored by Taku Amano · committed by GitHub · parent c514da0 · Browse files · 3 files·+27 added−22 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+27 added−22 removed
- tests:+17 added−0 removed
- other:+10 added−22 removed
Provenance
- Commit
c19fad7- Tree
0932e5a- Author
Taku Amano <taku@taaas.jp>- Committer
GitHub <noreply@github.com>- Parent
c514da0
3 files changed+27 added−22 removed
modified:deno_dist/router/reg-exp-router/router.ts+5 added-11 removed
1 /* eslint-disable @typescript-eslint/ban-ts-comment */2 import type { Router, Result, ParamIndexMap } from '../../router.ts'3 import {4 METHOD_NAME_ALL,5- METHODS,5 UnsupportedPathError,6 MESSAGE_MATCHER_IS_ALREADY_BUILT,7 } from '../../router.ts'8 import { checkOptionalParameter } from '../../utils/url.ts'9 import { PATH_ERROR, type ParamAssocArray } from './node.ts'10 import { Trie } from './trie.ts'11 13-const methodNames = [METHOD_NAME_ALL, ...METHODS].map((method) => method.toUpperCase())14-12 type HandlerData<T> = [T, ParamIndexMap][]13 type StaticMap<T> = Record<string, Result<T>>14 type Matcher<T> = [RegExp, HandlerData<T>[], StaticMap<T>]15 type HandlerWithMetadata<T> = [T, number] // [handler, paramCount]117 unmodified lines133 if (!middleware || !routes) {134 throw new Error(MESSAGE_MATCHER_IS_ALREADY_BUILT)135 }136 140- if (methodNames.indexOf(method) === -1) {141- methodNames.push(method)142- }137 if (!middleware[method]) {138 ;[middleware, routes].forEach((handlerMap) => {139 handlerMap[method] = {}140 Object.keys(handlerMap[METHOD_NAME_ALL]).forEach((p) => {64 unmodified lines205 206 const matchers = this.buildAllMatchers()207 208 this.match = (method, path) => {215- const matcher = matchers[method]209+ const matcher = (matchers[method] || matchers[METHOD_NAME_ALL]) as Matcher<T>210 211 const staticMatch = matcher[2][path]212 if (staticMatch) {213 return staticMatch10 unmodified lines224 225 return this.match(method, path)226 }227 234- private buildAllMatchers(): Record<string, Matcher<T>> {235- const matchers: Record<string, Matcher<T>> = {}228+ private buildAllMatchers(): Record<string, Matcher<T> | null> {229+ const matchers: Record<string, Matcher<T> | null> = {}230 237- methodNames.forEach((method) => {238- matchers[method] = this.buildMatcher(method) || matchers[METHOD_NAME_ALL]231+ ;[...Object.keys(this.routes!), ...Object.keys(this.middleware!)].forEach((method) => {232+ matchers[method] ||= this.buildMatcher(method)233 })234 235 // Release cache236 this.middleware = this.routes = undefined33 unmodified linesmodified:src/router/reg-exp-router/router.test.ts+17 added
676 unmodified lines677 }).toThrowError(UnsupportedPathError)678 })679 })680 })681+682+describe('Unknown method', () => {683+ const router = new RegExpRouter<string>()684+ router.add('GET', '/', 'index')685+ router.add('ALL', '/all', 'all')686+687+ it('UNKNOWN_METHOD /', () => {688+ const [res] = router.match('UNKNOWN_METHOD', '/')689+ expect(res.length).toBe(0)690+ })691+692+ it('UNKNOWN_METHOD /all', () => {693+ const [res] = router.match('UNKNOWN_METHOD', '/all')694+ expect(res.length).toBe(1)695+ expect(res[0][0]).toBe('all')696+ })697+})modified:src/router/reg-exp-router/router.ts+5 added-11 removed
1 /* eslint-disable @typescript-eslint/ban-ts-comment */2 import type { Router, Result, ParamIndexMap } from '../../router'3 import {4 METHOD_NAME_ALL,5- METHODS,5 UnsupportedPathError,6 MESSAGE_MATCHER_IS_ALREADY_BUILT,7 } from '../../router'8 import { checkOptionalParameter } from '../../utils/url'9 import { PATH_ERROR, type ParamAssocArray } from './node'10 import { Trie } from './trie'11 13-const methodNames = [METHOD_NAME_ALL, ...METHODS].map((method) => method.toUpperCase())14-12 type HandlerData<T> = [T, ParamIndexMap][]13 type StaticMap<T> = Record<string, Result<T>>14 type Matcher<T> = [RegExp, HandlerData<T>[], StaticMap<T>]15 type HandlerWithMetadata<T> = [T, number] // [handler, paramCount]117 unmodified lines133 if (!middleware || !routes) {134 throw new Error(MESSAGE_MATCHER_IS_ALREADY_BUILT)135 }136 140- if (methodNames.indexOf(method) === -1) {141- methodNames.push(method)142- }137 if (!middleware[method]) {138 ;[middleware, routes].forEach((handlerMap) => {139 handlerMap[method] = {}140 Object.keys(handlerMap[METHOD_NAME_ALL]).forEach((p) => {64 unmodified lines205 206 const matchers = this.buildAllMatchers()207 208 this.match = (method, path) => {215- const matcher = matchers[method]209+ const matcher = (matchers[method] || matchers[METHOD_NAME_ALL]) as Matcher<T>210 211 const staticMatch = matcher[2][path]212 if (staticMatch) {213 return staticMatch10 unmodified lines224 225 return this.match(method, path)226 }227 234- private buildAllMatchers(): Record<string, Matcher<T>> {235- const matchers: Record<string, Matcher<T>> = {}228+ private buildAllMatchers(): Record<string, Matcher<T> | null> {229+ const matchers: Record<string, Matcher<T> | null> = {}230 237- methodNames.forEach((method) => {238- matchers[method] = this.buildMatcher(method) || matchers[METHOD_NAME_ALL]231+ ;[...Object.keys(this.routes!), ...Object.keys(this.middleware!)].forEach((method) => {232+ matchers[method] ||= this.buildMatcher(method)233 })234 235 // Release cache236 this.middleware = this.routes = undefined33 unmodified lines