Skip to content

Commit 414fa4a

Browse files
koichikry
authored andcommitted
Better type checks for fd in net.js
1 parent 113b1e6 commit 414fa4a

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

lib/net.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ function setImplmentationMethods(self) {
118118
// emitting the same value that we see now. Otherwise, we can end up
119119
// calling emit() after recvMsg() has been called again and end up
120120
// emitting null (or another FD).
121-
if (recvMsg.fd !== null) {
121+
if (typeof recvMsg.fd === 'number') {
122122
var fd = recvMsg.fd;
123123
process.nextTick(function() {
124124
self.emit('fd', fd);
@@ -251,16 +251,16 @@ Object.defineProperty(Socket.prototype, 'readyState', {
251251
if (this._connecting) {
252252
return 'opening';
253253
} else if (this.readable && this.writable) {
254-
assert(typeof this.fd == 'number');
254+
assert(typeof this.fd === 'number');
255255
return 'open';
256256
} else if (this.readable && !this.writable) {
257-
assert(typeof this.fd == 'number');
257+
assert(typeof this.fd === 'number');
258258
return 'readOnly';
259259
} else if (!this.readable && this.writable) {
260-
assert(typeof this.fd == 'number');
260+
assert(typeof this.fd === 'number');
261261
return 'writeOnly';
262262
} else {
263-
assert(typeof this.fd != 'number');
263+
assert(typeof this.fd !== 'number');
264264
return 'closed';
265265
}
266266
}
@@ -665,7 +665,7 @@ Socket.prototype._onReadable = function() {
665665
Socket.prototype.connect = function() {
666666
var self = this;
667667
initSocket(self);
668-
if (self.fd) throw new Error('Socket already opened');
668+
if (typeof self.fd === 'number') throw new Error('Socket already opened');
669669
if (!self._readWatcher) throw new Error('No readWatcher');
670670

671671
timers.active(this);
@@ -723,7 +723,7 @@ Socket.prototype.setKeepAlive = function(enable, time) {
723723
Socket.prototype.setTimeout = function(msecs, callback) {
724724
if (msecs > 0) {
725725
timers.enroll(this, msecs);
726-
if (this.fd) { timers.active(this); }
726+
if (typeof this.fd === 'number') { timers.active(this); }
727727
if (callback) {
728728
this.once('timeout', callback);
729729
}
@@ -739,7 +739,9 @@ Socket.prototype.pause = function() {
739739

740740

741741
Socket.prototype.resume = function() {
742-
if (this.fd === null) throw new Error('Cannot resume() closed Socket.');
742+
if (typeof this.fd !== 'number') {
743+
throw new Error('Cannot resume() closed Socket.');
744+
}
743745
if (this._readWatcher) {
744746
this._readWatcher.stop();
745747
this._readWatcher.set(this.fd, true, false);
@@ -793,7 +795,7 @@ Socket.prototype.destroy = function(exception) {
793795
}
794796

795797
// FIXME Bug when this.fd == 0
796-
if (typeof this.fd == 'number') {
798+
if (typeof this.fd === 'number') {
797799
debug('close ' + this.fd);
798800
close(this.fd);
799801
this.fd = null;
@@ -986,7 +988,7 @@ Server.prototype._rejectPending = function() {
986988
// server.listen(8000, '192.168.1.2');
987989
Server.prototype.listen = function() {
988990
var self = this;
989-
if (self.fd) throw new Error('Server already opened');
991+
if (typeof self.fd === 'number') throw new Error('Server already opened');
990992

991993
var lastArg = arguments[arguments.length - 1];
992994
if (typeof lastArg == 'function') {
@@ -1041,7 +1043,7 @@ Server.prototype.listen = function() {
10411043
};
10421044

10431045
Server.prototype.listenFD = function(fd, type) {
1044-
if (this.fd) {
1046+
if (typeof this.fd === 'number') {
10451047
throw new Error('Server already opened');
10461048
}
10471049

@@ -1076,7 +1078,7 @@ Server.prototype._doListen = function() {
10761078
// It could be that server.close() was called between the time the
10771079
// original listen command was issued and this. Bail if that's the case.
10781080
// See test/simple/test-net-eaddrinuse.js
1079-
if (typeof self.fd != 'number') return;
1081+
if (typeof self.fd !== 'number') return;
10801082

10811083
try {
10821084
listen(self.fd, self._backlog || 128);

0 commit comments

Comments
 (0)