Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.

Commit 3bb51da

Browse files
CamilleDrapierCamille Drapierxzyfer
authored
Use make-fetch-happen instead of request (#3193)
Fixes #3206 Co-authored-by: Camille Drapier <[email protected]> Co-authored-by: Michael Mifsud <[email protected]>
1 parent adc2f8b commit 3bb51da

File tree

4 files changed

+20
-70
lines changed

4 files changed

+20
-70
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,10 @@
6060
"get-stdin": "^4.0.1",
6161
"glob": "^7.0.3",
6262
"lodash": "^4.17.15",
63+
"make-fetch-happen": "^9.1.0",
6364
"meow": "^9.0.0",
6465
"nan": "^2.13.2",
6566
"node-gyp": "^8.4.1",
66-
"npmlog": "^5.0.0",
67-
"request": "^2.88.0",
6867
"sass-graph": "4.0.0",
6968
"stdout-stream": "^1.4.0",
7069
"true-case-path": "^2.2.1"

scripts/install.js

Lines changed: 10 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
var fs = require('fs'),
66
eol = require('os').EOL,
77
path = require('path'),
8-
request = require('request'),
9-
log = require('npmlog'),
8+
fetch = require('make-fetch-happen'),
109
sass = require('../lib/extensions'),
1110
downloadOptions = require('./util/downloadoptions');
1211

@@ -21,21 +20,8 @@ var fs = require('fs'),
2120

2221
function download(url, dest, cb) {
2322
var reportError = function(err) {
24-
var timeoutMessge;
25-
26-
if (err.code === 'ETIMEDOUT') {
27-
if (err.connect === true) {
28-
// timeout is hit while your client is attempting to establish a connection to a remote machine
29-
timeoutMessge = 'Timed out attemping to establish a remote connection';
30-
} else {
31-
timeoutMessge = 'Timed out whilst downloading the prebuilt binary';
32-
// occurs any time the server is too slow to send back a part of the response
33-
}
34-
35-
}
3623
cb(['Cannot download "', url, '": ', eol, eol,
3724
typeof err.message === 'string' ? err.message : err, eol, eol,
38-
timeoutMessge ? timeoutMessge + eol + eol : timeoutMessge,
3925
'Hint: If github.com is not accessible in your location', eol,
4026
' try setting a proxy via HTTP_PROXY, e.g. ', eol, eol,
4127
' export HTTP_PROXY=http://example.com:1234',eol, eol,
@@ -44,45 +30,22 @@ function download(url, dest, cb) {
4430
};
4531

4632
var successful = function(response) {
47-
return response.statusCode >= 200 && response.statusCode < 300;
33+
return response.status >= 200 && response.status < 300;
4834
};
4935

5036
console.log('Downloading binary from', url);
5137

5238
try {
53-
request(url, downloadOptions(), function(err, response, buffer) {
54-
if (err) {
55-
reportError(err);
56-
} else if (!successful(response)) {
57-
reportError(['HTTP error', response.statusCode, response.statusMessage].join(' '));
39+
fetch(url, downloadOptions()).then(function (response) {
40+
fs.createWriteStream(dest).on('error', cb).end(response.data, cb);
41+
console.log('Download complete');
42+
}).catch(function(err) {
43+
if(!successful(err)) {
44+
reportError(['HTTP error', err.code, err.message].join(' '));
5845
} else {
59-
console.log('Download complete');
60-
61-
if (successful(response)) {
62-
fs.createWriteStream(dest)
63-
.on('error', cb)
64-
.end(buffer, cb);
65-
} else {
66-
cb();
67-
}
46+
reportError(err);
6847
}
69-
})
70-
.on('response', function(response) {
71-
var length = parseInt(response.headers['content-length'], 10);
72-
var progress = log.newItem('', length);
73-
74-
// The `progress` is true by default. However if it has not
75-
// been explicitly set it's `undefined` which is considered
76-
// as far as npm is concerned.
77-
if (process.env.npm_config_progress === 'true') {
78-
log.enableProgress();
79-
80-
response.on('data', function(chunk) {
81-
progress.completeWork(chunk.length);
82-
})
83-
.on('end', progress.finish);
84-
}
85-
});
48+
});
8649
} catch (err) {
8750
cb(err);
8851
}

scripts/util/downloadoptions.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,18 @@ var proxy = require('./proxy'),
33
rejectUnauthorized = require('./rejectUnauthorized');
44

55
/**
6-
* The options passed to request when downloading the bibary
6+
* The options passed to make-fetch-happen when downloading the binary
77
*
8-
* There some nuance to how request handles options. Specifically
9-
* we've been caught by their usage of `hasOwnProperty` rather than
10-
* falsey checks. By moving the options generation into a util helper
11-
* we can test for regressions.
12-
*
13-
* @return {Object} an options object for request
8+
* @return {Object} an options object for make-fetch-happen
149
* @api private
1510
*/
1611
module.exports = function() {
1712
var options = {
18-
rejectUnauthorized: rejectUnauthorized(),
13+
strictSSL: rejectUnauthorized(),
1914
timeout: 60000,
2015
headers: {
2116
'User-Agent': userAgent(),
2217
},
23-
encoding: null,
2418
};
2519

2620
var proxyConfig = proxy();

test/downloadoptions.js

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ describe('util', function() {
88
describe('without a proxy', function() {
99
it('should look as we expect', function() {
1010
var expected = {
11-
rejectUnauthorized: true,
11+
strictSSL: true,
1212
timeout: 60000,
1313
headers: {
1414
'User-Agent': ua(),
1515
},
16-
encoding: null,
1716
};
1817

1918
assert.deepStrictEqual(opts(), expected);
@@ -33,13 +32,12 @@ describe('util', function() {
3332

3433
it('should look as we expect', function() {
3534
var expected = {
36-
rejectUnauthorized: true,
35+
strictSSL: true,
3736
proxy: proxy,
3837
timeout: 60000,
3938
headers: {
4039
'User-Agent': ua(),
4140
},
42-
encoding: null,
4341
};
4442

4543
assert.deepStrictEqual(opts(), expected);
@@ -59,12 +57,11 @@ describe('util', function() {
5957

6058
it('should look as we expect', function() {
6159
var expected = {
62-
rejectUnauthorized: true,
60+
strictSSL: true,
6361
timeout: 60000,
6462
headers: {
6563
'User-Agent': ua(),
6664
},
67-
encoding: null,
6865
};
6966

7067
assert.deepStrictEqual(opts(), expected);
@@ -78,12 +75,11 @@ describe('util', function() {
7875

7976
it('should look as we expect', function() {
8077
var expected = {
81-
rejectUnauthorized: false,
78+
strictSSL: false,
8279
timeout: 60000,
8380
headers: {
8481
'User-Agent': ua(),
8582
},
86-
encoding: null,
8783
};
8884

8985
assert.deepStrictEqual(opts(), expected);
@@ -97,12 +93,11 @@ describe('util', function() {
9793

9894
it('should look as we expect', function() {
9995
var expected = {
100-
rejectUnauthorized: true,
96+
strictSSL: true,
10197
timeout: 60000,
10298
headers: {
10399
'User-Agent': ua(),
104100
},
105-
encoding: null,
106101
};
107102

108103
assert.deepStrictEqual(opts(), expected);
@@ -116,12 +111,11 @@ describe('util', function() {
116111

117112
it('should look as we expect', function() {
118113
var expected = {
119-
rejectUnauthorized: true,
114+
strictSSL: true,
120115
timeout: 60000,
121116
headers: {
122117
'User-Agent': ua(),
123118
},
124-
encoding: null,
125119
};
126120

127121
assert.deepStrictEqual(opts(), expected);

0 commit comments

Comments
 (0)