UNPKG

@embrace-io/web-sdk

Version:
3 lines (2 loc) 1.02 kB
class e{constructor(e){this._config=e}static async _compressRequest(e){const t=new CompressionStream("gzip"),r=t.writable.getWriter();r.write(e),r.close();const s=[],n=t.readable.getReader();let o=!1;for(;!o;){const e=await n.read();e.value&&s.push(e.value),o=e.done}const a=new Uint8Array(s.reduce(((e,t)=>e+t.length),0));let i=0;for(const e of s)a.set(e,i),i+=e.length;return a}send(e,t){return this._asyncSend(e,t)}shutdown(){}async _asyncSend(t,r){let s=t;const n=Object.assign({"Content-Type":"application/json"},this._config.headers);"gzip"===this._config.compression&&(s=await e._compressRequest(t),n["Content-Encoding"]="gzip",n["Content-Length"]=s.length.toString());try{return(await fetch(this._config.url,{method:"POST",keepalive:!0,headers:n,body:s,signal:AbortSignal.timeout(r)})).ok?{status:"success"}:{status:"failure",error:new Error("Fetch request failed")}}catch(e){return{status:"failure",error:new Error("Fetch request errored")}}}}export{e as FetchTransport}; //# sourceMappingURL=FetchTransport.js.map