Make extension respect http proxy settings

This commit is contained in:
Kam Y. Tse 2021-03-07 17:55:43 +08:00
parent f0b7c02f16
commit 77b7c96aea
No known key found for this signature in database
GPG key ID: FD2B87915048AB4F
5 changed files with 42 additions and 15 deletions

View file

@ -1,4 +1,6 @@
import fetch from "node-fetch";
var HttpsProxyAgent = require('https-proxy-agent');
import * as vscode from "vscode";
import * as stream from "stream";
import * as crypto from "crypto";
@ -17,6 +19,7 @@ const REPO = "rust-analyzer";
export async function fetchRelease(
releaseTag: string,
githubToken: string | null | undefined,
httpProxy: string | null | undefined,
): Promise<GithubRelease> {
const apiEndpointPath = `/repos/${OWNER}/${REPO}/releases/tags/${releaseTag}`;
@ -30,7 +33,14 @@ export async function fetchRelease(
headers.Authorization = "token " + githubToken;
}
const response = await fetch(requestUrl, { headers: headers });
const response = await (() => {
if (httpProxy) {
log.debug(`Fetching release metadata via proxy: ${httpProxy}`);
return fetch(requestUrl, { headers: headers, agent: new HttpsProxyAgent(httpProxy) });
}
return fetch(requestUrl, { headers: headers });
})();
if (!response.ok) {
log.error("Error fetching artifact release info", {
@ -73,6 +83,7 @@ interface DownloadOpts {
dest: string;
mode?: number;
gunzip?: boolean;
httpProxy?: string;
}
export async function download(opts: DownloadOpts) {
@ -91,7 +102,7 @@ export async function download(opts: DownloadOpts) {
},
async (progress, _cancellationToken) => {
let lastPercentage = 0;
await downloadFile(opts.url, tempFile, opts.mode, !!opts.gunzip, (readBytes, totalBytes) => {
await downloadFile(opts.url, tempFile, opts.mode, !!opts.gunzip, opts.httpProxy, (readBytes, totalBytes) => {
const newPercentage = Math.round((readBytes / totalBytes) * 100);
if (newPercentage !== lastPercentage) {
progress.report({
@ -113,9 +124,17 @@ async function downloadFile(
destFilePath: fs.PathLike,
mode: number | undefined,
gunzip: boolean,
httpProxy: string | null | undefined,
onProgress: (readBytes: number, totalBytes: number) => void
): Promise<void> {
const res = await fetch(url);
const res = await (() => {
if (httpProxy) {
log.debug(`Downloading ${url} via proxy: ${httpProxy}`);
return fetch(url, { agent: new HttpsProxyAgent(httpProxy) });
}
return fetch(url);
})();
if (!res.ok) {
log.error("Error", res.status, "while downloading file from", url);