aboutsummaryrefslogtreecommitdiffhomepage
path: root/lib/proxy.ts
blob: 6cc1550ce08444983eb208173981252acfc19a5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import is from '@sindresorhus/is';
import { createGlobalProxyAgent } from 'global-agent';
import { logger } from './logger';

const envVars = ['HTTP_PROXY', 'HTTPS_PROXY', 'NO_PROXY'];

let agent = false;

export function bootstrap(): void {
  envVars.forEach((envVar) => {
    /* istanbul ignore if: env is case-insensitive on windows */
    if (
      typeof process.env[envVar] === 'undefined' &&
      typeof process.env[envVar.toLowerCase()] !== 'undefined'
    ) {
      process.env[envVar] = process.env[envVar.toLowerCase()];
    }

    if (process.env[envVar]) {
      logger.debug(`Detected ${envVar} value in env`);
      process.env[envVar.toLowerCase()] = process.env[envVar];
    }
  });

  if (
    is.nonEmptyString(process.env.HTTP_PROXY) ||
    is.nonEmptyString(process.env.HTTPS_PROXY)
  ) {
    createGlobalProxyAgent({
      environmentVariableNamespace: '',
    });
    agent = true;
  } else {
    // for testing only, does not reset global agent
    agent = false;
  }
}

// will be used by our http layer later
export function hasProxy(): boolean {
  return agent === true;
}