Skip to content

Commit ac92d02

Browse files
claudiorodriguezMylesBorins
authored andcommitted
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". PR-URL: #11698 Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Franziska Hinkelmann <[email protected]>
1 parent 414df6c commit ac92d02

File tree

1 file changed

+34
-32
lines changed

1 file changed

+34
-32
lines changed

lib/net.js

+34-32
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ function isPipeName(s) {
4141
return typeof s === 'string' && toNumber(s) === false;
4242
}
4343

44-
exports.createServer = function(options, connectionListener) {
44+
function createServer(options, connectionListener) {
4545
return new Server(options, connectionListener);
46-
};
46+
}
4747

4848

4949
// Target API:
@@ -58,7 +58,7 @@ exports.createServer = function(options, connectionListener) {
5858
// connect(port, [host], [cb])
5959
// connect(path, [cb]);
6060
//
61-
exports.connect = exports.createConnection = function() {
61+
function connect() {
6262
const args = new Array(arguments.length);
6363
for (var i = 0; i < arguments.length; i++)
6464
args[i] = arguments[i];
@@ -74,7 +74,7 @@ exports.connect = exports.createConnection = function() {
7474
}
7575

7676
return Socket.prototype.connect.call(socket, options, cb);
77-
};
77+
}
7878

7979

8080
// Returns an array [options, cb], where options is an object,
@@ -114,7 +114,6 @@ function normalizeArgs(args) {
114114
else
115115
return [options, cb];
116116
}
117-
exports._normalizeArgs = normalizeArgs;
118117

119118

120119
// called when creating new Socket, or when re-using a closed Socket
@@ -312,9 +311,6 @@ function writeAfterFIN(chunk, encoding, cb) {
312311
}
313312
}
314313

315-
exports.Socket = Socket;
316-
exports.Stream = Socket; // Legacy naming.
317-
318314
Socket.prototype.read = function(n) {
319315
if (n === 0)
320316
return stream.Readable.prototype.read.call(this, n);
@@ -829,7 +825,8 @@ function afterWrite(status, handle, req, err) {
829825
}
830826

831827

832-
function connect(self, address, port, addressType, localAddress, localPort) {
828+
function internalConnect(
829+
self, address, port, addressType, localAddress, localPort) {
833830
// TODO return promise from Socket.prototype.connect which
834831
// wraps _connectReq.
835832

@@ -943,7 +940,7 @@ Socket.prototype.connect = function() {
943940
this.writable = true;
944941

945942
if (pipe) {
946-
connect(this, options.path);
943+
internalConnect(this, options.path);
947944
} else {
948945
lookupAndConnect(this, options);
949946
}
@@ -958,7 +955,7 @@ function lookupAndConnect(self, options) {
958955
var localAddress = options.localAddress;
959956
var localPort = options.localPort;
960957

961-
if (localAddress && !exports.isIP(localAddress))
958+
if (localAddress && !cares.isIP(localAddress))
962959
throw new TypeError('"localAddress" option must be a valid IP: ' +
963960
localAddress);
964961

@@ -975,11 +972,11 @@ function lookupAndConnect(self, options) {
975972
port |= 0;
976973

977974
// If host is an IP, skip performing a lookup
978-
var addressType = exports.isIP(host);
975+
var addressType = cares.isIP(host);
979976
if (addressType) {
980977
process.nextTick(function() {
981978
if (self.connecting)
982-
connect(self, host, port, addressType, localAddress, localPort);
979+
internalConnect(self, host, port, addressType, localAddress, localPort);
983980
});
984981
return;
985982
}
@@ -1019,12 +1016,12 @@ function lookupAndConnect(self, options) {
10191016
process.nextTick(connectErrorNT, self, err);
10201017
} else {
10211018
self._unrefTimer();
1022-
connect(self,
1023-
ip,
1024-
port,
1025-
addressType,
1026-
localAddress,
1027-
localPort);
1019+
internalConnect(self,
1020+
ip,
1021+
port,
1022+
addressType,
1023+
localAddress,
1024+
localPort);
10281025
}
10291026
});
10301027
}
@@ -1155,7 +1152,6 @@ function Server(options, connectionListener) {
11551152
this.pauseOnConnect = !!options.pauseOnConnect;
11561153
}
11571154
util.inherits(Server, EventEmitter);
1158-
exports.Server = Server;
11591155

11601156

11611157
function toNumber(x) { return (x = Number(x)) >= 0 ? x : false; }
@@ -1222,7 +1218,6 @@ function createServerHandle(address, port, addressType, fd) {
12221218

12231219
return handle;
12241220
}
1225-
exports._createServerHandle = createServerHandle;
12261221

12271222

12281223
Server.prototype._listen2 = function(address, port, addressType, backlog, fd) {
@@ -1595,20 +1590,12 @@ Server.prototype.unref = function() {
15951590
return this;
15961591
};
15971592

1598-
1599-
exports.isIP = cares.isIP;
1600-
1601-
1602-
exports.isIPv4 = cares.isIPv4;
1603-
1604-
1605-
exports.isIPv6 = cares.isIPv6;
1606-
1593+
var _setSimultaneousAccepts;
16071594

16081595
if (process.platform === 'win32') {
16091596
var simultaneousAccepts;
16101597

1611-
exports._setSimultaneousAccepts = function(handle) {
1598+
_setSimultaneousAccepts = function(handle) {
16121599
if (handle === undefined) {
16131600
return;
16141601
}
@@ -1624,5 +1611,20 @@ if (process.platform === 'win32') {
16241611
}
16251612
};
16261613
} else {
1627-
exports._setSimultaneousAccepts = function(handle) {};
1614+
_setSimultaneousAccepts = function(handle) {};
16281615
}
1616+
1617+
module.exports = {
1618+
_createServerHandle: createServerHandle,
1619+
_normalizeArgs: normalizeArgs,
1620+
_setSimultaneousAccepts,
1621+
connect,
1622+
createConnection: connect,
1623+
createServer,
1624+
isIP: cares.isIP,
1625+
isIPv4: cares.isIPv4,
1626+
isIPv6: cares.isIPv6,
1627+
Server,
1628+
Socket,
1629+
Stream: Socket, // Legacy naming
1630+
};

0 commit comments

Comments
 (0)