Skip to content
Myr

fix(router): accept reg exp meta characters in path (#2375)

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

* fix(router): accept reg exp meta characters in path * chore: denoify

authored by Taku Amano · committed by GitHub · parent 062871d · Browse files · 5 files+63 added−26 removed
This commit

State

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

Diff stats

5 files+63 added−26 removed

  • tests:+21 added−0 removed
  • other:+42 added−26 removed

5 files+63 added−26 removed

modified:deno_dist/router/pattern-router/router.ts+12 added-12 removed
modified:deno_dist/router/reg-exp-router/node.ts+9 added-1 removed
6 unmodified lines
7export interface Context {8 varIndex: number9}1011const regExpMetaChars = new Set('.\\+*[^]$()')1213/**14 * Sort order:15 * 1. literal16 * 2. special pattern (e.g. :label{[0-9]+})
116 unmodified lines
133 const childKeys = Object.keys(this.children).sort(compareKey)134135 const strList = childKeys.map((k) => {136 const c = this.children[k]135 return (typeof c.varIndex === 'number' ? `(${k})@${c.varIndex}` : k) + c.buildRegExpStr()137 return (138 (typeof c.varIndex === 'number'139 ? `(${k})@${c.varIndex}`140 : regExpMetaChars.has(k)141 ? `\\${k}`142 : k) + c.buildRegExpStr()143 )144 })145146 if (typeof this.index === 'number') {147 strList.unshift(`#${this.index}`)
12 unmodified lines
modified:src/router/pattern-router/router.ts+12 added-12 removed
modified:src/router/reg-exp-router/node.ts+9 added-1 removed
6 unmodified lines
7export interface Context {8 varIndex: number9}1011const regExpMetaChars = new Set('.\\+*[^]$()')1213/**14 * Sort order:15 * 1. literal16 * 2. special pattern (e.g. :label{[0-9]+})
116 unmodified lines
133 const childKeys = Object.keys(this.children).sort(compareKey)134135 const strList = childKeys.map((k) => {136 const c = this.children[k]135 return (typeof c.varIndex === 'number' ? `(${k})@${c.varIndex}` : k) + c.buildRegExpStr()137 return (138 (typeof c.varIndex === 'number'139 ? `(${k})@${c.varIndex}`140 : regExpMetaChars.has(k)141 ? `\\${k}`142 : k) + c.buildRegExpStr()143 )144 })145146 if (typeof this.index === 'number') {147 strList.unshift(`#${this.index}`)
12 unmodified lines
modified:src/router/common.case.test.ts+21 added
470 unmodified lines
471 expect(res[0].params['filename']).toEqual('chunk/nest/123.js')472 })473 })474475 describe('non ascii characters', () => {476 beforeEach(() => {477 router.add('GET', '/$/:name', 'get $ name')478 router.add('GET', '/()/:name', 'get () name')479 })480481 it('GET /$/hono', () => {482 const res = match('GET', '/$/hono')483 expect(res.length).toBe(1)484 expect(res[0].handler).toEqual('get $ name')485 expect(res[0].params['name']).toEqual('hono')486 })487488 it('GET /()/hono', () => {489 const res = match('GET', '/()/hono')490 expect(res.length).toBe(1)491 expect(res[0].handler).toEqual('get () name')492 expect(res[0].params['name']).toEqual('hono')493 })494 })495496 describe('REST API', () => {497 beforeEach(() => {498 router.add('GET', '/users/:username{[a-z]+}', 'profile')499 router.add('GET', '/users/:username{[a-z]+}/posts', 'posts')
253 unmodified lines