Skip to content
Myr

fix(reg-exp-router): escape meta characters for find middleware (#2411)

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

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

  • tests:+12 added−6 removed
  • other:+10 added−2 removed

3 files+22 added−8 removed

modified:deno_dist/router/reg-exp-router/router.ts+5 added-1 removed
19 unmodified lines
2021let wildcardRegExpCache: Record<string, RegExp> = Object.create(null)22function buildWildcardRegExp(path: string): RegExp {23 return (wildcardRegExpCache[path] ??= new RegExp(24 path === '*' ? '' : `^${path.replace(/\/\*/, '(?:|/.*)')}$`24 path === '*'25 ? ''26 : `^${path.replace(/\/\*$|([.\\+*[^\]$()])/g, (_, metaChar) =>27 metaChar ? `\\${metaChar}` : '(?:|/.*)'28 )}$`29 ))30}3132function clearWildcardRegExpCache() {
241 unmodified lines
modified:src/router/reg-exp-router/router.ts+5 added-1 removed
19 unmodified lines
2021let wildcardRegExpCache: Record<string, RegExp> = Object.create(null)22function buildWildcardRegExp(path: string): RegExp {23 return (wildcardRegExpCache[path] ??= new RegExp(24 path === '*' ? '' : `^${path.replace(/\/\*/, '(?:|/.*)')}$`24 path === '*'25 ? ''26 : `^${path.replace(/\/\*$|([.\\+*[^\]$()])/g, (_, metaChar) =>27 metaChar ? `\\${metaChar}` : '(?:|/.*)'28 )}$`29 ))30}3132function clearWildcardRegExpCache() {
241 unmodified lines
modified:src/router/common.case.test.ts+12 added-6 removed
497 unmodified lines
498 })499500 describe('non ascii characters', () => {501 beforeEach(() => {502 router.add('ALL', '/$/*', 'middleware $')503 router.add('GET', '/$/:name', 'get $ name')504 router.add('ALL', '/()/*', 'middleware ()')505 router.add('GET', '/()/:name', 'get () name')506 })507508 it('GET /$/hono', () => {509 const res = match('GET', '/$/hono')508 expect(res.length).toBe(1)509 expect(res[0].handler).toEqual('get $ name')510 expect(res[0].params['name']).toEqual('hono')510 expect(res.length).toBe(2)511 expect(res[0].handler).toEqual('middleware $')512 expect(res[0].params).toEqual({})513 expect(res[1].handler).toEqual('get $ name')514 expect(res[1].params['name']).toEqual('hono')515 })516517 it('GET /()/hono', () => {518 const res = match('GET', '/()/hono')515 expect(res.length).toBe(1)516 expect(res[0].handler).toEqual('get () name')517 expect(res[0].params['name']).toEqual('hono')519 expect(res.length).toBe(2)520 expect(res[0].handler).toEqual('middleware ()')521 expect(res[0].params).toEqual({})522 expect(res[1].handler).toEqual('get () name')523 expect(res[1].params['name']).toEqual('hono')524 })525 })526527 describe('REST API', () => {
256 unmodified lines