Skip to content

Commit 4d37ef5

Browse files
authored
Merge pull request #155 from shirish87/pr-gzip
feat: support for gzip download
2 parents 39b63ab + a3a4f34 commit 4d37ef5

File tree

2 files changed

+50
-11
lines changed

2 files changed

+50
-11
lines changed

lib/LocalBinary.js

+31-8
Original file line numberDiff line numberDiff line change
@@ -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+
1014
function 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(/darwin|mac os/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(/mswin|msys|mingw|cygwin|bccwin|wince|emc|win32/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(/gzip/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);

lib/download.js

+19-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
const https = require('https'),
22
fs = require('fs'),
33
HttpsProxyAgent = require('https-proxy-agent'),
4-
url = require('url');
4+
url = require('url'),
5+
zlib = require('zlib');
56

67
const binaryPath = process.argv[2], httpPath = process.argv[3], proxyHost = process.argv[4], proxyPort = process.argv[5], useCaCertificate = process.argv[6];
78

@@ -17,13 +18,28 @@ if(proxyHost && proxyPort) {
1718
try {
1819
options.ca = fs.readFileSync(useCaCertificate);
1920
} catch(err) {
20-
console.log("failed to read cert file", err)
21+
console.log('failed to read cert file', err);
2122
}
2223
}
2324
}
2425

26+
options.headers = Object.assign({}, options.headers, {
27+
'accept-encoding': 'gzip, *',
28+
'user-agent': process.env.USER_AGENT,
29+
});
30+
2531
https.get(options, function (response) {
26-
response.pipe(fileStream);
32+
const contentEncoding = response.headers['content-encoding'];
33+
if (typeof contentEncoding === 'string' && contentEncoding.match(/gzip/i)) {
34+
if (process.env.BROWSERSTACK_LOCAL_DEBUG_GZIP) {
35+
console.info('Using gzip in ' + options.headers['user-agent']);
36+
}
37+
38+
response.pipe(zlib.createGunzip()).pipe(fileStream);
39+
} else {
40+
response.pipe(fileStream);
41+
}
42+
2743
response.on('error', function(err) {
2844
console.error('Got Error in binary download response', err);
2945
});

0 commit comments

Comments
 (0)