From 27065e381218f93b9a4f88c14fc3c7947df7f44e Mon Sep 17 00:00:00 2001 From: Shirish Kamath Date: Mon, 7 Oct 2024 10:49:36 +0530 Subject: [PATCH 1/3] feat: add gzip support, send UA containing version --- lib/LocalBinary.js | 23 ++++++++++++++++++++--- lib/download.js | 18 +++++++++++++++--- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/lib/LocalBinary.js b/lib/LocalBinary.js index 9f7f672..3cd1cde 100644 --- a/lib/LocalBinary.js +++ b/lib/LocalBinary.js @@ -4,9 +4,13 @@ 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'; @@ -81,7 +85,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)){ @@ -122,12 +128,23 @@ 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)) { + 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); diff --git a/lib/download.js b/lib/download.js index 34f93a8..12c5ea2 100644 --- a/lib/download.js +++ b/lib/download.js @@ -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]; @@ -17,13 +18,24 @@ 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)) { + response.pipe(zlib.createGunzip()).pipe(fileStream); + } else { + response.pipe(fileStream); + } + response.on('error', function(err) { console.error('Got Error in binary download response', err); }); From 312c566c28bb598bbdd1f49a774feb4d6222b1b3 Mon Sep 17 00:00:00 2001 From: Shirish Kamath Date: Mon, 7 Oct 2024 10:52:21 +0530 Subject: [PATCH 2/3] chore: apply sourceURL override, log line for debugging --- lib/LocalBinary.js | 19 ++++++++++++++----- lib/download.js | 4 ++++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/lib/LocalBinary.js b/lib/LocalBinary.js index 3cd1cde..403edee 100644 --- a/lib/LocalBinary.js +++ b/lib/LocalBinary.js @@ -16,19 +16,24 @@ function LocalBinary(){ this.is64bits = process.arch == 'x64'; this.getDownloadPath = function () { + let sourceURL = 'https://www.browserstack.com/local-testing/downloads/binaries/'; + if (process.env.BROWSERSTACK_LOCAL_BIN_URL) { + sourceURL = process.env.BROWSERSTACK_LOCAL_BIN_URL; + } + 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'; } } }; @@ -140,6 +145,10 @@ function LocalBinary(){ https.get(options, function (response) { 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); diff --git a/lib/download.js b/lib/download.js index 12c5ea2..27207a3 100644 --- a/lib/download.js +++ b/lib/download.js @@ -31,6 +31,10 @@ options.headers = Object.assign({}, options.headers, { https.get(options, function (response) { 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); From a3a4f344e77a619f5ac62f79ac477daf94c63a31 Mon Sep 17 00:00:00 2001 From: Kamalpreet Kaur Date: Fri, 20 Dec 2024 17:21:22 +0530 Subject: [PATCH 3/3] update: remove URL flag --- lib/LocalBinary.js | 5 +---- lib/download.js | 2 +- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/LocalBinary.js b/lib/LocalBinary.js index 403edee..073bf61 100644 --- a/lib/LocalBinary.js +++ b/lib/LocalBinary.js @@ -17,9 +17,6 @@ function LocalBinary(){ this.getDownloadPath = function () { let sourceURL = 'https://www.browserstack.com/local-testing/downloads/binaries/'; - if (process.env.BROWSERSTACK_LOCAL_BIN_URL) { - sourceURL = process.env.BROWSERSTACK_LOCAL_BIN_URL; - } if(this.hostOS.match(/darwin|mac os/i)){ return sourceURL + 'BrowserStackLocal-darwin-x64'; @@ -146,7 +143,7 @@ function LocalBinary(){ 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']); + console.info('Using gzip in ' + options.headers['user-agent']); } response.pipe(zlib.createGunzip()).pipe(fileStream); diff --git a/lib/download.js b/lib/download.js index 27207a3..0b0e094 100644 --- a/lib/download.js +++ b/lib/download.js @@ -32,7 +32,7 @@ https.get(options, function (response) { 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']); + console.info('Using gzip in ' + options.headers['user-agent']); } response.pipe(zlib.createGunzip()).pipe(fileStream);