*Add new core dht plugin that allows joining a DHT topic and getting the peers of a given topic
This commit is contained in:
parent
ba86b3587b
commit
523f2c04f0
|
@ -10,6 +10,7 @@ import type { Logger } from "pino";
|
||||||
import { getHDKey, getSeed } from "../lib/seed.js";
|
import { getHDKey, getSeed } from "../lib/seed.js";
|
||||||
import pluginRpc from "./plugins/rpc";
|
import pluginRpc from "./plugins/rpc";
|
||||||
import pluginCore from "./plugins/core";
|
import pluginCore from "./plugins/core";
|
||||||
|
import pluginDht from "./plugins/dht";
|
||||||
import type Config from "@lumeweb/cfg";
|
import type Config from "@lumeweb/cfg";
|
||||||
import EventEmitter2 from "eventemitter2";
|
import EventEmitter2 from "eventemitter2";
|
||||||
import log from "../log.js";
|
import log from "../log.js";
|
||||||
|
@ -229,6 +230,7 @@ export async function loadPlugins() {
|
||||||
|
|
||||||
apiManager.loadPluginInstance(pluginCore);
|
apiManager.loadPluginInstance(pluginCore);
|
||||||
apiManager.loadPluginInstance(pluginRpc);
|
apiManager.loadPluginInstance(pluginRpc);
|
||||||
|
apiManager.loadPluginInstance(pluginDht);
|
||||||
|
|
||||||
for (const plugin of [...new Set(config.array("plugins", []))] as []) {
|
for (const plugin of [...new Set(config.array("plugins", []))] as []) {
|
||||||
await apiManager.loadPlugin(plugin);
|
await apiManager.loadPlugin(plugin);
|
||||||
|
|
|
@ -0,0 +1,27 @@
|
||||||
|
import { Plugin, PluginAPI } from "@lumeweb/relay-types";
|
||||||
|
import { getRpcServer } from "../rpc/server";
|
||||||
|
import b4a from "b4a";
|
||||||
|
|
||||||
|
const plugin: Plugin = {
|
||||||
|
name: "dht",
|
||||||
|
async plugin(api: PluginAPI): Promise<void> {
|
||||||
|
api.registerMethod("join_topic", {
|
||||||
|
cacheable: false,
|
||||||
|
async handler(topic: string): Promise<void> {
|
||||||
|
if (!api.swarm._discovery.has(topic)) {
|
||||||
|
api.swarm.join(topic);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
api.registerMethod("get_topic_peers", {
|
||||||
|
cacheable: false,
|
||||||
|
async handler(topic: string): Promise<string[]> {
|
||||||
|
return [...api.swarm.peers.values()]
|
||||||
|
.filter((peerInfo) => peerInfo._seenTopics.has(topic))
|
||||||
|
.map((peerInfo) => b4a.from(peerInfo.publicKey).toString());
|
||||||
|
},
|
||||||
|
});
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export default plugin;
|
Loading…
Reference in New Issue