Skip to content

Commit 897b1d2

Browse files
jasnellMylesBorins
authored andcommitted
lib: move isLegalPort to validators, refactor
isLegalPort was used multiple places in the same way -- to validate the port and throw if necessary. Moved into internal/validators. PR-URL: #31851 Reviewed-By: Richard Lau <[email protected]>
1 parent ff58854 commit 897b1d2

File tree

10 files changed

+69
-81
lines changed

10 files changed

+69
-81
lines changed

‎lib/dgram.js‎

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,11 @@ const{
3535
newHandle,
3636
}=require('internal/dgram');
3737
const{ guessHandleType }=internalBinding('util');
38-
const{
39-
isLegalPort,
40-
}=require('internal/net');
4138
const{
4239
ERR_INVALID_ARG_TYPE,
4340
ERR_MISSING_ARGS,
4441
ERR_SOCKET_ALREADY_BOUND,
4542
ERR_SOCKET_BAD_BUFFER_SIZE,
46-
ERR_SOCKET_BAD_PORT,
4743
ERR_SOCKET_BUFFER_SIZE,
4844
ERR_SOCKET_CANNOT_SEND,
4945
ERR_SOCKET_DGRAM_IS_CONNECTED,
@@ -54,7 +50,8 @@ const{
5450
const{
5551
isInt32,
5652
validateString,
57-
validateNumber
53+
validateNumber,
54+
validatePort,
5855
}=require('internal/validators');
5956
const{ Buffer }=require('buffer');
6057
const{ deprecate }=require('internal/util');
@@ -352,21 +349,8 @@ Socket.prototype.bind = function(port_, address_ /* , callback */){
352349
returnthis;
353350
};
354351

355-
356-
functionvalidatePort(port){
357-
constlegal=isLegalPort(port);
358-
if(legal)
359-
port=port|0;
360-
361-
if(!legal||port===0)
362-
thrownewERR_SOCKET_BAD_PORT(port);
363-
364-
returnport;
365-
}
366-
367-
368352
Socket.prototype.connect=function(port,address,callback){
369-
port=validatePort(port);
353+
port=validatePort(port,'Port',{allowZero: false});
370354
if(typeofaddress==='function'){
371355
callback=address;
372356
address='';
@@ -612,7 +596,7 @@ Socket.prototype.send = function(buffer,
612596
}
613597

614598
if(!connected)
615-
port=validatePort(port);
599+
port=validatePort(port,'Port',{allowZero: false});
616600

617601
// Normalize callback so it's either a function or undefined but not anything
618602
// else.

‎lib/dns.js‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const{
2929

3030
constcares=internalBinding('cares_wrap');
3131
const{ toASCII }=require('internal/idna');
32-
const{ isIP, isLegalPort}=require('internal/net');
32+
const{ isIP }=require('internal/net');
3333
const{ customPromisifyArgs }=require('internal/util');
3434
consterrors=require('internal/errors');
3535
const{
@@ -45,9 +45,11 @@ const{
4545
ERR_INVALID_CALLBACK,
4646
ERR_INVALID_OPT_VALUE,
4747
ERR_MISSING_ARGS,
48-
ERR_SOCKET_BAD_PORT
4948
}=errors.codes;
50-
const{ validateString }=require('internal/validators');
49+
const{
50+
validatePort,
51+
validateString,
52+
}=require('internal/validators');
5153

5254
const{
5355
GetAddrInfoReqWrap,
@@ -175,8 +177,7 @@ function lookupService(address, port, callback){
175177
if(isIP(address)===0)
176178
thrownewERR_INVALID_OPT_VALUE('address',address);
177179

178-
if(!isLegalPort(port))
179-
thrownewERR_SOCKET_BAD_PORT(port);
180+
validatePort(port);
180181

181182
if(typeofcallback!=='function')
182183
thrownewERR_INVALID_CALLBACK(callback);

‎lib/internal/cluster/master.js‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@ const RoundRobinHandle = require('internal/cluster/round_robin_handle');
1414
constSharedHandle=require('internal/cluster/shared_handle');
1515
constWorker=require('internal/cluster/worker');
1616
const{ internal, sendHelper }=require('internal/cluster/utils');
17-
const{ERR_SOCKET_BAD_PORT}=require('internal/errors').codes;
1817
constcluster=newEventEmitter();
1918
constintercom=newEventEmitter();
2019
constSCHED_NONE=1;
2120
constSCHED_RR=2;
22-
const{ isLegalPort }=require('internal/net');
2321
const[minPort,maxPort]=[1024,65535];
22+
const{ validatePort }=require('internal/validators');
2423

2524
module.exports=cluster;
2625

@@ -118,9 +117,7 @@ function createWorkerProcess(id, env){
118117
else
119118
inspectPort=cluster.settings.inspectPort;
120119

121-
if(!isLegalPort(inspectPort)){
122-
thrownewERR_SOCKET_BAD_PORT(inspectPort);
123-
}
120+
validatePort(inspectPort);
124121
}else{
125122
inspectPort=process.debugPort+debugPortOffset;
126123
if(inspectPort>maxPort)

‎lib/internal/dns/promises.js‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const{
1414
}=require('internal/dns/utils');
1515
const{ codes, dnsException }=require('internal/errors');
1616
const{ toASCII }=require('internal/idna');
17-
const{ isIP, isLegalPort}=require('internal/net');
17+
const{ isIP }=require('internal/net');
1818
const{
1919
getaddrinfo,
2020
getnameinfo,
@@ -27,10 +27,11 @@ const{
2727
ERR_INVALID_ARG_TYPE,
2828
ERR_INVALID_OPT_VALUE,
2929
ERR_MISSING_ARGS,
30-
ERR_SOCKET_BAD_PORT
3130
}=codes;
32-
const{ validateString }=require('internal/validators');
33-
31+
const{
32+
validatePort,
33+
validateString
34+
}=require('internal/validators');
3435

3536
functiononlookup(err,addresses){
3637
if(err){
@@ -162,8 +163,7 @@ function lookupService(address, port){
162163
if(isIP(address)===0)
163164
thrownewERR_INVALID_OPT_VALUE('address',address);
164165

165-
if(!isLegalPort(port))
166-
thrownewERR_SOCKET_BAD_PORT(port);
166+
validatePort(port);
167167

168168
returncreateLookupServicePromise(address,+port);
169169
}

‎lib/internal/errors.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1278,7 +1278,7 @@ E('ERR_SOCKET_ALREADY_BOUND', 'Socket is already bound', Error);
12781278
E('ERR_SOCKET_BAD_BUFFER_SIZE',
12791279
'Buffer size must be a positive integer',TypeError);
12801280
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);
12821282
E('ERR_SOCKET_BAD_TYPE',
12831283
'Bad socket type specified. Valid types are: udp4, udp6',TypeError);
12841284
E('ERR_SOCKET_BUFFER_SIZE',

‎lib/internal/net.js‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,6 @@ function isIP(s){
4141
return0;
4242
}
4343

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-
functionisLegalPort(port){
47-
if((typeofport!=='number'&&typeofport!=='string')||
48-
(typeofport==='string'&&port.trim().length===0))
49-
returnfalse;
50-
return+port===(+port>>>0)&&port<=0xFFFF;
51-
}
52-
5344
functionmakeSyncWrite(fd){
5445
returnfunction(chunk,enc,cb){
5546
if(enc!=='buffer')
@@ -72,7 +63,6 @@ module.exports ={
7263
isIP,
7364
isIPv4,
7465
isIPv6,
75-
isLegalPort,
7666
makeSyncWrite,
7767
normalizedArgsSymbol: Symbol('normalizedArgs')
7868
};

‎lib/internal/validators.js‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const{
1010
const{
1111
hideStackFrames,
1212
codes: {
13+
ERR_SOCKET_BAD_PORT,
1314
ERR_INVALID_ARG_TYPE,
1415
ERR_INVALID_ARG_VALUE,
1516
ERR_OUT_OF_RANGE,
@@ -180,6 +181,19 @@ function validateEncoding(data, encoding){
180181
}
181182
}
182183

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+
functionvalidatePort(port,name='Port',{ allowZero =true}={}){
187+
if((typeofport!=='number'&&typeofport!=='string')||
188+
(typeofport==='string'&&port.trim().length===0)||
189+
+port!==(+port>>>0)||
190+
port>0xFFFF||
191+
(port===0&&!allowZero)){
192+
thrownewERR_SOCKET_BAD_PORT(name,port);
193+
}
194+
returnport|0;
195+
}
196+
183197
module.exports={
184198
isInt32,
185199
isUint32,
@@ -188,11 +202,12 @@ module.exports ={
188202
validateBoolean,
189203
validateBuffer,
190204
validateEncoding,
191-
validateObject,
192-
validateInteger,
193205
validateInt32,
194-
validateUint32,
195-
validateString,
206+
validateInteger,
196207
validateNumber,
197-
validateSignalName
208+
validateObject,
209+
validatePort,
210+
validateSignalName,
211+
validateString,
212+
validateUint32,
198213
};

‎lib/net.js‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ const{
4141
isIP,
4242
isIPv4,
4343
isIPv6,
44-
isLegalPort,
4544
normalizedArgsSymbol,
4645
makeSyncWrite
4746
}=require('internal/net');
@@ -92,15 +91,18 @@ const{
9291
ERR_INVALID_OPT_VALUE,
9392
ERR_SERVER_ALREADY_LISTEN,
9493
ERR_SERVER_NOT_RUNNING,
95-
ERR_SOCKET_BAD_PORT,
9694
ERR_SOCKET_CLOSED
9795
},
9896
errnoException,
9997
exceptionWithHostPort,
10098
uvExceptionWithHostPort
10199
}=require('internal/errors');
102100
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');
104106
constkLastWriteQueueSize=Symbol('lastWriteQueueSize');
105107
const{
106108
DTRACE_NET_SERVER_CONNECTION,
@@ -997,9 +999,7 @@ function lookupAndConnect(self, options){
997999
thrownewERR_INVALID_ARG_TYPE('options.port',
9981000
['number','string'],port);
9991001
}
1000-
if(!isLegalPort(port)){
1001-
thrownewERR_SOCKET_BAD_PORT(port);
1002-
}
1002+
validatePort(port);
10031003
}
10041004
port|=0;
10051005

@@ -1436,9 +1436,7 @@ Server.prototype.listen = function(...args){
14361436
// or if options.port is normalized as 0 before
14371437
letbacklog;
14381438
if(typeofoptions.port==='number'||typeofoptions.port==='string'){
1439-
if(!isLegalPort(options.port)){
1440-
thrownewERR_SOCKET_BAD_PORT(options.port);
1441-
}
1439+
validatePort(options.port,'options.port');
14421440
backlog=options.backlog||backlogFromArgs;
14431441
// start TCP server listening on host:port
14441442
if(options.host){
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Flags: --expose-internals
2+
'use strict';
3+
4+
require('../common');
5+
constassert=require('assert');
6+
const{ validatePort }=require('internal/validators');
7+
8+
for(letn=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+
}));

‎test/parallel/test-net-internal.js‎

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
(0)