@@ -4,27 +4,33 @@ var https = require('https'),
4
4
path = require ( 'path' ) ,
5
5
os = require ( 'os' ) ,
6
6
childProcess = require ( 'child_process' ) ,
7
+ zlib = require ( 'zlib' ) ,
7
8
HttpsProxyAgent = require ( 'https-proxy-agent' ) ,
9
+ version = require ( '../package.json' ) . version ,
8
10
LocalError = require ( './LocalError' ) ;
9
11
12
+ const packageName = 'browserstack-local-nodejs' ;
13
+
10
14
function LocalBinary ( ) {
11
15
this . hostOS = process . platform ;
12
16
this . is64bits = process . arch == 'x64' ;
13
17
14
18
this . getDownloadPath = function ( ) {
19
+ let sourceURL = 'https://www.browserstack.com/local-testing/downloads/binaries/' ;
20
+
15
21
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';
17
23
} 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) ) {
18
24
this . windows = true ;
19
- return 'https://www.browserstack.com/local-testing/downloads/binaries/ BrowserStackLocal.exe';
25
+ return sourceURL + ' BrowserStackLocal.exe';
20
26
} else {
21
27
if ( this . is64bits ) {
22
28
if ( this . isAlpine ( ) )
23
- return 'https://www.browserstack.com/local-testing/downloads/binaries/ BrowserStackLocal-alpine';
29
+ return sourceURL + ' BrowserStackLocal-alpine';
24
30
else
25
- return 'https://www.browserstack.com/local-testing/downloads/binaries/ BrowserStackLocal-linux-x64';
31
+ return sourceURL + ' BrowserStackLocal-linux-x64';
26
32
} else {
27
- return 'https://www.browserstack.com/local-testing/downloads/binaries/ BrowserStackLocal-linux-ia32';
33
+ return sourceURL + ' BrowserStackLocal-linux-ia32';
28
34
}
29
35
}
30
36
} ;
@@ -81,7 +87,9 @@ function LocalBinary(){
81
87
}
82
88
83
89
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 } ) ;
85
93
let output ;
86
94
if ( obj . stdout . length > 0 ) {
87
95
if ( fs . existsSync ( binaryPath ) ) {
@@ -122,12 +130,27 @@ function LocalBinary(){
122
130
try {
123
131
options . ca = fs . readFileSync ( conf . useCaCertificate ) ;
124
132
} catch ( err ) {
125
- console . log ( " failed to read cert file" , err )
133
+ console . log ( ' failed to read cert file' , err ) ;
126
134
}
127
135
}
128
136
137
+ options . headers = Object . assign ( { } , options . headers , {
138
+ 'accept-encoding' : 'gzip, *' ,
139
+ 'user-agent' : [ packageName , version ] . join ( '/' ) ,
140
+ } ) ;
141
+
129
142
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
+
131
154
response . on ( 'error' , function ( err ) {
132
155
console . error ( 'Got Error in binary download response' , err ) ;
133
156
that . retryBinaryDownload ( conf , destParentDir , callback , retries , binaryPath ) ;
0 commit comments