require(`./paths-Bcdg3ma0.cjs`);const e=require(`./model-auth-BEMdAUrd.cjs`),t=require(`./logger-M7L0LfHQ.cjs`),n=require(`./proxy-env-DLG9q9UC.cjs`),r=require(`./fetch-timeout-DFDmks9T.cjs`);let i=require(`undici`);const a={STRICT:`strict`,TRUSTED_ENV_PROXY:`trusted_env_proxy`},o=new Set([`accept`,`accept-encoding`,`accept-language`,`cache-control`,`content-language`,`content-type`,`if-match`,`if-modified-since`,`if-none-match`,`if-unmodified-since`,`pragma`,`range`,`user-agent`]);function s(e){return{...e,mode:a.STRICT}}function c(e){return{...e,mode:a.TRUSTED_ENV_PROXY}}function l(e){return e.mode?e.mode:e.proxy===`env`&&e.dangerouslyAllowEnvProxyWithoutPinnedDns===!0?a.TRUSTED_ENV_PROXY:a.STRICT}function u(e){return e===301||e===302||e===303||e===307||e===308}function d(e){if(!e?.headers)return e;let t=new Headers(e.headers),n=new Headers;for(let[e,r]of t.entries())o.has(e.toLowerCase())&&n.set(e,r);return{...e,headers:n}}function f(e){let{timeoutMs:t,signal:n}=e;if(!t&&!n)return{signal:void 0,cleanup:()=>{}};if(!t)return{signal:n,cleanup:()=>{}};let i=new AbortController,a=setTimeout(i.abort.bind(i),t),o=r.t(i);return n&&(n.aborted?i.abort():n.addEventListener(`abort`,o,{once:!0})),{signal:i.signal,cleanup:()=>{clearTimeout(a),n&&n.removeEventListener(`abort`,o)}}}async function p(r){let o=r.fetchImpl??globalThis.fetch;if(!o)throw Error(`fetch is not available`);let s=typeof r.maxRedirects==`number`&&Number.isFinite(r.maxRedirects)?Math.max(0,Math.floor(r.maxRedirects)):3,c=l(r),{signal:p,cleanup:m}=f({timeoutMs:r.timeoutMs,signal:r.signal}),h=!1,g=async t=>{h||(h=!0,m(),await e.wi(t??void 0))},_=new Set,v=r.url,y=r.init?{...r.init}:void 0,b=0;for(;;){let l;try{l=new URL(v)}catch{throw await g(),Error(`Invalid URL: must be http or https`)}if(![`http:`,`https:`].includes(l.protocol))throw await g(),Error(`Invalid URL: must be http or https`);let f=null;try{let t=await e.Di(l.hostname,{lookupFn:r.lookupFn,policy:r.policy});c===a.TRUSTED_ENV_PROXY&&n.n()?f=new i.EnvHttpProxyAgent:r.pinDns!==!1&&(f=e.Ti(t));let m={...y?{...y}:{},redirect:`manual`,...f?{dispatcher:f}:{},...p?{signal:p}:{}},h=await o(l.toString(),m);if(u(h.status)){let t=h.headers.get(`location`);if(!t)throw await g(f),Error(`Redirect missing location header (${h.status})`);if(b+=1,b>s)throw await g(f),Error(`Too many redirects (limit: ${s})`);let n=new URL(t,l),r=n.toString();if(_.has(r))throw await g(f),Error(`Redirect loop detected`);n.origin!==l.origin&&(y=d(y)),_.add(r),h.body?.cancel(),await e.wi(f),v=r;continue}return{response:h,finalUrl:v,release:async()=>g(f)}}catch(n){throw n instanceof e.Ci&&t.i(`security: blocked URL fetch (${r.auditContext??`url-fetch`}) target=${l.origin}${l.pathname} reason=${n.message}`),await g(f),n}}}Object.defineProperty(exports,`n`,{enumerable:!0,get:function(){return s}}),Object.defineProperty(exports,`r`,{enumerable:!0,get:function(){return c}}),Object.defineProperty(exports,`t`,{enumerable:!0,get:function(){return p}});
