Skip to content

Commit 11c783f

Browse files
Y1D7NGbengl
authored andcommitted
net: remoteAddress always undefined called before connected
PR-URL: #43011 Reviewed-By: Paolo Insogna <paolo@cowtech.it> Reviewed-By: Zeyu "Alex" Yang <himself65@outlook.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 7d8d9d6 commit 11c783f

2 files changed

Lines changed: 12 additions & 2 deletions

File tree

‎lib/net.js‎

Lines changed: 4 additions & 2 deletions
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
returnthis._peername||{};
738738
}elseif(!this._peername){
739739
this._peername={};
@@ -760,7 +760,9 @@ protoGetter('remoteAddress', function remoteAddress() {
760760
});
761761

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

766768
protoGetter('remotePort',functionremotePort(){

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

Lines changed: 8 additions & 0 deletions
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
constclient=net.createConnection(this.address().port,'127.0.0.1');
5555
constclient2=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)