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.HttpProxyAgent = void 0; 30const net = __importStar(require("net")); 31const tls = __importStar(require("tls")); 32const debug_1 = __importDefault(require("debug")); 33const events_1 = require("events"); 34const agent_base_1 = require("agent-base"); 35const url_1 = require("url"); 36const debug = (0, debug_1.default)('http-proxy-agent'); 37/** 38 * The `HttpProxyAgent` implements an HTTP Agent subclass that connects 39 * to the specified "HTTP proxy server" in order to proxy HTTP requests. 40 */ 41class HttpProxyAgent extends agent_base_1.Agent { 42 constructor(proxy, opts) { 43 super(opts); 44 this.proxy = typeof proxy === 'string' ? new url_1.URL(proxy) : proxy; 45 this.proxyHeaders = opts?.headers ?? {}; 46 debug('Creating new HttpProxyAgent instance: %o', this.proxy.href); 47 // Trim off the brackets from IPv6 addresses 48 const host = (this.proxy.hostname || this.proxy.host).replace(/^\[|\]$/g, ''); 49 const port = this.proxy.port 50 ? parseInt(this.proxy.port, 10) 51 : this.proxy.protocol === 'https:' 52 ? 443 53 : 80; 54 this.connectOpts = { 55 ...(opts ? omit(opts, 'headers') : null), 56 host, 57 port, 58 }; 59 } 60 addRequest(req, opts) { 61 req._header = null; 62 this.setRequestProps(req, opts); 63 // @ts-expect-error `addRequest()` isn't defined in `@types/node` 64 super.addRequest(req, opts); 65 } 66 setRequestProps(req, opts) { 67 const { proxy } = this; 68 const protocol = opts.secureEndpoint ? 'https:' : 'http:'; 69 const hostname = req.getHeader('host') || 'localhost'; 70 const base = `${protocol}//${hostname}`; 71 const url = new url_1.URL(req.path, base); 72 if (opts.port !== 80) { 73 url.port = String(opts.port); 74 } 75 // Change the `http.ClientRequest` instance's "path" field 76 // to the absolute path of the URL that will be requested. 77 req.path = String(url); 78 // Inject the `Proxy-Authorization` header if necessary. 79 const headers = typeof this.proxyHeaders === 'function' 80 ? this.proxyHeaders() 81 : { ...this.proxyHeaders }; 82 if (proxy.username || proxy.password) { 83 const auth = `${decodeURIComponent(proxy.username)}:${decodeURIComponent(proxy.password)}`; 84 headers['Proxy-Authorization'] = `Basic ${Buffer.from(auth).toString('base64')}`; 85 } 86 if (!headers['Proxy-Connection']) { 87 headers['Proxy-Connection'] = this.keepAlive 88 ? 'Keep-Alive' 89 : 'close'; 90 } 91 for (const name of Object.keys(headers)) { 92 const value = headers[name]; 93 if (value) { 94 req.setHeader(name, value); 95 } 96 } 97 } 98 async connect(req, opts) { 99 req._header = null; 100 if (!req.path.includes('://')) { 101 this.setRequestProps(req, opts); 102 } 103 // At this point, the http ClientRequest's internal `_header` field 104 // might have already been set. If this is the case then we'll need 105 // to re-generate the string since we just changed the `req.path`. 106 let first; 107 let endOfHeaders; 108 debug('Regenerating stored HTTP header string for request'); 109 req._implicitHeader(); 110 if (req.outputData && req.outputData.length > 0) { 111 debug('Patching connection write() output buffer with updated header'); 112 first = req.outputData[0].data; 113 endOfHeaders = first.indexOf('\r\n\r\n') + 4; 114 req.outputData[0].data = 115 req._header + first.substring(endOfHeaders); 116 debug('Output buffer: %o', req.outputData[0].data); 117 } 118 // Create a socket connection to the proxy server. 119 let socket; 120 if (this.proxy.protocol === 'https:') { 121 debug('Creating `tls.Socket`: %o', this.connectOpts); 122 socket = tls.connect(this.connectOpts); 123 } 124 else { 125 debug('Creating `net.Socket`: %o', this.connectOpts); 126 socket = net.connect(this.connectOpts); 127 } 128 // Wait for the socket's `connect` event, so that this `callback()` 129 // function throws instead of the `http` request machinery. This is 130 // important for i.e. `PacProxyAgent` which determines a failed proxy 131 // connection via the `callback()` function throwing. 132 await (0, events_1.once)(socket, 'connect'); 133 return socket; 134 } 135} 136HttpProxyAgent.protocols = ['http', 'https']; 137exports.HttpProxyAgent = HttpProxyAgent; 138function omit(obj, ...keys) { 139 const ret = {}; 140 let key; 141 for (key in obj) { 142 if (!keys.includes(key)) { 143 ret[key] = obj[key]; 144 } 145 } 146 return ret; 147} 148//# sourceMappingURL=index.js.map