File tree 1 file changed +8
-6
lines changed
1 file changed +8
-6
lines changed Original file line number Diff line number Diff line change 1
1
import crypto from 'node:crypto' ;
2
2
import net from 'node:net' ;
3
3
import os from 'node:os' ;
4
+ import util from 'node:util' ;
4
5
import { SharedContext } from '@ava/cooperate' ;
5
6
6
7
const context = new SharedContext ( import . meta. url ) ;
@@ -11,14 +12,15 @@ const localHosts = new Set([
11
12
...Object . values ( os . networkInterfaces ( ) ) . flatMap ( interfaces => interfaces ?. map ( info => info . address ) ) ,
12
13
] ) ;
13
14
15
+ const minPort = 1024 ;
16
+ const maxPort = 65_535 ;
17
+ const size = 16 ;
18
+ const randomInt = util . promisify ( crypto . randomInt ) as ( max : number ) => Promise < number > ;
19
+
14
20
// Reserve a range of 16 addresses at a random offset.
15
21
const reserveRange = async ( ) : Promise < number [ ] > => {
16
- let from : number ;
17
- do {
18
- from = crypto . randomBytes ( 2 ) . readUInt16BE ( 0 ) ;
19
- } while ( from < 1024 || from > 65_520 ) ;
20
-
21
- const range = Array . from ( { length : 16 } , ( _ , index ) => from + index ) ;
22
+ const from = await randomInt ( maxPort - minPort - size + 1 ) ;
23
+ const range = Array . from ( { length : size } , ( _ , index ) => minPort + from + index ) ;
22
24
return context . reserve ( ...range ) ;
23
25
} ;
24
26
You can’t perform that action at this time.
0 commit comments