@@ -4,27 +4,33 @@ var https = require('https'),
44 path = require ( 'path' ) ,
55 os = require ( 'os' ) ,
66 childProcess = require ( 'child_process' ) ,
7+ zlib = require ( 'zlib' ) ,
78 HttpsProxyAgent = require ( 'https-proxy-agent' ) ,
9+ version = require ( '../package.json' ) . version ,
810 LocalError = require ( './LocalError' ) ;
911
12+ const packageName = 'browserstack-local-nodejs' ;
13+
1014function LocalBinary ( ) {
1115 this . hostOS = process . platform ;
1216 this . is64bits = process . arch == 'x64' ;
1317
1418 this . getDownloadPath = function ( ) {
19+ let sourceURL = 'https://www.browserstack.com/local-testing/downloads/binaries/' ;
20+
1521 if ( this . hostOS . match ( / d a r w i n | m a c o s / i) ) {
16- return 'https://www.browserstack.com/local-testing/downloads/binaries/ BrowserStackLocal-darwin-x64';
22+ return sourceURL + ' BrowserStackLocal-darwin-x64';
1723 } else if ( this . hostOS . match ( / m s w i n | m s y s | m i n g w | c y g w i n | b c c w i n | w i n c e | e m c | w i n 3 2 / i) ) {
1824 this . windows = true ;
19- return 'https://www.browserstack.com/local-testing/downloads/binaries/ BrowserStackLocal.exe';
25+ return sourceURL + ' BrowserStackLocal.exe';
2026 } else {
2127 if ( this . is64bits ) {
2228 if ( this . isAlpine ( ) )
23- return 'https://www.browserstack.com/local-testing/downloads/binaries/ BrowserStackLocal-alpine';
29+ return sourceURL + ' BrowserStackLocal-alpine';
2430 else
25- return 'https://www.browserstack.com/local-testing/downloads/binaries/ BrowserStackLocal-linux-x64';
31+ return sourceURL + ' BrowserStackLocal-linux-x64';
2632 } else {
27- return 'https://www.browserstack.com/local-testing/downloads/binaries/ BrowserStackLocal-linux-ia32';
33+ return sourceURL + ' BrowserStackLocal-linux-ia32';
2834 }
2935 }
3036 } ;
@@ -81,7 +87,9 @@ function LocalBinary(){
8187 }
8288
8389 try {
84- const obj = childProcess . spawnSync ( cmd , opts ) ;
90+ const userAgent = [ packageName , version ] . join ( '/' ) ;
91+ const env = Object . assign ( { 'USER_AGENT' : userAgent } , process . env ) ;
92+ const obj = childProcess . spawnSync ( cmd , opts , { env : env } ) ;
8593 let output ;
8694 if ( obj . stdout . length > 0 ) {
8795 if ( fs . existsSync ( binaryPath ) ) {
@@ -122,12 +130,27 @@ function LocalBinary(){
122130 try {
123131 options . ca = fs . readFileSync ( conf . useCaCertificate ) ;
124132 } catch ( err ) {
125- console . log ( " failed to read cert file" , err )
133+ console . log ( ' failed to read cert file' , err ) ;
126134 }
127135 }
128136
137+ options . headers = Object . assign ( { } , options . headers , {
138+ 'accept-encoding' : 'gzip, *' ,
139+ 'user-agent' : [ packageName , version ] . join ( '/' ) ,
140+ } ) ;
141+
129142 https . get ( options , function ( response ) {
130- response . pipe ( fileStream ) ;
143+ const contentEncoding = response . headers [ 'content-encoding' ] ;
144+ if ( typeof contentEncoding === 'string' && contentEncoding . match ( / g z i p / i) ) {
145+ if ( process . env . BROWSERSTACK_LOCAL_DEBUG_GZIP ) {
146+ console . info ( 'Using gzip in ' + options . headers [ 'user-agent' ] ) ;
147+ }
148+
149+ response . pipe ( zlib . createGunzip ( ) ) . pipe ( fileStream ) ;
150+ } else {
151+ response . pipe ( fileStream ) ;
152+ }
153+
131154 response . on ( 'error' , function ( err ) {
132155 console . error ( 'Got Error in binary download response' , err ) ;
133156 that . retryBinaryDownload ( conf , destParentDir , callback , retries , binaryPath ) ;
0 commit comments