| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent ff58854 commit 897b1d2
10 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -35,15 +35,11 @@ const { | |||
| 35 | 35 | newHandle, | |
| 36 | 36 | } = require('internal/dgram'); | |
| 37 | 37 | const { guessHandleType } = internalBinding('util'); | |
| 38 | - const { | ||
| 39 | - isLegalPort, | ||
| 40 | - } = require('internal/net'); | ||
| 41 | 38 | const { | |
| 42 | 39 | ERR_INVALID_ARG_TYPE, | |
| 43 | 40 | ERR_MISSING_ARGS, | |
| 44 | 41 | ERR_SOCKET_ALREADY_BOUND, | |
| 45 | 42 | ERR_SOCKET_BAD_BUFFER_SIZE, | |
| 46 | - ERR_SOCKET_BAD_PORT, | ||
| 47 | 43 | ERR_SOCKET_BUFFER_SIZE, | |
| 48 | 44 | ERR_SOCKET_CANNOT_SEND, | |
| 49 | 45 | ERR_SOCKET_DGRAM_IS_CONNECTED, | |
@@ -54,7 +50,8 @@ const { | |||
| 54 | 50 | const { | |
| 55 | 51 | isInt32, | |
| 56 | 52 | validateString, | |
| 57 | - validateNumber | ||
| 53 | + validateNumber, | ||
| 54 | + validatePort, | ||
| 58 | 55 | } = require('internal/validators'); | |
| 59 | 56 | const { Buffer } = require('buffer'); | |
| 60 | 57 | const { deprecate } = require('internal/util'); | |
@@ -352,21 +349,8 @@ Socket.prototype.bind = function(port_, address_ /* , callback */) { | |||
| 352 | 349 | return this; | |
| 353 | 350 | }; | |
| 354 | 351 | ||
| 355 | - | ||
| 356 | - function validatePort(port) { | ||
| 357 | - const legal = isLegalPort(port); | ||
| 358 | - if (legal) | ||
| 359 | - port = port | 0; | ||
| 360 | - | ||
| 361 | - if (!legal || port === 0) | ||
| 362 | - throw new ERR_SOCKET_BAD_PORT(port); | ||
| 363 | - | ||
| 364 | - return port; | ||
| 365 | - } | ||
| 366 | - | ||
| 367 | - | ||
| 368 | 352 | Socket.prototype.connect = function(port, address, callback) { | |
| 369 | - port = validatePort(port); | ||
| 353 | + port = validatePort(port, 'Port', { allowZero: false }); | ||
| 370 | 354 | if (typeof address === 'function') { | |
| 371 | 355 | callback = address; | |
| 372 | 356 | address = ''; | |
@@ -612,7 +596,7 @@ Socket.prototype.send = function(buffer, | |||
| 612 | 596 | } | |
| 613 | 597 | ||
| 614 | 598 | if (!connected) | |
| 615 | - port = validatePort(port); | ||
| 599 | + port = validatePort(port, 'Port', { allowZero: false }); | ||
| 616 | 600 | ||
| 617 | 601 | // Normalize callback so it's either a function or undefined but not anything | |
| 618 | 602 | // else. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -29,7 +29,7 @@ const { | |||
| 29 | 29 | ||
| 30 | 30 | const cares = internalBinding('cares_wrap'); | |
| 31 | 31 | const { toASCII } = require('internal/idna'); | |
| 32 | - const { isIP, isLegalPort } = require('internal/net'); | ||
| 32 | + const { isIP } = require('internal/net'); | ||
| 33 | 33 | const { customPromisifyArgs } = require('internal/util'); | |
| 34 | 34 | const errors = require('internal/errors'); | |
| 35 | 35 | const { | |
@@ -45,9 +45,11 @@ const { | |||
| 45 | 45 | ERR_INVALID_CALLBACK, | |
| 46 | 46 | ERR_INVALID_OPT_VALUE, | |
| 47 | 47 | ERR_MISSING_ARGS, | |
| 48 | - ERR_SOCKET_BAD_PORT | ||
| 49 | 48 | } = errors.codes; | |
| 50 | - const { validateString } = require('internal/validators'); | ||
| 49 | + const { | ||
| 50 | + validatePort, | ||
| 51 | + validateString, | ||
| 52 | + } = require('internal/validators'); | ||
| 51 | 53 | ||
| 52 | 54 | const { | |
| 53 | 55 | GetAddrInfoReqWrap, | |
@@ -175,8 +177,7 @@ function lookupService(address, port, callback) { | |||
| 175 | 177 | if (isIP(address) === 0) | |
| 176 | 178 | throw new ERR_INVALID_OPT_VALUE('address', address); | |
| 177 | 179 | ||
| 178 | - if (!isLegalPort(port)) | ||
| 179 | - throw new ERR_SOCKET_BAD_PORT(port); | ||
| 180 | + validatePort(port); | ||
| 180 | 181 | ||
| 181 | 182 | if (typeof callback !== 'function') | |
| 182 | 183 | throw new ERR_INVALID_CALLBACK(callback); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,13 +14,12 @@ const RoundRobinHandle = require('internal/cluster/round_robin_handle'); | |||
| 14 | 14 | const SharedHandle = require('internal/cluster/shared_handle'); | |
| 15 | 15 | const Worker = require('internal/cluster/worker'); | |
| 16 | 16 | const { internal, sendHelper } = require('internal/cluster/utils'); | |
| 17 | - const { ERR_SOCKET_BAD_PORT } = require('internal/errors').codes; | ||
| 18 | 17 | const cluster = new EventEmitter(); | |
| 19 | 18 | const intercom = new EventEmitter(); | |
| 20 | 19 | const SCHED_NONE = 1; | |
| 21 | 20 | const SCHED_RR = 2; | |
| 22 | - const { isLegalPort } = require('internal/net'); | ||
| 23 | 21 | const [ minPort, maxPort ] = [ 1024, 65535 ]; | |
| 22 | + const { validatePort } = require('internal/validators'); | ||
| 24 | 23 | ||
| 25 | 24 | module.exports = cluster; | |
| 26 | 25 | ||
@@ -118,9 +117,7 @@ function createWorkerProcess(id, env) { | |||
| 118 | 117 | else | |
| 119 | 118 | inspectPort = cluster.settings.inspectPort; | |
| 120 | 119 | ||
| 121 | - if (!isLegalPort(inspectPort)) { | ||
| 122 | - throw new ERR_SOCKET_BAD_PORT(inspectPort); | ||
| 123 | - } | ||
| 120 | + validatePort(inspectPort); | ||
| 124 | 121 | } else { | |
| 125 | 122 | inspectPort = process.debugPort + debugPortOffset; | |
| 126 | 123 | if (inspectPort > maxPort) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,7 +14,7 @@ const { | |||
| 14 | 14 | } = require('internal/dns/utils'); | |
| 15 | 15 | const { codes, dnsException } = require('internal/errors'); | |
| 16 | 16 | const { toASCII } = require('internal/idna'); | |
| 17 | - const { isIP, isLegalPort } = require('internal/net'); | ||
| 17 | + const { isIP } = require('internal/net'); | ||
| 18 | 18 | const { | |
| 19 | 19 | getaddrinfo, | |
| 20 | 20 | getnameinfo, | |
@@ -27,10 +27,11 @@ const { | |||
| 27 | 27 | ERR_INVALID_ARG_TYPE, | |
| 28 | 28 | ERR_INVALID_OPT_VALUE, | |
| 29 | 29 | ERR_MISSING_ARGS, | |
| 30 | - ERR_SOCKET_BAD_PORT | ||
| 31 | 30 | } = codes; | |
| 32 | - const { validateString } = require('internal/validators'); | ||
| 33 | - | ||
| 31 | + const { | ||
| 32 | + validatePort, | ||
| 33 | + validateString | ||
| 34 | + } = require('internal/validators'); | ||
| 34 | 35 | ||
| 35 | 36 | function onlookup(err, addresses) { | |
| 36 | 37 | if (err) { | |
@@ -162,8 +163,7 @@ function lookupService(address, port) { | |||
| 162 | 163 | if (isIP(address) === 0) | |
| 163 | 164 | throw new ERR_INVALID_OPT_VALUE('address', address); | |
| 164 | 165 | ||
| 165 | - if (!isLegalPort(port)) | ||
| 166 | - throw new ERR_SOCKET_BAD_PORT(port); | ||
| 166 | + validatePort(port); | ||
| 167 | 167 | ||
| 168 | 168 | return createLookupServicePromise(address, +port); | |
| 169 | 169 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1278,7 +1278,7 @@ E('ERR_SOCKET_ALREADY_BOUND', 'Socket is already bound', Error); | |||
| 1278 | 1278 | E('ERR_SOCKET_BAD_BUFFER_SIZE', | |
| 1279 | 1279 | 'Buffer size must be a positive integer', TypeError); | |
| 1280 | 1280 | E('ERR_SOCKET_BAD_PORT', | |
| 1281 | - 'Port should be >= 0 and < 65536. Received %s.', RangeError); | ||
| 1281 | + '%s should be >= 0 and < 65536. Received %s.', RangeError); | ||
| 1282 | 1282 | E('ERR_SOCKET_BAD_TYPE', | |
| 1283 | 1283 | 'Bad socket type specified. Valid types are: udp4, udp6', TypeError); | |
| 1284 | 1284 | E('ERR_SOCKET_BUFFER_SIZE', | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -41,15 +41,6 @@ function isIP(s) { | |||
| 41 | 41 | return 0; | |
| 42 | 42 | } | |
| 43 | 43 | ||
| 44 | - // Check that the port number is not NaN when coerced to a number, | ||
| 45 | - // is an integer and that it falls within the legal range of port numbers. | ||
| 46 | - function isLegalPort(port) { | ||
| 47 | - if ((typeof port !== 'number' && typeof port !== 'string') || | ||
| 48 | - (typeof port === 'string' && port.trim().length === 0)) | ||
| 49 | - return false; | ||
| 50 | - return +port === (+port >>> 0) && port <= 0xFFFF; | ||
| 51 | - } | ||
| 52 | - | ||
| 53 | 44 | function makeSyncWrite(fd) { | |
| 54 | 45 | return function(chunk, enc, cb) { | |
| 55 | 46 | if (enc !== 'buffer') | |
@@ -72,7 +63,6 @@ module.exports = { | |||
| 72 | 63 | isIP, | |
| 73 | 64 | isIPv4, | |
| 74 | 65 | isIPv6, | |
| 75 | - isLegalPort, | ||
| 76 | 66 | makeSyncWrite, | |
| 77 | 67 | normalizedArgsSymbol: Symbol('normalizedArgs') | |
| 78 | 68 | }; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -10,6 +10,7 @@ const { | |||
| 10 | 10 | const { | |
| 11 | 11 | hideStackFrames, | |
| 12 | 12 | codes: { | |
| 13 | + ERR_SOCKET_BAD_PORT, | ||
| 13 | 14 | ERR_INVALID_ARG_TYPE, | |
| 14 | 15 | ERR_INVALID_ARG_VALUE, | |
| 15 | 16 | ERR_OUT_OF_RANGE, | |
@@ -180,6 +181,19 @@ function validateEncoding(data, encoding) { | |||
| 180 | 181 | } | |
| 181 | 182 | } | |
| 182 | 183 | ||
| 184 | + // Check that the port number is not NaN when coerced to a number, | ||
| 185 | + // is an integer and that it falls within the legal range of port numbers. | ||
| 186 | + function validatePort(port, name = 'Port', { allowZero = true } = {}) { | ||
| 187 | + if ((typeof port !== 'number' && typeof port !== 'string') || | ||
| 188 | + (typeof port === 'string' && port.trim().length === 0) || | ||
| 189 | + +port !== (+port >>> 0) || | ||
| 190 | + port > 0xFFFF || | ||
| 191 | + (port === 0 && !allowZero)) { | ||
| 192 | + throw new ERR_SOCKET_BAD_PORT(name, port); | ||
| 193 | + } | ||
| 194 | + return port | 0; | ||
| 195 | + } | ||
| 196 | + | ||
| 183 | 197 | module.exports = { | |
| 184 | 198 | isInt32, | |
| 185 | 199 | isUint32, | |
@@ -188,11 +202,12 @@ module.exports = { | |||
| 188 | 202 | validateBoolean, | |
| 189 | 203 | validateBuffer, | |
| 190 | 204 | validateEncoding, | |
| 191 | - validateObject, | ||
| 192 | - validateInteger, | ||
| 193 | 205 | validateInt32, | |
| 194 | - validateUint32, | ||
| 195 | - validateString, | ||
| 206 | + validateInteger, | ||
| 196 | 207 | validateNumber, | |
| 197 | - validateSignalName | ||
| 208 | + validateObject, | ||
| 209 | + validatePort, | ||
| 210 | + validateSignalName, | ||
| 211 | + validateString, | ||
| 212 | + validateUint32, | ||
| 198 | 213 | }; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -41,7 +41,6 @@ const { | |||
| 41 | 41 | isIP, | |
| 42 | 42 | isIPv4, | |
| 43 | 43 | isIPv6, | |
| 44 | - isLegalPort, | ||
| 45 | 44 | normalizedArgsSymbol, | |
| 46 | 45 | makeSyncWrite | |
| 47 | 46 | } = require('internal/net'); | |
@@ -92,15 +91,18 @@ const { | |||
| 92 | 91 | ERR_INVALID_OPT_VALUE, | |
| 93 | 92 | ERR_SERVER_ALREADY_LISTEN, | |
| 94 | 93 | ERR_SERVER_NOT_RUNNING, | |
| 95 | - ERR_SOCKET_BAD_PORT, | ||
| 96 | 94 | ERR_SOCKET_CLOSED | |
| 97 | 95 | }, | |
| 98 | 96 | errnoException, | |
| 99 | 97 | exceptionWithHostPort, | |
| 100 | 98 | uvExceptionWithHostPort | |
| 101 | 99 | } = require('internal/errors'); | |
| 102 | 100 | const { isUint8Array } = require('internal/util/types'); | |
| 103 | - const { validateInt32, validateString } = require('internal/validators'); | ||
| 101 | + const { | ||
| 102 | + validateInt32, | ||
| 103 | + validatePort, | ||
| 104 | + validateString | ||
| 105 | + } = require('internal/validators'); | ||
| 104 | 106 | const kLastWriteQueueSize = Symbol('lastWriteQueueSize'); | |
| 105 | 107 | const { | |
| 106 | 108 | DTRACE_NET_SERVER_CONNECTION, | |
@@ -997,9 +999,7 @@ function lookupAndConnect(self, options) { | |||
| 997 | 999 | throw new ERR_INVALID_ARG_TYPE('options.port', | |
| 998 | 1000 | ['number', 'string'], port); | |
| 999 | 1001 | } | |
| 1000 | - if (!isLegalPort(port)) { | ||
| 1001 | - throw new ERR_SOCKET_BAD_PORT(port); | ||
| 1002 | - } | ||
| 1002 | + validatePort(port); | ||
| 1003 | 1003 | } | |
| 1004 | 1004 | port |= 0; | |
| 1005 | 1005 | ||
@@ -1436,9 +1436,7 @@ Server.prototype.listen = function(...args) { | |||
| 1436 | 1436 | // or if options.port is normalized as 0 before | |
| 1437 | 1437 | let backlog; | |
| 1438 | 1438 | if (typeof options.port === 'number' || typeof options.port === 'string') { | |
| 1439 | - if (!isLegalPort(options.port)) { | ||
| 1440 | - throw new ERR_SOCKET_BAD_PORT(options.port); | ||
| 1441 | - } | ||
| 1439 | + validatePort(options.port, 'options.port'); | ||
| 1442 | 1440 | backlog = options.backlog || backlogFromArgs; | |
| 1443 | 1441 | // start TCP server listening on host:port | |
| 1444 | 1442 | if (options.host) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,23 @@ | |||
| 1 | + // Flags: --expose-internals | ||
| 2 | + 'use strict'; | ||
| 3 | + | ||
| 4 | + require('../common'); | ||
| 5 | + const assert = require('assert'); | ||
| 6 | + const { validatePort } = require('internal/validators'); | ||
| 7 | + | ||
| 8 | + for (let n = 0; n <= 0xFFFF; n++) { | ||
| 9 | + validatePort(n); | ||
| 10 | + validatePort(`${n}`); | ||
| 11 | + validatePort(`0x${n.toString(16)}`); | ||
| 12 | + validatePort(`0o${n.toString(8)}`); | ||
| 13 | + validatePort(`0b${n.toString(2)}`); | ||
| 14 | + } | ||
| 15 | + | ||
| 16 | + [ | ||
| 17 | + -1, 'a', {}, [], false, true, | ||
| 18 | + 0xFFFF + 1, Infinity, -Infinity, NaN, | ||
| 19 | + undefined, null, '', ' ', 1.1, '0x', | ||
| 20 | + '-0x1', '-0o1', '-0b1', '0o', '0b' | ||
| 21 | + ].forEach((i) => assert.throws(() => validatePort(i), { | ||
| 22 | + code: 'ERR_SOCKET_BAD_PORT' | ||
| 23 | + })); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments