UNPKG

4.84 kBJavaScriptView Raw
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 var desc = Object.getOwnPropertyDescriptor(m, k);
5 if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6 desc = { enumerable: true, get: function() { return m[k]; } };
7 }
8 Object.defineProperty(o, k2, desc);
9}) : (function(o, m, k, k2) {
10 if (k2 === undefined) k2 = k;
11 o[k2] = m[k];
12}));
13var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14 Object.defineProperty(o, "default", { enumerable: true, value: v });
15}) : function(o, v) {
16 o["default"] = v;
17});
18var __importStar = (this && this.__importStar) || function (mod) {
19 if (mod && mod.__esModule) return mod;
20 var result = {};
21 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22 __setModuleDefault(result, mod);
23 return result;
24};
25var __importDefault = (this && this.__importDefault) || function (mod) {
26 return (mod && mod.__esModule) ? mod : { "default": mod };
27};
28Object.defineProperty(exports, "__esModule", { value: true });
29exports.ProxyAgent = exports.proxies = void 0;
30const http = __importStar(require("http"));
31const https = __importStar(require("https"));
32const lru_cache_1 = __importDefault(require("lru-cache"));
33const agent_base_1 = require("agent-base");
34const debug_1 = __importDefault(require("debug"));
35const proxy_from_env_1 = require("proxy-from-env");
36const pac_proxy_agent_1 = require("pac-proxy-agent");
37const http_proxy_agent_1 = require("http-proxy-agent");
38const https_proxy_agent_1 = require("https-proxy-agent");
39const socks_proxy_agent_1 = require("socks-proxy-agent");
40const debug = (0, debug_1.default)('proxy-agent');
41const PROTOCOLS = [
42 ...http_proxy_agent_1.HttpProxyAgent.protocols,
43 ...socks_proxy_agent_1.SocksProxyAgent.protocols,
44 ...pac_proxy_agent_1.PacProxyAgent.protocols,
45];
46/**
47 * Supported proxy types.
48 */
49exports.proxies = {
50 http: http_proxy_agent_1.HttpProxyAgent,
51 https: https_proxy_agent_1.HttpsProxyAgent,
52 socks: socks_proxy_agent_1.SocksProxyAgent,
53 socks4: socks_proxy_agent_1.SocksProxyAgent,
54 socks4a: socks_proxy_agent_1.SocksProxyAgent,
55 socks5: socks_proxy_agent_1.SocksProxyAgent,
56 socks5h: socks_proxy_agent_1.SocksProxyAgent,
57 'pac-data': pac_proxy_agent_1.PacProxyAgent,
58 'pac-file': pac_proxy_agent_1.PacProxyAgent,
59 'pac-ftp': pac_proxy_agent_1.PacProxyAgent,
60 'pac-http': pac_proxy_agent_1.PacProxyAgent,
61 'pac-https': pac_proxy_agent_1.PacProxyAgent,
62};
63function isValidProtocol(v) {
64 return PROTOCOLS.includes(v);
65}
66/**
67 * Uses the appropriate `Agent` subclass based off of the "proxy"
68 * environment variables that are currently set.
69 *
70 * An LRU cache is used, to prevent unnecessary creation of proxy
71 * `http.Agent` instances.
72 */
73class ProxyAgent extends agent_base_1.Agent {
74 constructor(opts) {
75 super(opts);
76 /**
77 * Cache for `Agent` instances.
78 */
79 this.cache = new lru_cache_1.default({ max: 20 });
80 debug('Creating new ProxyAgent instance: %o', opts);
81 this.connectOpts = opts;
82 this.httpAgent = opts?.httpAgent || new http.Agent(opts);
83 this.httpsAgent =
84 opts?.httpsAgent || new https.Agent(opts);
85 }
86 async connect(req, opts) {
87 const protocol = opts.secureEndpoint ? 'https:' : 'http:';
88 const host = req.getHeader('host');
89 const url = new URL(req.path, `${protocol}//${host}`).href;
90 const proxy = (0, proxy_from_env_1.getProxyForUrl)(url);
91 if (!proxy) {
92 debug('Proxy not enabled for URL: %o', url);
93 return opts.secureEndpoint ? this.httpsAgent : this.httpAgent;
94 }
95 debug('Request URL: %o', url);
96 debug('Proxy URL: %o', proxy);
97 // attempt to get a cached `http.Agent` instance first
98 let agent = this.cache.get(proxy);
99 if (!agent) {
100 const proxyUrl = new URL(proxy);
101 const proxyProto = proxyUrl.protocol.replace(':', '');
102 if (!isValidProtocol(proxyProto)) {
103 throw new Error(`Unsupported protocol for proxy URL: ${proxy}`);
104 }
105 const ctor = exports.proxies[proxyProto];
106 // @ts-expect-error meh…
107 agent = new ctor(proxy, this.connectOpts);
108 this.cache.set(proxy, agent);
109 }
110 else {
111 debug('Cache hit for proxy URL: %o', proxy);
112 }
113 return agent;
114 }
115 destroy() {
116 for (const agent of this.cache.values()) {
117 agent.destroy();
118 }
119 super.destroy();
120 }
121}
122exports.ProxyAgent = ProxyAgent;
123//# sourceMappingURL=index.js.map
\No newline at end of file