diff --git a/src/modules/rpc.ts b/src/modules/rpc.ts index 2464b19..7fbb04a 100644 --- a/src/modules/rpc.ts +++ b/src/modules/rpc.ts @@ -7,16 +7,24 @@ import { errorExit } from "../lib/error.js"; import stringify from "json-stable-stringify"; import { getRpcServer, + RPC_PROTOCOL_ID, RPC_PROTOCOL_SYMBOL, setupStream, } from "./rpc/server.js"; import { get as getSwarm, SecretStream } from "./swarm.js"; import b4a from "b4a"; +// @ts-ignore +import Protomux from "protomux"; export async function start() { - getSwarm().on("connection", (stream: SecretStream) => - getRpcServer().setup(stream) - ); + getSwarm().on("connection", (stream: SecretStream) => { + Protomux.from(stream).pair( + { protocol: "protomux-rpc", id: RPC_PROTOCOL_ID }, + async () => { + getRpcServer().setup(stream); + } + ); + }); } export async function getRpcByPeer(peer: Buffer | string) { @@ -30,15 +38,16 @@ export async function getRpcByPeer(peer: Buffer | string) { } return new Promise((resolve) => { - const listener = () => {}; - swarm.on("connection", (peer: any, info: any) => { + const listener = (peer: any, info: any) => { if (info.publicKey.toString("hex") !== peer.toString("hex")) { return; } swarm.removeListener("connection", listener); resolve(setupStream(peer)); - }); + }; + + swarm.on("connection", listener); swarm.joinPeer(peer); }); diff --git a/src/modules/rpc/server.ts b/src/modules/rpc/server.ts index 0d2369d..b926d1e 100644 --- a/src/modules/rpc/server.ts +++ b/src/modules/rpc/server.ts @@ -21,7 +21,7 @@ import jsonStringify from "json-stringify-deterministic"; const sodium = require("sodium-universal"); let server: RPCServer; -const RPC_PROTOCOL_ID = b4a.from("lumeweb"); +export const RPC_PROTOCOL_ID = b4a.from("lumeweb"); export const RPC_PROTOCOL_SYMBOL = Symbol.for(RPC_PROTOCOL_ID.toString()); export function getRpcServer(): RPCServer { diff --git a/src/modules/swarm.ts b/src/modules/swarm.ts index 2432ec7..e3ad9a3 100644 --- a/src/modules/swarm.ts +++ b/src/modules/swarm.ts @@ -69,6 +69,9 @@ export class ProtocolManager { this._swarm = swarm; this._swarm.on("connection", (peer: any) => { + if (!peer.userData) { + peer.userData = null; + } for (const protocol of this._protocols) { Protomux.from(peer).pair( { protocol: protocol[0] }, diff --git a/src/plugins/core.ts b/src/plugins/core.ts index 8e555ea..7aa29e1 100644 --- a/src/plugins/core.ts +++ b/src/plugins/core.ts @@ -1,8 +1,17 @@ import { Plugin, PluginAPI } from "@lumeweb/interface-relay"; +let pluginsLoadedResolve: () => void; +let pluginsLoadedPromise = new Promise((resolve) => { + pluginsLoadedResolve = resolve; +}); + const plugin: Plugin = { name: "core", async plugin(api: PluginAPI): Promise { + api.once("core.pluginsLoaded", () => { + pluginsLoadedResolve(); + }); + api.registerMethod("ping", { cacheable: false, async handler(): Promise { @@ -13,6 +22,8 @@ const plugin: Plugin = { api.registerMethod("get_methods", { cacheable: false, async handler(): Promise { + await pluginsLoadedPromise; + return api.rpcServer.getMethods(); }, });