@@ -19,8 +19,8 @@ import tcpPortUsed from 'tcp-port-used';
1919
2020const SERVER_LAUNCH_TIMEOUT = 30 ; // 30 seconds
2121const SERVER_AUTOSHUTDOWN_TIMEOUT = 3600 ; // 1 hour
22- const HTTP_PORT_BEGIN = 8010 ;
23- const HTTP_PORT_END = 8050 ;
22+ const HTTP_PORT_MIN = 45000 ;
23+ const HTTP_PORT_MAX = 45999 ;
2424const SESSION_ID = crypto
2525 . createHash ( 'sha1' )
2626 . update ( crypto . randomBytes ( 512 ) )
@@ -116,7 +116,7 @@ async function listenIDECommands(callback) {
116116 } ) ;
117117}
118118
119- async function isPortUsed ( host , port ) {
119+ async function isPortUsed ( port , host ) {
120120 return new Promise ( ( resolve ) => {
121121 tcpPortUsed . check ( port , host ) . then (
122122 ( result ) => {
@@ -130,18 +130,21 @@ async function isPortUsed(host, port) {
130130}
131131
132132async function findFreePort ( ) {
133- let port = HTTP_PORT_BEGIN ;
134- while ( port < HTTP_PORT_END ) {
135- if ( ! ( await isPortUsed ( _HTTP_HOST , port ) ) ) {
133+ let attemptNums = 0 ;
134+ while ( attemptNums < 13 ) {
135+ const port = Math . floor (
136+ Math . random ( ) * ( HTTP_PORT_MAX - HTTP_PORT_MIN ) + HTTP_PORT_MIN
137+ ) ;
138+ if ( ! ( await isPortUsed ( port , _HTTP_HOST ) ) ) {
136139 return port ;
137140 }
138- port ++ ;
141+ attemptNums ++ ;
139142 }
140143 return 0 ;
141144}
142145
143146export async function isServerStarted ( ) {
144- if ( ! ( await isPortUsed ( _HTTP_HOST , _HTTP_PORT ) ) ) {
147+ if ( ! ( await isPortUsed ( _HTTP_PORT , _HTTP_HOST ) ) ) {
145148 return false ;
146149 }
147150 return ! ! ( await getFrontendVersion ( ) ) ;
@@ -155,11 +158,9 @@ export async function ensureServerStarted(options = {}) {
155158 try {
156159 return await _ensureServerStarted ( options ) ;
157160 } catch ( err ) {
161+ _HTTP_PORT = 0 ;
158162 lastError = err ;
159163 console . warn ( err ) ;
160- _HTTP_PORT = 0 ;
161- // stop all PIO Home servers
162- await shutdownAllServers ( ) ;
163164 }
164165 attemptNums ++ ;
165166 }
@@ -176,6 +177,11 @@ async function _ensureServerStarted(options = {}) {
176177 }
177178 if ( ! ( await isServerStarted ( ) ) ) {
178179 await new Promise ( ( resolve , reject ) => {
180+ const timeoutID = setTimeout (
181+ ( ) => reject ( new Error ( 'Could not start PIO Home server: Timeout error' ) ) ,
182+ SERVER_LAUNCH_TIMEOUT * 1000
183+ ) ;
184+ let output = '' ;
179185 runPIOCommand (
180186 [
181187 'home' ,
@@ -194,38 +200,43 @@ async function _ensureServerStarted(options = {}) {
194200 _HTTP_PORT = 0 ;
195201 return reject ( new Error ( stderr ) ) ;
196202 }
203+ } ,
204+ {
205+ onProcStdout : ( data ) => {
206+ output += data . toString ( ) ;
207+ if ( output . includes ( 'PIO Home has been started' ) ) {
208+ clearTimeout ( timeoutID ) ;
209+ resolve ( true ) ;
210+ }
211+ } ,
197212 }
198213 ) ;
199- tcpPortUsed
200- . waitUntilUsedOnHost ( _HTTP_PORT , _HTTP_HOST , 500 , SERVER_LAUNCH_TIMEOUT * 1000 )
201- . then (
202- ( ) => {
203- resolve ( true ) ;
204- } ,
205- ( err ) => {
206- reject ( new Error ( 'Could not start PIO Home server: ' + err . toString ( ) ) ) ;
207- }
208- ) ;
209214 } ) ;
210215 }
211216 if ( options . onIDECommand ) {
212217 listenIDECommands ( options . onIDECommand ) ;
213218 }
214- return true ;
219+ return {
220+ host : _HTTP_HOST ,
221+ port : _HTTP_PORT ,
222+ sessionId : SESSION_ID ,
223+ } ;
215224}
216225
217226export async function shutdownServer ( ) {
218227 if ( ! _HTTP_PORT ) {
219228 return ;
220229 }
221- return await got . post ( constructServerUrl ( { path : '/__shutdown__' } ) , {
222- timeout : 1000 ,
223- } ) ;
230+ try {
231+ await got . post ( constructServerUrl ( { path : '/__shutdown__' } ) , {
232+ timeout : 1000 ,
233+ } ) ;
234+ } catch ( err ) { }
224235}
225236
226237export async function shutdownAllServers ( ) {
227- let port = HTTP_PORT_BEGIN ;
228- while ( port < HTTP_PORT_END ) {
238+ let port = HTTP_PORT_MIN ;
239+ while ( port < HTTP_PORT_MAX ) {
229240 try {
230241 got (
231242 constructServerUrl ( { port, includeSID : false , query : { __shutdown__ : '1' } } ) ,
0 commit comments