Skip to content

Commit e41a3c3

Browse files
net: refactor net module to module.exports
Refactor net module to use the more efficient module.exports = {} pattern. Also renames internal "connect" function to "internalConnect" to avoid collision with exported "connect".
1 parent 2dff3a2 commit e41a3c3

File tree

1 file changed

+34
-32
lines changed

1 file changed

+34
-32
lines changed

lib/net.js

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ function isPipeName(s) {
6262
return typeof s === 'string' && toNumber(s) === false;
6363
}
6464

65-
exports.createServer = function(options, connectionListener) {
65+
function createServer(options, connectionListener) {
6666
return new Server(options, connectionListener);
67-
};
67+
}
6868

6969

7070
// Target API:
@@ -79,7 +79,7 @@ exports.createServer = function(options, connectionListener) {
7979
// connect(port, [host], [cb])
8080
// connect(path, [cb]);
8181
//
82-
exports.connect = exports.createConnection = function() {
82+
function connect() {
8383
const args = new Array(arguments.length);
8484
for (var i = 0; i < arguments.length; i++)
8585
args[i] = arguments[i];
@@ -95,7 +95,7 @@ exports.connect = exports.createConnection = function() {
9595
}
9696

9797
return Socket.prototype.connect.call(socket, options, cb);
98-
};
98+
}
9999

100100

101101
// Returns an array [options, cb], where options is an object,
@@ -135,7 +135,6 @@ function normalizeArgs(args) {
135135
else
136136
return [options, cb];
137137
}
138-
exports._normalizeArgs = normalizeArgs;
139138

140139

141140
// called when creating new Socket, or when re-using a closed Socket
@@ -333,9 +332,6 @@ function writeAfterFIN(chunk, encoding, cb) {
333332
}
334333
}
335334

336-
exports.Socket = Socket;
337-
exports.Stream = Socket; // Legacy naming.
338-
339335
Socket.prototype.read = function(n) {
340336
if (n === 0)
341337
return stream.Readable.prototype.read.call(this, n);
@@ -850,7 +846,8 @@ function afterWrite(status, handle, req, err) {
850846
}
851847

852848

853-
function connect(self, address, port, addressType, localAddress, localPort) {
849+
function internalConnect(
850+
self, address, port, addressType, localAddress, localPort) {
854851
// TODO return promise from Socket.prototype.connect which
855852
// wraps _connectReq.
856853

@@ -964,7 +961,7 @@ Socket.prototype.connect = function() {
964961
this.writable = true;
965962

966963
if (pipe) {
967-
connect(this, options.path);
964+
internalConnect(this, options.path);
968965
} else {
969966
lookupAndConnect(this, options);
970967
}
@@ -979,7 +976,7 @@ function lookupAndConnect(self, options) {
979976
var localAddress = options.localAddress;
980977
var localPort = options.localPort;
981978

982-
if (localAddress && !exports.isIP(localAddress))
979+
if (localAddress && !cares.isIP(localAddress))
983980
throw new TypeError('"localAddress" option must be a valid IP: ' +
984981
localAddress);
985982

@@ -996,11 +993,11 @@ function lookupAndConnect(self, options) {
996993
port |= 0;
997994

998995
// If host is an IP, skip performing a lookup
999-
var addressType = exports.isIP(host);
996+
var addressType = cares.isIP(host);
1000997
if (addressType) {
1001998
process.nextTick(function() {
1002999
if (self.connecting)
1003-
connect(self, host, port, addressType, localAddress, localPort);
1000+
internalConnect(self, host, port, addressType, localAddress, localPort);
10041001
});
10051002
return;
10061003
}
@@ -1040,12 +1037,12 @@ function lookupAndConnect(self, options) {
10401037
process.nextTick(connectErrorNT, self, err);
10411038
} else {
10421039
self._unrefTimer();
1043-
connect(self,
1044-
ip,
1045-
port,
1046-
addressType,
1047-
localAddress,
1048-
localPort);
1040+
internalConnect(self,
1041+
ip,
1042+
port,
1043+
addressType,
1044+
localAddress,
1045+
localPort);
10491046
}
10501047
});
10511048
}
@@ -1177,7 +1174,6 @@ function Server(options, connectionListener) {
11771174
this.pauseOnConnect = !!options.pauseOnConnect;
11781175
}
11791176
util.inherits(Server, EventEmitter);
1180-
exports.Server = Server;
11811177

11821178

11831179
function toNumber(x) { return (x = Number(x)) >= 0 ? x : false; }
@@ -1244,7 +1240,6 @@ function createServerHandle(address, port, addressType, fd) {
12441240

12451241
return handle;
12461242
}
1247-
exports._createServerHandle = createServerHandle;
12481243

12491244

12501245
Server.prototype._listen2 = function(address, port, addressType, backlog, fd) {
@@ -1618,20 +1613,12 @@ Server.prototype.unref = function() {
16181613
return this;
16191614
};
16201615

1621-
1622-
exports.isIP = cares.isIP;
1623-
1624-
1625-
exports.isIPv4 = cares.isIPv4;
1626-
1627-
1628-
exports.isIPv6 = cares.isIPv6;
1629-
1616+
var _setSimultaneousAccepts;
16301617

16311618
if (process.platform === 'win32') {
16321619
var simultaneousAccepts;
16331620

1634-
exports._setSimultaneousAccepts = function(handle) {
1621+
_setSimultaneousAccepts = function(handle) {
16351622
if (handle === undefined) {
16361623
return;
16371624
}
@@ -1647,5 +1634,20 @@ if (process.platform === 'win32') {
16471634
}
16481635
};
16491636
} else {
1650-
exports._setSimultaneousAccepts = function(handle) {};
1637+
_setSimultaneousAccepts = function(handle) {};
16511638
}
1639+
1640+
module.exports = {
1641+
_createServerHandle: createServerHandle,
1642+
_normalizeArgs: normalizeArgs,
1643+
_setSimultaneousAccepts,
1644+
connect,
1645+
createConnection: connect,
1646+
createServer,
1647+
isIP: cares.isIP,
1648+
isIPv4: cares.isIPv4,
1649+
isIPv6: cares.isIPv6,
1650+
Server,
1651+
Socket,
1652+
Stream: Socket, // Legacy naming
1653+
};

0 commit comments

Comments
 (0)