@@ -4,9 +4,13 @@ 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' ;
@@ -81,7 +85,9 @@ function LocalBinary(){
8185 }
8286
8387 try {
84- const obj = childProcess . spawnSync ( cmd , opts ) ;
88+ const userAgent = [ packageName , version ] . join ( '/' ) ;
89+ const env = Object . assign ( { 'USER_AGENT' : userAgent } , process . env ) ;
90+ const obj = childProcess . spawnSync ( cmd , opts , { env : env } ) ;
8591 let output ;
8692 if ( obj . stdout . length > 0 ) {
8793 if ( fs . existsSync ( binaryPath ) ) {
@@ -122,12 +128,23 @@ function LocalBinary(){
122128 try {
123129 options . ca = fs . readFileSync ( conf . useCaCertificate ) ;
124130 } catch ( err ) {
125- console . log ( " failed to read cert file" , err )
131+ console . log ( ' failed to read cert file' , err ) ;
126132 }
127133 }
128134
135+ options . headers = Object . assign ( { } , options . headers , {
136+ 'accept-encoding' : 'gzip, *' ,
137+ 'user-agent' : [ packageName , version ] . join ( '/' ) ,
138+ } ) ;
139+
129140 https . get ( options , function ( response ) {
130- response . pipe ( fileStream ) ;
141+ const contentEncoding = response . headers [ 'content-encoding' ] ;
142+ if ( typeof contentEncoding === 'string' && contentEncoding . match ( / g z i p / i) ) {
143+ response . pipe ( zlib . createGunzip ( ) ) . pipe ( fileStream ) ;
144+ } else {
145+ response . pipe ( fileStream ) ;
146+ }
147+
131148 response . on ( 'error' , function ( err ) {
132149 console . error ( 'Got Error in binary download response' , err ) ;
133150 that . retryBinaryDownload ( conf , destParentDir , callback , retries , binaryPath ) ;
0 commit comments