net: fix SocketAddress.parse default port handling - #62912
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes net.SocketAddress.parse() so it correctly preserves explicit default ports (notably :80) that are otherwise dropped by URL.parse() when using an http:// base, aligning behavior with expectations and documentation.
Changes:
- Update
SocketAddress.parse()to derive the port from the raw input string rather thanurl.port. - Simplify the parse return path to a single
SocketAddressconstruction for both IPv4 and IPv6. - Add regression tests covering IPv4/IPv6 with explicit ports
80and443.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
lib/internal/socketaddress.js |
Fixes default-port handling by parsing the port from the raw input instead of URLParse(...).port. |
test/parallel/test-socketaddress.js |
Adds regression coverage ensuring explicit :80/:443 are preserved for both IPv4 and IPv6. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #62912 +/- ##
==========================================
- Coverage 90.16% 90.14% -0.03%
==========================================
Files 744 744
Lines 242518 242523 +5
Branches 45705 45691 -14
==========================================
- Hits 218670 218623 -47
- Misses 15357 15391 +34
- Partials 8491 8509 +18
🚀 New features to boost your workflow:
|
87d545a to
7563aff
Compare
|
I noticed one extra behavior change from parsing the port with For example, current SocketAddress.parse('user:80@5.6.7.8')
SocketAddress.parse('user:81@5.6.7.8')With this patch they become ports An alternative is to keep using the HTTP URL parse for the address and existing non-default ports, and only when URLParse(`socket://${input}`).portThat preserves the existing URL host parsing while recovering explicit host ports like |
Great finding and solution, thank you @cookesan |
Actually, I did some benchmarks and noticed the double |
5c4f673 to
8435ef4
Compare
Failed to start CI- Validating Jenkins credentials ✔ Jenkins credentials valid - Querying data for job/node-test-pull-request/73051/ [SyntaxError: Unexpected token '<', ..." https://github.com/nodejs/node/actions/runs/30286474954 |
| } | ||
| return new SocketAddress({ address, port: port | 0 }); | ||
| family: isIPv6 ? 'ipv6' : 'ipv4', | ||
| }); |
There was a problem hiding this comment.
The purpose of this method is stated as Parse an "${ip}:${port}" formatted string into a SocketAddress. Surely we want to be rejecting arbitrary URL components anyway?
There was a problem hiding this comment.
Agreed. Reverted the userinfo handling and its tests.
There was a problem hiding this comment.
We can reject the other URL components in a follow-up semver-major PR.
Signed-off-by: Guilherme Araújo <arauujogui@gmail.com>
8435ef4 to
dc09dc4
Compare
Fixes #62906