Skip to content

Commit

Permalink
fix: Update command does not work with proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
code2933 committed Oct 26, 2023
1 parent d3f0309 commit 526059e
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 60 deletions.
38 changes: 24 additions & 14 deletions lib/remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,39 @@ const path = require('path');
const fs = require('fs-extra');
const unzip = require('adm-zip');
const config = require('./config');
const axios = require('axios');
const fetch = require('node-fetch');
const { HttpsProxyAgent } = require('https-proxy-agent');
const { getProxyForUrl } = require('proxy-from-env');

// Downloads the zip file from github and extracts it to folder
exports.download = (loc, lang) => {
// If the lang is english then keep the url simple, otherwise add language.
const suffix = (lang === 'en' ? '' : '.' + lang);
const url = config.get().repositoryBase + suffix + '.zip';
const folderName = path.join(loc, 'pages' + suffix);
const REQUEST_TIMEOUT = 10000;

return axios({
method: 'get',
url: url,
responseType: 'stream',
headers: { 'User-Agent' : 'tldr-node-client' },
timeout: REQUEST_TIMEOUT,
}).then((response) => {

const headers = { 'User-Agent' : 'tldr-node-client' };
const fetchOptions = { headers };

const proxy = getProxyForUrl(url);
if (proxy) {
fetchOptions.agent = new HttpsProxyAgent(proxy, { headers });
}

return fetch(url, fetchOptions).then((response) => {
return new Promise((resolve, reject) => {
let fileName = path.join(loc, 'download_' + lang + '.zip');
if (!response.ok) {
reject(new Error(
`Fetch \`${url}\` failed, status = ${response.status}`
));
return;
}

const fileName = path.join(loc, 'download_' + lang + '.zip');

const writer = fs.createWriteStream(fileName);
response.data.pipe(writer);
response.body.pipe(writer);

writer.on('finish', () => {
writer.end();
const zip = new unzip(fileName);
Expand All @@ -41,4 +51,4 @@ exports.download = (loc, lang) => {
}).catch((err) => {
return Promise.reject(err);
});
};
};
163 changes: 119 additions & 44 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,18 @@
},
"dependencies": {
"adm-zip": "^0.5.10",
"axios": "^0.21.1",
"chalk": "^4.1.0",
"commander": "^6.1.0",
"fs-extra": "^9.0.1",
"glob": "^7.1.6",
"https-proxy-agent": "^7.0.2",
"lodash": "^4.17.20",
"marked": "^4.0.10",
"ms": "^2.1.2",
"natural": "^2.1.5",
"ora": "^5.1.0"
"node-fetch": "^2.7.0",
"ora": "^5.1.0",
"proxy-from-env": "^1.1.0"
},
"devDependencies": {
"eslint": "^8.39.0",
Expand Down

0 comments on commit 526059e

Please sign in to comment.