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: consistent callback checking across project #3539

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ ClientRequest.prototype._deferToConnect = function(method, arguments_, cb) {
};

ClientRequest.prototype.setTimeout = function(msecs, callback) {
if (callback) this.once('timeout', callback);
if (typeof callback === 'function') this.once('timeout', callback);

var self = this;
function emitTimeout() {
Expand Down
2 changes: 1 addition & 1 deletion lib/_http_incoming.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ exports.IncomingMessage = IncomingMessage;


IncomingMessage.prototype.setTimeout = function(msecs, callback) {
if (callback)
if (typeof callback === 'function')
this.on('timeout', callback);
this.socket.setTimeout(msecs);
return this;
Expand Down
9 changes: 5 additions & 4 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,12 @@ exports.OutgoingMessage = OutgoingMessage;

OutgoingMessage.prototype.setTimeout = function(msecs, callback) {

if (callback) {
if (typeof callback !== 'function')
throw new TypeError('callback must be a function');
if (typeof callback === 'function') {
this.on('timeout', callback);
}
else if (callback) {
throw new TypeError('callback must be a function');
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@thefourtheye so you are suggesting that this Type check shouldn't be done.

@Trott suggested otherwise in --> #3539 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jasnell was for it as well --> #3539 (comment)

Copy link
Member

Choose a reason for hiding this comment

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

I'm for it if there is the typeof check. I think @thefourtheye is saying that you can discard the typeof check and the else block and just pass blindly to .on() because .on() will throw a TypeError if the callback is not a function. At least, that's my understanding. So I'm for either approach, really. Don't make no neither nor to me.

Copy link
Member

Choose a reason for hiding this comment

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

In other words, I think @thefourtheye is saying this would be fine:

if (callback) {
    this.on('timeout', callback);
}

Which throws a wrench in the whole "standardize on typeof checks" approach.

Copy link
Member

Choose a reason for hiding this comment

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

We need that if guard if and only if callback might be optional and therefore undefined. I didn't look at the code to see if that is actually possible.

Copy link
Contributor

Choose a reason for hiding this comment

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

Right... At least the event handler code doesn't need this if block as it is done anyway in on, once, {add,remove}listener functions

}

if (!this.socket) {
this.once('socket', function(socket) {
Expand Down Expand Up @@ -482,7 +483,7 @@ OutgoingMessage.prototype.write = function(chunk, encoding, callback) {

function writeAfterEndNT(self, err, callback) {
self.emit('error', err);
if (callback) callback(err);
if (typeof callback === 'function') callback(err);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is an interesting one. Prior to the change a callback that isn't a function would throw. In this instance it would fail silently

Copy link
Member

Choose a reason for hiding this comment

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

Probably needs an else if (callback) throw … or something like that

}


Expand Down
2 changes: 1 addition & 1 deletion lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ util.inherits(Server, net.Server);

Server.prototype.setTimeout = function(msecs, callback) {
this.timeout = msecs;
if (callback)
if (typeof callback === 'function')
this.on('timeout', callback);
return this;
};
Expand Down
4 changes: 2 additions & 2 deletions lib/_tls_wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ TLSSocket.prototype.renegotiate = function(options, callback) {
this._rejectUnauthorized = rejectUnauthorized;
}
if (!this._handle.renegotiate()) {
if (callback) {
if (typeof callback === 'function') {
process.nextTick(callback, new Error('Failed to renegotiate'));
}
return false;
Expand All @@ -513,7 +513,7 @@ TLSSocket.prototype.renegotiate = function(options, callback) {
// Ensure that we'll cycle through internal openssl's state
this.write('');

if (callback) {
if (typeof callback === 'function') {
this.once('secure', function() {
callback(null);
});
Expand Down
2 changes: 1 addition & 1 deletion lib/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ function pbkdf2(password, salt, iterations, keylen, digest, callback) {

// at this point, we need to handle encodings.
var encoding = exports.DEFAULT_ENCODING;
if (callback) {
if (typeof callback === 'function') {
var next = function(er, ret) {
if (ret)
ret = ret.toString(encoding);
Expand Down
2 changes: 1 addition & 1 deletion lib/dgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ Socket.prototype.send = function(buffer,
req.length = length;
req.address = address;
req.port = port;
if (callback) {
if (typeof callback === 'function') {
req.callback = callback;
req.oncomplete = afterSend;
}
Expand Down
10 changes: 5 additions & 5 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ fs.exists = function(path, callback) {
req.oncomplete = cb;
binding.stat(pathModule._makeLong(path), req);
function cb(err, stats) {
if (callback) callback(err ? false : true);
if (typeof callback === 'function') callback(err ? false : true);
}
};

Expand Down Expand Up @@ -1151,16 +1151,16 @@ function writeAll(fd, isUserFd, buffer, offset, length, position, callback_) {
fs.write(fd, buffer, offset, length, position, function(writeErr, written) {
if (writeErr) {
if (isUserFd) {
if (callback) callback(writeErr);
if (typeof callback === 'function') callback(writeErr);
} else {
fs.close(fd, function() {
if (callback) callback(writeErr);
if (typeof callback === 'function') callback(writeErr);
});
}
} else {
if (written === length) {
if (isUserFd) {
if (callback) callback(null);
if (typeof callback === 'function') callback(null);
} else {
fs.close(fd, callback);
}
Expand Down Expand Up @@ -1198,7 +1198,7 @@ fs.writeFile = function(path, data, options, callback_) {

fs.open(path, flag, options.mode, function(openErr, fd) {
if (openErr) {
if (callback) callback(openErr);
if (typeof callback === 'function') callback(openErr);
} else {
writeFd(fd, false);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,13 +299,13 @@ Socket.prototype.listen = function() {
Socket.prototype.setTimeout = function(msecs, callback) {
if (msecs === 0) {
timers.unenroll(this);
if (callback) {
if (typeof callback === 'function') {
this.removeListener('timeout', callback);
}
} else {
timers.enroll(this, msecs);
timers._unrefActive(this);
if (callback) {
if (typeof callback === 'function') {
this.once('timeout', callback);
}
}
Expand Down
8 changes: 4 additions & 4 deletions lib/zlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ Zlib.prototype.params = function(level, strategy, callback) {
if (!self._hadError) {
self._level = level;
self._strategy = strategy;
if (callback) callback();
if (typeof callback === 'function') callback();
}
});
} else {
Expand Down Expand Up @@ -438,10 +438,10 @@ Zlib.prototype.flush = function(kind, callback) {
}

if (ws.ended) {
if (callback)
if (typeof callback === 'function')
process.nextTick(callback);
} else if (ws.ending) {
if (callback)
if (typeof callback === 'function')
this.once('end', callback);
} else if (ws.needDrain) {
var self = this;
Expand All @@ -455,7 +455,7 @@ Zlib.prototype.flush = function(kind, callback) {
};

Zlib.prototype.close = function(callback) {
if (callback)
if (typeof callback === 'function')
process.nextTick(callback);

if (this._closed)
Expand Down