Skip to content

Commit

Permalink
net: move isLegalPort to internal/net
Browse files Browse the repository at this point in the history
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: #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>
  • Loading branch information
evanlucas authored and Myles Borins committed Feb 18, 2016
1 parent 57a5f87 commit 96f306f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
11 changes: 11 additions & 0 deletions lib/internal/net.js
Original file line number Diff line number Diff line change
@@ -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;
}
11 changes: 2 additions & 9 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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() {}

Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
15 changes: 15 additions & 0 deletions test/parallel/test-net-internal.js
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit 96f306f

Please sign in to comment.