Skip to content

Commit

Permalink
feat: add no_proxy env variable (#361)
Browse files Browse the repository at this point in the history
* feat: add support for no_proxy environment variable

Co-authored-by: bcoe <bencoe@google.com>
Co-authored-by: Justin Beckwith <justin.beckwith@gmail.com>
  • Loading branch information
3 people committed Dec 8, 2020
1 parent 132568f commit efe72a7
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 12 deletions.
33 changes: 30 additions & 3 deletions src/gaxios.ts
Expand Up @@ -42,8 +42,6 @@ function hasFetch() {

let HttpsProxyAgent: any;

// Figure out if we should be using a proxy. Only if it's required, load
// the https-proxy-agent module as it adds startup cost.
function loadProxy() {
const proxy =
process.env.HTTPS_PROXY ||
Expand All @@ -57,6 +55,35 @@ function loadProxy() {
}
loadProxy();

function skipProxy(url: string) {
const noProxyEnv = process.env.NO_PROXY ?? process.env.no_proxy;
if (!noProxyEnv) {
return false;
}
const noProxyUrls = noProxyEnv.split(',');
const parsedURL = new URL(url);
return !!noProxyUrls.find(url => {
if (url.startsWith('*.') || url.startsWith('.')) {
url = url.replace('*', '');
return parsedURL.hostname.endsWith(url);
} else {
return url === parsedURL.origin || url === parsedURL.hostname;
}
});
}

// Figure out if we should be using a proxy. Only if it's required, load
// the https-proxy-agent module as it adds startup cost.
function getProxy(url: string) {
// If there is a match between the no_proxy env variables and the url, then do not proxy
if (skipProxy(url)) {
return undefined;
// If there is not a match between the no_proxy env variables and the url, check to see if there should be a proxy
} else {
return loadProxy();
}
}

export class Gaxios {
private agentCache = new Map<
string,
Expand Down Expand Up @@ -220,7 +247,7 @@ export class Gaxios {
}
opts.method = opts.method || 'GET';

const proxy = loadProxy();
const proxy = getProxy(opts.url);
if (proxy) {
if (this.agentCache.has(proxy)) {
opts.agent = this.agentCache.get(proxy);
Expand Down
119 changes: 110 additions & 9 deletions test/test.getch.ts
Expand Up @@ -250,14 +250,115 @@ describe('🥁 configuration options', () => {
assert.deepStrictEqual(res.data, {});
});

it('should use an https proxy if asked nicely', async () => {
sandbox.stub(process, 'env').value({https_proxy: 'https://fake.proxy'});
const body = {hello: '🌎'};
const scope = nock(url).get('/').reply(200, body);
const res = await request({url});
scope.done();
assert.deepStrictEqual(res.data, body);
assert.ok(res.config.agent instanceof HttpsProxyAgent);
describe('proxying', () => {
it('should use an https proxy if asked nicely', async () => {
const url = 'https://fake.proxy';
sandbox.stub(process, 'env').value({https_proxy: 'https://fake.proxy'});
const body = {hello: '🌎'};
const scope = nock(url).get('/').reply(200, body);
const res = await request({url});
scope.done();
assert.deepStrictEqual(res.data, body);
assert.ok(res.config.agent instanceof HttpsProxyAgent);
});

it('should not proxy when url matches no_proxy', async () => {
const url = 'https://example.com';
sandbox.stub(process, 'env').value({
https_proxy: 'https://fake.proxy',
no_proxy: 'https://example.com',
});
const body = {hello: '🌎'};
const scope = nock(url).get('/').reply(200, body);
const res = await request({url});
scope.done();
assert.deepStrictEqual(res.data, body);
assert.strictEqual(res.config.agent, undefined);
});

it('should proxy if url does not match no_proxy env variable', async () => {
const url = 'https://example2.com';
sandbox.stub(process, 'env').value({
https_proxy: 'https://fake.proxy',
no_proxy: 'https://example.com',
});
const body = {hello: '🌎'};
const scope = nock(url).get('/').reply(200, body);
const res = await request({url});
scope.done();
assert.deepStrictEqual(res.data, body);
assert.ok(res.config.agent instanceof HttpsProxyAgent);
});

it('should not proxy if no_proxy env var matches the origin or hostname of the URL', async () => {
const url = 'https://example2.com';
sandbox.stub(process, 'env').value({
https_proxy: 'https://fake.proxy',
no_proxy: 'example2.com',
});
const body = {hello: '🌎'};
const scope = nock(url).get('/').reply(200, body);
const res = await request({url});
scope.done();
assert.deepStrictEqual(res.data, body);
assert.strictEqual(res.config.agent, undefined);
});

it('should not proxy if no_proxy env variable has asterisk, and URL partially matches', async () => {
const url = 'https://domain.example.com';
sandbox.stub(process, 'env').value({
https_proxy: 'https://fake.proxy',
no_proxy: '*.example.com',
});
const body = {hello: '🌎'};
const scope = nock(url).get('/').reply(200, body);
const res = await request({url});
scope.done();
assert.deepStrictEqual(res.data, body);
assert.strictEqual(res.config.agent, undefined);
});

it('should proxy if no_proxy env variable has asterisk, but URL is not matching', async () => {
const url = 'https://domain.example2.com';
sandbox.stub(process, 'env').value({
https_proxy: 'https://fake.proxy',
no_proxy: '*.example.com',
});
const body = {hello: '🌎'};
const scope = nock(url).get('/').reply(200, body);
const res = await request({url});
scope.done();
assert.deepStrictEqual(res.data, body);
assert.ok(res.config.agent instanceof HttpsProxyAgent);
});

it('should not proxy if no_proxy env variable starts with a dot, and URL partially matches', async () => {
const url = 'https://domain.example.com';
sandbox.stub(process, 'env').value({
https_proxy: 'https://fake.proxy',
no_proxy: '.example.com',
});
const body = {hello: '🌎'};
const scope = nock(url).get('/').reply(200, body);
const res = await request({url});
scope.done();
assert.deepStrictEqual(res.data, body);
assert.strictEqual(res.config.agent, undefined);
});

it('should allow comma-separated lists for no_proxy env variables', async () => {
const url = 'https://api.google.com';
sandbox.stub(process, 'env').value({
https_proxy: 'https://fake.proxy',
no_proxy: 'example.com,*.google.com,hello.com',
});
const body = {hello: '🌎'};
const scope = nock(url).get('/').reply(200, body);
const res = await request({url});
scope.done();
assert.deepStrictEqual(res.data, body);
assert.strictEqual(res.config.agent, undefined);
});
});

it('should load the proxy from the cache', async () => {
Expand All @@ -267,7 +368,7 @@ describe('🥁 configuration options', () => {
const res1 = await request({url});
const agent = res1.config.agent;
const res2 = await request({url});
assert.strictEqual(agent, res2.config.agent);
assert.deepStrictEqual(agent, res2.config.agent);
scope.done();
});

Expand Down

0 comments on commit efe72a7

Please sign in to comment.