From d5214b3627fb317349736757263c86804a057149 Mon Sep 17 00:00:00 2001 From: Bradley Meck Date: Mon, 30 Aug 2010 13:26:53 -0700 Subject: [PATCH] Allow Strings for ports on net.Server.listen --- lib/net.js | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/net.js b/lib/net.js index cc12e9ec39..c91fd3fb2c 100644 --- a/lib/net.js +++ b/lib/net.js @@ -856,7 +856,7 @@ function doConnect (socket, port, host) { }; } -function isPort (x) { return parseInt(x) >= 0; } +function toPort (x) { return (x = Number(x)) >= 0 ? x : false } // var stream = new Stream(); @@ -873,9 +873,16 @@ Stream.prototype.connect = function () { self._connecting = true; // set false in doConnect - if (isPort(arguments[0])) { + var port = toPort(arguments[0]) + if (port === false) { + // UNIX + self.fd = socket('unix'); + self.type = 'unix'; + + setImplmentationMethods(this); + doConnect(self, arguments[0]); + } else { // TCP - var port = arguments[0]; dns.lookup(arguments[1], function (err, ip, addressType) { if (err) { self.emit('error', err); @@ -885,13 +892,6 @@ Stream.prototype.connect = function () { doConnect(self, port, ip); } }); - } else { - // UNIX - self.fd = socket('unix'); - self.type = 'unix'; - - setImplmentationMethods(this); - doConnect(self, arguments[0]); } }; @@ -1109,7 +1109,8 @@ Server.prototype.listen = function () { self.addListener('listening', lastArg); } - if (!isPort(arguments[0])) { + var port = toPort(arguments[0]) + if (port === false) { // the first argument specifies a path self.fd = socket('unix'); self.type = 'unix'; @@ -1144,13 +1145,12 @@ Server.prototype.listen = function () { // The port can be found with server.address() self.type = 'tcp4'; self.fd = socket(self.type); - bind(self.fd, arguments[0]); + bind(self.fd, port); process.nextTick(function () { self._doListen(); }); } else { // the first argument is the port, the second an IP - var port = arguments[0]; dns.lookup(arguments[1], function (err, ip, addressType) { if (err) { self.emit('error', err);