Skip to content

Commit 4db7929

Browse files
authored
add proxy support for axios requests (#1623)
* fix: add proxy support for axios requests in ParserIncludes and Utils * refactor axios proxy configuration into its own method * remove brackets
1 parent d36010a commit 4db7929

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

src/parser-includes.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {GitData} from "./git-data.js";
66
import assert, {AssertionError} from "assert";
77
import chalk from "chalk";
88
import {Parser} from "./parser.js";
9-
import axios from "axios";
9+
import axios, {AxiosRequestConfig} from "axios";
1010
import path from "path";
1111
import semver from "semver";
1212
import {RE2JS} from "re2js";
@@ -287,7 +287,11 @@ export class ParserIncludes {
287287
try {
288288
const target = `${cwd}/${stateDir}/includes/${fsUrl}`;
289289
if (await fs.pathExists(target) && !fetchIncludes) return;
290-
const res = await axios.get(url, {headers: {"User-Agent": "gitlab-ci-local"}});
290+
const axiosConfig: AxiosRequestConfig = {
291+
headers: {"User-Agent": "gitlab-ci-local"},
292+
...Utils.getAxiosProxyConfig(),
293+
};
294+
const res = await axios.get(url, axiosConfig);
291295
await fs.outputFile(target, res.data);
292296
} catch (e) {
293297
throw new AssertionError({message: `Remote include could not be fetched ${url}\n${e}`});

src/utils.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {CICDVariable} from "./variables-from-files.js";
1111
import {GitData, GitSchema} from "./git-data.js";
1212
import globby from "globby";
1313
import micromatch from "micromatch";
14-
import axios from "axios";
14+
import axios, {AxiosRequestConfig} from "axios";
1515
import path from "path";
1616
import {Argv} from "./argv.js";
1717

@@ -418,7 +418,11 @@ export class Utils {
418418
case "http":
419419
case "https": {
420420
try {
421-
const {status} = await axios.get(`${protocol}://${domain}:${port}/${projectPath}/-/raw/${ref}/${file}`);
421+
const axiosConfig: AxiosRequestConfig = Utils.getAxiosProxyConfig();
422+
const {status} = await axios.get(
423+
`${protocol}://${domain}:${port}/${projectPath}/-/raw/${ref}/${file}`,
424+
axiosConfig,
425+
);
422426
return (status === 200);
423427
} catch {
424428
return false;
@@ -442,4 +446,19 @@ export class Utils {
442446
}
443447
return lsFilesRes.stdout.split("\n");
444448
}
449+
450+
static getAxiosProxyConfig (): AxiosRequestConfig {
451+
const proxyEnv = process.env.HTTPS_PROXY || process.env.HTTP_PROXY;
452+
if (proxyEnv) {
453+
const proxyUrl = new URL(proxyEnv);
454+
return {
455+
proxy: {
456+
host: proxyUrl.hostname,
457+
port: proxyUrl.port ? parseInt(proxyUrl.port, 10) : 8080,
458+
protocol: proxyUrl.protocol.replace(":", ""),
459+
},
460+
};
461+
}
462+
return {};
463+
}
445464
}

0 commit comments

Comments
 (0)