Skip to content

Commit

Permalink
fix(http): use proxy-agent that respect env variables and "no-proxy"-env
Browse files Browse the repository at this point in the history
Related to #762

In the linked PR, the proxy config was restored, but it didn't respected
the `NO_PROXY` | `no_proxy` configuration. This commit moved to a
different approach that is working with all common proxy environment
variables.
  • Loading branch information
jontze committed Oct 18, 2024
1 parent 6b61664 commit 57e947a
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions apps/generator-cli/src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Inject, Module, OnApplicationBootstrap } from '@nestjs/common';
import { HttpModule, HttpModuleOptions } from '@nestjs/axios';
import { Command } from 'commander';
import { ProxyAgent } from 'proxy-agent';

import { COMMANDER_PROGRAM, LOGGER } from './constants';
import { VersionManagerController } from './controllers/version-manager.controller';
Expand All @@ -11,14 +12,21 @@ import {
UIService,
VersionManagerService,
} from './services';
import { HttpsProxyAgent } from 'https-proxy-agent';

const proxyUrl = process.env.HTTP_PROXY || process.env.HTTPS_PROXY;
const hasHttpProxyEnvs = process.env.HTTP_PROXY || process.env.http_proxy;
const hasHttpsProxyEnvs = process.env.HTTPS_PROXY || process.env.https_proxy;
const httpModuleConfig: HttpModuleOptions = {};

if (proxyUrl) {
const proxyAgent = new ProxyAgent();

if (hasHttpProxyEnvs) {
httpModuleConfig.proxy = false;
httpModuleConfig.httpAgent = proxyAgent;
}

if (hasHttpsProxyEnvs) {
httpModuleConfig.proxy = false;
httpModuleConfig.httpsAgent = new HttpsProxyAgent(proxyUrl);
httpModuleConfig.httpsAgent = proxyAgent;
}

@Module({
Expand Down

0 comments on commit 57e947a

Please sign in to comment.