diff options
Diffstat (limited to 'lib/net.js')
-rw-r--r-- | lib/net.js | 66 |
1 files changed, 31 insertions, 35 deletions
diff --git a/lib/net.js b/lib/net.js index 0b250aeb6e..ddce74b375 100644 --- a/lib/net.js +++ b/lib/net.js @@ -57,7 +57,7 @@ function createHandle(fd) { var debug = util.debuglog('net'); function isPipeName(s) { - return typeof s === 'string' && toNumber(s) === false; + return IS_STRING(s) && toNumber(s) === false; } @@ -90,7 +90,7 @@ exports.connect = exports.createConnection = function() { function normalizeConnectArgs(args) { var options = {}; - if (typeof args[0] === 'object') { + if (IS_OBJECT(args[0])) { // connect(options, [cb]) options = args[0]; } else if (isPipeName(args[0])) { @@ -99,13 +99,13 @@ function normalizeConnectArgs(args) { } else { // connect(port, [host], [cb]) options.port = args[0]; - if (typeof args[1] === 'string') { + if (IS_STRING(args[1])) { options.host = args[1]; } } var cb = args[args.length - 1]; - return (typeof cb === 'function') ? [options, cb] : [options]; + return IS_FUNCTION(cb) ? [options, cb] : [options]; } exports._normalizeConnectArgs = normalizeConnectArgs; @@ -135,20 +135,16 @@ function Socket(options) { this._hadError = false; this._handle = null; - switch (typeof options) { - case 'number': - options = { fd: options }; // Legacy interface. - break; - case 'undefined': - options = {}; - break; - } + if (IS_NUMBER(options)) + options = { fd: options }; // Legacy interface. + else if (IS_UNDEFINED(options)) + options = {}; stream.Duplex.call(this, options); if (options.handle) { this._handle = options.handle; // private - } else if (typeof options.fd !== 'undefined') { + } else if (!IS_UNDEFINED(options.fd)) { this._handle = createHandle(options.fd); this._handle.open(options.fd); this.readable = options.readable !== false; @@ -263,7 +259,7 @@ function onSocketEnd() { // of the other side sending a FIN. The standard 'write after end' // is overly vague, and makes it seem like the user's code is to blame. function writeAfterFIN(chunk, encoding, cb) { - if (typeof encoding === 'function') { + if (IS_FUNCTION(encoding)) { cb = encoding; encoding = null; } @@ -273,7 +269,7 @@ function writeAfterFIN(chunk, encoding, cb) { var self = this; // TODO: defer error events consistently everywhere, not just the cb self.emit('error', er); - if (typeof cb === 'function') { + if (IS_FUNCTION(cb)) { process.nextTick(function() { cb(er); }); @@ -326,7 +322,7 @@ Socket.prototype._onTimeout = function() { Socket.prototype.setNoDelay = function(enable) { // backwards compatibility: assume true when `enable` is omitted if (this._handle && this._handle.setNoDelay) - this._handle.setNoDelay(typeof enable === 'undefined' ? true : !!enable); + this._handle.setNoDelay(IS_UNDEFINED(enable) ? true : !!enable); }; @@ -605,7 +601,7 @@ Socket.prototype.__defineGetter__('localPort', function() { Socket.prototype.write = function(chunk, encoding, cb) { - if (typeof chunk !== 'string' && !Buffer.isBuffer(chunk)) + if (!IS_STRING(chunk) && !IS_BUFFER(chunk)) throw new TypeError('invalid data'); return stream.Duplex.prototype.write.apply(this, arguments); }; @@ -650,7 +646,7 @@ Socket.prototype._writeGeneric = function(writev, data, encoding, cb) { // Retain chunks if (err === 0) req._chunks = chunks; } else { - var enc = Buffer.isBuffer(data) ? 'buffer' : encoding; + var enc = IS_BUFFER(data) ? 'buffer' : encoding; err = createWriteReq(req, this._handle, data, enc); } @@ -732,14 +728,14 @@ Socket.prototype.__defineGetter__('bytesWritten', function() { encoding = this._pendingEncoding; state.buffer.forEach(function(el) { - if (Buffer.isBuffer(el.chunk)) + if (IS_BUFFER(el.chunk)) bytes += el.chunk.length; else bytes += Buffer.byteLength(el.chunk, el.encoding); }); if (data) { - if (Buffer.isBuffer(data)) + if (IS_BUFFER(data)) bytes += data.length; else bytes += Buffer.byteLength(data, encoding); @@ -817,7 +813,7 @@ Socket.prototype.connect = function(options, cb) { if (this.write !== Socket.prototype.write) this.write = Socket.prototype.write; - if (typeof options !== 'object') { + if (!IS_OBJECT(options)) { // Old API: // connect(port, [host], [cb]) // connect(path, [cb]); @@ -844,7 +840,7 @@ Socket.prototype.connect = function(options, cb) { initSocketHandle(this); } - if (typeof cb === 'function') { + if (IS_FUNCTION(cb)) { self.once('connect', cb); } @@ -952,13 +948,13 @@ function Server(/* [ options, ] listener */) { var options; - if (typeof arguments[0] == 'function') { + if (IS_FUNCTION(arguments[0])) { options = {}; self.on('connection', arguments[0]); } else { options = arguments[0] || {}; - if (typeof arguments[1] == 'function') { + if (IS_FUNCTION(arguments[1])) { self.on('connection', arguments[1]); } } @@ -998,7 +994,7 @@ var createServerHandle = exports._createServerHandle = // assign handle in listen, and clean up if bind or listen fails var handle; - if (typeof fd === 'number' && fd >= 0) { + if (IS_NUMBER(fd) && fd >= 0) { try { handle = createHandle(fd); } @@ -1051,7 +1047,7 @@ Server.prototype._listen2 = function(address, port, addressType, backlog, fd) { if (!self._handle) { debug('_listen2: create a handle'); var rval = createServerHandle(address, port, addressType, fd); - if (typeof rval === 'number') { + if (IS_NUMBER(rval)) { var error = errnoException(rval, 'listen'); process.nextTick(function() { self.emit('error', error); @@ -1126,7 +1122,7 @@ Server.prototype.listen = function() { var self = this; var lastArg = arguments[arguments.length - 1]; - if (typeof lastArg == 'function') { + if (IS_FUNCTION(lastArg)) { self.once('listening', lastArg); } @@ -1138,11 +1134,11 @@ Server.prototype.listen = function() { var TCP = process.binding('tcp_wrap').TCP; - if (arguments.length == 0 || typeof arguments[0] == 'function') { + if (arguments.length == 0 || IS_FUNCTION(arguments[0])) { // Bind to a random port. listen(self, '0.0.0.0', 0, null, backlog); - } else if (arguments[0] && typeof arguments[0] === 'object') { + } else if (arguments[0] && IS_OBJECT(arguments[0])) { var h = arguments[0]; if (h._handle) { h = h._handle; @@ -1152,7 +1148,7 @@ Server.prototype.listen = function() { if (h instanceof TCP) { self._handle = h; listen(self, null, -1, -1, backlog); - } else if (typeof h.fd === 'number' && h.fd >= 0) { + } else if (IS_NUMBER(h.fd) && h.fd >= 0) { listen(self, null, null, null, backlog, h.fd); } else { throw new Error('Invalid listen argument: ' + h); @@ -1162,9 +1158,9 @@ Server.prototype.listen = function() { var pipeName = self._pipeName = arguments[0]; listen(self, pipeName, -1, -1, backlog); - } else if (typeof arguments[1] == 'undefined' || - typeof arguments[1] == 'function' || - typeof arguments[1] == 'number') { + } else if (IS_UNDEFINED(arguments[1]) || + IS_FUNCTION(arguments[1]) || + IS_NUMBER(arguments[1])) { // The first argument is the port, no IP given. listen(self, '0.0.0.0', port, 4, backlog); @@ -1351,11 +1347,11 @@ if (process.platform === 'win32') { var simultaneousAccepts; exports._setSimultaneousAccepts = function(handle) { - if (typeof handle === 'undefined') { + if (IS_UNDEFINED(handle)) { return; } - if (typeof simultaneousAccepts === 'undefined') { + if (IS_UNDEFINED(simultaneousAccepts)) { simultaneousAccepts = (process.env.NODE_MANY_ACCEPTS && process.env.NODE_MANY_ACCEPTS !== '0'); } |