@broxus/js-bridge-essentials
Version:
Bridge JavaScript Essentials library
34 lines (33 loc) • 1.18 kB
JavaScript
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));
}
}