Skip to content

Commit d121bfc

Browse files
committed
minor fixes
1 parent 66f260e commit d121bfc

File tree

3 files changed

+46
-25
lines changed

3 files changed

+46
-25
lines changed

lib/Local.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function Local(){
1717
this.windows = os.platform().match(/mswin|msys|mingw|cygwin|bccwin|wince|emc|win32/i);
1818
this.pid = undefined;
1919
this.isProcessRunning = false;
20-
this.retriesLeft = 5;
20+
this.retriesLeft = 9;
2121
this.key = process.env.BROWSERSTACK_ACCESS_KEY;
2222
this.logfile = this.sanitizePath(path.join(process.cwd(), 'local.log'));
2323
this.opcode = 'start';
@@ -59,12 +59,15 @@ function Local(){
5959
return;
6060
}
6161
}catch(error){
62-
console.error('Error while trying to execute binary', error);
62+
const binaryDownloadErrorMessage = `Error while trying to execute binary: ${error}`;
63+
console.error(binaryDownloadErrorMessage);
6364
if(that.retriesLeft > 0) {
6465
console.log('Retrying Binary Download. Retries Left', that.retriesLeft);
6566
that.retriesLeft -= 1;
6667
fs.unlinkSync(that.binaryPath);
6768
delete(that.binaryPath);
69+
process.env.BINARY_DOWNLOAD_ERROR_MESSAGE = binaryDownloadErrorMessage;
70+
process.env.BINARY_DOWNLOAD_FALLBACK_ENABLED = true;
6871
return that.startSync(options);
6972
} else {
7073
throw new LocalError(error.toString());
@@ -87,12 +90,15 @@ function Local(){
8790
that.opcode = 'start';
8891
that.tunnel = childProcess.execFile(that.binaryPath, that.getBinaryArgs(), function(error, stdout, stderr){
8992
if(error) {
90-
console.error('Error while trying to execute binary', error);
93+
const binaryDownloadErrorMessage = `Error while trying to execute binary: ${error}`;
94+
console.error(binaryDownloadErrorMessage);
9195
if(that.retriesLeft > 0) {
9296
console.log('Retrying Binary Download. Retries Left', that.retriesLeft);
9397
that.retriesLeft -= 1;
9498
fs.unlinkSync(that.binaryPath);
9599
delete(that.binaryPath);
100+
process.env.BINARY_DOWNLOAD_ERROR_MESSAGE = binaryDownloadErrorMessage;
101+
process.env.BINARY_DOWNLOAD_FALLBACK_ENABLED = true;
96102
that.start(options, callback);
97103
return;
98104
} else {
@@ -254,9 +260,9 @@ function Local(){
254260
conf.useCaCertificate = this.useCaCertificate;
255261
}
256262
if(!callback) {
257-
return this.binary.binaryPath(conf, this.key);
263+
return this.binary.binaryPath(conf, this.key, this.retriesLeft);
258264
}
259-
this.binary.binaryPath(conf, this.key, callback);
265+
this.binary.binaryPath(conf, this.key, this.retriesLeft, callback);
260266
} else {
261267
console.log('BINARY PATH IS DEFINED');
262268
if(!callback) {

lib/LocalBinary.js

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,31 @@ const packageName = 'browserstack-local-nodejs';
1414
function LocalBinary(){
1515
this.hostOS = process.platform;
1616
this.is64bits = process.arch == 'x64';
17-
this.baseRetries = 10;
17+
this.baseRetries = 9;
1818
this.sourceURL = null;
1919
this.downloadErrorMessage = null;
2020

2121
this.getSourceUrl = function(conf, retries) {
2222
/* Request for an endpoint from Rails no more than twice with 5 retries each */
23-
if (![5, 10].includes(retries) && this.sourceURL) return this.sourceURL;
23+
if (![4, 9].includes(retries) && this.sourceURL != null) {
24+
return this.sourceURL;
25+
}
26+
27+
if (process.env.BINARY_DOWNLOAD_SOURCE_URL !== undefined && process.env.BINARY_DOWNLOAD_FALLBACK_ENABLED == "true" && this.parentRetries != 4) {
28+
/* This is triggered from Local.js if there's an error executing the downloaded binary */
29+
return process.env.BINARY_DOWNLOAD_SOURCE_URL;
30+
}
2431

2532
let cmd, opts;
2633
cmd = 'node';
2734
opts = [path.join(__dirname, 'fetchDownloadSourceUrl.js'), this.key];
35+
36+
if (retries == 4 || (process.env.BINARY_DOWNLOAD_FALLBACK_ENABLED == "true" && this.parentRetries == 4)) {
37+
opts.push(true, this.downloadErrorMessage || process.env.BINARY_DOWNLOAD_ERROR_MESSAGE);
38+
} else {
39+
opts.push(false, null);
40+
}
41+
2842
if(conf.proxyHost && conf.proxyPort) {
2943
opts.push(conf.proxyHost, conf.proxyPort);
3044
if (conf.useCaCertificate) {
@@ -33,16 +47,13 @@ function LocalBinary(){
3347
} else if (conf.useCaCertificate) {
3448
opts.push(undefined, undefined, conf.useCaCertificate);
3549
}
36-
37-
if (retries == 5) {
38-
opts.push(true, this.binaryDownloadError);
39-
}
4050

4151
const userAgent = [packageName, version].join('/');
4252
const env = Object.assign({ 'USER_AGENT': userAgent }, process.env);
4353
const obj = childProcess.spawnSync(cmd, opts, { env: env });
4454
if(obj.stdout.length > 0) {
45-
this.sourceURL = obj.stdout;
55+
this.sourceURL = obj.stdout.toString().replace(/\n+$/, '');
56+
process.env.BINARY_DOWNLOAD_SOURCE_URL = this.sourceURL;
4657
return this.sourceURL;
4758
} else if(obj.stderr.length > 0) {
4859
let output = Buffer.from(JSON.parse(JSON.stringify(obj.stderr)).data).toString();
@@ -78,9 +89,9 @@ function LocalBinary(){
7889
}
7990
};
8091

81-
this.binaryDownloadError = function(errorMessagePrefix, errorObject) {
82-
console.error(errorMessagePrefix, errorObject);
83-
this.downloadErrorMessage = errorMessagePrefix + " : " + errorObject ? errorObject.getMessage() : "null";
92+
this.binaryDownloadError = function(errorMessagePrefix, errorMessage) {
93+
console.error(errorMessagePrefix, errorMessage);
94+
this.downloadErrorMessage = errorMessagePrefix + " : " + errorMessage;
8495
}
8596

8697
this.retryBinaryDownload = function(conf, destParentDir, callback, retries, binaryPath) {
@@ -147,7 +158,7 @@ function LocalBinary(){
147158
return that.retryBinaryDownload(conf, destParentDir, null, retries, binaryPath);
148159
}
149160
} catch(err) {
150-
that.binaryDownloadError('Download failed with error', err);
161+
that.binaryDownloadError('Download failed with error', err.getMessage());
151162
return that.retryBinaryDownload(conf, destParentDir, null, retries, binaryPath);
152163
}
153164
};
@@ -200,11 +211,11 @@ function LocalBinary(){
200211
}
201212

202213
response.on('error', function(err) {
203-
that.binaryDownloadError('Got Error in binary download response', err);
214+
that.binaryDownloadError('Got Error in binary download response', err.message);
204215
that.retryBinaryDownload(conf, destParentDir, callback, retries, binaryPath);
205216
});
206217
fileStream.on('error', function (err) {
207-
that.binaryDownloadError('Got Error while downloading binary file', err);
218+
that.binaryDownloadError('Got Error while downloading binary file', err.message);
208219
that.retryBinaryDownload(conf, destParentDir, callback, retries, binaryPath);
209220
});
210221
fileStream.on('close', function () {
@@ -213,13 +224,14 @@ function LocalBinary(){
213224
});
214225
});
215226
}).on('error', function(err) {
216-
that.binaryDownloadError('Got Error in binary downloading request', err);
227+
that.binaryDownloadError('Got Error in binary downloading request', err.message);
217228
that.retryBinaryDownload(conf, destParentDir, callback, retries, binaryPath);
218229
});
219230
};
220231

221-
this.binaryPath = function(conf, key, callback){
232+
this.binaryPath = function(conf, key, parentRetries, callback){
222233
this.key = key;
234+
this.parentRetries = parentRetries;
223235
var destParentDir = this.getAvailableDirs();
224236
var destBinaryName = (this.windows) ? 'BrowserStackLocal.exe' : 'BrowserStackLocal';
225237
var binaryPath = path.join(destParentDir, destBinaryName);

lib/fetchDownloadSourceUrl.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const https = require('https'),
22
fs = require('fs'),
33
HttpsProxyAgent = require('https-proxy-agent');
44

5-
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];
5+
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];
66

77
let body = '', data = {"auth_token": authToken};
88
const options = {
@@ -15,7 +15,7 @@ const options = {
1515
'user-agent': process.env.USER_AGENT
1616
}
1717
};
18-
if (downloadFallback) {
18+
if (downloadFallback == "true") {
1919
options.headers['X-Local-Fallback-Cloudflare'] = true;
2020
data["error_message"] = downloadErrorMessage;
2121
}
@@ -40,14 +40,17 @@ const req = https.request(options, res => {
4040
});
4141
res.on('end', () => {
4242
try {
43-
const url = JSON.parse(body).data.endpoint;
44-
console.log(url);
43+
const reqBody = JSON.parse(body);
44+
if(reqBody.error) {
45+
throw reqBody.error;
46+
}
47+
console.log(reqBody.data.endpoint);
4548
} catch (e) {
4649
console.error(e);
4750
}
4851
});
4952
res.on('error', (err) => {
50-
console.error(err);
53+
console.error(err);
5154
})
5255
});
5356
req.on('error', e => {

0 commit comments

Comments
 (0)