From ac9f9c91fceb91d749dd5eac3c367a14f20a953b Mon Sep 17 00:00:00 2001 From: amaanbs Date: Fri, 6 Jun 2025 04:27:16 +0530 Subject: [PATCH 1/5] change binary download distribution --- lib/Local.js | 4 +- lib/LocalBinary.js | 78 ++++++++++++++++++++++++++++------- lib/fetchDownloadSourceUrl.js | 57 +++++++++++++++++++++++++ test/local.js | 6 +-- 4 files changed, 126 insertions(+), 19 deletions(-) create mode 100644 lib/fetchDownloadSourceUrl.js diff --git a/lib/Local.js b/lib/Local.js index 9876c6d..41945c8 100644 --- a/lib/Local.js +++ b/lib/Local.js @@ -254,9 +254,9 @@ function Local(){ conf.useCaCertificate = this.useCaCertificate; } if(!callback) { - return this.binary.binaryPath(conf); + return this.binary.binaryPath(conf, this.key); } - this.binary.binaryPath(conf, callback); + this.binary.binaryPath(conf, this.key, callback); } else { console.log('BINARY PATH IS DEFINED'); if(!callback) { diff --git a/lib/LocalBinary.js b/lib/LocalBinary.js index 073bf61..d5bb22d 100644 --- a/lib/LocalBinary.js +++ b/lib/LocalBinary.js @@ -14,9 +14,44 @@ const packageName = 'browserstack-local-nodejs'; function LocalBinary(){ this.hostOS = process.platform; this.is64bits = process.arch == 'x64'; + this.baseRetries = 10; + this.sourceURL = null; + this.downloadErrorMessage = null; + + this.getSourceUrl = function(conf, retries) { + /* Request for an endpoint from Rails no more than twice with 5 retries each */ + if (![5, 10].includes(retries) && this.sourceURL) return this.sourceURL; + + let cmd, opts; + cmd = 'node'; + opts = [path.join(__dirname, 'fetchDownloadSourceUrl.js'), this.key]; + if(conf.proxyHost && conf.proxyPort) { + opts.push(conf.proxyHost, conf.proxyPort); + if (conf.useCaCertificate) { + opts.push(conf.useCaCertificate); + } + } else if (conf.useCaCertificate) { + opts.push(undefined, undefined, conf.useCaCertificate); + } - this.getDownloadPath = function () { - let sourceURL = 'https://www.browserstack.com/local-testing/downloads/binaries/'; + if (retries == 5) { + opts.push(true, this.binaryDownloadError); + } + + const userAgent = [packageName, version].join('/'); + const env = Object.assign({ 'USER_AGENT': userAgent }, process.env); + const obj = childProcess.spawnSync(cmd, opts, { env: env }); + if(obj.stdout.length > 0) { + this.sourceURL = obj.stdout; + return this.sourceURL; + } else if(obj.stderr.length > 0) { + let output = Buffer.from(JSON.parse(JSON.stringify(obj.stderr)).data).toString(); + throw(output); + } + } + + this.getDownloadPath = function (conf, retries) { + let sourceURL = this.getSourceUrl(conf, retries) + '/'; if(this.hostOS.match(/darwin|mac os/i)){ return sourceURL + 'BrowserStackLocal-darwin-x64'; @@ -43,9 +78,10 @@ function LocalBinary(){ } }; - this.httpPath = this.getDownloadPath(); - - + this.binaryDownloadError = function(errorMessagePrefix, errorObject) { + console.error(errorMessagePrefix, errorObject); + this.downloadErrorMessage = errorMessagePrefix + " : " + errorObject ? errorObject.getMessage() : "null"; + } this.retryBinaryDownload = function(conf, destParentDir, callback, retries, binaryPath) { var that = this; @@ -66,6 +102,12 @@ function LocalBinary(){ }; this.downloadSync = function(conf, destParentDir, retries) { + try { + this.httpPath = this.getDownloadPath(conf, retries); + } catch (e) { + return console.error(`Unable to fetch the source url to download the binary with error: ${e}`); + } + console.log('Downloading in sync'); var that = this; if(!this.checkPath(destParentDir)) @@ -96,21 +138,27 @@ function LocalBinary(){ fs.chmodSync(binaryPath, '0755'); return binaryPath; }else{ - console.log('failed to download'); + that.binaryDownloadError('failed to download'); return that.retryBinaryDownload(conf, destParentDir, null, retries, binaryPath); } } else if(obj.stderr.length > 0) { output = Buffer.from(JSON.parse(JSON.stringify(obj.stderr)).data).toString(); - console.error(output); + that.binaryDownloadError(output); return that.retryBinaryDownload(conf, destParentDir, null, retries, binaryPath); } } catch(err) { - console.error('Download failed with error', err); + that.binaryDownloadError('Download failed with error', err); return that.retryBinaryDownload(conf, destParentDir, null, retries, binaryPath); } }; this.download = function(conf, destParentDir, callback, retries){ + try { + this.httpPath = this.getDownloadPath(conf, retries); + } catch (e) { + return console.error(`Unable to fetch the source url to download the binary with error: ${e}`); + } + var that = this; if(!this.checkPath(destParentDir)) fs.mkdirSync(destParentDir); @@ -152,11 +200,11 @@ function LocalBinary(){ } response.on('error', function(err) { - console.error('Got Error in binary download response', err); + that.binaryDownloadError('Got Error in binary download response', err); that.retryBinaryDownload(conf, destParentDir, callback, retries, binaryPath); }); fileStream.on('error', function (err) { - console.error('Got Error while downloading binary file', err); + that.binaryDownloadError('Got Error while downloading binary file', err); that.retryBinaryDownload(conf, destParentDir, callback, retries, binaryPath); }); fileStream.on('close', function () { @@ -165,12 +213,13 @@ function LocalBinary(){ }); }); }).on('error', function(err) { - console.error('Got Error in binary downloading request', err); + that.binaryDownloadError('Got Error in binary downloading request', err); that.retryBinaryDownload(conf, destParentDir, callback, retries, binaryPath); }); }; - this.binaryPath = function(conf, callback){ + this.binaryPath = function(conf, key, callback){ + this.key = key; var destParentDir = this.getAvailableDirs(); var destBinaryName = (this.windows) ? 'BrowserStackLocal.exe' : 'BrowserStackLocal'; var binaryPath = path.join(destParentDir, destBinaryName); @@ -180,10 +229,11 @@ function LocalBinary(){ } callback(binaryPath); } else { + let retries = this.baseRetries; if(!callback) { - return this.downloadSync(conf, destParentDir, 5); + return this.downloadSync(conf, destParentDir, retries); } - this.download(conf, destParentDir, callback, 5); + this.download(conf, destParentDir, callback, retries); } }; diff --git a/lib/fetchDownloadSourceUrl.js b/lib/fetchDownloadSourceUrl.js new file mode 100644 index 0000000..503b3de --- /dev/null +++ b/lib/fetchDownloadSourceUrl.js @@ -0,0 +1,57 @@ +const https = require('https'), + fs = require('fs'), + HttpsProxyAgent = require('https-proxy-agent'); + +const authToken = process.argv[2], proxyHost = process.argv[3], proxyPort = process.argv[4], useCaCertificate = process.argv[5], downloadFallback = process.argv[6], downloadErrorMessage = process.argv[7]; + +let body = '', data = {"auth_token": authToken}; +const options = { + hostname: 'k8s-devlocal.bsstag.com', + port: 443, + path: '/binary/api/v1/endpoint', + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'user-agent': process.env.USER_AGENT + } +}; +if (downloadFallback) { + options.headers['X-Local-Fallback-Cloudflare'] = true; + data["error_message"] = downloadErrorMessage; +} + +if(proxyHost && proxyPort) { + options.agent = new HttpsProxyAgent({ + host: proxyHost, + port: proxyPort + }); +} +if (useCaCertificate) { + try { + options.ca = fs.readFileSync(useCaCertificate); + } catch(err) { + console.log('failed to read cert file', err); + } +} + +const req = https.request(options, res => { + res.on('data', d => { + body += d; + }); + res.on('end', () => { + try { + const url = JSON.parse(body).data.endpoint; + console.log(url); + } catch (e) { + console.error(e); + } + }); + res.on('error', (err) => { + console.error(err); + }) +}); +req.on('error', e => { + console.error(e); +}); +req.write(JSON.stringify(data)); +req.end(); diff --git a/test/local.js b/test/local.js index cdda649..5405a93 100644 --- a/test/local.js +++ b/test/local.js @@ -280,7 +280,7 @@ describe('LocalBinary', function () { // ensure that we have a valid binary downloaded // removeIfInvalid(); - (new LocalBinary()).binaryPath({}, function(binaryPath) { + (new LocalBinary()).binaryPath({}, 'abc', function(binaryPath) { defaultBinaryPath = binaryPath; tempfs.mkdir({ recursive: true @@ -313,7 +313,7 @@ describe('LocalBinary', function () { fs.writeFile(defaultBinaryPath, 'Random String', function() { fs.chmod(defaultBinaryPath, '0755', function() { localBinary.binaryPath({ - }, function(binaryPath) { + }, 'abc', function(binaryPath) { expect(downloadStub.called).to.be.true; done(); }); @@ -331,7 +331,7 @@ describe('LocalBinary', function () { }); localBinary.binaryPath({ - }, function(binaryPath) { + }, 'abc', function(binaryPath) { expect(downloadStub.called).to.be.true; done(); }); From 66f260ef0195d36bd0b4f0aa36f5c7476f4cd05b Mon Sep 17 00:00:00 2001 From: amaanbs Date: Fri, 6 Jun 2025 05:26:02 +0530 Subject: [PATCH 2/5] fix hostname --- lib/fetchDownloadSourceUrl.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/fetchDownloadSourceUrl.js b/lib/fetchDownloadSourceUrl.js index 503b3de..829f37e 100644 --- a/lib/fetchDownloadSourceUrl.js +++ b/lib/fetchDownloadSourceUrl.js @@ -6,7 +6,7 @@ const authToken = process.argv[2], proxyHost = process.argv[3], proxyPort = proc let body = '', data = {"auth_token": authToken}; const options = { - hostname: 'k8s-devlocal.bsstag.com', + hostname: 'local.browserstack.com', port: 443, path: '/binary/api/v1/endpoint', method: 'POST', From d121bfcc568f943cbd6e38d931fd5b1a3b3143ea Mon Sep 17 00:00:00 2001 From: amaanbs Date: Tue, 10 Jun 2025 00:59:59 +0530 Subject: [PATCH 3/5] minor fixes --- lib/Local.js | 16 ++++++++----- lib/LocalBinary.js | 42 ++++++++++++++++++++++------------- lib/fetchDownloadSourceUrl.js | 13 ++++++----- 3 files changed, 46 insertions(+), 25 deletions(-) diff --git a/lib/Local.js b/lib/Local.js index 41945c8..3f9469f 100644 --- a/lib/Local.js +++ b/lib/Local.js @@ -17,7 +17,7 @@ function Local(){ this.windows = os.platform().match(/mswin|msys|mingw|cygwin|bccwin|wince|emc|win32/i); this.pid = undefined; this.isProcessRunning = false; - this.retriesLeft = 5; + this.retriesLeft = 9; this.key = process.env.BROWSERSTACK_ACCESS_KEY; this.logfile = this.sanitizePath(path.join(process.cwd(), 'local.log')); this.opcode = 'start'; @@ -59,12 +59,15 @@ function Local(){ return; } }catch(error){ - console.error('Error while trying to execute binary', error); + const binaryDownloadErrorMessage = `Error while trying to execute binary: ${error}`; + console.error(binaryDownloadErrorMessage); if(that.retriesLeft > 0) { console.log('Retrying Binary Download. Retries Left', that.retriesLeft); that.retriesLeft -= 1; fs.unlinkSync(that.binaryPath); delete(that.binaryPath); + process.env.BINARY_DOWNLOAD_ERROR_MESSAGE = binaryDownloadErrorMessage; + process.env.BINARY_DOWNLOAD_FALLBACK_ENABLED = true; return that.startSync(options); } else { throw new LocalError(error.toString()); @@ -87,12 +90,15 @@ function Local(){ that.opcode = 'start'; that.tunnel = childProcess.execFile(that.binaryPath, that.getBinaryArgs(), function(error, stdout, stderr){ if(error) { - console.error('Error while trying to execute binary', error); + const binaryDownloadErrorMessage = `Error while trying to execute binary: ${error}`; + console.error(binaryDownloadErrorMessage); if(that.retriesLeft > 0) { console.log('Retrying Binary Download. Retries Left', that.retriesLeft); that.retriesLeft -= 1; fs.unlinkSync(that.binaryPath); delete(that.binaryPath); + process.env.BINARY_DOWNLOAD_ERROR_MESSAGE = binaryDownloadErrorMessage; + process.env.BINARY_DOWNLOAD_FALLBACK_ENABLED = true; that.start(options, callback); return; } else { @@ -254,9 +260,9 @@ function Local(){ conf.useCaCertificate = this.useCaCertificate; } if(!callback) { - return this.binary.binaryPath(conf, this.key); + return this.binary.binaryPath(conf, this.key, this.retriesLeft); } - this.binary.binaryPath(conf, this.key, callback); + this.binary.binaryPath(conf, 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 d5bb22d..05fb306 100644 --- a/lib/LocalBinary.js +++ b/lib/LocalBinary.js @@ -14,17 +14,31 @@ const packageName = 'browserstack-local-nodejs'; function LocalBinary(){ this.hostOS = process.platform; this.is64bits = process.arch == 'x64'; - this.baseRetries = 10; + this.baseRetries = 9; this.sourceURL = null; this.downloadErrorMessage = null; this.getSourceUrl = function(conf, retries) { /* Request for an endpoint from Rails no more than twice with 5 retries each */ - if (![5, 10].includes(retries) && this.sourceURL) return this.sourceURL; + if (![4, 9].includes(retries) && this.sourceURL != null) { + return this.sourceURL; + } + + if (process.env.BINARY_DOWNLOAD_SOURCE_URL !== undefined && process.env.BINARY_DOWNLOAD_FALLBACK_ENABLED == "true" && this.parentRetries != 4) { + /* This is triggered from Local.js if there's an error executing the downloaded binary */ + return process.env.BINARY_DOWNLOAD_SOURCE_URL; + } let cmd, opts; cmd = 'node'; opts = [path.join(__dirname, 'fetchDownloadSourceUrl.js'), this.key]; + + if (retries == 4 || (process.env.BINARY_DOWNLOAD_FALLBACK_ENABLED == "true" && this.parentRetries == 4)) { + opts.push(true, this.downloadErrorMessage || process.env.BINARY_DOWNLOAD_ERROR_MESSAGE); + } else { + opts.push(false, null); + } + if(conf.proxyHost && conf.proxyPort) { opts.push(conf.proxyHost, conf.proxyPort); if (conf.useCaCertificate) { @@ -33,16 +47,13 @@ function LocalBinary(){ } else if (conf.useCaCertificate) { opts.push(undefined, undefined, conf.useCaCertificate); } - - if (retries == 5) { - opts.push(true, this.binaryDownloadError); - } const userAgent = [packageName, version].join('/'); const env = Object.assign({ 'USER_AGENT': userAgent }, process.env); const obj = childProcess.spawnSync(cmd, opts, { env: env }); if(obj.stdout.length > 0) { - this.sourceURL = obj.stdout; + this.sourceURL = obj.stdout.toString().replace(/\n+$/, ''); + process.env.BINARY_DOWNLOAD_SOURCE_URL = this.sourceURL; return this.sourceURL; } else if(obj.stderr.length > 0) { let output = Buffer.from(JSON.parse(JSON.stringify(obj.stderr)).data).toString(); @@ -78,9 +89,9 @@ function LocalBinary(){ } }; - this.binaryDownloadError = function(errorMessagePrefix, errorObject) { - console.error(errorMessagePrefix, errorObject); - this.downloadErrorMessage = errorMessagePrefix + " : " + errorObject ? errorObject.getMessage() : "null"; + this.binaryDownloadError = function(errorMessagePrefix, errorMessage) { + console.error(errorMessagePrefix, errorMessage); + this.downloadErrorMessage = errorMessagePrefix + " : " + errorMessage; } this.retryBinaryDownload = function(conf, destParentDir, callback, retries, binaryPath) { @@ -147,7 +158,7 @@ function LocalBinary(){ return that.retryBinaryDownload(conf, destParentDir, null, retries, binaryPath); } } catch(err) { - that.binaryDownloadError('Download failed with error', err); + that.binaryDownloadError('Download failed with error', err.getMessage()); return that.retryBinaryDownload(conf, destParentDir, null, retries, binaryPath); } }; @@ -200,11 +211,11 @@ function LocalBinary(){ } response.on('error', function(err) { - that.binaryDownloadError('Got Error in binary download response', err); + that.binaryDownloadError('Got Error in binary download response', err.message); that.retryBinaryDownload(conf, destParentDir, callback, retries, binaryPath); }); fileStream.on('error', function (err) { - that.binaryDownloadError('Got Error while downloading binary file', err); + that.binaryDownloadError('Got Error while downloading binary file', err.message); that.retryBinaryDownload(conf, destParentDir, callback, retries, binaryPath); }); fileStream.on('close', function () { @@ -213,13 +224,14 @@ function LocalBinary(){ }); }); }).on('error', function(err) { - that.binaryDownloadError('Got Error in binary downloading request', err); + that.binaryDownloadError('Got Error in binary downloading request', err.message); that.retryBinaryDownload(conf, destParentDir, callback, retries, binaryPath); }); }; - this.binaryPath = function(conf, key, callback){ + this.binaryPath = function(conf, key, parentRetries, callback){ this.key = key; + this.parentRetries = parentRetries; var destParentDir = this.getAvailableDirs(); var destBinaryName = (this.windows) ? 'BrowserStackLocal.exe' : 'BrowserStackLocal'; var binaryPath = path.join(destParentDir, destBinaryName); diff --git a/lib/fetchDownloadSourceUrl.js b/lib/fetchDownloadSourceUrl.js index 829f37e..6bc7362 100644 --- a/lib/fetchDownloadSourceUrl.js +++ b/lib/fetchDownloadSourceUrl.js @@ -2,7 +2,7 @@ const https = require('https'), fs = require('fs'), HttpsProxyAgent = require('https-proxy-agent'); -const authToken = process.argv[2], proxyHost = process.argv[3], proxyPort = process.argv[4], useCaCertificate = process.argv[5], downloadFallback = process.argv[6], downloadErrorMessage = process.argv[7]; +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]; let body = '', data = {"auth_token": authToken}; const options = { @@ -15,7 +15,7 @@ const options = { 'user-agent': process.env.USER_AGENT } }; -if (downloadFallback) { +if (downloadFallback == "true") { options.headers['X-Local-Fallback-Cloudflare'] = true; data["error_message"] = downloadErrorMessage; } @@ -40,14 +40,17 @@ const req = https.request(options, res => { }); res.on('end', () => { try { - const url = JSON.parse(body).data.endpoint; - console.log(url); + const reqBody = JSON.parse(body); + if(reqBody.error) { + throw reqBody.error; + } + console.log(reqBody.data.endpoint); } catch (e) { console.error(e); } }); res.on('error', (err) => { - console.error(err); + console.error(err); }) }); req.on('error', e => { From ea7c4c6fbbd2c08ceebe217e2196d15b28dd2abf Mon Sep 17 00:00:00 2001 From: amaanbs Date: Tue, 10 Jun 2025 01:13:04 +0530 Subject: [PATCH 4/5] test fixes --- lib/LocalBinary.js | 20 ++++++++++---------- lib/fetchDownloadSourceUrl.js | 11 ++++++----- test/local.js | 6 +++--- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/lib/LocalBinary.js b/lib/LocalBinary.js index 05fb306..c618324 100644 --- a/lib/LocalBinary.js +++ b/lib/LocalBinary.js @@ -24,16 +24,16 @@ function LocalBinary(){ return this.sourceURL; } - if (process.env.BINARY_DOWNLOAD_SOURCE_URL !== undefined && process.env.BINARY_DOWNLOAD_FALLBACK_ENABLED == "true" && this.parentRetries != 4) { + if (process.env.BINARY_DOWNLOAD_SOURCE_URL !== undefined && process.env.BINARY_DOWNLOAD_FALLBACK_ENABLED == 'true' && this.parentRetries != 4) { /* This is triggered from Local.js if there's an error executing the downloaded binary */ return process.env.BINARY_DOWNLOAD_SOURCE_URL; } - + let cmd, opts; cmd = 'node'; opts = [path.join(__dirname, 'fetchDownloadSourceUrl.js'), this.key]; - - if (retries == 4 || (process.env.BINARY_DOWNLOAD_FALLBACK_ENABLED == "true" && this.parentRetries == 4)) { + + if (retries == 4 || (process.env.BINARY_DOWNLOAD_FALLBACK_ENABLED == 'true' && this.parentRetries == 4)) { opts.push(true, this.downloadErrorMessage || process.env.BINARY_DOWNLOAD_ERROR_MESSAGE); } else { opts.push(false, null); @@ -47,7 +47,7 @@ function LocalBinary(){ } else if (conf.useCaCertificate) { opts.push(undefined, undefined, conf.useCaCertificate); } - + const userAgent = [packageName, version].join('/'); const env = Object.assign({ 'USER_AGENT': userAgent }, process.env); const obj = childProcess.spawnSync(cmd, opts, { env: env }); @@ -59,7 +59,7 @@ function LocalBinary(){ let output = Buffer.from(JSON.parse(JSON.stringify(obj.stderr)).data).toString(); throw(output); } - } + }; this.getDownloadPath = function (conf, retries) { let sourceURL = this.getSourceUrl(conf, retries) + '/'; @@ -91,8 +91,8 @@ function LocalBinary(){ this.binaryDownloadError = function(errorMessagePrefix, errorMessage) { console.error(errorMessagePrefix, errorMessage); - this.downloadErrorMessage = errorMessagePrefix + " : " + errorMessage; - } + this.downloadErrorMessage = errorMessagePrefix + ' : ' + errorMessage; + }; this.retryBinaryDownload = function(conf, destParentDir, callback, retries, binaryPath) { var that = this; @@ -118,7 +118,7 @@ function LocalBinary(){ } catch (e) { return console.error(`Unable to fetch the source url to download the binary with error: ${e}`); } - + console.log('Downloading in sync'); var that = this; if(!this.checkPath(destParentDir)) @@ -169,7 +169,7 @@ function LocalBinary(){ } catch (e) { return console.error(`Unable to fetch the source url to download the binary with error: ${e}`); } - + var that = this; if(!this.checkPath(destParentDir)) fs.mkdirSync(destParentDir); diff --git a/lib/fetchDownloadSourceUrl.js b/lib/fetchDownloadSourceUrl.js index 6bc7362..ec60dfd 100644 --- a/lib/fetchDownloadSourceUrl.js +++ b/lib/fetchDownloadSourceUrl.js @@ -4,9 +4,10 @@ const https = require('https'), 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]; -let body = '', data = {"auth_token": authToken}; +let body = '', data = {'auth_token': authToken}; const options = { - hostname: 'local.browserstack.com', + // hostname: 'local.browserstack.com', + hostname: 'k8s-devlocal.bsstag.com', port: 443, path: '/binary/api/v1/endpoint', method: 'POST', @@ -15,9 +16,9 @@ const options = { 'user-agent': process.env.USER_AGENT } }; -if (downloadFallback == "true") { +if (downloadFallback == 'true') { options.headers['X-Local-Fallback-Cloudflare'] = true; - data["error_message"] = downloadErrorMessage; + data['error_message'] = downloadErrorMessage; } if(proxyHost && proxyPort) { @@ -51,7 +52,7 @@ const req = https.request(options, res => { }); res.on('error', (err) => { console.error(err); - }) + }); }); req.on('error', e => { console.error(e); diff --git a/test/local.js b/test/local.js index 5405a93..79c10eb 100644 --- a/test/local.js +++ b/test/local.js @@ -280,7 +280,7 @@ describe('LocalBinary', function () { // ensure that we have a valid binary downloaded // removeIfInvalid(); - (new LocalBinary()).binaryPath({}, 'abc', function(binaryPath) { + (new LocalBinary()).binaryPath({}, 'abc', 9, function(binaryPath) { defaultBinaryPath = binaryPath; tempfs.mkdir({ recursive: true @@ -313,7 +313,7 @@ describe('LocalBinary', function () { fs.writeFile(defaultBinaryPath, 'Random String', function() { fs.chmod(defaultBinaryPath, '0755', function() { localBinary.binaryPath({ - }, 'abc', function(binaryPath) { + }, 'abc', 9, function(binaryPath) { expect(downloadStub.called).to.be.true; done(); }); @@ -331,7 +331,7 @@ describe('LocalBinary', function () { }); localBinary.binaryPath({ - }, 'abc', function(binaryPath) { + }, 'abc', 9, function(binaryPath) { expect(downloadStub.called).to.be.true; done(); }); From 3198df323fc581cac8d4884970c73ac35738c8a1 Mon Sep 17 00:00:00 2001 From: amaanbs Date: Tue, 10 Jun 2025 17:12:19 +0530 Subject: [PATCH 5/5] PR Review Changes --- lib/Local.js | 5 +++-- lib/LocalBinary.js | 11 ++++++----- lib/fetchDownloadSourceUrl.js | 3 +-- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/Local.js b/lib/Local.js index 3f9469f..63e901f 100644 --- a/lib/Local.js +++ b/lib/Local.js @@ -1,6 +1,7 @@ var childProcess = require('child_process'), os = require('os'), fs = require('fs'), + util = require('util'), path = require('path'), running = require('is-running'), LocalBinary = require('./LocalBinary'), @@ -59,7 +60,7 @@ function Local(){ return; } }catch(error){ - const binaryDownloadErrorMessage = `Error while trying to execute binary: ${error}`; + const binaryDownloadErrorMessage = `Error while trying to execute binary: ${util.format(error)}`; console.error(binaryDownloadErrorMessage); if(that.retriesLeft > 0) { console.log('Retrying Binary Download. Retries Left', that.retriesLeft); @@ -90,7 +91,7 @@ function Local(){ that.opcode = 'start'; that.tunnel = childProcess.execFile(that.binaryPath, that.getBinaryArgs(), function(error, stdout, stderr){ if(error) { - const binaryDownloadErrorMessage = `Error while trying to execute binary: ${error}`; + const binaryDownloadErrorMessage = `Error while trying to execute binary: ${util.format(error)}`; console.error(binaryDownloadErrorMessage); if(that.retriesLeft > 0) { console.log('Retrying Binary Download. Retries Left', that.retriesLeft); diff --git a/lib/LocalBinary.js b/lib/LocalBinary.js index c618324..25564d3 100644 --- a/lib/LocalBinary.js +++ b/lib/LocalBinary.js @@ -3,6 +3,7 @@ var https = require('https'), fs = require('fs'), path = require('path'), os = require('os'), + util = require('util'), childProcess = require('child_process'), zlib = require('zlib'), HttpsProxyAgent = require('https-proxy-agent'), @@ -19,7 +20,7 @@ function LocalBinary(){ this.downloadErrorMessage = null; this.getSourceUrl = function(conf, retries) { - /* Request for an endpoint from Rails no more than twice with 5 retries each */ + /* Request for an endpoint to download the local binary from Rails no more than twice with 5 retries each */ if (![4, 9].includes(retries) && this.sourceURL != null) { return this.sourceURL; } @@ -158,7 +159,7 @@ function LocalBinary(){ return that.retryBinaryDownload(conf, destParentDir, null, retries, binaryPath); } } catch(err) { - that.binaryDownloadError('Download failed with error', err.getMessage()); + that.binaryDownloadError('Download failed with error', util.format(err)); return that.retryBinaryDownload(conf, destParentDir, null, retries, binaryPath); } }; @@ -211,11 +212,11 @@ function LocalBinary(){ } response.on('error', function(err) { - that.binaryDownloadError('Got Error in binary download response', err.message); + that.binaryDownloadError('Got Error in binary download response', util.format(err)); that.retryBinaryDownload(conf, destParentDir, callback, retries, binaryPath); }); fileStream.on('error', function (err) { - that.binaryDownloadError('Got Error while downloading binary file', err.message); + that.binaryDownloadError('Got Error while downloading binary file', util.format(err)); that.retryBinaryDownload(conf, destParentDir, callback, retries, binaryPath); }); fileStream.on('close', function () { @@ -224,7 +225,7 @@ function LocalBinary(){ }); }); }).on('error', function(err) { - that.binaryDownloadError('Got Error in binary downloading request', err.message); + that.binaryDownloadError('Got Error in binary downloading request', util.format(err)); that.retryBinaryDownload(conf, destParentDir, callback, retries, binaryPath); }); }; diff --git a/lib/fetchDownloadSourceUrl.js b/lib/fetchDownloadSourceUrl.js index ec60dfd..8ebc57b 100644 --- a/lib/fetchDownloadSourceUrl.js +++ b/lib/fetchDownloadSourceUrl.js @@ -6,8 +6,7 @@ const authToken = process.argv[2], proxyHost = process.argv[5], proxyPort = proc let body = '', data = {'auth_token': authToken}; const options = { - // hostname: 'local.browserstack.com', - hostname: 'k8s-devlocal.bsstag.com', + hostname: 'local.browserstack.com', port: 443, path: '/binary/api/v1/endpoint', method: 'POST',