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
Provenance
- Commit
6d5de04- Tree
b58df1a- Author
Taku Amano <taku@taaas.jp>- Committer
GitHub <noreply@github.com>- Parent
4c89a7a
3 files changed+82 added−18 removed
modified:deno_dist/router/reg-exp-router/router.ts+25 added-9 removed
13 unmodified lines14 const emptyParam = {}15 // eslint-disable-next-line @typescript-eslint/no-explicit-any16 const nullMatcher: Matcher<any> = [/^$/, []]17 18+let wildcardRegExpCache: Record<string, RegExp> = {}19 function buildWildcardRegExp(path: string): RegExp {19- return new RegExp(path === '*' ? '' : `^${path.replace(/\/\*/, '(?:|/.*)')}$`)20+ return (wildcardRegExpCache[path] ??= new RegExp(21+ path === '*' ? '' : `^${path.replace(/\/\*/, '(?:|/.*)')}$`22+ ))23 }24 25+function clearWildcardRegExpCache() {26+ wildcardRegExpCache = {}27+}28+29 function buildMatcherFromPreprocessedRoutes<T>(routes: [string, T[]][]): Matcher<T> {30 const trie = new Trie()31 const handlers: HandlerData<T>[] = []32 if (routes.length === 0) {80 unmodified lines113 }114 115 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 })137 138 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 })143 144 return16 unmodified lines161 }162 }163 164 match(method: string, path: string): Result<T> | null {165+ clearWildcardRegExpCache() // no longer used.166+167 const matchers = this.buildAllMatchers()168 169 this.match = (method, path) => {170 const matcher = matchers[method]64 unmodified linesmodified:src/router/reg-exp-router/router.test.ts+32 added
369 unmodified lines370 expect(res).not.toBeNull()371 expect(res?.handlers).toEqual(['star1', '/x', 'star2', 'star3'])372 })373 })374+375+// https://github.com/honojs/hono/issues/699376+describe('GET star, GET static, ALL star...', () => {377+ const router = new RegExpRouter<string>()378+379+ 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')383+384+ 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+})390+391+describe('ALL star, ALL star, GET static, ALL star...', () => {392+ const router = new RegExpRouter<string>()393+394+ 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')399+400+ 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 lines14 const emptyParam = {}15 // eslint-disable-next-line @typescript-eslint/no-explicit-any16 const nullMatcher: Matcher<any> = [/^$/, []]17 18+let wildcardRegExpCache: Record<string, RegExp> = {}19 function buildWildcardRegExp(path: string): RegExp {19- return new RegExp(path === '*' ? '' : `^${path.replace(/\/\*/, '(?:|/.*)')}$`)20+ return (wildcardRegExpCache[path] ??= new RegExp(21+ path === '*' ? '' : `^${path.replace(/\/\*/, '(?:|/.*)')}$`22+ ))23 }24 25+function clearWildcardRegExpCache() {26+ wildcardRegExpCache = {}27+}28+29 function buildMatcherFromPreprocessedRoutes<T>(routes: [string, T[]][]): Matcher<T> {30 const trie = new Trie()31 const handlers: HandlerData<T>[] = []32 if (routes.length === 0) {80 unmodified lines113 }114 115 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 })137 138 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 })143 144 return16 unmodified lines161 }162 }163 164 match(method: string, path: string): Result<T> | null {165+ clearWildcardRegExpCache() // no longer used.166+167 const matchers = this.buildAllMatchers()168 169 this.match = (method, path) => {170 const matcher = matchers[method]64 unmodified lines