Skip to content

Commit f483430

Browse files
perf: ⚡️ replace request with axios (#314)
As I described in #234 `request` is slow and deprecated. This change replaces it with `axios` which is a better alternative. This also removes the proxy configuration because it wasn't even working (no `.get()` when setting the defaults) and `axios` supports `http_proxy` by default (I tested)
1 parent e0ac7ac commit f483430

File tree

4 files changed

+69
-8880
lines changed

4 files changed

+69
-8880
lines changed

lib/config.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ exports.get = () => {
1111

1212
let defaultConfig = JSON.parse(fs.readFileSync(DEFAULT));
1313
defaultConfig.cache = path.join(osHomedir(), '.tldr');
14-
/*eslint-disable no-process-env */
15-
defaultConfig.proxy = process.env.HTTP_PROXY || process.env.http_proxy;
16-
/*eslint-enable no-process-env */
1714

1815
let customConfig = {};
1916
try {

lib/remote.js

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,27 @@
11
'use strict';
22

33
const unzip = require('node-unzip-2');
4-
const config = require('./config');
5-
6-
let request = require('request');
4+
const config = require('./config');
5+
const axios = require('axios');
76

87
// Downloads the zip file from github and extracts it to folder
98
exports.download = (path) => {
10-
let url = config.get().repository;
9+
const url = config.get().repository;
1110

1211
// Creating the extractor
13-
let extractor = unzip.Extract({ path });
14-
15-
// Setting the proxy if set by config
16-
if (config.get().proxy) {
17-
request = request.defaults({ proxy: config.proxy });
18-
}
12+
const extractor = unzip.Extract({ path });
1913

20-
// Creating the request and passing the extractor
21-
let req = request.get({
14+
let req = axios({
15+
method: 'get',
2216
url: url,
17+
responseType: 'stream',
2318
headers: { 'User-Agent' : 'tldr-node-client' }
19+
}).then(function (response) {
20+
response.data.pipe(extractor);
2421
});
2522

26-
req.pipe(extractor);
27-
2823
return new Promise((resolve, reject) => {
29-
req.on('error', (err) => {
24+
req.catch((err) => {
3025
reject(err);
3126
});
3227
extractor.on('error', () => {

0 commit comments

Comments
 (0)