*Update rpc methods that require cache to only be enabled if on

*Make get_direct_peers, get_connected_peers, and get_bootstrap_info debug methods
This commit is contained in:
Derrick Hammer 2022-12-19 15:34:32 -05:00
parent af4e6155bc
commit 7b26856720
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 82 additions and 79 deletions

View File

@ -48,6 +48,7 @@ async function broadcastRequest(
const plugin: Plugin = { const plugin: Plugin = {
name: "rpc", name: "rpc",
async plugin(api: PluginAPI): Promise<void> { async plugin(api: PluginAPI): Promise<void> {
if (api.config.bool("cache")) {
api.registerMethod("get_cached_item", { api.registerMethod("get_cached_item", {
cacheable: false, cacheable: false,
async handler(req: string): Promise<RPCResponse> { async handler(req: string): Promise<RPCResponse> {
@ -55,9 +56,9 @@ const plugin: Plugin = {
throw new Error("item must be a string"); throw new Error("item must be a string");
} }
const cache = getRpcServer().cache.data; const cache = api.rpcServer.cache?.data;
if (!Object.keys(cache).includes(req)) { if (!cache?.has(req)) {
throw new Error("item does not exist"); throw new Error("item does not exist");
} }
@ -75,12 +76,63 @@ const plugin: Plugin = {
throw new Error("item must be a string"); throw new Error("item must be a string");
} }
try { try {
api.getRpcServer().cache.deleteItem(req); api.rpcServer.cache.deleteItem(req);
} catch (e: any) { } catch (e: any) {
throw e; throw e;
} }
}, },
}); });
api.registerMethod("get_peers", {
cacheable: false,
async handler(): Promise<string[]> {
const pubkey = b4a.from(api.identity.publicKeyRaw).toString("hex");
const online = api.rpcServer.cache?.dhtCache.online || new Set();
if (online.has(pubkey)) {
online.delete(pubkey);
}
return [...online];
},
});
if (api.logger.level === "debug") {
api.registerMethod("get_direct_peers", {
cacheable: false,
async handler(): Promise<string[]> {
const online = api.rpcServer.cache.dhtCache.online;
const pubkey = b4a
.from(api.swarm.keyPair.publicKeyRaw())
.toString("hex");
if (online.has(pubkey)) {
online.delete(pubkey);
}
const topic = LUMEWEB_TOPIC_HASH.toString("hex");
return [...api.swarm.peers.values()]
.filter((item: any) =>
[...item._seenTopics.keys()].includes(topic)
)
.map((item: any) => item.publicKey.toString("hex"))
.filter((item: any) => online.has(item));
},
});
api.registerMethod("get_bootstrap_info", {
cacheable: false,
async handler(): Promise<string[]> {
// @ts-ignore
return api.rpcServer.cache.dhtCache._getBootstrapInfo();
},
});
api.registerMethod("get_connected_peers", {
cacheable: false,
async handler(): Promise<string[]> {
// @ts-ignore
return [...api.rpcServer.cache.dhtCache.connectedTo];
},
});
}
}
api.registerMethod("broadcast_request", { api.registerMethod("broadcast_request", {
cacheable: false, cacheable: false,
async handler(req: RPCBroadcastRequest): Promise<RPCBroadcastResponse> { async handler(req: RPCBroadcastRequest): Promise<RPCBroadcastResponse> {
@ -128,55 +180,6 @@ const plugin: Plugin = {
return result; return result;
}, },
}); });
api.registerMethod("get_peers", {
cacheable: false,
async handler(): Promise<string[]> {
const pubkey = b4a
.from(getRpcServer().cache.swarm.keyPair.publicKey)
.toString("hex");
const online = getRpcServer().cache.dhtCache.online;
if (online.has(pubkey)) {
online.delete(pubkey);
}
return [...online];
},
});
api.registerMethod("get_direct_peers", {
cacheable: false,
async handler(): Promise<string[]> {
const online = getRpcServer().cache.dhtCache.online;
const pubkey = b4a
.from(getRpcServer().cache.swarm.keyPair.publicKey)
.toString("hex");
if (online.has(pubkey)) {
online.delete(pubkey);
}
const topic = LUMEWEB_TOPIC_HASH.toString("hex");
return [...getRpcServer().cache.swarm.peers.values()]
.filter((item: any) => [...item._seenTopics.keys()].includes(topic))
.map((item: any) => item.publicKey.toString("hex"))
.filter((item: any) => online.has(item));
},
});
api.registerMethod("get_bootstrap_info", {
cacheable: false,
async handler(): Promise<string[]> {
// @ts-ignore
return getRpcServer().cache.dhtCache._getBootstrapInfo();
},
});
api.registerMethod("get_connected_peers", {
cacheable: false,
async handler(): Promise<string[]> {
// @ts-ignore
return [...getRpcServer().cache.dhtCache.connectedTo];
},
});
}, },
}; };