From 414fa4abae9059efb57c7c940ccbc6bc33cdacc1 Mon Sep 17 00:00:00 2001 From: koichik Date: Sat, 12 Mar 2011 01:35:02 +0900 Subject: [PATCH] Better type checks for fd in net.js --- lib/net.js | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/lib/net.js b/lib/net.js index 6aa1453d65..90e8cf7e3c 100644 --- a/lib/net.js +++ b/lib/net.js @@ -118,7 +118,7 @@ function setImplmentationMethods(self) { // emitting the same value that we see now. Otherwise, we can end up // calling emit() after recvMsg() has been called again and end up // emitting null (or another FD). - if (recvMsg.fd !== null) { + if (typeof recvMsg.fd === 'number') { var fd = recvMsg.fd; process.nextTick(function() { self.emit('fd', fd); @@ -251,16 +251,16 @@ Object.defineProperty(Socket.prototype, 'readyState', { if (this._connecting) { return 'opening'; } else if (this.readable && this.writable) { - assert(typeof this.fd == 'number'); + assert(typeof this.fd === 'number'); return 'open'; } else if (this.readable && !this.writable) { - assert(typeof this.fd == 'number'); + assert(typeof this.fd === 'number'); return 'readOnly'; } else if (!this.readable && this.writable) { - assert(typeof this.fd == 'number'); + assert(typeof this.fd === 'number'); return 'writeOnly'; } else { - assert(typeof this.fd != 'number'); + assert(typeof this.fd !== 'number'); return 'closed'; } } @@ -665,7 +665,7 @@ Socket.prototype._onReadable = function() { Socket.prototype.connect = function() { var self = this; initSocket(self); - if (self.fd) throw new Error('Socket already opened'); + if (typeof self.fd === 'number') throw new Error('Socket already opened'); if (!self._readWatcher) throw new Error('No readWatcher'); timers.active(this); @@ -723,7 +723,7 @@ Socket.prototype.setKeepAlive = function(enable, time) { Socket.prototype.setTimeout = function(msecs, callback) { if (msecs > 0) { timers.enroll(this, msecs); - if (this.fd) { timers.active(this); } + if (typeof this.fd === 'number') { timers.active(this); } if (callback) { this.once('timeout', callback); } @@ -739,7 +739,9 @@ Socket.prototype.pause = function() { Socket.prototype.resume = function() { - if (this.fd === null) throw new Error('Cannot resume() closed Socket.'); + if (typeof this.fd !== 'number') { + throw new Error('Cannot resume() closed Socket.'); + } if (this._readWatcher) { this._readWatcher.stop(); this._readWatcher.set(this.fd, true, false); @@ -793,7 +795,7 @@ Socket.prototype.destroy = function(exception) { } // FIXME Bug when this.fd == 0 - if (typeof this.fd == 'number') { + if (typeof this.fd === 'number') { debug('close ' + this.fd); close(this.fd); this.fd = null; @@ -986,7 +988,7 @@ Server.prototype._rejectPending = function() { // server.listen(8000, '192.168.1.2'); Server.prototype.listen = function() { var self = this; - if (self.fd) throw new Error('Server already opened'); + if (typeof self.fd === 'number') throw new Error('Server already opened'); var lastArg = arguments[arguments.length - 1]; if (typeof lastArg == 'function') { @@ -1041,7 +1043,7 @@ Server.prototype.listen = function() { }; Server.prototype.listenFD = function(fd, type) { - if (this.fd) { + if (typeof this.fd === 'number') { throw new Error('Server already opened'); } @@ -1076,7 +1078,7 @@ Server.prototype._doListen = function() { // It could be that server.close() was called between the time the // original listen command was issued and this. Bail if that's the case. // See test/simple/test-net-eaddrinuse.js - if (typeof self.fd != 'number') return; + if (typeof self.fd !== 'number') return; try { listen(self.fd, self._backlog || 128);