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 authored and aduh95 committed Oct 19, 2020
1 parent 2b3acc4 commit 1428db8
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -674,14 +674,12 @@ 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 this._peername || {};
} else if (!this._peername) {
this._peername = {};
// FIXME(bnoordhuis) Throw when the return value is not 0?
this._handle.getpeername(this._peername);
}
return this._peername;
};
Expand Down Expand Up @@ -714,12 +712,10 @@ 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) {
this._sockname = {};
// FIXME(bnoordhuis) Throw when the return value is not 0?
this._handle.getsockname(this._sockname);
}
return this._sockname;
};
Expand Down

0 comments on commit 1428db8

Please sign in to comment.