Skip to content

Commit 1428db8

Browse files
himself65aduh95
authored andcommitted
lib: refactor Socket._getpeername and Socket._getsockname
PR-URL: #32969 Refs: https://github.com/nodejs/node/blob/7893c70970adfbefb1684c48d42aff7385a2afb8/src/node_internals.h#L79-L85 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 2b3acc4 commit 1428db8

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

lib/net.js

+10-14
Original file line numberDiff line numberDiff line change
@@ -674,14 +674,12 @@ Socket.prototype._destroy = function(exception, cb) {
674674
};
675675

676676
Socket.prototype._getpeername = function() {
677-
if (!this._peername) {
678-
if (!this._handle || !this._handle.getpeername) {
679-
return {};
680-
}
681-
const out = {};
682-
const err = this._handle.getpeername(out);
683-
if (err) return {}; // FIXME(bnoordhuis) Throw?
684-
this._peername = out;
677+
if (!this._handle || !this._handle.getpeername) {
678+
return this._peername || {};
679+
} else if (!this._peername) {
680+
this._peername = {};
681+
// FIXME(bnoordhuis) Throw when the return value is not 0?
682+
this._handle.getpeername(this._peername);
685683
}
686684
return this._peername;
687685
};
@@ -714,12 +712,10 @@ protoGetter('remotePort', function remotePort() {
714712
Socket.prototype._getsockname = function() {
715713
if (!this._handle || !this._handle.getsockname) {
716714
return {};
717-
}
718-
if (!this._sockname) {
719-
const out = {};
720-
const err = this._handle.getsockname(out);
721-
if (err) return {}; // FIXME(bnoordhuis) Throw?
722-
this._sockname = out;
715+
} else if (!this._sockname) {
716+
this._sockname = {};
717+
// FIXME(bnoordhuis) Throw when the return value is not 0?
718+
this._handle.getsockname(this._sockname);
723719
}
724720
return this._sockname;
725721
};

0 commit comments

Comments
 (0)