Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 2, 2025

Conversation

yusukebe
Copy link
Member

I investigated to fix #4325, and I found bugs in the routers.

If the registered path is /files/:name{.*}, the behaviors of some routers are incorrect.

Fixes #4325

The author should do the following, if applicable

  • Add tests
  • Run tests
  • bun run format:fix && bun run lint:fix to format the code
  • Add TSDoc/JSDoc to document the code

This comment has been minimized.

This comment has been minimized.

@yusukebe
Copy link
Member Author

I fixed the TrieRouter, but RegExpRouter still throws the error:

 FAIL  src/router/reg-exp-router/router.test.ts > RegExpRouter > Common > Complex > Parameter with {.*} regexp
TypeError: Cannot read properties of undefined (reading 'map')
 ❯ match src/router/common.case.test.ts:42:30
     40|       match = (method: string, path: string) => {
     41|         const [matchRes, stash] = router.match(method, path)
     42|         const res = matchRes.map((r) =>
       |                              ^
     43|           stash
     44|             ? {
 ❯ src/router/common.case.test.ts:162:15

Hey @usualoma , can you take a look at it? I think the expected behavior in the test is correct.

@usualoma
Copy link
Member

usualoma commented Aug 2, 2025

Hi @yusukebe, thank you for addressing the issue.

RegExpRouter cannot accept parameters that may result in a string length of 0, so I would like to change .* to
UnsupportedPath.

diff --git i/src/router/reg-exp-router/node.ts w/src/router/reg-exp-router/node.ts
index 91abea7f..ce521420 100644
--- i/src/router/reg-exp-router/node.ts
+++ w/src/router/reg-exp-router/node.ts
@@ -81,6 +81,9 @@ export class Node {
       const name = pattern[1]
       let regexpStr = pattern[2] || LABEL_REG_EXP_STR
       if (name && pattern[2]) {
+        if (regexpStr === '.*') {
+          throw PATH_ERROR
+        }
         regexpStr = regexpStr.replace(/^\((?!\?:)(?=[^)]+\)$)/, '(?:') // (a|b) => (?:a|b)
         if (/\((?!\?:)/.test(regexpStr)) {
           // prefix(?:a|b) is allowed, but prefix(a|b) is not
diff --git i/src/router/reg-exp-router/router.test.ts w/src/router/reg-exp-router/router.test.ts
index 305cee27..736c2ca9 100644
--- i/src/router/reg-exp-router/router.test.ts
+++ w/src/router/reg-exp-router/router.test.ts
@@ -14,6 +14,7 @@ describe('RegExpRouter', () => {
           'Capture Group > Complex capturing group > GET request',
           'Capture complex multiple directories > GET /part1/middle-b/latest',
           'Capture complex multiple directories > GET /part1/middle-b/end-c/latest',
+          'Complex > Parameter with {.*} regexp',
         ],
       },
       {

Copy link

github-actions bot commented Aug 2, 2025

Bundle size check

main (f43afc6) #4329 (d14a7c0) +/-
Bundle Size (B) 18,280B 18,320B 40B
Bundle Size (KB) 17.85K 17.89K 0.04K

Compiler Diagnostics (tsc)

main (f43afc6) #4329 (d14a7c0) +/-
Files 262 262 0
Lines 116,392 116,395 3
Identifiers 114,317 114,321 4
Symbols 259,914 259,914 0
Types 162,579 162,579 0
Instantiations 3,037,134 3,037,134 0
Memory used 278,081K 270,777K -7,304K
I/O read 0.02s 0.03s 0.01s
I/O write 0s 0s 0s
Parse time 0.65s 0.72s 0.07s
Bind time 0.27s 0.36s 0.09s
Check time 3.72s 4.36s 0.64s
Emit time 0s 0s 0s
Total time 4.65s 5.44s 0.79s

Compiler Diagnostics (typescript-go)

main (f43afc6) #4329 (d14a7c0) +/-
Files 232 232 0
Lines 106,291 106,294 3
Identifiers 106,057 106,061 4
Symbols 371,546 371,546 0
Types 293,026 293,026 0
Instantiations 3,564,730 3,564,730 0
Memory used 229,672K 229,633K -39K
Memory allocs 9,996,693 9,996,745 52
Parse time 0.067s 0.104s 0.037s
Bind time 0.031s 0.03s -0.001s
Check time 1.36s 1.585s 0.225s
Emit time 0s 0s 0s
Total time 1.46s 1.732s 0.272s

Reported by octocov

Copy link

github-actions bot commented Aug 2, 2025

HTTP Performance Benchmark

Framework Runtime Average Ping Query Body
hono (origin/main) bun 38,639.72 53,183.36 34,137.13 28,598.67
hono (current) bun 38,436.95 52,003.17 34,580.48 28,727.21
Change -0.52% -2.22% +1.30% +0.45%

Copy link

codecov bot commented Aug 2, 2025

Codecov Report

❌ Patch coverage is 83.33333% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 91.63%. Comparing base (bd454fe) to head (7181a13).
⚠️ Report is 6 commits behind head on main.

Files with missing lines Patch % Lines
src/router/trie-router/node.ts 66.66% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4329      +/-   ##
==========================================
+ Coverage   91.60%   91.63%   +0.03%     
==========================================
  Files         170      170              
  Lines       10881    10798      -83     
  Branches     3211     3129      -82     
==========================================
- Hits         9967     9895      -72     
+ Misses        913      902      -11     
  Partials        1        1              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@yusukebe
Copy link
Member Author

yusukebe commented Aug 2, 2025

@usualoma

RegExpRouter cannot accept parameters that may result in a string length of 0, so I would like to change .* to
UnsupportedPath.

Yeah. It's okay. Thank you for the patch.

@yusukebe yusukebe merged commit e316dbe into main Aug 2, 2025
20 checks passed
@yusukebe yusukebe deleted the fix/router-paremeter-with-regexp branch August 2, 2025 01:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Hono throws when using optional catch-all route
2 participants