*Refactor broadcast handling to call RPCServer.handleRequest if it is a loopback request
ci/woodpecker/push/woodpecker Pipeline failed Details

*check for request module and request method
*Bug fix processing of responses
This commit is contained in:
Derrick Hammer 2022-11-28 01:37:54 -05:00
parent 9b15c738e9
commit 0387316e4f
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 26 additions and 3 deletions

View File

@ -8,6 +8,8 @@ import {
RPCResponse,
} from "@lumeweb/relay-types";
import { getRpcByPeer } from "../rpc";
import { get as getSwarm } from "../swarm";
import b4a from "b4a";
async function broadcastRequest(
request: RPCRequest,
@ -21,7 +23,17 @@ async function broadcastRequest(
let relayMap = new Map<string, Promise<any>>();
for (const relay of relays) {
relayMap.set(relay, makeRequest(relay));
let req;
if (b4a.equals(b4a.from(relay, "hex"), getSwarm().keyPair.publicKey)) {
req = getRpcServer().handleRequest(request);
} else {
req = makeRequest(relay);
}
relayMap.set(
relay,
req.catch((error: Error) => error)
);
}
await Promise.allSettled([...relays.values()]);
@ -70,6 +82,12 @@ const plugin: Plugin = {
if (!req?.request) {
throw new Error("request required");
}
if (!req?.request?.module) {
throw new Error("request.module required");
}
if (!req?.request?.method) {
throw new Error("request.method required");
}
if (!req?.relays?.length) {
throw new Error("relays required");
}
@ -81,10 +99,15 @@ const plugin: Plugin = {
data: true,
signedField: "relays",
};
for (const relay in resp) {
let ret: RPCResponse;
for (const relay of resp.keys()) {
let ret: RPCResponse | Error;
try {
ret = await resp.get(relay);
if (ret instanceof Error) {
result.relays[relay] = { error: ret.message };
} else {
result.relays[relay] = ret as RPCResponse;
}
} catch (e: any) {
result.relays[relay] = { error: e.message };
}