Skip to content
Myr

fix(pattern-router/linear-router): prevent prefix overmatch on wildcard routes (#5252)

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

* fix(pattern-router): prevent prefix overmatch on wildcard routes * move the test into common tests and fix the linear router * use endsWith * support `/a/*/b/*`

authored by Yusuke Wada · committed by GitHub · parent 63bbcf5 · Browse files · 3 files+87 added−11 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+87 added−11 removed

  • tests:+75 added−0 removed
  • other:+12 added−11 removed

3 files+87 added−11 removed

modified:src/router/linear-router/router.ts+9 added-2 removed
modified:src/router/pattern-router/router.ts+3 added-9 removed
8 unmodified lines
9 name: string = 'PatternRouter'10 #routes: Route<T>[] = []1112 add(method: string, path: string, handler: T) {13 const endsWithWildcard = path.at(-1) === '*'14 if (endsWithWildcard) {15 path = path.slice(0, -2)16 }13 const suffix = path.endsWith('/*') ? '(?:$|/)' : path.endsWith('*') ? '' : '/?$'14 path = path.replace(/\*$/, '')15 if (path.at(-1) === '?') {16 path = path.slice(0, -1)17 this.add(method, path.replace(/\/[^/]+$/, ''), handler)18 }
9 unmodified lines
28 }29 )3031 try {34 this.#routes.push([35 new RegExp(`^${parts.join('')}${endsWithWildcard ? '' : '/?$'}`),36 method,37 handler,38 ])32 this.#routes.push([new RegExp(`^${parts.join('')}${suffix}`), method, handler])33 } catch {34 throw new UnsupportedPathError()35 }36 }
18 unmodified lines
modified:src/router/common.case.test.ts+75 added
368 unmodified lines
369 expect(res[3].handler).toEqual('/x/*')370 })371 })372373 describe('Suffix wildcard', () => {374 beforeEach(() => {375 router.add('GET', '/assets*', 'assets')376 })377378 it('GET /assets', async () => {379 const res = match('GET', '/assets')380 expect(res.length).toBe(1)381 expect(res[0].handler).toEqual('assets')382 })383384 it('GET /assets-v2', async () => {385 const res = match('GET', '/assets-v2')386 expect(res.length).toBe(1)387 expect(res[0].handler).toEqual('assets')388 })389390 it('GET /assets/app.js', async () => {391 const res = match('GET', '/assets/app.js')392 expect(res.length).toBe(1)393 expect(res[0].handler).toEqual('assets')394 })395396 it('GET /asset', async () => {397 const res = match('GET', '/asset')398 expect(res.length).toBe(0)399 })400 })401402 describe('Trailing wildcard', () => {403 beforeEach(() => {404 router.add('GET', '/path/*', 'path')405 })406407 it('GET /path', async () => {408 const res = match('GET', '/path')409 expect(res.length).toBe(1)410 expect(res[0].handler).toEqual('path')411 })412413 it('GET /path/to/file', async () => {414 const res = match('GET', '/path/to/file')415 expect(res.length).toBe(1)416 expect(res[0].handler).toEqual('path')417 })418419 it('GET /pathfoo', async () => {420 const res = match('GET', '/pathfoo')421 expect(res.length).toBe(0)422 })423 })424425 describe('Trailing wildcard after a middle wildcard', () => {426 beforeEach(() => {427 router.add('GET', '/a/*/b/*', 'b')428 })429430 it('GET /a/x/b', async () => {431 const res = match('GET', '/a/x/b')432 expect(res.length).toBe(1)433 expect(res[0].handler).toEqual('b')434 })435436 it('GET /a/x/b/c', async () => {437 const res = match('GET', '/a/x/b/c')438 expect(res.length).toBe(1)439 expect(res[0].handler).toEqual('b')440 })441442 it('GET /a/x/bz', async () => {443 const res = match('GET', '/a/x/bz')444 expect(res.length).toBe(0)445 })446 })447448 describe('Optional route', () => {449 beforeEach(() => {450 router.add('GET', '/api/animals/:type?', 'animals')451 router.add('GET', '/v1/:version?/:platform?', 'result')
547 unmodified lines