on master help sign in
try to use the 'request' package

This will impose some unfortunate costs: - extra download time - transitively needs "48 packages from 59 contributors" The benefit is that it will handle HTTPS_PROXY and NO_PROXY for people with corporate firewalls. Will this trade be worth it? TBD.

commit41ec49ed921a2409afda483eb9e29197e262fe27 authorEvan Czaplicki <evancz@users.noreply.github.com> dateFri 30 Aug 2019, 12:34 (7 years ago) parent435729f
2 files changed+20−60
installers/npm/download.js+19−60 installers/npm/package.json+1
installers/npm/download.js+19−60
@@ -1,7 +1,6 @@
11var fs = require('fs');
22var package = require('./package.json');
3−var URL = require('url').URL;
4−var https = require('https');
3+var request = require('request');
54var zlib = require('zlib');
65
76
@@ -28,67 +27,27 @@ module.exports = function(destinationPath, callback)
2827
2928function download(destinationPath, url, callback)
3029{
31− requestWithRedirects(url, 10,
32− function(error) {
33− exitFailure(url, 'I ran into trouble while fetching ' + url + '\n\n' + error);
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);
46− });
47−
48− 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);
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− }
30+ // set up handler for request failure
31+ function reportDownloadFailure(error)
32+ {
33+ exitFailure(url,'Something went wrong while fetching the following URL:\n\n' + url + '\n\nIt is saying:\n\n' + error);
34+ }
35+
36+ // set up decompression pipe
37+ var gunzip = zlib.createGunzip().on('error', function(error) {
38+ exitFailure(url, 'I ran into trouble decompressing the downloaded binary. It is saying:\n\n' + error);
8739 });
8840
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+ });
9048
91− req.end();
49+ // put it all together
50+ request(url).on('error', reportDownloadFailure).pipe(gunzip).pipe(write);
9251}
9352
9453
installers/npm/package.json+1
@@ -39,5 +39,6 @@
3939 "elm": "bin/elm.cmd"
4040 },
4141 "dependencies": {
42+ "request": "^2.88.0"
4243 }
4344}