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
Provenance
- Commit
c2a2136- Tree
1588dcc- Author
Taku Amano <taku@taaas.jp>- Committer
GitHub <noreply@github.com>- Parent
062871d
5 files changed+63 added−26 removed
modified:deno_dist/router/pattern-router/router.ts+12 added-12 removed
10 unmodified lines111213141516171819151617181922232425262720212223242526272829293031323321 unmodified lines10 unmodified lines const endsWithWildcard = path[path.length - 1] === '*' if (endsWithWildcard) { path = path.slice(0, -2) }
const parts = path.match(/\/?(:\w+(?:{(?:(?:{[\d,]+})|[^}])+})?)|\/?[^\/\?]+|(\?)/g) || [] if (parts[parts.length - 1] === '?') { this.add(method, parts.slice(0, parts.length - 2).join(''), handler) parts.pop() if (path.at(-1) === '?') { path = path.slice(0, -1) this.add(method, path.replace(/\/[^/]+$/, ''), handler) }
for (let i = 0, len = parts.length; i < len; i++) { const match = parts[i].match(/^\/:([^{]+)(?:{(.*)})?/) if (match) { parts[i] = `/(?<${match[1]}>${match[2] || '[^/]+'})` } else if (parts[i] === '/*') { parts[i] = '/[^/]+' const parts = (path.match(/\/?(:\w+(?:{(?:(?:{[\d,]+})|[^}])+})?)|\/?[^\/\?]+/g) || []).map( (part) => { const match = part.match(/^\/:([^{]+)(?:{(.*)})?/) return match ? `/(?<${match[1]}>${match[2] || '[^/]+'})` : part === '/*' ? '/[^/]+' : part.replace(/[.\\+*[^\]$()]/g, '\\$&') } } )
let re try { re = new RegExp(`^${parts.join('')}${endsWithWildcard ? '' : '/?$'}`)21 unmodified linesmodified:deno_dist/router/reg-exp-router/node.ts+9 added-1 removed
6 unmodified lines7 export interface Context {8 varIndex: number9 }10 11+const regExpMetaChars = new Set('.\\+*[^]$()')12+13 /**14 * Sort order:15 * 1. literal16 * 2. special pattern (e.g. :label{[0-9]+})116 unmodified lines133 const childKeys = Object.keys(this.children).sort(compareKey)134 135 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 })145 146 if (typeof this.index === 'number') {147 strList.unshift(`#${this.index}`)12 unmodified linesmodified:src/router/pattern-router/router.ts+12 added-12 removed
10 unmodified lines111213141516171819151617181922232425262720212223242526272829293031323321 unmodified lines10 unmodified lines const endsWithWildcard = path[path.length - 1] === '*' if (endsWithWildcard) { path = path.slice(0, -2) }
const parts = path.match(/\/?(:\w+(?:{(?:(?:{[\d,]+})|[^}])+})?)|\/?[^\/\?]+|(\?)/g) || [] if (parts[parts.length - 1] === '?') { this.add(method, parts.slice(0, parts.length - 2).join(''), handler) parts.pop() if (path.at(-1) === '?') { path = path.slice(0, -1) this.add(method, path.replace(/\/[^/]+$/, ''), handler) }
for (let i = 0, len = parts.length; i < len; i++) { const match = parts[i].match(/^\/:([^{]+)(?:{(.*)})?/) if (match) { parts[i] = `/(?<${match[1]}>${match[2] || '[^/]+'})` } else if (parts[i] === '/*') { parts[i] = '/[^/]+' const parts = (path.match(/\/?(:\w+(?:{(?:(?:{[\d,]+})|[^}])+})?)|\/?[^\/\?]+/g) || []).map( (part) => { const match = part.match(/^\/:([^{]+)(?:{(.*)})?/) return match ? `/(?<${match[1]}>${match[2] || '[^/]+'})` : part === '/*' ? '/[^/]+' : part.replace(/[.\\+*[^\]$()]/g, '\\$&') } } )
let re try { re = new RegExp(`^${parts.join('')}${endsWithWildcard ? '' : '/?$'}`)21 unmodified linesmodified:src/router/reg-exp-router/node.ts+9 added-1 removed
6 unmodified lines7 export interface Context {8 varIndex: number9 }10 11+const regExpMetaChars = new Set('.\\+*[^]$()')12+13 /**14 * Sort order:15 * 1. literal16 * 2. special pattern (e.g. :label{[0-9]+})116 unmodified lines133 const childKeys = Object.keys(this.children).sort(compareKey)134 135 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 })145 146 if (typeof this.index === 'number') {147 strList.unshift(`#${this.index}`)12 unmodified linesmodified:src/router/common.case.test.ts+21 added
470 unmodified lines471 expect(res[0].params['filename']).toEqual('chunk/nest/123.js')472 })473 })474 475+ describe('non ascii characters', () => {476+ beforeEach(() => {477+ router.add('GET', '/$/:name', 'get $ name')478+ router.add('GET', '/()/:name', 'get () name')479+ })480+481+ 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+ })487+488+ 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+ })495+496 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