Skip to content
Myr

fix(reg-exp-router): bug of "star and all" routing (#707)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix(reg-exp-router): Comparisons between middleware must be checked in both orientations. * refactor(reg-exp-router): Simplify the conditions. * chore: denoify * fix(reg-exp-router): Copy middleware if the method of new middleware is METHOD_NAME_ALL. * chore: denoify

authored by Taku Amano · committed by GitHub · parent 4c89a7a · Browse files · 3 files+82 added−18 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+82 added−18 removed

  • tests:+32 added−0 removed
  • other:+50 added−18 removed

3 files+82 added−18 removed

modified:deno_dist/router/reg-exp-router/router.ts+25 added-9 removed
13 unmodified lines
14const emptyParam = {}15// eslint-disable-next-line @typescript-eslint/no-explicit-any16const nullMatcher: Matcher<any> = [/^$/, []]1718let wildcardRegExpCache: Record<string, RegExp> = {}19function buildWildcardRegExp(path: string): RegExp {19 return new RegExp(path === '*' ? '' : `^${path.replace(/\/\*/, '(?:|/.*)')}$`)20 return (wildcardRegExpCache[path] ??= new RegExp(21 path === '*' ? '' : `^${path.replace(/\/\*/, '(?:|/.*)')}$`22 ))23}2425function clearWildcardRegExpCache() {26 wildcardRegExpCache = {}27}2829function buildMatcherFromPreprocessedRoutes<T>(routes: [string, T[]][]): Matcher<T> {30 const trie = new Trie()31 const handlers: HandlerData<T>[] = []32 if (routes.length === 0) {
80 unmodified lines
113 }114115 if (/\*$/.test(path)) {116 const re = buildWildcardRegExp(path)117 if (method === METHOD_NAME_ALL) {118 Object.keys(middleware).forEach((m) => {119 middleware[m][path] ||=120 findMiddleware(middleware[m], path) ||121 findMiddleware(middleware[METHOD_NAME_ALL], path) ||122 []123 })124 } else {110 middleware[method][path] ||=111 findMiddleware(middleware[method], path) ||112 findMiddleware(middleware[METHOD_NAME_ALL], path) ||113 []125 middleware[method][path] ||=126 findMiddleware(middleware[method], path) ||127 findMiddleware(middleware[METHOD_NAME_ALL], path) ||128 []129 }130 Object.keys(middleware).forEach((m) => {131 if (method === METHOD_NAME_ALL || method === m) {132 Object.keys(middleware[m]).forEach((p) => {117 ;(path === '*' || re.test(p)) && middleware[m][p].push(handler)133 re.test(p) && middleware[m][p].push(handler)134 })135 }136 })137138 Object.keys(routes).forEach((m) => {139 if (method === METHOD_NAME_ALL || method === m) {124 Object.keys(routes[m]).forEach(125 (p) => (path === '*' || re.test(p)) && routes[m][p].push(handler)126 )140 Object.keys(routes[m]).forEach((p) => re.test(p) && routes[m][p].push(handler))141 }142 })143144 return
16 unmodified lines
161 }162 }163164 match(method: string, path: string): Result<T> | null {165 clearWildcardRegExpCache() // no longer used.166167 const matchers = this.buildAllMatchers()168169 this.match = (method, path) => {170 const matcher = matchers[method]
64 unmodified lines
modified:src/router/reg-exp-router/router.test.ts+32 added
369 unmodified lines
370 expect(res).not.toBeNull()371 expect(res?.handlers).toEqual(['star1', '/x', 'star2', 'star3'])372 })373})374375// https://github.com/honojs/hono/issues/699376describe('GET star, GET static, ALL star...', () => {377 const router = new RegExpRouter<string>()378379 router.add('GET', '/y/*', 'star1')380 router.add('GET', '/y/a', 'a')381 router.add('ALL', '/y/b/*', 'star2')382 router.add('GET', '/y/b/bar', 'bar')383384 it('Should return star1, star2, and bar', async () => {385 const res = router.match('GET', '/y/b/bar')386 expect(res).not.toBeNull()387 expect(res?.handlers).toEqual(['star1', 'star2', 'bar'])388 })389})390391describe('ALL star, ALL star, GET static, ALL star...', () => {392 const router = new RegExpRouter<string>()393394 router.add('ALL', '*', 'wildcard')395 router.add('ALL', '/a/*', 'star1')396 router.add('GET', '/a/foo', 'foo')397 router.add('ALL', '/b/*', 'star2')398 router.add('GET', '/b/bar', 'bar')399400 it('Should return wildcard, star2 and bar', async () => {401 const res = router.match('GET', '/b/bar')402 expect(res).not.toBeNull()403 expect(res?.handlers).toEqual(['wildcard', 'star2', 'bar'])404 })405})
modified:src/router/reg-exp-router/router.ts+25 added-9 removed
13 unmodified lines
14const emptyParam = {}15// eslint-disable-next-line @typescript-eslint/no-explicit-any16const nullMatcher: Matcher<any> = [/^$/, []]1718let wildcardRegExpCache: Record<string, RegExp> = {}19function buildWildcardRegExp(path: string): RegExp {19 return new RegExp(path === '*' ? '' : `^${path.replace(/\/\*/, '(?:|/.*)')}$`)20 return (wildcardRegExpCache[path] ??= new RegExp(21 path === '*' ? '' : `^${path.replace(/\/\*/, '(?:|/.*)')}$`22 ))23}2425function clearWildcardRegExpCache() {26 wildcardRegExpCache = {}27}2829function buildMatcherFromPreprocessedRoutes<T>(routes: [string, T[]][]): Matcher<T> {30 const trie = new Trie()31 const handlers: HandlerData<T>[] = []32 if (routes.length === 0) {
80 unmodified lines
113 }114115 if (/\*$/.test(path)) {116 const re = buildWildcardRegExp(path)117 if (method === METHOD_NAME_ALL) {118 Object.keys(middleware).forEach((m) => {119 middleware[m][path] ||=120 findMiddleware(middleware[m], path) ||121 findMiddleware(middleware[METHOD_NAME_ALL], path) ||122 []123 })124 } else {110 middleware[method][path] ||=111 findMiddleware(middleware[method], path) ||112 findMiddleware(middleware[METHOD_NAME_ALL], path) ||113 []125 middleware[method][path] ||=126 findMiddleware(middleware[method], path) ||127 findMiddleware(middleware[METHOD_NAME_ALL], path) ||128 []129 }130 Object.keys(middleware).forEach((m) => {131 if (method === METHOD_NAME_ALL || method === m) {132 Object.keys(middleware[m]).forEach((p) => {117 ;(path === '*' || re.test(p)) && middleware[m][p].push(handler)133 re.test(p) && middleware[m][p].push(handler)134 })135 }136 })137138 Object.keys(routes).forEach((m) => {139 if (method === METHOD_NAME_ALL || method === m) {124 Object.keys(routes[m]).forEach(125 (p) => (path === '*' || re.test(p)) && routes[m][p].push(handler)126 )140 Object.keys(routes[m]).forEach((p) => re.test(p) && routes[m][p].push(handler))141 }142 })143144 return
16 unmodified lines
161 }162 }163164 match(method: string, path: string): Result<T> | null {165 clearWildcardRegExpCache() // no longer used.166167 const matchers = this.buildAllMatchers()168169 this.match = (method, path) => {170 const matcher = matchers[method]
64 unmodified lines