Skip to content

Commit 5131964

Browse files
gnestorlgeiger
authored andcommitted
Follow redirect in download-zmq.js
1 parent f637738 commit 5131964

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

download-zmq.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
11
var https = require('https');
22
var fs = require('fs');
3+
var url = require('url');
34

4-
var file = fs.createWriteStream('zeromq-' + process.env.ZMQ + '.tar.gz');
5-
var request = https.get('https://github.com/' + process.env.ZMQ_REPO + '/releases/download/v' + process.env.ZMQ + '/zeromq-' + process.env.ZMQ + '.tar.gz', function(response) {
6-
response.pipe(file);
5+
var TAR_URL = 'https://github.com/' + process.env.ZMQ_REPO + '/releases/download/v' + process.env.ZMQ + '/zeromq-' + process.env.ZMQ + '.tar.gz';
6+
var FILE_NAME = 'zeromq-' + process.env.ZMQ + '.tar.gz';
7+
8+
function writeToFile(response) {
9+
response.pipe(fs.createWriteStream(FILE_NAME));
10+
}
11+
12+
https.get(TAR_URL, function(response) {
13+
if (response.statusCode > 300 && response.statusCode < 400 && response.headers.location) {
14+
if (url.parse(response.headers.location).hostname) {
15+
https.get(response.headers.location, writeToFile);
16+
} else {
17+
https.get(url.resolve(url.parse(TAR_URL).hostname, response.headers.location), writeToFile);
18+
}
19+
} else {
20+
writeToFile(response);
21+
}
722
});

0 commit comments

Comments
 (0)