2023-08-20 09:29:40 +00:00
|
|
|
import { grpc } from "@improbable-eng/grpc-web";
|
|
|
|
import { RpcNetwork } from "@lumeweb/kernel-rpc-client";
|
|
|
|
import Metadata = grpc.Metadata;
|
|
|
|
|
|
|
|
class HyperTransport implements grpc.Transport {
|
|
|
|
private options: grpc.TransportOptions;
|
|
|
|
private rpc: RpcNetwork;
|
|
|
|
|
|
|
|
constructor(transportOptions: grpc.TransportOptions, rpc: RpcNetwork) {
|
|
|
|
this.options = transportOptions;
|
|
|
|
this.rpc = rpc;
|
|
|
|
}
|
|
|
|
|
|
|
|
cancel(): void {}
|
|
|
|
|
|
|
|
finishSend(): void {}
|
|
|
|
|
|
|
|
async sendMessage(msgBytes: Uint8Array): Promise<void> {
|
|
|
|
const req = this.rpc.simpleQuery({
|
|
|
|
query: {
|
|
|
|
module: "lavanet",
|
|
|
|
method: "badge_request",
|
|
|
|
data: {
|
|
|
|
data: msgBytes,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const ret = await req.result;
|
|
|
|
if (ret.error) {
|
|
|
|
this.options.onEnd({ message: ret.error, name: "", stack: "" });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-21 00:08:18 +00:00
|
|
|
if (ret.data) {
|
|
|
|
this.options.onHeaders(new grpc.Metadata(), 200);
|
|
|
|
ret.data = new Uint8Array(Object.values(ret.data));
|
|
|
|
this.options.onChunk(ret.data);
|
|
|
|
}
|
|
|
|
|
2023-08-20 09:29:40 +00:00
|
|
|
this.options.onEnd();
|
|
|
|
}
|
|
|
|
|
2023-08-20 12:58:23 +00:00
|
|
|
start(metadata: Metadata): void {}
|
2023-08-20 09:29:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function hyperTransport(rpc: RpcNetwork): grpc.TransportFactory {
|
|
|
|
return (opts: grpc.TransportOptions) => {
|
|
|
|
return new HyperTransport(opts, rpc);
|
|
|
|
};
|
|
|
|
}
|