Skip to content

Commit

Permalink
lib: refactor Socket._getpeername and Socket._getsockname
Browse files Browse the repository at this point in the history
  • Loading branch information
himself65 committed May 24, 2020
1 parent 5a4f24e commit 7d4d597
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -669,14 +669,11 @@ Socket.prototype._destroy = function(exception, cb) {
};

Socket.prototype._getpeername = function() {
if (!this._peername) {
if (!this._handle || !this._handle.getpeername) {
return {};
}
const out = {};
const err = this._handle.getpeername(out);
if (err) return {}; // FIXME(bnoordhuis) Throw?
this._peername = out;
if (!this._handle || !this._handle.getpeername) {
return {};
} else if (!this._peername) {
// FIXME(bnoordhuis) Throws when returns not 0?
this._handle.getpeername(this._peername = {});
}
return this._peername;
};
Expand Down Expand Up @@ -709,12 +706,9 @@ protoGetter('remotePort', function remotePort() {
Socket.prototype._getsockname = function() {
if (!this._handle || !this._handle.getsockname) {
return {};
}
if (!this._sockname) {
const out = {};
const err = this._handle.getsockname(out);
if (err) return {}; // FIXME(bnoordhuis) Throw?
this._sockname = out;
} else if (!this._sockname) {
// FIXME(bnoordhuis) Throws when returns not 0?
this._handle.getsockname(this._sockname = {});
}
return this._sockname;
};
Expand Down

0 comments on commit 7d4d597

Please sign in to comment.