// @bun
import{gj as v}from"./chunk-3vry2v18.js";import{hj as m}from"./chunk-tvj79kyp.js";import{MG as P,PG as U}from"./chunk-xyhgvt88.js";var j=P((Z)=>{var i=Z&&Z.__importDefault||function(w){return w&&w.__esModule?w:{default:w}};Object.defineProperty(Z,"__esModule",{value:!0});Z.parseProxyResponse=void 0;var x=i(v()),N=(0,x.default)("https-proxy-agent:parse-proxy-response");function u(w){return new Promise((z,E)=>{let G=0,J=[];function W(){let V=w.read();if(V)M(V);else w.once("readable",W)}function X(){w.removeListener("end",F),w.removeListener("error",Y),w.removeListener("readable",W)}function F(){X(),N("onend"),E(Error("Proxy connection ended before receiving CONNECT response"))}function Y(V){X(),N("onerror %o",V),E(V)}function M(V){J.push(V),G+=V.length;let K=Buffer.concat(J,G),q=K.indexOf(`\r
\r
`);if(q===-1){N("have not received end of HTTP headers yet..."),W();return}let H=K.slice(0,q).toString("ascii").split(`\r
`),A=H.shift();if(!A)return w.destroy(),E(Error("No header received from proxy CONNECT response"));let L=A.split(" "),f=+L[1],b=L.slice(2).join(" "),$={};for(let B of H){if(!B)continue;let C=B.indexOf(":");if(C===-1)return w.destroy(),E(Error(`Invalid header from proxy CONNECT response: "${B}"`));let D=B.slice(0,C).toLowerCase(),R=B.slice(C+1).trimStart(),I=$[D];if(typeof I==="string")$[D]=[I,R];else if(Array.isArray(I))I.push(R);else $[D]=R}N("got proxy server response: %o %o",A,$),X(),z({connect:{statusCode:f,statusText:b,headers:$},buffered:K})}w.on("error",Y),w.on("end",F),W()})}Z.parseProxyResponse=u});var s=P((Q)=>{var c=Q&&Q.__createBinding||(Object.create?function(w,z,E,G){if(G===void 0)G=E;var J=Object.getOwnPropertyDescriptor(z,E);if(!J||("get"in J?!z.__esModule:J.writable||J.configurable))J={enumerable:!0,get:function(){return z[E]}};Object.defineProperty(w,G,J)}:function(w,z,E,G){if(G===void 0)G=E;w[G]=z[E]}),k=Q&&Q.__setModuleDefault||(Object.create?function(w,z){Object.defineProperty(w,"default",{enumerable:!0,value:z})}:function(w,z){w.default=z}),l=Q&&Q.__importStar||function(w){if(w&&w.__esModule)return w;var z={};if(w!=null){for(var E in w)if(E!=="default"&&Object.prototype.hasOwnProperty.call(w,E))c(z,w,E)}return k(z,w),z},y=Q&&Q.__importDefault||function(w){return w&&w.__esModule?w:{default:w}};Object.defineProperty(Q,"__esModule",{value:!0});Q.HttpsProxyAgent=void 0;var O=l(U("net")),_=l(U("tls")),a=y(U("assert")),n=y(v()),d=m(),p=U("url"),r=j(),T=(0,n.default)("https-proxy-agent"),g=(w)=>{if(w.servername===void 0&&w.host&&!O.isIP(w.host))return{...w,servername:w.host};return w};class S extends d.Agent{constructor(w,z){super(z);this.options={path:void 0},this.proxy=typeof w==="string"?new p.URL(w):w,this.proxyHeaders=z?.headers??{},T("Creating new HttpsProxyAgent instance: %o",this.proxy.href);let E=(this.proxy.hostname||this.proxy.host).replace(/^\[|\]$/g,""),G=this.proxy.port?parseInt(this.proxy.port,10):this.proxy.protocol==="https:"?443:80;this.connectOpts={ALPNProtocols:["http/1.1"],...z?h(z,"headers"):null,host:E,port:G}}async connect(w,z){let{proxy:E}=this;if(!z.host)throw TypeError('No "host" provided');let G;if(E.protocol==="https:")T("Creating `tls.Socket`: %o",this.connectOpts),G=_.connect(g(this.connectOpts));else T("Creating `net.Socket`: %o",this.connectOpts),G=O.connect(this.connectOpts);let J=typeof this.proxyHeaders==="function"?this.proxyHeaders():{...this.proxyHeaders},W=O.isIPv6(z.host)?`[${z.host}]`:z.host,X=`CONNECT ${W}:${z.port} HTTP/1.1\r
`;if(E.username||E.password){let K=`${decodeURIComponent(E.username)}:${decodeURIComponent(E.password)}`;J["Proxy-Authorization"]=`Basic ${Buffer.from(K).toString("base64")}`}if(J.Host=`${W}:${z.port}`,!J["Proxy-Connection"])J["Proxy-Connection"]=this.keepAlive?"Keep-Alive":"close";for(let K of Object.keys(J))X+=`${K}: ${J[K]}\r
`;let F=(0,r.parseProxyResponse)(G);G.write(`${X}\r
`);let{connect:Y,buffered:M}=await F;if(w.emit("proxyConnect",Y),this.emit("proxyConnect",Y,w),Y.statusCode===200){if(w.once("socket",t),z.secureEndpoint)return T("Upgrading socket connection to TLS"),_.connect({...h(g(z),"host","path","port"),socket:G});return G}G.destroy();let V=new O.Socket({writable:!1});return V.readable=!0,w.once("socket",(K)=>{T("Replaying proxy buffer for failed request"),(0,a.default)(K.listenerCount("data")>0),K.push(M),K.push(null)}),V}}S.protocols=["http","https"];Q.HttpsProxyAgent=S;function t(w){w.resume()}function h(w,...z){let E={},G;for(G in w)if(!z.includes(G))E[G]=w[G];return E}});
export{s as fj};
