Skip to content

Commit c9b05da

Browse files
evanlucasrvagg
authored andcommitted
net: move isLegalPort to internal/net
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 <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]>
1 parent d47dadc commit c9b05da

File tree

4 files changed

+29
-9
lines changed

4 files changed

+29
-9
lines changed

lib/internal/net.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
module.exports = { isLegalPort };
4+
5+
// Check that the port number is not NaN when coerced to a number,
6+
// is an integer and that it falls within the legal range of port numbers.
7+
function isLegalPort(port) {
8+
if (typeof port === 'string' && port.trim() === '')
9+
return false;
10+
return +port === (port >>> 0) && port >= 0 && port <= 0xFFFF;
11+
}

lib/net.js

+2-9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const stream = require('stream');
55
const timers = require('timers');
66
const util = require('util');
77
const internalUtil = require('internal/util');
8+
const internalNet = require('internal/net');
89
const assert = require('assert');
910
const cares = process.binding('cares_wrap');
1011
const uv = process.binding('uv');
@@ -22,6 +23,7 @@ const WriteWrap = process.binding('stream_wrap').WriteWrap;
2223
var cluster;
2324
const errnoException = util._errnoException;
2425
const exceptionWithHostPort = util._exceptionWithHostPort;
26+
const isLegalPort = internalNet.isLegalPort;
2527

2628
function noop() {}
2729

@@ -846,15 +848,6 @@ function connect(self, address, port, addressType, localAddress, localPort) {
846848
}
847849

848850

849-
// Check that the port number is not NaN when coerced to a number,
850-
// is an integer and that it falls within the legal range of port numbers.
851-
function isLegalPort(port) {
852-
if (typeof port === 'string' && port.trim() === '')
853-
return false;
854-
return +port === (port >>> 0) && port >= 0 && port <= 0xFFFF;
855-
}
856-
857-
858851
Socket.prototype.connect = function(options, cb) {
859852
if (this.write !== Socket.prototype.write)
860853
this.write = Socket.prototype.write;

node.gyp

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
'lib/internal/cluster.js',
7575
'lib/internal/freelist.js',
7676
'lib/internal/linkedlist.js',
77+
'lib/internal/net.js',
7778
'lib/internal/module.js',
7879
'lib/internal/repl.js',
7980
'lib/internal/socket_list.js',

test/parallel/test-net-internal.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
3+
// Flags: --expose-internals
4+
5+
require('../common');
6+
const assert = require('assert');
7+
const net = require('internal/net');
8+
9+
assert.strictEqual(net.isLegalPort(''), false);
10+
assert.strictEqual(net.isLegalPort('0'), true);
11+
assert.strictEqual(net.isLegalPort(0), true);
12+
assert.strictEqual(net.isLegalPort(65536), false);
13+
assert.strictEqual(net.isLegalPort('65535'), true);
14+
assert.strictEqual(net.isLegalPort(undefined), false);
15+
assert.strictEqual(net.isLegalPort(null), true);

0 commit comments

Comments
 (0)