Skip to content

Commit

Permalink
Merge pull request #155 from shirish87/pr-gzip
Browse files Browse the repository at this point in the history
feat: support for gzip download
  • Loading branch information
yashdsaraf authored Dec 20, 2024
2 parents 39b63ab + a3a4f34 commit 4d37ef5
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 11 deletions.
39 changes: 31 additions & 8 deletions lib/LocalBinary.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,33 @@ var https = require('https'),
path = require('path'),
os = require('os'),
childProcess = require('child_process'),
zlib = require('zlib'),
HttpsProxyAgent = require('https-proxy-agent'),
version = require('../package.json').version,
LocalError = require('./LocalError');

const packageName = 'browserstack-local-nodejs';

function LocalBinary(){
this.hostOS = process.platform;
this.is64bits = process.arch == 'x64';

this.getDownloadPath = function () {
let sourceURL = 'https://www.browserstack.com/local-testing/downloads/binaries/';

if(this.hostOS.match(/darwin|mac os/i)){
return 'https://www.browserstack.com/local-testing/downloads/binaries/BrowserStackLocal-darwin-x64';
return sourceURL + 'BrowserStackLocal-darwin-x64';
} else if(this.hostOS.match(/mswin|msys|mingw|cygwin|bccwin|wince|emc|win32/i)) {
this.windows = true;
return 'https://www.browserstack.com/local-testing/downloads/binaries/BrowserStackLocal.exe';
return sourceURL + 'BrowserStackLocal.exe';
} else {
if(this.is64bits) {
if(this.isAlpine())
return 'https://www.browserstack.com/local-testing/downloads/binaries/BrowserStackLocal-alpine';
return sourceURL + 'BrowserStackLocal-alpine';
else
return 'https://www.browserstack.com/local-testing/downloads/binaries/BrowserStackLocal-linux-x64';
return sourceURL + 'BrowserStackLocal-linux-x64';
} else {
return 'https://www.browserstack.com/local-testing/downloads/binaries/BrowserStackLocal-linux-ia32';
return sourceURL + 'BrowserStackLocal-linux-ia32';
}
}
};
Expand Down Expand Up @@ -81,7 +87,9 @@ function LocalBinary(){
}

try{
const obj = childProcess.spawnSync(cmd, opts);
const userAgent = [packageName, version].join('/');
const env = Object.assign({ 'USER_AGENT': userAgent }, process.env);
const obj = childProcess.spawnSync(cmd, opts, { env: env });
let output;
if(obj.stdout.length > 0) {
if(fs.existsSync(binaryPath)){
Expand Down Expand Up @@ -122,12 +130,27 @@ function LocalBinary(){
try {
options.ca = fs.readFileSync(conf.useCaCertificate);
} catch(err) {
console.log("failed to read cert file", err)
console.log('failed to read cert file', err);
}
}

options.headers = Object.assign({}, options.headers, {
'accept-encoding': 'gzip, *',
'user-agent': [packageName, version].join('/'),
});

https.get(options, function (response) {
response.pipe(fileStream);
const contentEncoding = response.headers['content-encoding'];
if (typeof contentEncoding === 'string' && contentEncoding.match(/gzip/i)) {
if (process.env.BROWSERSTACK_LOCAL_DEBUG_GZIP) {
console.info('Using gzip in ' + options.headers['user-agent']);
}

response.pipe(zlib.createGunzip()).pipe(fileStream);
} else {
response.pipe(fileStream);
}

response.on('error', function(err) {
console.error('Got Error in binary download response', err);
that.retryBinaryDownload(conf, destParentDir, callback, retries, binaryPath);
Expand Down
22 changes: 19 additions & 3 deletions lib/download.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const https = require('https'),
fs = require('fs'),
HttpsProxyAgent = require('https-proxy-agent'),
url = require('url');
url = require('url'),
zlib = require('zlib');

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

Expand All @@ -17,13 +18,28 @@ if(proxyHost && proxyPort) {
try {
options.ca = fs.readFileSync(useCaCertificate);
} catch(err) {
console.log("failed to read cert file", err)
console.log('failed to read cert file', err);
}
}
}

options.headers = Object.assign({}, options.headers, {
'accept-encoding': 'gzip, *',
'user-agent': process.env.USER_AGENT,
});

https.get(options, function (response) {
response.pipe(fileStream);
const contentEncoding = response.headers['content-encoding'];
if (typeof contentEncoding === 'string' && contentEncoding.match(/gzip/i)) {
if (process.env.BROWSERSTACK_LOCAL_DEBUG_GZIP) {
console.info('Using gzip in ' + options.headers['user-agent']);
}

response.pipe(zlib.createGunzip()).pipe(fileStream);
} else {
response.pipe(fileStream);
}

response.on('error', function(err) {
console.error('Got Error in binary download response', err);
});
Expand Down

0 comments on commit 4d37ef5

Please sign in to comment.