hono icon indicating copy to clipboard operation
hono copied to clipboard

LinearRouter and PatternRouter does not support regexp "quantifiers"

Open yusukebe opened this issue 1 year ago • 4 comments

What version of Hono are you using?

4.0.1

What runtime/platform is your app running on?

Cloudflare Workers

What steps can reproduce the bug?

import { Hono } from 'hono'
import { getRouterName } from 'hono/dev'
import { LinearRouter } from 'hono/router/linear-router'
import { PatternRouter } from 'hono/router/pattern-router'
import { RegExpRouter } from 'hono/router/reg-exp-router'
import { TrieRouter } from 'hono/router/trie-router'

const routers = [new TrieRouter(), new RegExpRouter(), new LinearRouter(), new PatternRouter()]

for (const router of routers) {
  const app = new Hono({ router })
  app.get('/year/:year{[0-9]{4}}/month/:month{[0-9]{1,2}}', (c) => {
    return c.json(c.req.param())
  })
  const res = await app.request('/year/2024/month/2')
  console.log(getRouterName(app), res.status)
}

What is the expected behavior?

TrieRouter 200
RegExpRouter 200
LinearRouter 200
PatternRouter 200

What do you see instead?

TrieRouter 200
RegExpRouter 200
LinearRouter 404
PatternRouter 404

Additional information

No response

yusukebe avatar Feb 13 '24 06:02 yusukebe

I think we should use (?:{(?:(?:{[\d,]+})|[^}])+})? instead of (?:{[^}]+})?). The regular expression shown in #2193 also meets the specification, but is slightly longer.

usualoma avatar Feb 13 '24 07:02 usualoma

@usualoma

How about using (?:{[^/]+})?) as mentioned on the following:

https://github.com/honojs/hono/pull/2193/files#r1487327201

yusukebe avatar Feb 13 '24 08:02 yusukebe

@yusukebe Ah, you're right, that would be better! > (?:{[^/]+})?

I think (?:{(?:(?:{[\d,]+})|[^}])+})? is more faithful to the spec, but (?:{[^/]+})? is also used in existing codes such as RegExpRouter, and I think this is simpler and better!

usualoma avatar Feb 13 '24 09:02 usualoma

@yusukebe @usualoma Hi, I was working on https://github.com/honojs/hono/pull/2193. Thank you for the suggestion! The pattern (?:{[^/]+})? seems to be shorter and smarter, so I will commit in the PR.

the-fukui avatar Feb 13 '24 12:02 the-fukui