diff --git a/lib/net.js b/lib/net.js index 41a8e24c5a3404..898f14e9ae27c0 100644 --- a/lib/net.js +++ b/lib/net.js @@ -40,8 +40,9 @@ const PipeConnectWrap = process.binding('pipe_wrap').PipeConnectWrap; const ShutdownWrap = process.binding('stream_wrap').ShutdownWrap; const WriteWrap = process.binding('stream_wrap').WriteWrap; - var cluster; +var dns; + const errnoException = util._errnoException; const exceptionWithHostPort = util._exceptionWithHostPort; const isLegalPort = internalNet.isLegalPort; @@ -94,7 +95,7 @@ function connect() { socket.setTimeout(options.timeout); } - return Socket.prototype.connect.call(socket, options, cb); + return realConnect.call(socket, options, cb); } @@ -498,7 +499,7 @@ Socket.prototype.destroySoon = function() { Socket.prototype._destroy = function(exception, cb) { debug('destroy'); - function fireErrorCallbacks(self) { + function fireErrorCallbacks(self, exception, cb) { if (cb) cb(exception); if (exception && !self._writableState.errorEmitted) { process.nextTick(emitErrorNT, self, exception); @@ -508,7 +509,7 @@ Socket.prototype._destroy = function(exception, cb) { if (this.destroyed) { debug('already destroyed, fire error callbacks'); - fireErrorCallbacks(this); + fireErrorCallbacks(this, exception, cb); return; } @@ -540,7 +541,7 @@ Socket.prototype._destroy = function(exception, cb) { // to make it re-entrance safe in case Socket.prototype.destroy() // is called within callbacks this.destroyed = true; - fireErrorCallbacks(this); + fireErrorCallbacks(this, exception, cb); if (this._server) { COUNTER_NET_SERVER_CONNECTION_CLOSE(this); @@ -858,26 +859,20 @@ function internalConnect( var err; if (localAddress || localPort) { - var bind; + debug('binding to localAddress: %s and localPort: %d (addressType: %d)', + localAddress, localPort, addressType); if (addressType === 4) { localAddress = localAddress || '0.0.0.0'; - bind = self._handle.bind; + err = self._handle.bind(localAddress, localPort); } else if (addressType === 6) { localAddress = localAddress || '::'; - bind = self._handle.bind6; + err = self._handle.bind6(localAddress, localPort); } else { self._destroy(new TypeError('Invalid addressType: ' + addressType)); return; } - debug('binding to localAddress: %s and localPort: %d', - localAddress, - localPort); - - bind = bind.bind(self._handle); - err = bind(localAddress, localPort); - if (err) { const ex = exceptionWithHostPort(err, 'bind', localAddress, localPort); self._destroy(ex); @@ -927,7 +922,11 @@ Socket.prototype.connect = function() { const normalized = normalizeArgs(args); const options = normalized[0]; const cb = normalized[1]; + return realConnect.call(this, options, cb); +}; + +function realConnect(options, cb) { if (this.write !== Socket.prototype.write) this.write = Socket.prototype.write; @@ -968,11 +967,11 @@ Socket.prototype.connect = function() { lookupAndConnect(this, options); } return this; -}; +} function lookupAndConnect(self, options) { - const dns = require('dns'); + const dns = lazyDns(); var host = options.host || 'localhost'; var port = options.port; var localAddress = options.localAddress; @@ -997,10 +996,7 @@ function lookupAndConnect(self, options) { // If host is an IP, skip performing a lookup var addressType = cares.isIP(host); if (addressType) { - process.nextTick(function() { - if (self.connecting) - internalConnect(self, host, port, addressType, localAddress, localPort); - }); + internalConnect(self, host, port, addressType, localAddress, localPort); return; } @@ -1016,7 +1012,7 @@ function lookupAndConnect(self, options) { dnsopts.hints = dns.ADDRCONFIG; } - debug('connect: find host ' + host); + debug('connect: find host', host); debug('connect: dns options', dnsopts); self._host = host; var lookup = options.lookup || dns.lookup; @@ -1192,7 +1188,7 @@ function createServerHandle(address, port, addressType, fd) { handle = createHandle(fd); } catch (e) { // Not a fd we can listen on. This will trigger an error. - debug('listen invalid fd=' + fd + ': ' + e.message); + debug('listen invalid fd=%d:', fd, e.message); return uv.UV_EINVAL; } handle.open(fd); @@ -1213,7 +1209,7 @@ function createServerHandle(address, port, addressType, fd) { } if (address || port || isTCP) { - debug('bind to ' + (address || 'anycast')); + debug('bind to', address || 'any'); if (!address) { // Try binding to ipv6 first err = handle.bind6('::', port); @@ -1314,6 +1310,13 @@ function emitListeningNT(self) { } +function lazyDns() { + if (dns === undefined) + dns = require('dns'); + return dns; +} + + function listenInCluster(server, address, port, addressType, backlog, fd, exclusive) { exclusive = !!exclusive; @@ -1442,7 +1445,7 @@ Server.prototype.listen = function() { }; function lookupAndListen(self, port, address, backlog, exclusive) { - const dns = require('dns'); + const dns = lazyDns(); dns.lookup(address, function doListen(err, ip, addressType) { if (err) { self.emit('error', err);