From 4c0de9ded45228b163573a8a1293576bbfc2afc3 Mon Sep 17 00:00:00 2001 From: amaanbs Date: Tue, 1 Jul 2025 16:01:52 +0530 Subject: [PATCH 1/2] Request download source from specified host --- lib/Local.js | 10 +++++----- lib/LocalBinary.js | 5 +++-- lib/fetchDownloadSourceUrl.js | 8 ++++---- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/lib/Local.js b/lib/Local.js index 63e901f..ad35fd4 100644 --- a/lib/Local.js +++ b/lib/Local.js @@ -35,7 +35,7 @@ function Local(){ if(typeof options['onlyCommand'] !== 'undefined') return; - const binaryPath = this.getBinaryPath(); + const binaryPath = this.getBinaryPath(null, options['bs-host']); that.binaryPath = binaryPath; childProcess.exec('echo "" > ' + that.logfile); that.opcode = 'start'; @@ -123,7 +123,7 @@ function Local(){ callback(); } }); - }); + }, options['bs-host']); }; this.isRunning = function(){ @@ -249,7 +249,7 @@ function Local(){ } }; - this.getBinaryPath = function(callback){ + this.getBinaryPath = function(callback, bsHost){ if(typeof(this.binaryPath) == 'undefined'){ this.binary = new LocalBinary(); var conf = {}; @@ -261,9 +261,9 @@ function Local(){ conf.useCaCertificate = this.useCaCertificate; } if(!callback) { - return this.binary.binaryPath(conf, this.key, this.retriesLeft); + return this.binary.binaryPath(conf, bsHost, this.key, this.retriesLeft); } - this.binary.binaryPath(conf, this.key, this.retriesLeft, callback); + this.binary.binaryPath(conf, bsHost, this.key, this.retriesLeft, callback); } else { console.log('BINARY PATH IS DEFINED'); if(!callback) { diff --git a/lib/LocalBinary.js b/lib/LocalBinary.js index 25564d3..ef06e69 100644 --- a/lib/LocalBinary.js +++ b/lib/LocalBinary.js @@ -32,7 +32,7 @@ function LocalBinary(){ let cmd, opts; cmd = 'node'; - opts = [path.join(__dirname, 'fetchDownloadSourceUrl.js'), this.key]; + opts = [path.join(__dirname, 'fetchDownloadSourceUrl.js'), this.key, this.bsHost]; if (retries == 4 || (process.env.BINARY_DOWNLOAD_FALLBACK_ENABLED == 'true' && this.parentRetries == 4)) { opts.push(true, this.downloadErrorMessage || process.env.BINARY_DOWNLOAD_ERROR_MESSAGE); @@ -230,8 +230,9 @@ function LocalBinary(){ }); }; - this.binaryPath = function(conf, key, parentRetries, callback){ + this.binaryPath = function(conf, bsHost, key, parentRetries, callback){ this.key = key; + this.bsHost = bsHost; this.parentRetries = parentRetries; var destParentDir = this.getAvailableDirs(); var destBinaryName = (this.windows) ? 'BrowserStackLocal.exe' : 'BrowserStackLocal'; diff --git a/lib/fetchDownloadSourceUrl.js b/lib/fetchDownloadSourceUrl.js index 37af002..90fdbf9 100644 --- a/lib/fetchDownloadSourceUrl.js +++ b/lib/fetchDownloadSourceUrl.js @@ -2,11 +2,11 @@ const https = require('https'), fs = require('fs'), HttpsProxyAgent = require('https-proxy-agent'); -const authToken = process.argv[2], proxyHost = process.argv[5], proxyPort = process.argv[6], useCaCertificate = process.argv[7], downloadFallback = process.argv[3], downloadErrorMessage = process.argv[4]; +const authToken = process.argv[2], bsHost = process.argv[3], proxyHost = process.argv[6], proxyPort = process.argv[7], useCaCertificate = process.argv[8], downloadFallback = process.argv[4], downloadErrorMessage = process.argv[5]; let body = '', data = {'auth_token': authToken}; const options = { - hostname: 'local.browserstack.com', + hostname: bsHost && bsHost !== 'undefined' ? bsHost : 'local.browserstack.com', port: 443, path: '/binary/api/v1/endpoint', method: 'POST', @@ -20,13 +20,13 @@ if (downloadFallback == 'true') { data['error_message'] = downloadErrorMessage; } -if(proxyHost && proxyPort) { +if(proxyHost && proxyPort && proxyHost !== 'undefined' && proxyPort !== 'undefined') { options.agent = new HttpsProxyAgent({ host: proxyHost, port: proxyPort }); } -if (useCaCertificate) { +if (useCaCertificate && useCaCertificate !== 'undefined') { try { options.ca = fs.readFileSync(useCaCertificate); } catch(err) { From 17a583ae8ccddce15d48150b5bb134614913e0bf Mon Sep 17 00:00:00 2001 From: amaanbs Date: Tue, 1 Jul 2025 17:46:50 +0530 Subject: [PATCH 2/2] refactor into utility methods --- lib/fetchDownloadSourceUrl.js | 9 +++++---- lib/util.js | 1 + 2 files changed, 6 insertions(+), 4 deletions(-) create mode 100644 lib/util.js diff --git a/lib/fetchDownloadSourceUrl.js b/lib/fetchDownloadSourceUrl.js index 90fdbf9..df5c8f2 100644 --- a/lib/fetchDownloadSourceUrl.js +++ b/lib/fetchDownloadSourceUrl.js @@ -1,12 +1,13 @@ const https = require('https'), fs = require('fs'), - HttpsProxyAgent = require('https-proxy-agent'); + HttpsProxyAgent = require('https-proxy-agent'), + { isUndefined } = require('./util'); const authToken = process.argv[2], bsHost = process.argv[3], proxyHost = process.argv[6], proxyPort = process.argv[7], useCaCertificate = process.argv[8], downloadFallback = process.argv[4], downloadErrorMessage = process.argv[5]; let body = '', data = {'auth_token': authToken}; const options = { - hostname: bsHost && bsHost !== 'undefined' ? bsHost : 'local.browserstack.com', + hostname: !isUndefined(bsHost) ? bsHost : 'local.browserstack.com', port: 443, path: '/binary/api/v1/endpoint', method: 'POST', @@ -20,13 +21,13 @@ if (downloadFallback == 'true') { data['error_message'] = downloadErrorMessage; } -if(proxyHost && proxyPort && proxyHost !== 'undefined' && proxyPort !== 'undefined') { +if(!isUndefined(proxyHost) && !isUndefined(proxyPort)) { options.agent = new HttpsProxyAgent({ host: proxyHost, port: proxyPort }); } -if (useCaCertificate && useCaCertificate !== 'undefined') { +if (!isUndefined(useCaCertificate)) { try { options.ca = fs.readFileSync(useCaCertificate); } catch(err) { diff --git a/lib/util.js b/lib/util.js new file mode 100644 index 0000000..b7de5b5 --- /dev/null +++ b/lib/util.js @@ -0,0 +1 @@ +module.exports.isUndefined = value => (value === undefined || value === null || value === 'undefined');