import{b as d,c as b,f as p,h as y}from"./chunk-224RJRPV.js";import{b as u,e as m}from"./chunk-XB6R2VIU.js";import{Readable as c}from"stream";var w=async e=>typeof Blob=="function"&&e instanceof Blob||e.constructor?.name==="Blob"?Blob.prototype.arrayBuffer!==void 0?new Uint8Array(await e.arrayBuffer()):E(e):x(e);async function E(e){let o=await T(e),n=d(o);return new Uint8Array(n)}async function x(e){let o=[],n=e.getReader(),t=!1,r=0;for(;!t;){let{done:f,value:i}=await n.read();i&&(o.push(i),r+=i.length),t=f}let a=new Uint8Array(r),s=0;for(let f of o)a.set(f,s),s+=f.length;return a}function T(e){return new Promise((o,n)=>{let t=new FileReader;t.onloadend=()=>{if(t.readyState!==2)return n(new Error("Reader aborted too early"));let r=t.result??"",a=r.indexOf(","),s=a>-1?a+1:r.length;o(r.substring(s))},t.onabort=()=>n(new Error("Read aborted")),t.onerror=()=>n(t.error),t.readAsDataURL(e)})}var l=e=>typeof ReadableStream=="function"&&(e?.constructor?.name===ReadableStream.name||e instanceof ReadableStream);var h="The stream has already been transformed.",S=e=>{if(!B(e)&&!l(e)){let r=e?.__proto__?.constructor?.name||e;throw new Error(`Unexpected stream implementation, expect Blob or ReadableStream, got ${r}`)}let o=!1,n=async()=>{if(o)throw new Error(h);return o=!0,await w(e)},t=r=>{if(typeof r.stream!="function")throw new Error(`Cannot transform payload Blob to web stream. Please make sure the Blob.stream() is polyfilled.
If you are using React Native, this API is not yet supported, see: https://react-native.canny.io/feature-requests/p/fetch-streaming-body`);return r.stream()};return Object.assign(e,{transformToByteArray:n,transformToString:async r=>{let a=await n();if(r==="base64")return b(a);if(r==="hex")return y(a);if(r===void 0||r==="utf8"||r==="utf-8")return m(a);if(typeof TextDecoder=="function")return new TextDecoder(r).decode(a);throw new Error("TextDecoder is not available, please make sure polyfill is provided.")},transformToWebStream:()=>{if(o)throw new Error(h);if(o=!0,B(e))return t(e);if(l(e))return e;throw new Error(`Cannot transform payload to web stream, got ${e}`)}})},B=e=>typeof Blob=="function"&&e instanceof Blob;var R="The stream has already been transformed.",I=e=>{if(!(e instanceof c))try{return S(e)}catch{let r=e?.__proto__?.constructor?.name||e;throw new Error(`Unexpected stream implementation, expect Stream.Readable instance, got ${r}`)}let o=!1,n=async()=>{if(o)throw new Error(R);return o=!0,await p(e)};return Object.assign(e,{transformToByteArray:n,transformToString:async t=>{let r=await n();return t===void 0||Buffer.isEncoding(t)?u(r.buffer,r.byteOffset,r.byteLength).toString(t):new TextDecoder(t).decode(r)},transformToWebStream:()=>{if(o)throw new Error(R);if(e.readableFlowing!==null)throw new Error("The stream has been consumed by other callbacks.");if(typeof c.toWeb!="function")throw new Error("Readable.toWeb() is not supported. Please ensure a polyfill is available.");return o=!0,c.toWeb(e)}})};export{I as a};
