@@ -30,15 +30,12 @@ import {
3030 SYMBOL_ITERATOR ,
3131 TO_STRING_TAG ,
3232 SC_PARAMS_TO_STRIP ,
33- SC_READY_MESSAGE ,
34- SC_CLOSE_MESSAGE ,
35- SC_CLOSE_TIMEOUT ,
3633 DEFAULT_SAUCE_CONNECT_VERSION ,
37- SC_FAILURE_MESSAGES ,
3834 SC_BOOLEAN_CLI_PARAMS ,
3935 DEFAULT_RUNNER_NAME ,
4036} from './constants' ;
4137import SauceConnectLoader from './sauceConnectLoader' ;
38+ import { SauceConnectManager } from './sauceConnectManager' ;
4239
4340export default class SauceLabs {
4441 constructor ( options ) {
@@ -259,6 +256,7 @@ export default class SauceLabs {
259256 ! [
260257 '_' ,
261258 '$0' ,
259+ 'api-address' ,
262260 'metadata' ,
263261 'sc-version' ,
264262 'sc-upstream-proxy' ,
@@ -294,14 +292,17 @@ export default class SauceLabs {
294292 }
295293
296294 // 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=' ) ) {
299297 metadata = [ metadata , `runner=${ DEFAULT_RUNNER_NAME } ` ]
300298 . filter ( Boolean )
301- . join ( ',' )
299+ . join ( ',' ) ;
302300 }
303301 args . push ( `--metadata=${ metadata } ` ) ;
304302
303+ const apiAddress = argv . apiAddress || ':8032' ;
304+ args . push ( `--api-address=${ apiAddress } ` ) ;
305+
305306 const region = argv . region || this . region ;
306307 if ( region ) {
307308 const scRegion = getRegionSubDomain ( { region} ) ;
@@ -338,60 +339,20 @@ export default class SauceLabs {
338339 args . unshift ( 'run' ) ;
339340 }
340341
342+ const logger = fromCLI
343+ ? process . stdout . write . bind ( process . stdout )
344+ : argv . logger ;
341345 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 ( ) ) ;
383348
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+ }
395356 }
396357
397358 /**
0 commit comments