@@ -39,6 +39,22 @@ function LocalBinary(){
3939
4040 this . httpPath = this . getDownloadPath ( ) ;
4141
42+ this . retryBinaryDownloadSync = function ( conf , destParentDir , retries , binaryPath ) {
43+ var that = this ;
44+ if ( retries > 0 ) {
45+ console . log ( 'Retrying Download. Retries left' , retries ) ;
46+ try {
47+ const stats = fs . statSync ( binaryPath )
48+ fs . unlinkSync ( binaryPath ) ;
49+ } catch ( er ) { }
50+
51+ that . downloadSync ( conf , destParentDir , retries - 1 )
52+
53+ } else {
54+ console . error ( 'Number of retries to download exceeded.' ) ;
55+ }
56+ }
57+
4258 this . retryBinaryDownload = function ( conf , destParentDir , callback , retries , binaryPath ) {
4359 var that = this ;
4460 if ( retries > 0 ) {
@@ -54,6 +70,56 @@ function LocalBinary(){
5470 }
5571 } ;
5672
73+ this . downloadSync = function ( conf , destParentDir , retries ) {
74+ console . log ( 'downloading in sync' )
75+ var that = this ;
76+ if ( ! this . checkPath ( destParentDir ) )
77+ fs . mkdirSync ( destParentDir ) ;
78+
79+ var destBinaryName = ( this . windows ) ? 'BrowserStackLocal.exe' : 'BrowserStackLocal' ;
80+ var binaryPath = path . join ( destParentDir , destBinaryName ) ;
81+
82+ var options = url . parse ( this . httpPath ) ;
83+ if ( conf . proxyHost && conf . proxyPort ) {
84+ options . agent = new HttpsProxyAgent ( {
85+ host : conf . proxyHost ,
86+ port : conf . proxyPort
87+ } ) ;
88+ }
89+
90+ const cmd = `curl` ;
91+ const opts = [ '-o' , binaryPath , options . href ] ;
92+
93+ if ( conf . proxyHost && conf . proxyPort ) {
94+ opts . push ( '--proxy' , `${ conf . proxyHost } :${ conf . proxyPort } ` ) ;
95+ }
96+
97+ try {
98+ const obj = childProcess . spawnSync ( cmd , opts ) ;
99+ let output ;
100+ if ( obj . stdout . length > 0 ) {
101+ output = Buffer . from ( JSON . parse ( JSON . stringify ( obj . stderr ) ) . data ) . toString ( ) ;
102+ } else if ( obj . stderr . length > 0 ) {
103+ output = Buffer . from ( JSON . parse ( JSON . stringify ( obj . stderr ) ) . data ) . toString ( ) ;
104+ }
105+ if ( output . includes ( 'failed' ) ) {
106+ console . log ( 'failed to download' ) ;
107+ that . retryBinaryDownloadSync ( conf , destParentDir , retries , binaryPath ) ;
108+ } else {
109+ if ( fs . existsSync ( binaryPath ) ) {
110+ fs . chmodSync ( binaryPath , '0755' )
111+ return binaryPath ;
112+ } else {
113+ console . log ( 'failed to download' ) ;
114+ that . retryBinaryDownloadSync ( conf , destParentDir , retries , binaryPath ) ;
115+ }
116+ }
117+ } catch ( err ) {
118+ console . log ( 'Download failed with error' , err ) ;
119+ that . retryBinaryDownloadSync ( conf , destParentDir , retries , binaryPath ) ;
120+ }
121+ }
122+
57123 this . download = function ( conf , destParentDir , callback , retries ) {
58124 var that = this ;
59125 if ( ! this . checkPath ( destParentDir ) )
@@ -92,6 +158,17 @@ function LocalBinary(){
92158 } ) ;
93159 } ;
94160
161+ this . binaryPathSync = function ( conf ) {
162+ var destParentDir = this . getAvailableDirs ( ) ;
163+ var destBinaryName = ( this . windows ) ? 'BrowserStackLocal.exe' : 'BrowserStackLocal' ;
164+ var binaryPath = path . join ( destParentDir , destBinaryName ) ;
165+ if ( this . checkPath ( binaryPath , fs . X_OK ) ) {
166+ return binaryPath ;
167+ } else {
168+ return this . downloadSync ( conf , destParentDir , 5 ) ;
169+ }
170+ }
171+
95172 this . binaryPath = function ( conf , callback ) {
96173 var destParentDir = this . getAvailableDirs ( ) ;
97174 var destBinaryName = ( this . windows ) ? 'BrowserStackLocal.exe' : 'BrowserStackLocal' ;
0 commit comments