@@ -22,7 +22,23 @@ function LocalBinary(){
22
22
this . httpPath = 'https://s3.amazonaws.com/browserStack/browserstack-local/BrowserStackLocal-linux-ia32' ;
23
23
}
24
24
25
- this . download = function ( conf , destParentDir , callback ) {
25
+ this . retryBinaryDownload = function ( conf , destParentDir , callback , retries , binaryPath ) {
26
+ var that = this ;
27
+ if ( retries > 0 ) {
28
+ console . log ( 'Retrying Download. Retries left' , retries ) ;
29
+ fs . stat ( binaryPath , function ( err ) {
30
+ if ( err == null ) {
31
+ fs . unlinkSync ( binaryPath ) ;
32
+ }
33
+ that . download ( conf , destParentDir , callback , retries - 1 ) ;
34
+ } ) ;
35
+ } else {
36
+ console . error ( 'Number of retries to download exceeded.' ) ;
37
+ }
38
+ } ;
39
+
40
+ this . download = function ( conf , destParentDir , callback , retries ) {
41
+ var that = this ;
26
42
if ( ! this . checkPath ( destParentDir ) )
27
43
fs . mkdirSync ( destParentDir ) ;
28
44
@@ -31,7 +47,7 @@ function LocalBinary(){
31
47
var fileStream = fs . createWriteStream ( binaryPath ) ;
32
48
33
49
var options = url . parse ( this . httpPath ) ;
34
- if ( conf . proxyHost && conf . proxyPort ) {
50
+ if ( conf . proxyHost && conf . proxyPort ) {
35
51
options . agent = new HttpsProxyAgent ( {
36
52
host : conf . proxyHost ,
37
53
port : conf . proxyPort
@@ -40,14 +56,22 @@ function LocalBinary(){
40
56
41
57
https . get ( options , function ( response ) {
42
58
response . pipe ( fileStream ) ;
59
+ response . on ( 'error' , function ( err ) {
60
+ console . error ( 'Got Error in binary download response' , err ) ;
61
+ that . retryBinaryDownload ( conf , destParentDir , callback , retries , binaryPath ) ;
62
+ } ) ;
43
63
fileStream . on ( 'error' , function ( err ) {
44
64
console . error ( 'Got Error while downloading binary file' , err ) ;
65
+ that . retryBinaryDownload ( conf , destParentDir , callback , retries , binaryPath ) ;
45
66
} ) ;
46
67
fileStream . on ( 'close' , function ( ) {
47
68
fs . chmod ( binaryPath , '0755' , function ( ) {
48
69
callback ( binaryPath ) ;
49
70
} ) ;
50
71
} ) ;
72
+ } ) . on ( 'error' , function ( err ) {
73
+ console . error ( 'Got Error in binary downloading request' , err ) ;
74
+ that . retryBinaryDownload ( conf , destParentDir , callback , retries , binaryPath ) ;
51
75
} ) ;
52
76
} ;
53
77
@@ -58,7 +82,7 @@ function LocalBinary(){
58
82
if ( this . checkPath ( binaryPath , fs . X_OK ) ) {
59
83
callback ( binaryPath ) ;
60
84
} else {
61
- this . download ( conf , destParentDir , callback ) ;
85
+ this . download ( conf , destParentDir , callback , 5 ) ;
62
86
}
63
87
} ;
64
88
0 commit comments