Skip to content
Myr

fix(router): support `/files/:name{.*}` (#4329)

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

* fix(router): support `/files/:name{.*}` * Make `RegExpRouter` throwing `UnsupportedPath` Co-authored-by: Taku Amano <taku@taaas.jp> ---------

authored by Yusuke Wada · committed by GitHub · parent f43afc6 · Browse files · 5 files+28 added−3 removed
This commit

State

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

Trailers

  • Co-authored-by: Taku Amano <taku@taaas.jp>

Diff stats

5 files+28 added−3 removed

  • tests:+22 added−0 removed
  • other:+6 added−3 removed

5 files+28 added−3 removed

modified:src/router/linear-router/router.test.ts+1 added
modified:src/router/reg-exp-router/node.ts+3 added
79 unmodified lines
80 if (pattern) {81 const name = pattern[1]82 let regexpStr = pattern[2] || LABEL_REG_EXP_STR83 if (name && pattern[2]) {84 if (regexpStr === '.*') {85 throw PATH_ERROR86 }87 regexpStr = regexpStr.replace(/^\((?!\?:)(?=[^)]+\)$)/, '(?:') // (a|b) => (?:a|b)88 if (/\((?!\?:)/.test(regexpStr)) {89 // prefix(?:a|b) is allowed, but prefix(a|b) is not90 throw PATH_ERROR
72 unmodified lines
modified:src/router/reg-exp-router/router.test.ts+1 added
12 unmodified lines
13 'Duplicate param name > child',14 'Capture Group > Complex capturing group > GET request',15 'Capture complex multiple directories > GET /part1/middle-b/latest',16 'Capture complex multiple directories > GET /part1/middle-b/end-c/latest',17 'Complex > Parameter with {.*} regexp',18 ],19 },20 {21 reason: 'This route can not be added with `:label` to RegExpRouter. This is ambiguous',
120 unmodified lines
modified:src/router/trie-router/node.ts+3 added-3 removed
153 unmodified lines
154 }155 continue156 }157158 if (!part) {158 const [key, name, matcher] = pattern159160 if (!part && !(matcher instanceof RegExp)) {161 continue162 }163162 const [key, name, matcher] = pattern163164 const child = node.#children[key]165166 // `/js/:filename{[a-z]+.js}` => match /js/chunk/123.js167 const restPathString = parts.slice(i).join('/')
46 unmodified lines
modified:src/router/common.case.test.ts+20 added
142 unmodified lines
143 res = match('GET', '/post/123/123')144 expect(res.length).toBe(0)145 })146147 it('Parameter with {.*} regexp', () => {148 router.add('GET', '/files/:name{.*}', 'file')149 let res = match('GET', '/files')150 expect(res.length).toBe(0)151152 res = match('GET', '/files/a')153 expect(res.length).toBe(1)154 expect(res[0].handler).toEqual('file')155 expect(res[0].params['name']).toEqual('a')156157 res = match('GET', '/files/a/b')158 expect(res.length).toBe(1)159 expect(res[0].handler).toEqual('file')160 expect(res[0].params['name']).toEqual('a/b')161162 res = match('GET', '/files/')163 expect(res.length).toBe(1)164 expect(res[0].handler).toEqual('file')165 })166167 it('/*', async () => {168 router.add('GET', '/api/*', 'auth middleware')169 router.add('GET', '/api', 'top')170 router.add('GET', '/api/posts', 'posts')
705 unmodified lines