fix: More strictly, determine when new URL() should be used#310
fix: More strictly, determine when new URL() should be used#310yusukebe merged 8 commits intopref/request-v2from
Conversation
There was a problem hiding this comment.
How about creating url.ts and url.test.ts to include the code for a URL?
There was a problem hiding this comment.
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' |
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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:
Lines 323 to 324 in 606a905
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.
| @@ -0,0 +1,70 @@ | |||
| import { RequestError } from '../src/error' | |||
There was a problem hiding this comment.
../src/error is right? Or did you forget to push error.ts?
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
It's better to compare also the error content Invalid host header. I think you can use the RequestError('Invalid host header') instance.
|
Nice! I'll merge this into https://github.com/honojs/node-server/tree/pref/request-v2. Thanks! |
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