/**
 * Minified by jsDelivr using Terser v5.39.0.
 * Original file: /npm/http2-proxy@5.0.53/index.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
const net=require("net"),compat=require("./compat"),CONNECTION="connection",HOST="host",KEEP_ALIVE="keep-alive",PROXY_AUTHORIZATION="proxy-authorization",PROXY_AUTHENTICATE="proxy-authenticate",PROXY_CONNECTION="proxy-connection",TE="te",FORWARDED="forwarded",TRAILER="trailer",TRANSFER_ENCODING="transfer-encoding",UPGRADE="upgrade",VIA="via",AUTHORITY=":authority",HTTP2_SETTINGS="http2-settings",kReq=Symbol("req"),kRes=Symbol("res"),kProxyCallback=Symbol("callback"),kProxyReq=Symbol("proxyReq"),kProxyRes=Symbol("proxyRes"),kProxySocket=Symbol("proxySocket"),kConnected=Symbol("connected"),kOnRes=Symbol("onRes");async function proxy({req:e,socket:o,res:t=o,head:r,proxyName:n},s,i){if(e.aborted)return;const c=getRequestHeaders(e,n);if(void 0!==r){if("GET"!==e.method)throw new HttpError("only GET request allowed",null,405);if("websocket"!==e.headers[UPGRADE])throw new HttpError("missing upgrade header",null,400);r&&r.length&&t.unshift(r),setupSocket(t),c[CONNECTION]="upgrade",c[UPGRADE]="websocket"}const a=await s({method:e.method,path:e.originalUrl||e.url,headers:c});if(e.aborted)return void(a.abort?a.abort():a.destroy&&a.destroy());let l;const d=new Promise(((e,o)=>{l=t=>t?o(t):e()}));return e[kRes]=t,e[kProxyReq]=a,t[kReq]=e,t[kRes]=t,t[kProxySocket]=null,t[kProxyRes]=null,t[kProxyCallback]=l,a[kReq]=e,a[kRes]=t,a[kConnected]=!1,a[kOnRes]=i,t.on("close",onComplete).on("finish",onComplete).on("error",onComplete),e.on("aborted",onComplete).on("error",onComplete),a.on("error",onProxyReqError).on("timeout",onProxyReqTimeout).on("response",onProxyReqResponse).on("upgrade",onProxyReqUpgrade),deferToConnect.call(a),d}function onSocket(e){e.connecting?e.once("connect",onProxyConnect.bind(this)):onProxyConnect.call(this)}function deferToConnect(){this.socket?onSocket.call(this,this.socket):this.once("socket",onSocket)}function onComplete(e){const o=this[kRes],t=o[kReq];if(!o[kProxyCallback])return;const r=t[kProxyReq],n=o[kProxySocket],s=o[kProxyRes],i=o[kProxyCallback];t[kProxyReq]=null,o[kProxySocket]=null,o[kProxyRes]=null,o[kProxyCallback]=null,o.off("close",onComplete).off("finish",onComplete).off("error",onComplete),t.off("close",onComplete).off("aborted",onComplete).off("error",onComplete).off("data",onReqData).off("end",onReqEnd),e&&(e.connectedSocket=Boolean(r&&r[kConnected]),e.reusedSocket=Boolean(r&&r.reusedSocket)),r&&(r.off("drain",onProxyReqDrain),r.abort?r.abort():r.destroy&&r.destroy()),n&&n.destroy(),s&&s.destroy(),i(e)}function onProxyConnect(){this[kConnected]=!0,"GET"===this.method||"HEAD"===this.method||"OPTIONS"===this.method?(this[kReq].resume(),this.end()):(this[kReq].on("data",onReqData).on("end",onReqEnd),this.on("drain",onProxyReqDrain))}function onReqEnd(){this[kProxyReq].end()}function onReqData(e){this[kProxyReq].write(e)||this.pause()}function onProxyReqDrain(){this[kReq].resume()}function onProxyReqError(e){e.statusCode=this[kConnected]?502:503,onComplete.call(this,e)}function onProxyReqTimeout(){onComplete.call(this,new HttpError("proxy timeout","ETIMEDOUT",504))}async function onProxyReqResponse(e){const o=this[kRes];o[kProxyRes]=e,e[kRes]=o;const t=setupHeaders(e.headers);if(e.on("aborted",onProxyResAborted).on("error",onProxyResError),this[kOnRes])try{await this[kOnRes](e,t)}catch(e){onComplete.call(this,e)}else if(o.writeHead){o.statusCode=e.statusCode;for(const[e,r]of Object.entries(t))o.setHeader(e,r);e.on("end",onProxyResEnd).pipe(o)}else e.upgrade||(o.write(createHttpHeader(`HTTP/${e.httpVersion} ${e.statusCode} ${e.statusMessage}`,e.headers)),e.pipe(o))}function onProxyReqUpgrade(e,o,t){const r=this[kRes];r[kProxySocket]=o,o[kRes]=r,setupSocket(o),t&&t.length&&o.unshift(t),r.write(createHttpHeader("HTTP/1.1 101 Switching Protocols",e.headers)),o.on("error",onProxyResError).on("close",onProxyResAborted).pipe(r).pipe(o)}function onProxyResError(e){e.statusCode=502,onComplete.call(this,e)}function onProxyResAborted(){onComplete.call(this,new HttpError("proxy aborted","ECONNRESET",502))}function onProxyResEnd(){this.trailers&&this[kRes].addTrailers(this.trailers)}function createHttpHeader(e,o){let t=e;for(const[e,r]of Object.entries(o))if(Array.isArray(r))for(let o=0;o<r.length;o++)t+=`\r\n${e}: ${r[o]}`;else t+=`\r\n${e}: ${r}`;return t+="\r\n\r\n",Buffer.from(t,"ascii")}function getRequestHeaders(e,o){const t={};for(const[o,r]of Object.entries(e.headers))":"!==o.charAt(0)&&"host"!==o&&(t[o]=r);if(o){if(t[VIA]){for(const e of t[VIA].split(","))if(e.endsWith(o))throw new HttpError("loop detected",null,508);t[VIA]+=","}else t[VIA]="";t[VIA]+=`${e.httpVersion} ${o}`}function r(e,o){const t=net.isIPv6(e);let r=`${e}`;return t&&(r=`[${r}]`),o&&(r=`${r}:${o}`),(t||o)&&(r=`"${r}"`),r}const n=[`by=${r(e.socket.localAddress,e.socket.localPort)}`,`for=${r(e.socket.remoteAddress,e.socket.remotePort)}`,"proto="+(e.socket.encrypted?"https":"http"),`host=${r(e.headers[AUTHORITY]||e.headers[HOST]||"")}`].join(";");return t[FORWARDED]?t[FORWARDED]+=`, ${n}`:t[FORWARDED]=`${n}`,setupHeaders(t)}function setupSocket(e){e.setTimeout(0),e.setNoDelay(!0),e.setKeepAlive(!0,0)}function setupHeaders(e){const o=e[CONNECTION];if(o&&o!==CONNECTION&&o!==KEEP_ALIVE)for(const t of o.toLowerCase().split(","))delete e[t.trim()];return delete e[CONNECTION],delete e[PROXY_CONNECTION],delete e[KEEP_ALIVE],delete e[PROXY_AUTHENTICATE],delete e[PROXY_AUTHORIZATION],delete e[TE],delete e[TRAILER],delete e[TRANSFER_ENCODING],delete e[UPGRADE],delete e[HTTP2_SETTINGS],e}module.exports=compat(proxy);class HttpError extends Error{constructor(e,o,t){super(e),this.code=o,this.statusCode=t||500}}
//# sourceMappingURL=/sm/30594a53c5030adae467b818528410f86883bc708fa59c7ed5802e10bc682f9c.map