Skip to content

Commit 11c783f

Browse files
y1d7ngbengl
authored andcommitted
net: remoteAddress always undefined called before connected
PR-URL: #43011 Reviewed-By: Paolo Insogna <[email protected]> Reviewed-By: Zeyu "Alex" Yang <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 7d8d9d6 commit 11c783f

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

lib/net.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ Socket.prototype._destroy = function(exception, cb) {
733733
};
734734

735735
Socket.prototype._getpeername = function() {
736-
if (!this._handle || !this._handle.getpeername) {
736+
if (!this._handle || !this._handle.getpeername || this.connecting) {
737737
return this._peername || {};
738738
} else if (!this._peername) {
739739
this._peername = {};
@@ -760,7 +760,9 @@ protoGetter('remoteAddress', function remoteAddress() {
760760
});
761761

762762
protoGetter('remoteFamily', function remoteFamily() {
763-
return `IPv${this._getpeername().family}`;
763+
const { family } = this._getpeername();
764+
765+
return family ? `IPv${family}` : family;
764766
});
765767

766768
protoGetter('remotePort', function remotePort() {

test/parallel/test-net-remote-address-port.js

+8
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ const server = net.createServer(common.mustCall(function(socket) {
5353
server.listen(0, function() {
5454
const client = net.createConnection(this.address().port, '127.0.0.1');
5555
const client2 = net.createConnection(this.address().port);
56+
57+
assert.strictEqual(client.remoteAddress, undefined);
58+
assert.strictEqual(client.remoteFamily, undefined);
59+
assert.strictEqual(client.remotePort, undefined);
60+
assert.strictEqual(client2.remoteAddress, undefined);
61+
assert.strictEqual(client2.remoteFamily, undefined);
62+
assert.strictEqual(client2.remotePort, undefined);
63+
5664
client.on('connect', function() {
5765
assert.ok(remoteAddrCandidates.includes(client.remoteAddress));
5866
assert.ok(remoteFamilyCandidates.includes(client.remoteFamily));

0 commit comments

Comments
 (0)