From 1428db8a1fce0a9eb9bf2b691164444430d1bad0 Mon Sep 17 00:00:00 2001 From: himself65 Date: Tue, 21 Apr 2020 19:24:29 +0800 Subject: [PATCH] lib: refactor Socket._getpeername and Socket._getsockname PR-URL: https://github.com/nodejs/node/pull/32969 Refs: https://github.com/nodejs/node/blob/7893c70970adfbefb1684c48d42aff7385a2afb8/src/node_internals.h#L79-L85 Reviewed-By: James M Snell Reviewed-By: Ben Noordhuis Reviewed-By: Matteo Collina --- lib/net.js | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/lib/net.js b/lib/net.js index b149878f6574dc..667fbc60c2d171 100644 --- a/lib/net.js +++ b/lib/net.js @@ -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; }; @@ -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; };