Skip to content

Commit d94d743

Browse files
himself65codebytere
authored andcommitted
feat: support system proxy (nodejs#408)
1 parent c2a22ec commit d94d743

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

lib/proxy.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const ProxyAgent = require('proxy-agent');
2+
const { globalAgent } = require('https');
3+
const { spawnSync } = require('child_process');
4+
const { getMergedConfig } = require('./config');
5+
6+
function proxy() {
7+
let proxyUrl = getMergedConfig().proxy;
8+
if (proxyUrl == null || proxyUrl === '') {
9+
proxyUrl = spawnSync(
10+
'git',
11+
['config', '--get', '--path', 'https.proxy']
12+
).stdout.toString();
13+
}
14+
if (proxyUrl == null || proxyUrl === '') {
15+
return globalAgent;
16+
} else {
17+
return new ProxyAgent(proxyUrl);
18+
}
19+
}
20+
21+
module.exports = proxy;

lib/request.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ const fetch = require('node-fetch');
44
const fs = require('fs');
55
const path = require('path');
66
const { CI_DOMAIN } = require('./ci/ci_type_parser');
7+
const proxy = require('./proxy');
78

89
class Request {
910
constructor(credentials) {
1011
this.credentials = credentials;
12+
this.proxyAgent = proxy();
1113
}
1214

1315
loadQuery(file) {
@@ -16,6 +18,7 @@ class Request {
1618
}
1719

1820
async text(url, options = {}) {
21+
options.agent = this.proxyAgent;
1922
if (url.startsWith(`https://${CI_DOMAIN}`)) {
2023
options.headers = options.headers || {};
2124
Object.assign(options.headers, this.getJenkinsHeaders());
@@ -24,6 +27,7 @@ class Request {
2427
}
2528

2629
async json(url, options = {}) {
30+
options.agent = this.proxyAgent;
2731
options.headers = options.headers || {};
2832
options.headers.Accept = 'application/json';
2933
if (url.startsWith(`https://${CI_DOMAIN}`)) {
@@ -65,6 +69,7 @@ class Request {
6569
}
6670
const url = 'https://api.github.com/graphql';
6771
const options = {
72+
agent: this.proxyAgent,
6873
method: 'POST',
6974
headers: {
7075
Authorization: `Basic ${githubCredentials}`,

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"lodash": "^4.17.15",
4848
"node-fetch": "^2.6.0",
4949
"ora": "^4.0.4",
50+
"proxy-agent": "^3.1.1",
5051
"replace-in-file": "^6.0.0",
5152
"rimraf": "^3.0.2",
5253
"yargs": "^15.3.1"

0 commit comments

Comments
 (0)