@broxus/js-bridge-essentials
Version:
Bridge JavaScript Essentials library
35 lines (34 loc) • 1.26 kB
JavaScript
// @ts-expect-error S2307: Cannot find module or its corresponding type declarations.
import core from 'everscale-standalone-client/core';
const DEFAULT_HEADERS = {
'Content-Type': 'application/x-protobuf',
};
export class ProtoSocket {
async connect(params) {
class ProtoSender {
endpoint;
endpointAgent;
constructor(_params) {
this.endpoint = params.endpoint;
this.endpointAgent = core.fetchAgent(this.endpoint);
}
send(data, handler, _) {
(async () => {
try {
const response = await fetch(this.endpoint, {
agent: this.endpointAgent,
body: new Uint8Array(data),
headers: DEFAULT_HEADERS,
method: 'post',
}).then(res => res.arrayBuffer());
handler.onReceive(new Uint8Array(response));
}
catch (e) {
handler.onError(e);
}
})();
}
}
return new core.nekoton.ProtoConnection(new ProtoSender(params));
}
}