@hashgraph/sdk
Version:
3 lines (2 loc) • 2.55 kB
JavaScript
import t,{encodeRequest as e,decodeUnaryResponse as a}from"./Channel.js";import{encode as s,decode as r}from"../encoding/base64.native.js";import o from"../http/HttpError.js";import i from"../http/HttpStatus.js";import{SDK_NAME as n,SDK_VERSION as c}from"../version.js";import p from"../grpc/GrpcServiceError.js";import d from"../grpc/GrpcStatus.js";class l extends t{constructor(t,e){super(e),this._address=t,this._isReady=!1}close(){}async _waitForReady(t){if(this._isReady)return;const e=!(this._address.includes("localhost")||this._address.includes("127.0.0.1"))?`https://${this._address}`:`http://${this._address}`,a=t.getTime()-Date.now();if(a<=0)throw new p(d.Timeout);const r=new AbortController,o=setTimeout(()=>r.abort(),a);try{const t=await fetch(e,{method:"POST",headers:{"content-type":"application/grpc-web-text","x-user-agent":`${n}/${c}`,"x-grpc-web":"1"},body:s(new Uint8Array(0)),signal:r.signal});if(clearTimeout(o),200===t.status){const e=t.headers.get("grpc-status"),a=t.headers.get("grpc-message");if(null!=e||null!=a)return void(this._isReady=!0)}throw new p(d.Unavailable)}catch(t){if(clearTimeout(o),t instanceof Error&&"AbortError"===t.name)throw new p(d.Timeout);if(t instanceof p)throw t;throw new p(d.Unavailable)}}_createUnaryClient(t){return async(d,l,h)=>{const u=new Date,w=this._grpcDeadline;u.setMilliseconds(u.getMilliseconds()+w);try{await this._waitForReady(u);const p=s(new Uint8Array(e(l))),w=!(this._address.includes("localhost")||this._address.includes("127.0.0.1"))?`https://${this._address}`:`http://${this._address}`,f=await fetch(`${w}/proto.${t}/${d.name}`,{method:"POST",headers:{"content-type":"application/grpc-web-text","x-user-agent":`${n}/${c}`,"x-accept-content-transfer-encoding":"base64","x-grpc-web":"1"},body:p});if(!f.ok){h(new o(i._fromValue(f.status)),null)}const b=await f.blob(),m=await new Promise((t,e)=>{const a=new FileReader;a.readAsDataURL(b),a.onloadend=()=>{t(a.result)},a.onerror=e});let g;if(m.startsWith("data:application/octet-stream;base64,"))g=r(m.split("data:application/octet-stream;base64,")[1]);else{if(!m.startsWith("data:application/grpc-web+proto;base64,"))throw new Error(`Expected response data to be base64 encode with a 'data:application/octet-stream;base64,' or 'data:application/grpc-web+proto;base64,' prefix, but found: ${m}`);g=r(m.split("data:application/grpc-web+proto;base64,")[1])}h(null,a(g.buffer,g.byteOffset,g.byteLength))}catch(t){if(t instanceof p)return void h(t,null);h(t,null)}}}}export{l as default};
//# sourceMappingURL=NativeChannel.js.map