@@ -30,15 +30,12 @@ import {
30
30
SYMBOL_ITERATOR ,
31
31
TO_STRING_TAG ,
32
32
SC_PARAMS_TO_STRIP ,
33
- SC_READY_MESSAGE ,
34
- SC_CLOSE_MESSAGE ,
35
- SC_CLOSE_TIMEOUT ,
36
33
DEFAULT_SAUCE_CONNECT_VERSION ,
37
- SC_FAILURE_MESSAGES ,
38
34
SC_BOOLEAN_CLI_PARAMS ,
39
35
DEFAULT_RUNNER_NAME ,
40
36
} from './constants' ;
41
37
import SauceConnectLoader from './sauceConnectLoader' ;
38
+ import { SauceConnectManager } from './sauceConnectManager' ;
42
39
43
40
export default class SauceLabs {
44
41
constructor ( options ) {
@@ -259,6 +256,7 @@ export default class SauceLabs {
259
256
! [
260
257
'_' ,
261
258
'$0' ,
259
+ 'api-address' ,
262
260
'metadata' ,
263
261
'sc-version' ,
264
262
'sc-upstream-proxy' ,
@@ -294,14 +292,17 @@ export default class SauceLabs {
294
292
}
295
293
296
294
// Provide a default runner name. It's used for identifying the tunnel's initiation method.
297
- let metadata = argv . metadata || "" ;
298
- if ( ! metadata . includes ( " runner=" ) ) {
295
+ let metadata = argv . metadata || '' ;
296
+ if ( ! metadata . includes ( ' runner=' ) ) {
299
297
metadata = [ metadata , `runner=${ DEFAULT_RUNNER_NAME } ` ]
300
298
. filter ( Boolean )
301
- . join ( ',' )
299
+ . join ( ',' ) ;
302
300
}
303
301
args . push ( `--metadata=${ metadata } ` ) ;
304
302
303
+ const apiAddress = argv . apiAddress || ':8032' ;
304
+ args . push ( `--api-address=${ apiAddress } ` ) ;
305
+
305
306
const region = argv . region || this . region ;
306
307
if ( region ) {
307
308
const scRegion = getRegionSubDomain ( { region} ) ;
@@ -338,60 +339,20 @@ export default class SauceLabs {
338
339
args . unshift ( 'run' ) ;
339
340
}
340
341
342
+ const logger = fromCLI
343
+ ? process . stdout . write . bind ( process . stdout )
344
+ : argv . logger ;
341
345
const cp = spawn ( scLoader . path , args ) ;
342
- return new Promise ( ( resolve , reject ) => {
343
- const close = ( ) =>
344
- new Promise ( ( resolveClose ) => {
345
- process . kill ( cp . pid , 'SIGINT' ) ;
346
- const timeout = setTimeout ( resolveClose , SC_CLOSE_TIMEOUT ) ;
347
- cp . stdout . on ( 'data' , ( data ) => {
348
- const output = data . toString ( ) ;
349
- if ( output . includes ( SC_CLOSE_MESSAGE ) ) {
350
- clearTimeout ( timeout ) ;
351
- return resolveClose ( returnObj ) ;
352
- }
353
- } ) ;
354
- } ) ;
355
- const returnObj = { cp, close} ;
356
-
357
- cp . stderr . on ( 'data' , ( data ) => {
358
- const output = data . toString ( ) ;
359
- return reject ( new Error ( output ) ) ;
360
- } ) ;
361
- cp . stdout . on ( 'data' , ( data ) => {
362
- const logger = fromCLI
363
- ? process . stdout . write . bind ( process . stdout )
364
- : argv . logger ;
365
- const output = data . toString ( ) ;
366
- /**
367
- * print to stdout if called via CLI
368
- */
369
- if ( typeof logger === 'function' ) {
370
- logger ( output ) ;
371
- }
372
-
373
- /**
374
- * fail if SauceConnect could not establish a connection
375
- */
376
- if (
377
- SC_FAILURE_MESSAGES . find ( ( msg ) =>
378
- escape ( output ) . includes ( escape ( msg ) )
379
- )
380
- ) {
381
- return reject ( new Error ( output ) ) ;
382
- }
346
+ const manager = new SauceConnectManager ( cp , logger ) ;
347
+ process . on ( 'SIGINT' , manager . close ) ;
383
348
384
- /**
385
- * continue if connection was established
386
- */
387
- if ( output . includes ( SC_READY_MESSAGE ) ) {
388
- return resolve ( returnObj ) ;
389
- }
390
- } ) ;
391
-
392
- process . on ( 'SIGINT' , close ) ;
393
- return returnObj ;
394
- } ) ;
349
+ try {
350
+ await manager . waitForReady ( apiAddress ) ;
351
+ return { cp, close : manager . close } ;
352
+ } catch ( err ) {
353
+ await manager . close ( ) ;
354
+ throw err ;
355
+ }
395
356
}
396
357
397
358
/**
0 commit comments