Skip to content

Commit

Permalink
net: refactor net module to module.exports
Browse files Browse the repository at this point in the history
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 <joyeec9h3@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
  • Loading branch information
claudiorodriguez authored and MylesBorins committed Mar 28, 2017
1 parent 414df6c commit ac92d02
Showing 1 changed file with 34 additions and 32 deletions.
66 changes: 34 additions & 32 deletions lib/net.js
Expand Up @@ -41,9 +41,9 @@ function isPipeName(s) {
return typeof s === 'string' && toNumber(s) === false;
}

exports.createServer = function(options, connectionListener) {
function createServer(options, connectionListener) {
return new Server(options, connectionListener);
};
}


// Target API:
Expand All @@ -58,7 +58,7 @@ exports.createServer = function(options, connectionListener) {
// connect(port, [host], [cb])
// connect(path, [cb]);
//
exports.connect = exports.createConnection = function() {
function connect() {
const args = new Array(arguments.length);
for (var i = 0; i < arguments.length; i++)
args[i] = arguments[i];
Expand All @@ -74,7 +74,7 @@ exports.connect = exports.createConnection = function() {
}

return Socket.prototype.connect.call(socket, options, cb);
};
}


// Returns an array [options, cb], where options is an object,
Expand Down Expand Up @@ -114,7 +114,6 @@ function normalizeArgs(args) {
else
return [options, cb];
}
exports._normalizeArgs = normalizeArgs;


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

exports.Socket = Socket;
exports.Stream = Socket; // Legacy naming.

Socket.prototype.read = function(n) {
if (n === 0)
return stream.Readable.prototype.read.call(this, n);
Expand Down Expand Up @@ -829,7 +825,8 @@ function afterWrite(status, handle, req, err) {
}


function connect(self, address, port, addressType, localAddress, localPort) {
function internalConnect(
self, address, port, addressType, localAddress, localPort) {
// TODO return promise from Socket.prototype.connect which
// wraps _connectReq.

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

if (pipe) {
connect(this, options.path);
internalConnect(this, options.path);
} else {
lookupAndConnect(this, options);
}
Expand All @@ -958,7 +955,7 @@ function lookupAndConnect(self, options) {
var localAddress = options.localAddress;
var localPort = options.localPort;

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

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

// If host is an IP, skip performing a lookup
var addressType = exports.isIP(host);
var addressType = cares.isIP(host);
if (addressType) {
process.nextTick(function() {
if (self.connecting)
connect(self, host, port, addressType, localAddress, localPort);
internalConnect(self, host, port, addressType, localAddress, localPort);
});
return;
}
Expand Down Expand Up @@ -1019,12 +1016,12 @@ function lookupAndConnect(self, options) {
process.nextTick(connectErrorNT, self, err);
} else {
self._unrefTimer();
connect(self,
ip,
port,
addressType,
localAddress,
localPort);
internalConnect(self,
ip,
port,
addressType,
localAddress,
localPort);
}
});
}
Expand Down Expand Up @@ -1155,7 +1152,6 @@ function Server(options, connectionListener) {
this.pauseOnConnect = !!options.pauseOnConnect;
}
util.inherits(Server, EventEmitter);
exports.Server = Server;


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

return handle;
}
exports._createServerHandle = createServerHandle;


Server.prototype._listen2 = function(address, port, addressType, backlog, fd) {
Expand Down Expand Up @@ -1595,20 +1590,12 @@ Server.prototype.unref = function() {
return this;
};


exports.isIP = cares.isIP;


exports.isIPv4 = cares.isIPv4;


exports.isIPv6 = cares.isIPv6;

var _setSimultaneousAccepts;

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

exports._setSimultaneousAccepts = function(handle) {
_setSimultaneousAccepts = function(handle) {
if (handle === undefined) {
return;
}
Expand All @@ -1624,5 +1611,20 @@ if (process.platform === 'win32') {
}
};
} else {
exports._setSimultaneousAccepts = function(handle) {};
_setSimultaneousAccepts = function(handle) {};
}

module.exports = {
_createServerHandle: createServerHandle,
_normalizeArgs: normalizeArgs,
_setSimultaneousAccepts,
connect,
createConnection: connect,
createServer,
isIP: cares.isIP,
isIPv4: cares.isIPv4,
isIPv6: cares.isIPv6,
Server,
Socket,
Stream: Socket, // Legacy naming
};

0 comments on commit ac92d02

Please sign in to comment.