*refactor handling of requests and responses

*Ensure consensus query args are handled correctly
This commit is contained in:
Derrick Hammer 2023-03-27 08:04:12 -04:00
parent df6da15952
commit 8501074b25
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 37 additions and 12 deletions

View File

@ -1,22 +1,29 @@
import type { Plugin, PluginAPI } from "@lumeweb/interface-relay"; import type { Plugin, PluginAPI } from "@lumeweb/interface-relay";
import fetch, { Request, RequestInit } from "node-fetch"; import fetch, { Request, RequestInit } from "node-fetch";
import { URL } from "url";
const EXECUTION_RPC_URL = const EXECUTION_RPC_URL =
"https://g.w.lavanet.xyz:443/gateway/eth/rpc-http/f195d68175eb091ec1f71d00f8952b85"; "https://g.w.lavanet.xyz:443/gateway/eth/rpc-http/f195d68175eb091ec1f71d00f8952b85";
const CONSENSUS_RPC_URL = "https://www.lightclientdata.org"; const CONSENSUS_RPC_URL = "https://www.lightclientdata.org";
async function doFetch(url: string, request: RequestInit) { async function doFetch(url: string, request: RequestInit) {
sanitizeRequestArgs(request); sanitizeRequestArgs(url, request);
let req = new Request(url, request); let req = new Request(url, {
...request,
headers: {
"Content-Type": "application/json",
},
});
const resp = await fetch(req); const resp = await fetch(req);
return (await resp.json()) as any; return (await resp.json()) as any;
} }
function sanitizeRequestArgs(request: RequestInit) { function sanitizeRequestArgs(url: string, request: RequestInit) {
if (!request || typeof request !== "object") { if (!request || typeof request !== "object") {
throw Error("invalid request"); throw Error("invalid request");
} }
@ -40,20 +47,38 @@ function sanitizeRequestArgs(request: RequestInit) {
}); });
} }
interface ConsensusRequest extends RequestInit {
path: string;
}
interface ExecutionRequest {
method: string;
params: string;
}
const plugin: Plugin = { const plugin: Plugin = {
name: "eth", name: "eth",
async plugin(api: PluginAPI): Promise<void> { async plugin(api: PluginAPI): Promise<void> {
api.registerMethod("execution_request", {
cacheable: false,
async handler(request: RequestInit): Promise<object> {
return doFetch(EXECUTION_RPC_URL, request);
},
});
api.registerMethod("consensus_request", { api.registerMethod("consensus_request", {
cacheable: false, cacheable: false,
async handler(request: RequestInit): Promise<object> { async handler(request: ConsensusRequest): Promise<object> {
return doFetch(CONSENSUS_RPC_URL, request); const consensusUrl = new URL(CONSENSUS_RPC_URL);
let dummyUrl = new URL(
`http://dummy/${request.path.replace(/^\/+/, "")}`
);
consensusUrl.pathname = dummyUrl.pathname;
consensusUrl.search = dummyUrl.search;
return doFetch(consensusUrl.toString(), request);
},
});
api.registerMethod("execution_request", {
cacheable: false,
async handler(request: ExecutionRequest): Promise<object> {
return doFetch(EXECUTION_RPC_URL, {
method: "POST",
body: JSON.stringify(request),
});
}, },
}); });
}, },