-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathdownload.js
54 lines (47 loc) · 1.6 KB
/
download.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const https = require('https'),
fs = require('fs'),
HttpsProxyAgent = require('https-proxy-agent'),
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];
var fileStream = fs.createWriteStream(binaryPath);
var options = url.parse(httpPath);
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);
}
}
}
options.headers = Object.assign({}, options.headers, {
'accept-encoding': 'gzip, *',
'user-agent': process.env.USER_AGENT,
});
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);
}
response.on('error', function(err) {
console.error('Got Error in binary download response', err);
});
fileStream.on('error', function (err) {
console.error('Got Error while downloading binary file', err);
});
fileStream.on('close', function () {
console.log('Done');
});
}).on('error', function(err) {
console.error('Got Error in binary downloading request', err);
});