-
Notifications
You must be signed in to change notification settings - Fork 30.5k
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
net: support passing a host with port to Server.prototype.listen #29891
base: main
Are you sure you want to change the base?
Conversation
This looks reasonable to me - @nodejs/http @nodejs/collaborators can this get some opinions/reviews? |
@Zirak is it intentional that this is kept as draft? |
port = ''; | ||
} else { | ||
// eslint-disable-next-line | ||
throw new TypeError('invalid `host` value'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than throwing TypeErrors directly, can you use the infrastructure from lib/internal/errors.js
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Absolutely. As mentioned in the initial post, if this is an interesting change this is one of the things that will be addressed.
So far I haven't seen any team-member approval, or that this PR is of any interest. If there is any, and the concerns in the main post are no problem, then I'll gladly pick it back up. |
let hostname, port; | ||
|
||
try { | ||
// eslint-disable-next-line |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the ESLint rule the line below breaks?
8ae28ff
to
2935f72
Compare
@nodejs/http any chance we can get reviews on this? ❤️ |
Distinguish between a
host
andhostname
in theServer.prototype.listen
argument, to better align with how browsersand other places in node distinguish between
host
andhostname
.For more context, see #16712.
This commit:
host
key to also potentially include a port (i.e.hostname:port
)host
option, one can choose to not supply aport
parameterhostname
key, which behaves just likehost
in present-day node.The following three are now equivalent:
Backwards-compatibility
An important aspect was keeping this change backwards-compatible. As such, the
host
option is parsed into itshostname
andport
components.In case of a conflict between a
host
component and one of the other options, an exception is thrown:Caveats
My goal here was to mock-up a PoC which implements the spirit of the
proposal in a backwards-compatible matter. Below are some caveats with
how I wrote the code and some tradeoffs I took to keep this a PoC. If
this proves to be interesting to the project, they'll be fixed, no
worries :)
Right now,
host
is parsed using a hack aroundnew URL
. It'sprobably not the smartest way of going about this. Regard it as
temporary until this gets more traction.
Similarly, the new errors thrown here should be internal and worded
better overall. So far they serve as an example.
Since ipv6 addresses in urls must be enclosed in square brackets
(i.e.
[80:fe::]
), and the rest of the code expects them to be freeoutside their cages, we need to trim those. To maintain current
behaviour, we need to only trim those if the value inside is an ipv6
address. Current node:
After a tick throws
Error: getaddrinfo ENOTFOUND [foo].com
. Havingsaid that,
new URL('http://[foo].com')
throws an exception, andFirefox doesn't believes it's a real URL.
This pollutes the
options
variable with an extrahostname
keythe user might not have specified. The
options
variable getsprinted in some cases, and this may not be ideal.
Right now, a known bug is when you pass a host containing a port and
you also pass the
port
key as a string (i.e.listen({ host: 'whatever:123', port: '123' })
), an error will be thrown aboutmismatching port values. One potential fix is moving the casting up,
from the argument of
lookupAndListen
orlistenInCluster
tosomewhere handling the arguments.
The tests are fairly verbose and can be compacted. I opted to making
them very explicit and long-form at this point for clarity.
There are currently no tests for the lonely
hostname
option.node questions
Is there a reason to try and identify invalid IPv6 addresses (like
1::2::3
or:::
)? Currently, they fall back on trying to beresolved via dns and failing.
Did I put the tests in the correct file? Is there a better place for
it? e.g. test-net-server-address?
Sorry for the extra long PR comment, and thank you for your time.
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passes