Skip to content

Commit dd2812e

Browse files
07souravkundafrancisf
authored andcommitted
Make download logic os independant
1 parent 2c4df2b commit dd2812e

File tree

2 files changed

+41
-35
lines changed

2 files changed

+41
-35
lines changed

lib/LocalBinary.js

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -65,57 +65,31 @@ function LocalBinary(){
6565
var destBinaryName = (this.windows) ? 'BrowserStackLocal.exe' : 'BrowserStackLocal';
6666
var binaryPath = path.join(destParentDir, destBinaryName);
6767

68-
var options = url.parse(this.httpPath);
69-
if(conf.proxyHost && conf.proxyPort) {
70-
options.agent = new HttpsProxyAgent({
71-
host: conf.proxyHost,
72-
port: conf.proxyPort
73-
});
74-
}
75-
76-
const osPlatform = os.platform();
77-
const osVersion = parseInt(os.release());
78-
7968
let cmd, opts;
80-
81-
if(osPlatform.includes('win') && osVersion < 10) {
82-
cmd = 'wget';
83-
opts = [];
84-
if(conf.proxyHost && conf.proxyPort) {
85-
opts.push('-e', `https_proxy=${conf.proxyHost}:${conf.proxyPort}`, '--no-check-certificate');
86-
}
87-
opts.push('-O', binaryPath, options.href);
88-
}else{
89-
cmd = 'curl';
90-
opts = ['-o', binaryPath, options.href];
91-
92-
if(conf.proxyHost && conf.proxyPort) {
93-
opts.push('--proxy', `${conf.proxyHost}:${conf.proxyPort}`);
94-
}
69+
cmd = 'node';
70+
opts = [path.join(__dirname, 'download.js'), binaryPath, this.httpPath]
71+
if(conf.proxyHost && conf.proxyPort) {
72+
opts.push(conf.proxyHost, conf.proxyPort);
9573
}
9674

9775
try{
9876
const obj = childProcess.spawnSync(cmd, opts);
9977
let output;
10078
if(obj.stdout.length > 0) {
101-
output = Buffer.from(JSON.parse(JSON.stringify(obj.stderr)).data).toString();
102-
} else if(obj.stderr.length > 0) {
103-
output = Buffer.from(JSON.parse(JSON.stringify(obj.stderr)).data).toString();
104-
}
105-
if(output.includes('failed')){
106-
console.log('failed to download');
107-
return that.retryBinaryDownload(conf, destParentDir, null, retries, binaryPath);
108-
}else{
10979
if(fs.existsSync(binaryPath)){
11080
fs.chmodSync(binaryPath, '0755');
11181
return binaryPath;
11282
}else{
11383
console.log('failed to download');
11484
return that.retryBinaryDownload(conf, destParentDir, null, retries, binaryPath);
11585
}
86+
} else if(obj.stderr.length > 0) {
87+
output = Buffer.from(JSON.parse(JSON.stringify(obj.stderr)).data).toString();
88+
console.error(output)
89+
return that.retryBinaryDownload(conf, destParentDir, null, retries, binaryPath);
11690
}
11791
} catch(err) {
118-
console.log('Download failed with error', err);
92+
console.error('Download failed with error', err);
11993
return that.retryBinaryDownload(conf, destParentDir, null, retries, binaryPath);
12094
}
12195
};

lib/download.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const https = require('https'),
2+
fs = require('fs'),
3+
path = require('path'),
4+
HttpsProxyAgent = require('https-proxy-agent'),
5+
url = require('url');
6+
7+
const binaryPath = process.argv[2],httpPath = process.argv[3], proxyHost = process.argv[4], proxyPort = process.argv[5];
8+
9+
var fileStream = fs.createWriteStream(binaryPath);
10+
11+
var options = url.parse(httpPath);
12+
if(proxyHost && proxyPort) {
13+
options.agent = new HttpsProxyAgent({
14+
host: proxyHost,
15+
port: proxyPort
16+
});
17+
}
18+
19+
https.get(options, function (response) {
20+
response.pipe(fileStream);
21+
response.on('error', function(err) {
22+
console.error('Got Error in binary download response', err);
23+
});
24+
fileStream.on('error', function (err) {
25+
console.error('Got Error while downloading binary file', err);
26+
});
27+
fileStream.on('close', function () {
28+
console.log('Done');
29+
});
30+
}).on('error', function(err) {
31+
console.error('Got Error in binary downloading request', err);
32+
});

0 commit comments

Comments
 (0)