Browse Source

Better type checks for fd in net.js

v0.7.4-release
koichik 14 years ago
committed by Ryan Dahl
parent
commit
414fa4abae
  1. 26
      lib/net.js

26
lib/net.js

@ -118,7 +118,7 @@ function setImplmentationMethods(self) {
// emitting the same value that we see now. Otherwise, we can end up // emitting the same value that we see now. Otherwise, we can end up
// calling emit() after recvMsg() has been called again and end up // calling emit() after recvMsg() has been called again and end up
// emitting null (or another FD). // emitting null (or another FD).
if (recvMsg.fd !== null) { if (typeof recvMsg.fd === 'number') {
var fd = recvMsg.fd; var fd = recvMsg.fd;
process.nextTick(function() { process.nextTick(function() {
self.emit('fd', fd); self.emit('fd', fd);
@ -251,16 +251,16 @@ Object.defineProperty(Socket.prototype, 'readyState', {
if (this._connecting) { if (this._connecting) {
return 'opening'; return 'opening';
} else if (this.readable && this.writable) { } else if (this.readable && this.writable) {
assert(typeof this.fd == 'number'); assert(typeof this.fd === 'number');
return 'open'; return 'open';
} else if (this.readable && !this.writable) { } else if (this.readable && !this.writable) {
assert(typeof this.fd == 'number'); assert(typeof this.fd === 'number');
return 'readOnly'; return 'readOnly';
} else if (!this.readable && this.writable) { } else if (!this.readable && this.writable) {
assert(typeof this.fd == 'number'); assert(typeof this.fd === 'number');
return 'writeOnly'; return 'writeOnly';
} else { } else {
assert(typeof this.fd != 'number'); assert(typeof this.fd !== 'number');
return 'closed'; return 'closed';
} }
} }
@ -665,7 +665,7 @@ Socket.prototype._onReadable = function() {
Socket.prototype.connect = function() { Socket.prototype.connect = function() {
var self = this; var self = this;
initSocket(self); 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'); if (!self._readWatcher) throw new Error('No readWatcher');
timers.active(this); timers.active(this);
@ -723,7 +723,7 @@ Socket.prototype.setKeepAlive = function(enable, time) {
Socket.prototype.setTimeout = function(msecs, callback) { Socket.prototype.setTimeout = function(msecs, callback) {
if (msecs > 0) { if (msecs > 0) {
timers.enroll(this, msecs); timers.enroll(this, msecs);
if (this.fd) { timers.active(this); } if (typeof this.fd === 'number') { timers.active(this); }
if (callback) { if (callback) {
this.once('timeout', callback); this.once('timeout', callback);
} }
@ -739,7 +739,9 @@ Socket.prototype.pause = function() {
Socket.prototype.resume = 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) { if (this._readWatcher) {
this._readWatcher.stop(); this._readWatcher.stop();
this._readWatcher.set(this.fd, true, false); this._readWatcher.set(this.fd, true, false);
@ -793,7 +795,7 @@ Socket.prototype.destroy = function(exception) {
} }
// FIXME Bug when this.fd == 0 // FIXME Bug when this.fd == 0
if (typeof this.fd == 'number') { if (typeof this.fd === 'number') {
debug('close ' + this.fd); debug('close ' + this.fd);
close(this.fd); close(this.fd);
this.fd = null; this.fd = null;
@ -986,7 +988,7 @@ Server.prototype._rejectPending = function() {
// server.listen(8000, '192.168.1.2'); // server.listen(8000, '192.168.1.2');
Server.prototype.listen = function() { Server.prototype.listen = function() {
var self = this; 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]; var lastArg = arguments[arguments.length - 1];
if (typeof lastArg == 'function') { if (typeof lastArg == 'function') {
@ -1041,7 +1043,7 @@ Server.prototype.listen = function() {
}; };
Server.prototype.listenFD = function(fd, type) { Server.prototype.listenFD = function(fd, type) {
if (this.fd) { if (typeof this.fd === 'number') {
throw new Error('Server already opened'); 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 // 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. // original listen command was issued and this. Bail if that's the case.
// See test/simple/test-net-eaddrinuse.js // See test/simple/test-net-eaddrinuse.js
if (typeof self.fd != 'number') return; if (typeof self.fd !== 'number') return;
try { try {
listen(self.fd, self._backlog || 128); listen(self.fd, self._backlog || 128);

Loading…
Cancel
Save