on master help sign in
try to use the 'request' package
41ec49eEvan Czaplicki
installers/npm/download.js+19−60
@@ -1,7 +1,6 @@
1var fs = require('fs');
1var fs = require('fs');
2var package = require('./package.json');
2var package = require('./package.json');
3−var URL = require('url').URL;
3+var request = require('request');
4−var https = require('https');
5var zlib = require('zlib');
4var zlib = require('zlib');
6
5
7
6
@@ -28,67 +27,27 @@ module.exports = function(destinationPath, callback)
28
27
29function download(destinationPath, url, callback)
28function download(destinationPath, url, callback)
30{
29{
31− requestWithRedirects(url, 10,
30+ // set up handler for request failure
32− function(error) {
31+ function reportDownloadFailure(error)
33− exitFailure(url, 'I ran into trouble while fetching ' + url + '\n\n' + error);
32+ {
34− },
35− function(response) {
36− if (response.statusCode == 404)
37− {
38− exitFailure(url, 'I got a "404 Not Found" trying to download from the following URL:\n'
39− + url
40− + '\n\nThis may mean you are trying to install on an unsupported platform. (e.g. some 32-bit systems?)'
41− );
42− }
43−
44− response.on('error', function() {
45− exitFailure(url, 'Something went wrong while receiving the following URL:\n\n' + url);
33+ exitFailure(url,'Something went wrong while fetching the following URL:\n\n' + url + '\n\nIt is saying:\n\n' + error);
46− });
34+ }
47−
35+
36+ // set up decompression pipe
48− var gunzip = zlib.createGunzip().on('error', function(error) {
37+ var gunzip = zlib.createGunzip().on('error', function(error) {
49− exitFailure(url, 'I ran into trouble decompressing the downloaded binary. It is saying:\n\n' + error);
38+ exitFailure(url, 'I ran into trouble decompressing the downloaded binary. It is saying:\n\n' + error);
50− });
51− var write = fs.createWriteStream(destinationPath, {
52− encoding: 'binary',
53− mode: 0o755
54− }).on('finish', callback).on('error', function(error) {
55− exitFailure(url, 'I had some trouble writing file to disk. It is saying:\n\n' + error);
56− });
57−
58− response.pipe(gunzip).pipe(write);
59− }
60− );
61−}
62−
63−
64−
65−// HANDLE REDIRECTS
66−
67−
68−function requestWithRedirects(url, maxRedirects, failure, success)
69−{
70− var req = https.request(url);
71−
72− req.on('response', function(response) {
73− // Check for redirects
74−
75− var redirectUrl = new URL(response.headers['location'], new URL(url).origin).href;
76−
77− if (typeof redirectUrl === 'string' && response.statusCode >= 300 && response.statusCode < 400)
78− {
79− return (maxRedirects <= 0)
80− ? failure(new Error('followed 10 redirects and gave up'))
81− : requestWithRedirects(redirectUrl, maxRedirects - 1, failure, success);
82− }
83− else
84− {
85− return success(response);
86− }
87 });
39 });
88
40
89− req.on('error', failure);
41+ // set up file write pipe
42+ var write = fs.createWriteStream(destinationPath, {
43+ encoding: 'binary',
44+ mode: 0o755
45+ }).on('finish', callback).on('error', function(error) {
46+ exitFailure(url, 'I had some trouble writing file to disk. It is saying:\n\n' + error);
47+ });
90
48
91− req.end();
49+ // put it all together
50+ request(url).on('error', reportDownloadFailure).pipe(gunzip).pipe(write);
92}
51}
93
52
94
53
installers/npm/package.json+1
@@ -39,5 +39,6 @@
39 "elm": "bin/elm.cmd"
39 "elm": "bin/elm.cmd"
40 },
40 },
41 "dependencies": {
41 "dependencies": {
42+ "request": "^2.88.0"
42 }
43 }
43}
44}