Skip to content
Myr

fix(reg-exp-router): Allows registering a star after a long prefix. (#684)

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

* fix(reg-exp-router): Allows registering a star after a long prefix. * fix(reg-exp-router): It now works even if a short GET is registered after a long ALL. * chore: denoify

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

  • tests:+50 added−0 removed
  • other:+30 added−4 removed

3 files+80 added−4 removed

modified:deno_dist/router/reg-exp-router/router.ts+15 added-2 removed
23 unmodified lines
24 if (routes.length === 0) {25 return nullMatcher26 }2728 routes = routes.sort(([a], [b]) => a.length - b.length)2930 for (let i = 0, len = routes.length; i < len; i++) {31 let paramMap32 try {33 paramMap = trie.insert(routes[i][0], i)
64 unmodified lines
9899 if (/\*$/.test(path)) {100 middleware[method] ||= {}101 const re = buildWildcardRegExp(path)100 middleware[method][path] ||= findMiddleware(middleware[METHOD_NAME_ALL], path) || []102 middleware[method][path] ||=103 findMiddleware(middleware[method], path) ||104 findMiddleware(middleware[METHOD_NAME_ALL], path) ||105 []106 Object.keys(middleware).forEach((m) => {107 if (method === METHOD_NAME_ALL || method === m) {108 Object.keys(middleware[m]).forEach((p) => {104 ;(path === '*' || path === p) && middleware[m][p].push(handler)109 ;(path === '*' || re.test(p)) && middleware[m][p].push(handler)110 })111 }112 })113 if (method !== METHOD_NAME_ALL) {114 Object.keys(middleware[METHOD_NAME_ALL]).forEach((p) => {115 if (!middleware[method][p] && (path === '*' || re.test(p))) {116 middleware[method][p] = [...middleware[METHOD_NAME_ALL][p], handler]117 }118 })119 }120121 Object.keys(routes).forEach((m) => {122 if (method === METHOD_NAME_ALL || method === m) {123 Object.keys(routes[m]).forEach(124 (p) => (path === '*' || re.test(p)) && routes[m][p].push(handler)
96 unmodified lines
modified:src/router/reg-exp-router/router.test.ts+50 added
279 unmodified lines
280 const res = router.match('GET', '/all')281 expect(res).not.toBeNull()282 })283})284285describe('long prefix, then star', () => {286 describe('GET only', () => {287 const router = new RegExpRouter<string>()288289 router.add('GET', '/long/prefix/*', 'long-prefix')290 router.add('GET', '/long/*', 'long')291 router.add('GET', '*', 'star1')292 router.add('GET', '*', 'star2')293294 it('get /', () => {295 const res = router.match('GET', '/')296 expect(res?.handlers).toEqual(['star1', 'star2'])297 })298299 it('get /long/prefix', () => {300 const res = router.match('GET', '/long/prefix')301 expect(res?.handlers).toEqual(['long-prefix', 'long', 'star1', 'star2'])302 })303304 it('get /long/prefix/test', () => {305 const res = router.match('GET', '/long/prefix/test')306 expect(res?.handlers).toEqual(['long-prefix', 'long', 'star1', 'star2'])307 })308 })309310 describe('ALL and GET', () => {311 const router = new RegExpRouter<string>()312313 router.add('ALL', '/long/prefix/*', 'long-prefix')314 router.add('ALL', '/long/*', 'long')315 router.add('GET', '*', 'star1')316 router.add('GET', '*', 'star2')317318 it('get /', () => {319 const res = router.match('GET', '/')320 expect(res?.handlers).toEqual(['star1', 'star2'])321 })322323 it('get /long/prefix', () => {324 const res = router.match('GET', '/long/prefix')325 expect(res?.handlers).toEqual(['long-prefix', 'long', 'star1', 'star2'])326 })327328 it('get /long/prefix/test', () => {329 const res = router.match('GET', '/long/prefix/test')330 expect(res?.handlers).toEqual(['long-prefix', 'long', 'star1', 'star2'])331 })332 })333})
modified:src/router/reg-exp-router/router.ts+15 added-2 removed
23 unmodified lines
24 if (routes.length === 0) {25 return nullMatcher26 }2728 routes = routes.sort(([a], [b]) => a.length - b.length)2930 for (let i = 0, len = routes.length; i < len; i++) {31 let paramMap32 try {33 paramMap = trie.insert(routes[i][0], i)
64 unmodified lines
9899 if (/\*$/.test(path)) {100 middleware[method] ||= {}101 const re = buildWildcardRegExp(path)100 middleware[method][path] ||= findMiddleware(middleware[METHOD_NAME_ALL], path) || []102 middleware[method][path] ||=103 findMiddleware(middleware[method], path) ||104 findMiddleware(middleware[METHOD_NAME_ALL], path) ||105 []106 Object.keys(middleware).forEach((m) => {107 if (method === METHOD_NAME_ALL || method === m) {108 Object.keys(middleware[m]).forEach((p) => {104 ;(path === '*' || path === p) && middleware[m][p].push(handler)109 ;(path === '*' || re.test(p)) && middleware[m][p].push(handler)110 })111 }112 })113 if (method !== METHOD_NAME_ALL) {114 Object.keys(middleware[METHOD_NAME_ALL]).forEach((p) => {115 if (!middleware[method][p] && (path === '*' || re.test(p))) {116 middleware[method][p] = [...middleware[METHOD_NAME_ALL][p], handler]117 }118 })119 }120121 Object.keys(routes).forEach((m) => {122 if (method === METHOD_NAME_ALL || method === m) {123 Object.keys(routes[m]).forEach(124 (p) => (path === '*' || re.test(p)) && routes[m][p].push(handler)
96 unmodified lines