kernel-rpc/src/index.ts

191 lines
3.7 KiB
TypeScript
Raw Normal View History

2022-07-21 17:04:17 +00:00
import { addHandler, handleMessage } from "libkmodule";
import type { ActiveQuery } from "libkmodule";
import { DHT } from "@lumeweb/kernel-dht-client";
2022-08-31 04:28:34 +00:00
import {
RpcNetwork,
RpcQueryOptions,
StreamingRpcQueryOptions,
} from "@lumeweb/dht-rpc-client";
import type { RPCRequest, RPCResponse } from "@lumeweb/relay-types";
2022-07-21 17:04:17 +00:00
onmessage = handleMessage;
const network = new RpcNetwork(new DHT());
const dht = network.dht as DHT;
addHandler("addRelay", handleAddRelay);
addHandler("removeRelay", handleRemoveRelay);
addHandler("clearRelays", handleClearRelays);
2022-08-31 04:28:34 +00:00
addHandler("simpleQuery", handleSimpleQuery);
addHandler("streamingQuery", handleStreamingQuery);
addHandler("wisdomQuery", handleWisdomQuery);
2022-07-21 17:04:17 +00:00
addHandler("ready", handleReady);
async function handleAddRelay(aq: ActiveQuery) {
const { pubkey = null } = aq.callerInput;
if (!pubkey) {
aq.reject("invalid pubkey");
return;
}
network.addRelay(pubkey);
try {
await dht.addRelay(pubkey);
} catch (e: any) {}
aq.respond();
}
function handleRemoveRelay(aq: ActiveQuery) {
const { pubkey = null } = aq.callerInput;
if (!pubkey) {
aq.reject("invalid pubkey");
return;
}
aq.respond(network.removeRelay(pubkey));
}
async function handleClearRelays(aq: ActiveQuery) {
network.clearRelays();
await dht.clearRelays();
aq.respond();
}
2022-08-31 04:28:34 +00:00
async function handleSimpleQuery(aq: ActiveQuery) {
const {
query = null,
relay = null,
options = undefined,
} = aq.callerInput as {
query: RPCRequest;
options: RpcQueryOptions;
relay: Buffer | string;
};
if (!query) {
aq.reject("RPCRequest query required");
return;
}
if (!relay) {
aq.reject("relay required");
return;
}
let resp: RPCResponse | null = null;
try {
const rpcQuery = network.simpleQuery(
relay as Buffer | string,
query.method,
query.module,
query.data,
query.bypassCache,
options
);
resp = await rpcQuery.result;
} catch (e: any) {
aq.reject(e);
}
2022-07-21 17:04:17 +00:00
2022-08-31 04:28:34 +00:00
if (resp?.error) {
aq.reject(resp?.error);
2022-07-21 17:04:17 +00:00
return;
}
2022-08-31 04:28:34 +00:00
aq.respond(resp);
}
async function handleStreamingQuery(aq: ActiveQuery) {
const {
query = null,
relay = null,
options = undefined,
} = aq.callerInput as {
query: RPCRequest;
options: StreamingRpcQueryOptions;
relay: Buffer | string;
};
if (!query) {
aq.reject("RPCRequest query required");
2022-07-21 17:04:17 +00:00
return;
}
2022-08-31 04:28:34 +00:00
if (!relay) {
aq.reject("relay required");
2022-07-21 17:04:17 +00:00
return;
}
2022-08-31 04:28:34 +00:00
if (!options || !options?.streamHandler) {
aq.reject("RPCRequest query required");
return;
}
let resp: RPCResponse | null = null;
2022-07-21 17:04:17 +00:00
try {
2022-08-31 04:28:34 +00:00
const rpcQuery = network.streamingQuery(
relay as Buffer | string,
query.method,
query.module,
2022-07-21 17:04:17 +00:00
query.data,
2022-08-31 04:28:34 +00:00
{ ...options, streamHandler: aq.sendUpdate }
2022-07-21 17:04:17 +00:00
);
resp = await rpcQuery.result;
} catch (e: any) {
aq.reject(e);
}
2022-08-31 04:28:34 +00:00
if (resp?.error) {
aq.reject(resp?.error);
return;
}
aq.respond(resp);
}
async function handleWisdomQuery(aq: ActiveQuery) {
const { query = null, options = undefined } = aq.callerInput as {
query: RPCRequest;
options: RpcQueryOptions;
relay: Buffer | string;
};
if (!query) {
aq.reject("RPCRequest query required");
return;
}
let resp: RPCResponse | null = null;
try {
const rpcQuery = network.wisdomQuery(
query.method,
query.module,
query.data,
query.bypassCache ?? undefined,
options
);
resp = await rpcQuery.result;
} catch (e: any) {
aq.reject(e);
}
if (resp?.error) {
aq.reject(resp?.error);
return;
}
2022-07-21 17:04:17 +00:00
aq.respond(resp);
}
async function handleReady(aq: ActiveQuery) {
await network.ready;
aq.respond();
}