Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: refactor Socket._getpeername and Socket._getsockname #32969

Closed
wants to merge 5 commits into from
Closed
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 10 additions & 14 deletions lib/net.js
Expand Up @@ -669,14 +669,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 = {};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a functional change in that before errors can be transient, i.e., the first call can return an empty object while the second call can return a full-fledged address object. Now errors are no longer transient because the empty object is cached.

It's a probably inconsequential change in behavior but I'd label it semver-major anyway.

// FIXME(bnoordhuis) Throws when returns not 0?
himself65 marked this conversation as resolved.
Show resolved Hide resolved
this._handle.getpeername(this._peername);
}
return this._peername;
};
Expand Down Expand Up @@ -709,12 +707,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) Throws when returns not 0?
this._handle.getsockname(this._sockname);
}
return this._sockname;
};
Expand Down