Skip to content
Myr

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

3 files+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 */2import type { Router, Result, ParamIndexMap } from '../../router.ts'3import {4 METHOD_NAME_ALL,5 METHODS,5 UnsupportedPathError,6 MESSAGE_MATCHER_IS_ALREADY_BUILT,7} from '../../router.ts'8import { checkOptionalParameter } from '../../utils/url.ts'9import { PATH_ERROR, type ParamAssocArray } from './node.ts'10import { Trie } from './trie.ts'1113const methodNames = [METHOD_NAME_ALL, ...METHODS].map((method) => method.toUpperCase())1412type HandlerData<T> = [T, ParamIndexMap][]13type StaticMap<T> = Record<string, Result<T>>14type Matcher<T> = [RegExp, HandlerData<T>[], StaticMap<T>]15type HandlerWithMetadata<T> = [T, number] // [handler, paramCount]
117 unmodified lines
133 if (!middleware || !routes) {134 throw new Error(MESSAGE_MATCHER_IS_ALREADY_BUILT)135 }136140 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 lines
205206 const matchers = this.buildAllMatchers()207208 this.match = (method, path) => {215 const matcher = matchers[method]209 const matcher = (matchers[method] || matchers[METHOD_NAME_ALL]) as Matcher<T>210211 const staticMatch = matcher[2][path]212 if (staticMatch) {213 return staticMatch
10 unmodified lines
224225 return this.match(method, path)226 }227234 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> = {}230237 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 })234235 // Release cache236 this.middleware = this.routes = undefined
33 unmodified lines
modified:src/router/reg-exp-router/router.test.ts+17 added
676 unmodified lines
677 }).toThrowError(UnsupportedPathError)678 })679 })680})681682describe('Unknown method', () => {683 const router = new RegExpRouter<string>()684 router.add('GET', '/', 'index')685 router.add('ALL', '/all', 'all')686687 it('UNKNOWN_METHOD /', () => {688 const [res] = router.match('UNKNOWN_METHOD', '/')689 expect(res.length).toBe(0)690 })691692 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 */2import type { Router, Result, ParamIndexMap } from '../../router'3import {4 METHOD_NAME_ALL,5 METHODS,5 UnsupportedPathError,6 MESSAGE_MATCHER_IS_ALREADY_BUILT,7} from '../../router'8import { checkOptionalParameter } from '../../utils/url'9import { PATH_ERROR, type ParamAssocArray } from './node'10import { Trie } from './trie'1113const methodNames = [METHOD_NAME_ALL, ...METHODS].map((method) => method.toUpperCase())1412type HandlerData<T> = [T, ParamIndexMap][]13type StaticMap<T> = Record<string, Result<T>>14type Matcher<T> = [RegExp, HandlerData<T>[], StaticMap<T>]15type HandlerWithMetadata<T> = [T, number] // [handler, paramCount]
117 unmodified lines
133 if (!middleware || !routes) {134 throw new Error(MESSAGE_MATCHER_IS_ALREADY_BUILT)135 }136140 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 lines
205206 const matchers = this.buildAllMatchers()207208 this.match = (method, path) => {215 const matcher = matchers[method]209 const matcher = (matchers[method] || matchers[METHOD_NAME_ALL]) as Matcher<T>210211 const staticMatch = matcher[2][path]212 if (staticMatch) {213 return staticMatch
10 unmodified lines
224225 return this.match(method, path)226 }227234 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> = {}230237 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 })234235 // Release cache236 this.middleware = this.routes = undefined
33 unmodified lines