mirror of https://github.com/lukechilds/node.git
Browse Source
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>v5.x
Evan Lucas
9 years ago
committed by
Rod Vagg
4 changed files with 29 additions and 9 deletions
@ -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; |
|||
} |
@ -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); |
Loading…
Reference in new issue