net: move isLegalPort to internal/net
authorEvan Lucas <evanlucas@me.com>
Tue, 26 Jan 2016 14:12:41 +0000 (08:12 -0600)
committerMyles Borins <mborins@us.ibm.com>
Wed, 2 Mar 2016 22:01:11 +0000 (14:01 -0800)
isLegalPort can be used in more places than just net.js. This change
moves it to a new internal net module in preparation for using it in
the dns module.

PR-URL: https://github.com/nodejs/node/pull/4882
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
lib/internal/net.js [new file with mode: 0644]
lib/net.js
node.gyp
test/parallel/test-net-internal.js [new file with mode: 0644]

diff --git a/lib/internal/net.js b/lib/internal/net.js
new file mode 100644 (file)
index 0000000..effc648
--- /dev/null
@@ -0,0 +1,11 @@
+'use strict';
+
+module.exports = { isLegalPort };
+
+// Check that the port number is not NaN when coerced to a number,
+// is an integer and that it falls within the legal range of port numbers.
+function isLegalPort(port) {
+  if (typeof port === 'string' && port.trim() === '')
+    return false;
+  return +port === (port >>> 0) && port >= 0 && port <= 0xFFFF;
+}
index 7a61209..ea19201 100644 (file)
@@ -5,6 +5,7 @@ const stream = require('stream');
 const timers = require('timers');
 const util = require('util');
 const internalUtil = require('internal/util');
+const internalNet = require('internal/net');
 const assert = require('assert');
 const cares = process.binding('cares_wrap');
 const uv = process.binding('uv');
@@ -22,6 +23,7 @@ const WriteWrap = process.binding('stream_wrap').WriteWrap;
 var cluster;
 const errnoException = util._errnoException;
 const exceptionWithHostPort = util._exceptionWithHostPort;
+const isLegalPort = internalNet.isLegalPort;
 
 function noop() {}
 
@@ -846,15 +848,6 @@ function connect(self, address, port, addressType, localAddress, localPort) {
 }
 
 
-// Check that the port number is not NaN when coerced to a number,
-// is an integer and that it falls within the legal range of port numbers.
-function isLegalPort(port) {
-  if (typeof port === 'string' && port.trim() === '')
-    return false;
-  return +port === (port >>> 0) && port >= 0 && port <= 0xFFFF;
-}
-
-
 Socket.prototype.connect = function(options, cb) {
   if (this.write !== Socket.prototype.write)
     this.write = Socket.prototype.write;
index 720d519..0aee6d1 100644 (file)
--- a/node.gyp
+++ b/node.gyp
@@ -72,6 +72,7 @@
       'lib/internal/child_process.js',
       'lib/internal/cluster.js',
       'lib/internal/freelist.js',
+      'lib/internal/net.js',
       'lib/internal/module.js',
       'lib/internal/socket_list.js',
       'lib/internal/repl.js',
diff --git a/test/parallel/test-net-internal.js b/test/parallel/test-net-internal.js
new file mode 100644 (file)
index 0000000..b59b92d
--- /dev/null
@@ -0,0 +1,15 @@
+'use strict';
+
+// Flags: --expose-internals
+
+require('../common');
+const assert = require('assert');
+const net = require('internal/net');
+
+assert.strictEqual(net.isLegalPort(''), false);
+assert.strictEqual(net.isLegalPort('0'), true);
+assert.strictEqual(net.isLegalPort(0), true);
+assert.strictEqual(net.isLegalPort(65536), false);
+assert.strictEqual(net.isLegalPort('65535'), true);
+assert.strictEqual(net.isLegalPort(undefined), false);
+assert.strictEqual(net.isLegalPort(null), true);