@@ -14,17 +14,31 @@ const packageName = 'browserstack-local-nodejs';
1414function LocalBinary ( ) {
1515 this . hostOS = process . platform ;
1616 this . is64bits = process . arch == 'x64' ;
17- this . baseRetries = 10 ;
17+ this . baseRetries = 9 ;
1818 this . sourceURL = null ;
1919 this . downloadErrorMessage = null ;
2020
2121 this . getSourceUrl = function ( conf , retries ) {
2222 /* Request for an endpoint from Rails no more than twice with 5 retries each */
23- if ( ! [ 5 , 10 ] . includes ( retries ) && this . sourceURL ) return this . sourceURL ;
23+ if ( ! [ 4 , 9 ] . includes ( retries ) && this . sourceURL != null ) {
24+ return this . sourceURL ;
25+ }
26+
27+ if ( process . env . BINARY_DOWNLOAD_SOURCE_URL !== undefined && process . env . BINARY_DOWNLOAD_FALLBACK_ENABLED == "true" && this . parentRetries != 4 ) {
28+ /* This is triggered from Local.js if there's an error executing the downloaded binary */
29+ return process . env . BINARY_DOWNLOAD_SOURCE_URL ;
30+ }
2431
2532 let cmd , opts ;
2633 cmd = 'node' ;
2734 opts = [ path . join ( __dirname , 'fetchDownloadSourceUrl.js' ) , this . key ] ;
35+
36+ if ( retries == 4 || ( process . env . BINARY_DOWNLOAD_FALLBACK_ENABLED == "true" && this . parentRetries == 4 ) ) {
37+ opts . push ( true , this . downloadErrorMessage || process . env . BINARY_DOWNLOAD_ERROR_MESSAGE ) ;
38+ } else {
39+ opts . push ( false , null ) ;
40+ }
41+
2842 if ( conf . proxyHost && conf . proxyPort ) {
2943 opts . push ( conf . proxyHost , conf . proxyPort ) ;
3044 if ( conf . useCaCertificate ) {
@@ -33,16 +47,13 @@ function LocalBinary(){
3347 } else if ( conf . useCaCertificate ) {
3448 opts . push ( undefined , undefined , conf . useCaCertificate ) ;
3549 }
36-
37- if ( retries == 5 ) {
38- opts . push ( true , this . binaryDownloadError ) ;
39- }
4050
4151 const userAgent = [ packageName , version ] . join ( '/' ) ;
4252 const env = Object . assign ( { 'USER_AGENT' : userAgent } , process . env ) ;
4353 const obj = childProcess . spawnSync ( cmd , opts , { env : env } ) ;
4454 if ( obj . stdout . length > 0 ) {
45- this . sourceURL = obj . stdout ;
55+ this . sourceURL = obj . stdout . toString ( ) . replace ( / \n + $ / , '' ) ;
56+ process . env . BINARY_DOWNLOAD_SOURCE_URL = this . sourceURL ;
4657 return this . sourceURL ;
4758 } else if ( obj . stderr . length > 0 ) {
4859 let output = Buffer . from ( JSON . parse ( JSON . stringify ( obj . stderr ) ) . data ) . toString ( ) ;
@@ -78,9 +89,9 @@ function LocalBinary(){
7889 }
7990 } ;
8091
81- this . binaryDownloadError = function ( errorMessagePrefix , errorObject ) {
82- console . error ( errorMessagePrefix , errorObject ) ;
83- this . downloadErrorMessage = errorMessagePrefix + " : " + errorObject ? errorObject . getMessage ( ) : "null" ;
92+ this . binaryDownloadError = function ( errorMessagePrefix , errorMessage ) {
93+ console . error ( errorMessagePrefix , errorMessage ) ;
94+ this . downloadErrorMessage = errorMessagePrefix + " : " + errorMessage ;
8495 }
8596
8697 this . retryBinaryDownload = function ( conf , destParentDir , callback , retries , binaryPath ) {
@@ -147,7 +158,7 @@ function LocalBinary(){
147158 return that . retryBinaryDownload ( conf , destParentDir , null , retries , binaryPath ) ;
148159 }
149160 } catch ( err ) {
150- that . binaryDownloadError ( 'Download failed with error' , err ) ;
161+ that . binaryDownloadError ( 'Download failed with error' , err . getMessage ( ) ) ;
151162 return that . retryBinaryDownload ( conf , destParentDir , null , retries , binaryPath ) ;
152163 }
153164 } ;
@@ -200,11 +211,11 @@ function LocalBinary(){
200211 }
201212
202213 response . on ( 'error' , function ( err ) {
203- that . binaryDownloadError ( 'Got Error in binary download response' , err ) ;
214+ that . binaryDownloadError ( 'Got Error in binary download response' , err . message ) ;
204215 that . retryBinaryDownload ( conf , destParentDir , callback , retries , binaryPath ) ;
205216 } ) ;
206217 fileStream . on ( 'error' , function ( err ) {
207- that . binaryDownloadError ( 'Got Error while downloading binary file' , err ) ;
218+ that . binaryDownloadError ( 'Got Error while downloading binary file' , err . message ) ;
208219 that . retryBinaryDownload ( conf , destParentDir , callback , retries , binaryPath ) ;
209220 } ) ;
210221 fileStream . on ( 'close' , function ( ) {
@@ -213,13 +224,14 @@ function LocalBinary(){
213224 } ) ;
214225 } ) ;
215226 } ) . on ( 'error' , function ( err ) {
216- that . binaryDownloadError ( 'Got Error in binary downloading request' , err ) ;
227+ that . binaryDownloadError ( 'Got Error in binary downloading request' , err . message ) ;
217228 that . retryBinaryDownload ( conf , destParentDir , callback , retries , binaryPath ) ;
218229 } ) ;
219230 } ;
220231
221- this . binaryPath = function ( conf , key , callback ) {
232+ this . binaryPath = function ( conf , key , parentRetries , callback ) {
222233 this . key = key ;
234+ this . parentRetries = parentRetries ;
223235 var destParentDir = this . getAvailableDirs ( ) ;
224236 var destBinaryName = ( this . windows ) ? 'BrowserStackLocal.exe' : 'BrowserStackLocal' ;
225237 var binaryPath = path . join ( destParentDir , destBinaryName ) ;
0 commit comments