rpc/src/index.ts

52 lines
1.1 KiB
TypeScript
Raw Permalink Normal View History

2022-11-27 23:22:49 +00:00
import ProtomuxRPC from "protomux-rpc";
2022-11-14 16:09:02 +00:00
// @ts-ignore
2022-11-27 23:22:49 +00:00
import c from "compact-encoding";
import b4a from "b4a";
2022-11-14 16:09:02 +00:00
2022-11-27 23:22:49 +00:00
const ID = b4a.from("lumeweb");
2022-11-14 16:09:02 +00:00
export default class RPC extends ProtomuxRPC {
private _ready: Promise<void>;
2022-11-27 23:22:49 +00:00
constructor(stream: any, options = {}) {
options = {
...{
id: ID,
valueEncoding: c.json,
},
...options,
};
super(stream, options);
this._ready = new Promise((resolve, reject) => {
stream.on("open", onopen);
stream.on("destroy", ondestroy);
function onopen(handshake) {
removeListener();
resolve(handshake);
}
function ondestroy() {
removeListener();
reject(new Error("Client could not connect"));
}
function removeListener() {
stream.off("open", onopen);
stream.off("destroy", ondestroy);
}
});
2023-07-23 18:08:18 +00:00
stream.setKeepAlive(5000);
2022-11-27 23:22:49 +00:00
}
async request(method: any, value: any | string = "", options = {}) {
2022-11-27 23:22:49 +00:00
return super.request(method, value, options);
}
get ready(): Promise<void> {
return this._ready;
}
2022-11-14 16:09:02 +00:00
}