@@ -22,7 +22,23 @@ function LocalBinary(){
2222 this . httpPath = 'https://s3.amazonaws.com/browserStack/browserstack-local/BrowserStackLocal-linux-ia32' ;
2323 }
2424
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 ;
2642 if ( ! this . checkPath ( destParentDir ) )
2743 fs . mkdirSync ( destParentDir ) ;
2844
@@ -31,7 +47,7 @@ function LocalBinary(){
3147 var fileStream = fs . createWriteStream ( binaryPath ) ;
3248
3349 var options = url . parse ( this . httpPath ) ;
34- if ( conf . proxyHost && conf . proxyPort ) {
50+ if ( conf . proxyHost && conf . proxyPort ) {
3551 options . agent = new HttpsProxyAgent ( {
3652 host : conf . proxyHost ,
3753 port : conf . proxyPort
@@ -40,14 +56,22 @@ function LocalBinary(){
4056
4157 https . get ( options , function ( response ) {
4258 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+ } ) ;
4363 fileStream . on ( 'error' , function ( err ) {
4464 console . error ( 'Got Error while downloading binary file' , err ) ;
65+ that . retryBinaryDownload ( conf , destParentDir , callback , retries , binaryPath ) ;
4566 } ) ;
4667 fileStream . on ( 'close' , function ( ) {
4768 fs . chmod ( binaryPath , '0755' , function ( ) {
4869 callback ( binaryPath ) ;
4970 } ) ;
5071 } ) ;
72+ } ) . on ( 'error' , function ( err ) {
73+ console . error ( 'Got Error in binary downloading request' , err ) ;
74+ that . retryBinaryDownload ( conf , destParentDir , callback , retries , binaryPath ) ;
5175 } ) ;
5276 } ;
5377
@@ -58,7 +82,7 @@ function LocalBinary(){
5882 if ( this . checkPath ( binaryPath , fs . X_OK ) ) {
5983 callback ( binaryPath ) ;
6084 } else {
61- this . download ( conf , destParentDir , callback ) ;
85+ this . download ( conf , destParentDir , callback , 5 ) ;
6286 }
6387 } ;
6488
0 commit comments