Skip to content

fix: More strictly, determine when new URL() should be used#310

Merged
yusukebe merged 8 commits intopref/request-v2from
improve-normalize-url
Mar 7, 2026
Merged

fix: More strictly, determine when new URL() should be used#310
yusukebe merged 8 commits intopref/request-v2from
improve-normalize-url

Conversation

@usualoma
Copy link
Member

@usualoma usualoma commented Mar 1, 2026

This PR tightens the fast-path eligibility introduced in #301 for request URL handling.

The optimization remains performance-focused, but the conditions for taking the fast path are now more strict to avoid unsafe edge cases.
In short: this is slightly slower than #301 in some cases, but it is the best balance we can achieve while keeping behavior correct and performance impact minimal.

benchmarks

  • improve-normalize-url (This PR's branch)
============================================================
BENCHMARK RESULTS
============================================================

| Benchmark         | npm            | dev            | Difference  |
| ----------------- | -------------- | -------------- | ----------- |
| Average           | 48,671.60      | 57,971.12      | +19.11%     |
| Ping (GET /)      | 63,550.47      | 66,520.31      | +4.67%      |
| Query (GET /id)   | 61,283.56      | 63,584.95      | +3.76%      |
| Body (POST /json) | 21,180.76      | 43,808.09      | +106.83%    |
  • pref/request-v2 (base branch)
============================================================
BENCHMARK RESULTS
============================================================

| Benchmark         | npm            | dev            | Difference  |
| ----------------- | -------------- | -------------- | ----------- |
| Average           | 48,239.95      | 59,463.41      | +23.27%     |
| Ping (GET /)      | 62,649.83      | 67,914.39      | +8.40%      |
| Query (GET /id)   | 60,818.91      | 65,278.81      | +7.33%      |
| Body (POST /json) | 21,251.12      | 45,197.04      | +112.68%    |

@usualoma
Copy link
Member Author

usualoma commented Mar 1, 2026

Hi @yusukebe, would you please review this?

Hi @mgcrea, if you have any comments, I’d really appreciate your feedback.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about creating url.ts and url.test.ts to include the code for a URL?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're absolutely right! I've moved it to a separate file.
9d03243

src/url.ts Outdated
urlObj.hostname.length !== host.length &&
urlObj.hostname !== (host.includes(':') ? host.replace(/:\d+$/, '') : host).toLowerCase()
) {
throw 'Invalid host header'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throwing a string is intended? (I think it's intended because it's you!) Are there any concerns about this? If no, I think it's okay!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing that out.
Yeah, it might not be the best way to write general code. (Because it confuses readers like you.)

In the actual flow of the code, I wanted to change this:

node-server/src/request.ts

Lines 323 to 324 in 606a905

if (typeof e === 'string') {
throw new RequestError(e)

I wanted to use a plain RequestError like this. (Because this isn't "an invalid URL", but rather "an invalid hostname" being specified by Host header.)

RequestError(‘Invalid host header’);

Well, code that throws a string definitely has a bad smell.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about a refactoring such as 10b44e0?

@@ -0,0 +1,70 @@
import { RequestError } from '../src/error'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

../src/error is right? Or did you forget to push error.ts?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

? The CI does not work now!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the trouble!
Added a file at bc49af9.

Oh, right—under the current setup, CI doesn't run on branches outside of main.

},
url: '/foo.txt',
} as IncomingMessage)
}).toThrow(RequestError)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to compare also the error content Invalid host header. I think you can use the RequestError('Invalid host header') instance.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!
updated: dd5a383

Copy link
Member

@yusukebe yusukebe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@yusukebe
Copy link
Member

yusukebe commented Mar 7, 2026

@usualoma

Nice! I'll merge this into https://github.com/honojs/node-server/tree/pref/request-v2. Thanks!

@yusukebe yusukebe merged commit 68a5815 into pref/request-v2 Mar 7, 2026
@yusukebe yusukebe deleted the improve-normalize-url branch March 7, 2026 05:05
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.

2 participants