17
17
18
18
'use strict' ;
19
19
20
- var exec = require ( 'child_process' ) . exec ,
21
- fs = require ( 'fs' ) ,
22
- net = require ( 'net ' ) ;
20
+ const fs = require ( 'fs' ) ;
21
+ const net = require ( 'net' ) ;
22
+ const { exec } = require ( 'child_process ' ) ;
23
23
24
24
25
25
/**
@@ -34,9 +34,9 @@ const DEFAULT_IANA_RANGE = {min: 49152, max: 65535};
34
34
/**
35
35
* The epheremal port range for the current system. Lazily computed on first
36
36
* access.
37
- * @type {Promise. <{min: number, max: number}> }
37
+ * @type {Promise<{min: number, max: number}> }
38
38
*/
39
- var systemRange = null ;
39
+ let systemRange = null ;
40
40
41
41
42
42
/**
@@ -49,11 +49,10 @@ function findSystemPortRange() {
49
49
if ( systemRange ) {
50
50
return systemRange ;
51
51
}
52
- var range = process . platform === 'win32' ?
53
- findWindowsPortRange ( ) : findUnixPortRange ( ) ;
54
- return systemRange = range . catch ( function ( ) {
55
- return DEFAULT_IANA_RANGE ;
56
- } ) ;
52
+ let range =
53
+ process . platform === 'win32'
54
+ ? findWindowsPortRange ( ) : findUnixPortRange ( ) ;
55
+ return systemRange = range . catch ( ( ) => DEFAULT_IANA_RANGE ) ;
57
56
}
58
57
59
58
@@ -81,7 +80,7 @@ function execute(cmd) {
81
80
* @return {!Promise<{min: number, max: number}> } A promise that will resolve
82
81
* with the ephemeral port range on the current system.
83
82
*/
84
- function findUnixPortRange ( ) {
83
+ async function findUnixPortRange ( ) {
85
84
var cmd ;
86
85
if ( process . platform === 'sunos' ) {
87
86
cmd =
@@ -94,12 +93,17 @@ function findUnixPortRange() {
94
93
' | sed -e "s/.*:\\s*//"' ;
95
94
}
96
95
97
- return execute ( cmd ) . then ( function ( stdout ) {
98
- if ( ! stdout || ! stdout . length ) return DEFAULT_IANA_RANGE ;
99
- var range = stdout . trim ( ) . split ( / \s + / ) . map ( Number ) ;
100
- if ( range . some ( isNaN ) ) return DEFAULT_IANA_RANGE ;
101
- return { min : range [ 0 ] , max : range [ 1 ] } ;
102
- } ) ;
96
+ let stdout = await execute ( cmd ) ;
97
+ if ( ! stdout || ! stdout . length ) {
98
+ return DEFAULT_IANA_RANGE ;
99
+ } ;
100
+
101
+ let range = stdout . trim ( ) . split ( / \s + / ) . map ( Number ) ;
102
+ if ( range . some ( isNaN ) ) {
103
+ return DEFAULT_IANA_RANGE ;
104
+ } ;
105
+
106
+ return { min : range [ 0 ] , max : range [ 1 ] } ;
103
107
}
104
108
105
109
@@ -108,35 +112,29 @@ function findUnixPortRange() {
108
112
* @return {!Promise<{min: number, max: number}> } A promise that will resolve
109
113
* with the ephemeral port range on the current system.
110
114
*/
111
- function findWindowsPortRange ( ) {
115
+ async function findWindowsPortRange ( ) {
112
116
// First, check if we're running on XP. If this initial command fails,
113
117
// we just fallback on the default IANA range.
114
- return execute ( 'cmd.exe /c ver' ) . then ( function ( stdout ) {
115
- if ( / W i n d o w s X P / . test ( stdout ) ) {
116
- // TODO: Try to read these values from the registry.
117
- return { min : 1025 , max : 5000 } ;
118
- } else {
119
- return execute ( 'netsh int ipv4 show dynamicport tcp' ) .
120
- then ( function ( stdout ) {
121
- /* > netsh int ipv4 show dynamicport tcp
122
- Protocol tcp Dynamic Port Range
123
- ---------------------------------
124
- Start Port : 49152
125
- Number of Ports : 16384
126
- */
127
- var range = stdout . split ( / \n / ) . filter ( function ( line ) {
128
- return / .* : \s * \d + / . test ( line ) ;
129
- } ) . map ( function ( line ) {
130
- return Number ( line . split ( / : \s * / ) [ 1 ] ) ;
131
- } ) ;
132
-
133
- return {
134
- min : range [ 0 ] ,
135
- max : range [ 0 ] + range [ 1 ]
136
- } ;
137
- } ) ;
138
- }
139
- } ) ;
118
+ let stdout = await execute ( 'cmd.exe /c ver' ) ;
119
+ if ( / W i n d o w s X P / . test ( stdout ) ) {
120
+ // TODO: Try to read these values from the registry.
121
+ return { min : 1025 , max : 5000 } ;
122
+ } else {
123
+ stdout = await execute ( 'netsh int ipv4 show dynamicport tcp' ) ;
124
+ /* > netsh int ipv4 show dynamicport tcp
125
+ Protocol tcp Dynamic Port Range
126
+ ---------------------------------
127
+ Start Port : 49152
128
+ Number of Ports : 16384
129
+ */
130
+ let range = stdout . split ( / \n / )
131
+ . filter ( ( line ) => / .* : \s * \d + / . test ( line ) )
132
+ . map ( ( line ) => Number ( line . split ( / : \s * / ) [ 1 ] ) ) ;
133
+ return {
134
+ min : range [ 0 ] ,
135
+ max : range [ 0 ] + range [ 1 ]
136
+ } ;
137
+ }
140
138
}
141
139
142
140
@@ -171,31 +169,15 @@ function isFree(port, opt_host) {
171
169
* @return {!Promise<number> } A promise that will resolve to a free port. If a
172
170
* port cannot be found, the promise will be rejected.
173
171
*/
174
- function findFreePort ( opt_host ) {
175
- return findSystemPortRange ( ) . then ( function ( range ) {
176
- var attempts = 0 ;
177
- var maxAttempts = 100 ;
178
- return new Promise ( ( resolve , reject ) => {
179
- findPort ( ) ;
180
-
181
- function findPort ( ) {
182
- attempts += 1 ;
183
- if ( attempts > maxAttempts ) {
184
- reject ( Error ( 'Unable to find a free port' ) ) ;
185
- }
186
-
187
- var port = Math . floor (
188
- Math . random ( ) * ( range . max - range . min ) + range . min ) ;
189
- isFree ( port , opt_host ) . then ( function ( isFree ) {
190
- if ( isFree ) {
191
- resolve ( port ) ;
192
- } else {
193
- findPort ( ) ;
194
- }
195
- } , findPort ) ;
196
- }
197
- } ) ;
198
- } ) ;
172
+ async function findFreePort ( opt_host ) {
173
+ let range = await findSystemPortRange ( ) ;
174
+ for ( let i = 0 ; i < 100 ; i ++ ) {
175
+ let port = Math . floor ( Math . random ( ) * ( range . max - range . min ) + range . min ) ;
176
+ if ( await isFree ( port , opt_host ) ) {
177
+ return port ;
178
+ }
179
+ }
180
+ throw Error ( 'Unable to find a free port' ) ;
199
181
}
200
182
201
183
0 commit comments