import{i as s}from"./index-Bkuhln3H.js";const c="Request cancelled";async function j(w,e){const n=e?.signal;if(n?.aborted)throw new Error(c);const b=e?.maxRedirections,g=e?.connectTimeout,m=e?.proxy,R=e?.danger;e&&(delete e.maxRedirections,delete e.connectTimeout,delete e.proxy,delete e.danger);const a=e?.headers?e.headers instanceof Headers?e.headers:new Headers(e.headers):new Headers,o=new Request(w,e),h=await o.arrayBuffer(),E=h.byteLength!==0?Array.from(new Uint8Array(h)):null;for(const[r,t]of o.headers)a.get(r)||a.set(r,t);const A=(a instanceof Headers?Array.from(a.entries()):Array.isArray(a)?a:Object.entries(a)).map(([r,t])=>[r,typeof t=="string"?t:t.toString()]);if(n?.aborted)throw new Error(c);const l=await s("plugin:http|fetch",{clientConfig:{method:o.method,url:o.url,headers:A,data:E,maxRedirections:b,connectTimeout:g,proxy:m,danger:R}}),f=()=>s("plugin:http|fetch_cancel",{rid:l});if(n?.aborted)throw f(),new Error(c);n?.addEventListener("abort",()=>{f()});const{status:i,statusText:x,url:_,headers:v,rid:y}=await s("plugin:http|fetch_send",{rid:l}),p=()=>s("plugin:http|fetch_cancel_body",{rid:y}),H=async r=>{let t;try{t=await s("plugin:http|fetch_read_body",{rid:y})}catch(C){r.error(C),p();return}const d=new Uint8Array(t),T=d[d.byteLength-1],q=d.slice(0,d.byteLength-1);if(T===1){r.close();return}r.enqueue(q)},L=[101,103,204,205,304].includes(i)?null:new ReadableStream({start:r=>{n?.addEventListener("abort",()=>{r.error(c),p()})},pull:r=>H(r)}),u=new Response(L,{status:i,statusText:x});return Object.defineProperty(u,"url",{value:_}),Object.defineProperty(u,"headers",{value:new Headers(v)}),u}export{j as fetch};
