@@ -14,17 +14,31 @@ const packageName = 'browserstack-local-nodejs';
14
14
function LocalBinary ( ) {
15
15
this . hostOS = process . platform ;
16
16
this . is64bits = process . arch == 'x64' ;
17
- this . baseRetries = 10 ;
17
+ this . baseRetries = 9 ;
18
18
this . sourceURL = null ;
19
19
this . downloadErrorMessage = null ;
20
20
21
21
this . getSourceUrl = function ( conf , retries ) {
22
22
/* 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
+ }
24
31
25
32
let cmd , opts ;
26
33
cmd = 'node' ;
27
34
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
+
28
42
if ( conf . proxyHost && conf . proxyPort ) {
29
43
opts . push ( conf . proxyHost , conf . proxyPort ) ;
30
44
if ( conf . useCaCertificate ) {
@@ -33,16 +47,13 @@ function LocalBinary(){
33
47
} else if ( conf . useCaCertificate ) {
34
48
opts . push ( undefined , undefined , conf . useCaCertificate ) ;
35
49
}
36
-
37
- if ( retries == 5 ) {
38
- opts . push ( true , this . binaryDownloadError ) ;
39
- }
40
50
41
51
const userAgent = [ packageName , version ] . join ( '/' ) ;
42
52
const env = Object . assign ( { 'USER_AGENT' : userAgent } , process . env ) ;
43
53
const obj = childProcess . spawnSync ( cmd , opts , { env : env } ) ;
44
54
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 ;
46
57
return this . sourceURL ;
47
58
} else if ( obj . stderr . length > 0 ) {
48
59
let output = Buffer . from ( JSON . parse ( JSON . stringify ( obj . stderr ) ) . data ) . toString ( ) ;
@@ -78,9 +89,9 @@ function LocalBinary(){
78
89
}
79
90
} ;
80
91
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 ;
84
95
}
85
96
86
97
this . retryBinaryDownload = function ( conf , destParentDir , callback , retries , binaryPath ) {
@@ -147,7 +158,7 @@ function LocalBinary(){
147
158
return that . retryBinaryDownload ( conf , destParentDir , null , retries , binaryPath ) ;
148
159
}
149
160
} catch ( err ) {
150
- that . binaryDownloadError ( 'Download failed with error' , err ) ;
161
+ that . binaryDownloadError ( 'Download failed with error' , err . getMessage ( ) ) ;
151
162
return that . retryBinaryDownload ( conf , destParentDir , null , retries , binaryPath ) ;
152
163
}
153
164
} ;
@@ -200,11 +211,11 @@ function LocalBinary(){
200
211
}
201
212
202
213
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 ) ;
204
215
that . retryBinaryDownload ( conf , destParentDir , callback , retries , binaryPath ) ;
205
216
} ) ;
206
217
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 ) ;
208
219
that . retryBinaryDownload ( conf , destParentDir , callback , retries , binaryPath ) ;
209
220
} ) ;
210
221
fileStream . on ( 'close' , function ( ) {
@@ -213,13 +224,14 @@ function LocalBinary(){
213
224
} ) ;
214
225
} ) ;
215
226
} ) . 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 ) ;
217
228
that . retryBinaryDownload ( conf , destParentDir , callback , retries , binaryPath ) ;
218
229
} ) ;
219
230
} ;
220
231
221
- this . binaryPath = function ( conf , key , callback ) {
232
+ this . binaryPath = function ( conf , key , parentRetries , callback ) {
222
233
this . key = key ;
234
+ this . parentRetries = parentRetries ;
223
235
var destParentDir = this . getAvailableDirs ( ) ;
224
236
var destBinaryName = ( this . windows ) ? 'BrowserStackLocal.exe' : 'BrowserStackLocal' ;
225
237
var binaryPath = path . join ( destParentDir , destBinaryName ) ;
0 commit comments