We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 7563aff commit 5c4f673Copy full SHA for 5c4f673
2 files changed
lib/internal/socketaddress.js
@@ -160,10 +160,13 @@ class SocketAddress {
160
161
const isIPv6 = address[0] === '[' && address.endsWith(']');
162
163
+ // Strip userinfo (e.g. "user:pass@") before searching for port separator.
164
// For IPv6, indexOf(']:') + 1 points at ':' (or 0 if absent, treated as no port).
165
// For IPv4, lastIndexOf(':') points at ':' (or -1 if absent).
- const sepIdx = isIPv6 ? input.indexOf(']:') + 1 : input.lastIndexOf(':');
166
- const port = sepIdx > 0 ? NumberParseInt(input.slice(sepIdx + 1), 10) || 0 : 0;
+ const atIdx = input.lastIndexOf('@');
167
+ const hostPart = atIdx >= 0 ? input.slice(atIdx + 1) : input;
168
+ const sepIdx = isIPv6 ? hostPart.indexOf(']:') + 1 : hostPart.lastIndexOf(':');
169
+ const port = sepIdx > 0 ? NumberParseInt(hostPart.slice(sepIdx + 1), 10) || 0 : 0;
170
171
return new SocketAddress({
172
address: isIPv6 ? address.slice(1, -1) : address,
test/parallel/test-socketaddress.js
@@ -152,6 +152,12 @@ describe('net.SocketAddress...', () => {
152
{ input: '127.0.0.1:443', address: '127.0.0.1', port: 443, family: 'ipv4' },
153
{ input: '[::1]:80', address: '::1', port: 80, family: 'ipv6' },
154
{ input: '[::1]:443', address: '::1', port: 443, family: 'ipv6' },
155
+ { input: 'user:80@1.2.3.4', address: '1.2.3.4', port: 0, family: 'ipv4' },
156
+ { input: 'user:80@1.2.3.4:80', address: '1.2.3.4', port: 80, family: 'ipv4' },
157
+ { input: 'user:80@1.2.3.4:123', address: '1.2.3.4', port: 123, family: 'ipv4' },
158
+ { input: 'user:80@[::1]', address: '::1', port: 0, family: 'ipv6' },
159
+ { input: 'user:80@[::1]:80', address: '::1', port: 80, family: 'ipv6' },
+ { input: 'user:80@[::1]:123', address: '::1', port: 123, family: 'ipv6' },
];
good.forEach((i) => {
0 commit comments