11cb0ef41Sopenharmony_ci"use strict"; 21cb0ef41Sopenharmony_civar __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { 31cb0ef41Sopenharmony_ci if (k2 === undefined) k2 = k; 41cb0ef41Sopenharmony_ci var desc = Object.getOwnPropertyDescriptor(m, k); 51cb0ef41Sopenharmony_ci if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { 61cb0ef41Sopenharmony_ci desc = { enumerable: true, get: function() { return m[k]; } }; 71cb0ef41Sopenharmony_ci } 81cb0ef41Sopenharmony_ci Object.defineProperty(o, k2, desc); 91cb0ef41Sopenharmony_ci}) : (function(o, m, k, k2) { 101cb0ef41Sopenharmony_ci if (k2 === undefined) k2 = k; 111cb0ef41Sopenharmony_ci o[k2] = m[k]; 121cb0ef41Sopenharmony_ci})); 131cb0ef41Sopenharmony_civar __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { 141cb0ef41Sopenharmony_ci Object.defineProperty(o, "default", { enumerable: true, value: v }); 151cb0ef41Sopenharmony_ci}) : function(o, v) { 161cb0ef41Sopenharmony_ci o["default"] = v; 171cb0ef41Sopenharmony_ci}); 181cb0ef41Sopenharmony_civar __importStar = (this && this.__importStar) || function (mod) { 191cb0ef41Sopenharmony_ci if (mod && mod.__esModule) return mod; 201cb0ef41Sopenharmony_ci var result = {}; 211cb0ef41Sopenharmony_ci if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); 221cb0ef41Sopenharmony_ci __setModuleDefault(result, mod); 231cb0ef41Sopenharmony_ci return result; 241cb0ef41Sopenharmony_ci}; 251cb0ef41Sopenharmony_civar __importDefault = (this && this.__importDefault) || function (mod) { 261cb0ef41Sopenharmony_ci return (mod && mod.__esModule) ? mod : { "default": mod }; 271cb0ef41Sopenharmony_ci}; 281cb0ef41Sopenharmony_ciObject.defineProperty(exports, "__esModule", { value: true }); 291cb0ef41Sopenharmony_ciexports.HttpProxyAgent = void 0; 301cb0ef41Sopenharmony_ciconst net = __importStar(require("net")); 311cb0ef41Sopenharmony_ciconst tls = __importStar(require("tls")); 321cb0ef41Sopenharmony_ciconst debug_1 = __importDefault(require("debug")); 331cb0ef41Sopenharmony_ciconst events_1 = require("events"); 341cb0ef41Sopenharmony_ciconst agent_base_1 = require("agent-base"); 351cb0ef41Sopenharmony_ciconst url_1 = require("url"); 361cb0ef41Sopenharmony_ciconst debug = (0, debug_1.default)('http-proxy-agent'); 371cb0ef41Sopenharmony_ci/** 381cb0ef41Sopenharmony_ci * The `HttpProxyAgent` implements an HTTP Agent subclass that connects 391cb0ef41Sopenharmony_ci * to the specified "HTTP proxy server" in order to proxy HTTP requests. 401cb0ef41Sopenharmony_ci */ 411cb0ef41Sopenharmony_ciclass HttpProxyAgent extends agent_base_1.Agent { 421cb0ef41Sopenharmony_ci constructor(proxy, opts) { 431cb0ef41Sopenharmony_ci super(opts); 441cb0ef41Sopenharmony_ci this.proxy = typeof proxy === 'string' ? new url_1.URL(proxy) : proxy; 451cb0ef41Sopenharmony_ci this.proxyHeaders = opts?.headers ?? {}; 461cb0ef41Sopenharmony_ci debug('Creating new HttpProxyAgent instance: %o', this.proxy.href); 471cb0ef41Sopenharmony_ci // Trim off the brackets from IPv6 addresses 481cb0ef41Sopenharmony_ci const host = (this.proxy.hostname || this.proxy.host).replace(/^\[|\]$/g, ''); 491cb0ef41Sopenharmony_ci const port = this.proxy.port 501cb0ef41Sopenharmony_ci ? parseInt(this.proxy.port, 10) 511cb0ef41Sopenharmony_ci : this.proxy.protocol === 'https:' 521cb0ef41Sopenharmony_ci ? 443 531cb0ef41Sopenharmony_ci : 80; 541cb0ef41Sopenharmony_ci this.connectOpts = { 551cb0ef41Sopenharmony_ci ...(opts ? omit(opts, 'headers') : null), 561cb0ef41Sopenharmony_ci host, 571cb0ef41Sopenharmony_ci port, 581cb0ef41Sopenharmony_ci }; 591cb0ef41Sopenharmony_ci } 601cb0ef41Sopenharmony_ci addRequest(req, opts) { 611cb0ef41Sopenharmony_ci req._header = null; 621cb0ef41Sopenharmony_ci this.setRequestProps(req, opts); 631cb0ef41Sopenharmony_ci // @ts-expect-error `addRequest()` isn't defined in `@types/node` 641cb0ef41Sopenharmony_ci super.addRequest(req, opts); 651cb0ef41Sopenharmony_ci } 661cb0ef41Sopenharmony_ci setRequestProps(req, opts) { 671cb0ef41Sopenharmony_ci const { proxy } = this; 681cb0ef41Sopenharmony_ci const protocol = opts.secureEndpoint ? 'https:' : 'http:'; 691cb0ef41Sopenharmony_ci const hostname = req.getHeader('host') || 'localhost'; 701cb0ef41Sopenharmony_ci const base = `${protocol}//${hostname}`; 711cb0ef41Sopenharmony_ci const url = new url_1.URL(req.path, base); 721cb0ef41Sopenharmony_ci if (opts.port !== 80) { 731cb0ef41Sopenharmony_ci url.port = String(opts.port); 741cb0ef41Sopenharmony_ci } 751cb0ef41Sopenharmony_ci // Change the `http.ClientRequest` instance's "path" field 761cb0ef41Sopenharmony_ci // to the absolute path of the URL that will be requested. 771cb0ef41Sopenharmony_ci req.path = String(url); 781cb0ef41Sopenharmony_ci // Inject the `Proxy-Authorization` header if necessary. 791cb0ef41Sopenharmony_ci const headers = typeof this.proxyHeaders === 'function' 801cb0ef41Sopenharmony_ci ? this.proxyHeaders() 811cb0ef41Sopenharmony_ci : { ...this.proxyHeaders }; 821cb0ef41Sopenharmony_ci if (proxy.username || proxy.password) { 831cb0ef41Sopenharmony_ci const auth = `${decodeURIComponent(proxy.username)}:${decodeURIComponent(proxy.password)}`; 841cb0ef41Sopenharmony_ci headers['Proxy-Authorization'] = `Basic ${Buffer.from(auth).toString('base64')}`; 851cb0ef41Sopenharmony_ci } 861cb0ef41Sopenharmony_ci if (!headers['Proxy-Connection']) { 871cb0ef41Sopenharmony_ci headers['Proxy-Connection'] = this.keepAlive 881cb0ef41Sopenharmony_ci ? 'Keep-Alive' 891cb0ef41Sopenharmony_ci : 'close'; 901cb0ef41Sopenharmony_ci } 911cb0ef41Sopenharmony_ci for (const name of Object.keys(headers)) { 921cb0ef41Sopenharmony_ci const value = headers[name]; 931cb0ef41Sopenharmony_ci if (value) { 941cb0ef41Sopenharmony_ci req.setHeader(name, value); 951cb0ef41Sopenharmony_ci } 961cb0ef41Sopenharmony_ci } 971cb0ef41Sopenharmony_ci } 981cb0ef41Sopenharmony_ci async connect(req, opts) { 991cb0ef41Sopenharmony_ci req._header = null; 1001cb0ef41Sopenharmony_ci if (!req.path.includes('://')) { 1011cb0ef41Sopenharmony_ci this.setRequestProps(req, opts); 1021cb0ef41Sopenharmony_ci } 1031cb0ef41Sopenharmony_ci // At this point, the http ClientRequest's internal `_header` field 1041cb0ef41Sopenharmony_ci // might have already been set. If this is the case then we'll need 1051cb0ef41Sopenharmony_ci // to re-generate the string since we just changed the `req.path`. 1061cb0ef41Sopenharmony_ci let first; 1071cb0ef41Sopenharmony_ci let endOfHeaders; 1081cb0ef41Sopenharmony_ci debug('Regenerating stored HTTP header string for request'); 1091cb0ef41Sopenharmony_ci req._implicitHeader(); 1101cb0ef41Sopenharmony_ci if (req.outputData && req.outputData.length > 0) { 1111cb0ef41Sopenharmony_ci debug('Patching connection write() output buffer with updated header'); 1121cb0ef41Sopenharmony_ci first = req.outputData[0].data; 1131cb0ef41Sopenharmony_ci endOfHeaders = first.indexOf('\r\n\r\n') + 4; 1141cb0ef41Sopenharmony_ci req.outputData[0].data = 1151cb0ef41Sopenharmony_ci req._header + first.substring(endOfHeaders); 1161cb0ef41Sopenharmony_ci debug('Output buffer: %o', req.outputData[0].data); 1171cb0ef41Sopenharmony_ci } 1181cb0ef41Sopenharmony_ci // Create a socket connection to the proxy server. 1191cb0ef41Sopenharmony_ci let socket; 1201cb0ef41Sopenharmony_ci if (this.proxy.protocol === 'https:') { 1211cb0ef41Sopenharmony_ci debug('Creating `tls.Socket`: %o', this.connectOpts); 1221cb0ef41Sopenharmony_ci socket = tls.connect(this.connectOpts); 1231cb0ef41Sopenharmony_ci } 1241cb0ef41Sopenharmony_ci else { 1251cb0ef41Sopenharmony_ci debug('Creating `net.Socket`: %o', this.connectOpts); 1261cb0ef41Sopenharmony_ci socket = net.connect(this.connectOpts); 1271cb0ef41Sopenharmony_ci } 1281cb0ef41Sopenharmony_ci // Wait for the socket's `connect` event, so that this `callback()` 1291cb0ef41Sopenharmony_ci // function throws instead of the `http` request machinery. This is 1301cb0ef41Sopenharmony_ci // important for i.e. `PacProxyAgent` which determines a failed proxy 1311cb0ef41Sopenharmony_ci // connection via the `callback()` function throwing. 1321cb0ef41Sopenharmony_ci await (0, events_1.once)(socket, 'connect'); 1331cb0ef41Sopenharmony_ci return socket; 1341cb0ef41Sopenharmony_ci } 1351cb0ef41Sopenharmony_ci} 1361cb0ef41Sopenharmony_ciHttpProxyAgent.protocols = ['http', 'https']; 1371cb0ef41Sopenharmony_ciexports.HttpProxyAgent = HttpProxyAgent; 1381cb0ef41Sopenharmony_cifunction omit(obj, ...keys) { 1391cb0ef41Sopenharmony_ci const ret = {}; 1401cb0ef41Sopenharmony_ci let key; 1411cb0ef41Sopenharmony_ci for (key in obj) { 1421cb0ef41Sopenharmony_ci if (!keys.includes(key)) { 1431cb0ef41Sopenharmony_ci ret[key] = obj[key]; 1441cb0ef41Sopenharmony_ci } 1451cb0ef41Sopenharmony_ci } 1461cb0ef41Sopenharmony_ci return ret; 1471cb0ef41Sopenharmony_ci} 1481cb0ef41Sopenharmony_ci//# sourceMappingURL=index.js.map