From eac7aad55e15740fd68c28568f39f108eea8ca5f Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Mon, 7 May 2018 05:12:47 +0200 Subject: [PATCH] net: lazy load dns PR-URL: https://github.com/nodejs/node/pull/20567 Reviewed-By: Gus Caplan Reviewed-By: Matteo Collina Reviewed-By: James M Snell Reviewed-By: Jeremiah Senkpiel --- lib/net.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/net.js b/lib/net.js index 5537c472fe05e3..7d0a4a0e54a255 100644 --- a/lib/net.js +++ b/lib/net.js @@ -75,13 +75,12 @@ const { ERR_SOCKET_BAD_PORT, ERR_SOCKET_CLOSED } = errors.codes; -const dns = require('dns'); const kLastWriteQueueSize = Symbol('lastWriteQueueSize'); -// `cluster` is only used by `listenInCluster` so for startup performance -// reasons it's lazy loaded. -var cluster = null; +// Lazy loaded to improve startup performance. +let cluster; +let dns; const errnoException = errors.errnoException; const exceptionWithHostPort = errors.exceptionWithHostPort; @@ -1034,6 +1033,8 @@ function lookupAndConnect(self, options) { throw new ERR_INVALID_ARG_TYPE('options.lookup', 'Function', options.lookup); + + if (dns === undefined) dns = require('dns'); var dnsopts = { family: options.family, hints: options.hints || 0 @@ -1368,7 +1369,7 @@ function listenInCluster(server, address, port, addressType, backlog, fd, exclusive) { exclusive = !!exclusive; - if (cluster === null) cluster = require('cluster'); + if (cluster === undefined) cluster = require('cluster'); if (cluster.isMaster || exclusive) { // Will create a new handle @@ -1482,6 +1483,7 @@ Server.prototype.listen = function(...args) { }; function lookupAndListen(self, port, address, backlog, exclusive) { + if (dns === undefined) dns = require('dns'); dns.lookup(address, function doListen(err, ip, addressType) { if (err) { self.emit('error', err);