Allow Strings for ports on net.Server.listen
authorBradley Meck <bradley.meck@gmail.com>
Mon, 30 Aug 2010 20:26:53 +0000 (13:26 -0700)
committerRyan Dahl <ry@tinyclouds.org>
Mon, 30 Aug 2010 20:27:38 +0000 (13:27 -0700)
lib/net.js

index cc12e9e..c91fd3f 100644 (file)
@@ -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);