UNPKG

1.06 kBJavaScriptView Raw
1// Copyright 2013 Lovell Fuller and others.
2// SPDX-License-Identifier: Apache-2.0
3
4'use strict';
5
6const url = require('url');
7const tunnelAgent = require('tunnel-agent');
8
9const is = require('./is');
10
11const proxies = [
12 'HTTPS_PROXY',
13 'https_proxy',
14 'HTTP_PROXY',
15 'http_proxy',
16 'npm_config_https_proxy',
17 'npm_config_proxy'
18];
19
20function env (key) {
21 return process.env[key];
22}
23
24module.exports = function (log) {
25 try {
26 const proxy = new url.URL(proxies.map(env).find(is.string));
27 const tunnel = proxy.protocol === 'https:'
28 ? tunnelAgent.httpsOverHttps
29 : tunnelAgent.httpsOverHttp;
30 const proxyAuth = proxy.username && proxy.password
31 ? `${decodeURIComponent(proxy.username)}:${decodeURIComponent(proxy.password)}`
32 : null;
33 log(`Via proxy ${proxy.protocol}//${proxy.hostname}:${proxy.port} ${proxyAuth ? 'with' : 'no'} credentials`);
34 return tunnel({
35 proxy: {
36 port: Number(proxy.port),
37 host: proxy.hostname,
38 proxyAuth
39 }
40 });
41 } catch (err) {
42 return null;
43 }
44};