From 2d30390fa26efc3addc8d515510a1229bbca9908 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Sat, 18 Mar 2023 15:07:52 -0400 Subject: [PATCH 1/6] *fix listener callback --- src/modules/rpc.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/modules/rpc.ts b/src/modules/rpc.ts index 2464b19..852da9d 100644 --- a/src/modules/rpc.ts +++ b/src/modules/rpc.ts @@ -30,15 +30,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); }); From 3600fbfdcfea056a9a01c54d29ff1f88536669f2 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Wed, 29 Mar 2023 16:23:17 -0400 Subject: [PATCH 2/6] *await on plugin loading --- src/modules/plugin.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/plugin.ts b/src/modules/plugin.ts index 5675746..08a09ab 100644 --- a/src/modules/plugin.ts +++ b/src/modules/plugin.ts @@ -163,7 +163,7 @@ export class PluginAPIManager { this.registeredPlugins.set(plugin.name, plugin); try { - plugin.plugin( + await plugin.plugin( // @ts-ignore new Proxy(getPluginAPI(), { get(target: PluginAPI, prop: string): any { From 58e95806d006e0c58df1b6a3e0f4162337dc2b42 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Wed, 29 Mar 2023 16:23:56 -0400 Subject: [PATCH 3/6] *hook on core.pluginsLoaded to ensure that we don't answer until all plugins are loaded --- src/plugins/core.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/plugins/core.ts b/src/plugins/core.ts index e1d9c60..e761c89 100644 --- a/src/plugins/core.ts +++ b/src/plugins/core.ts @@ -1,8 +1,17 @@ import { Plugin, PluginAPI } from "@lumeweb/relay-types"; +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,10 @@ const plugin: Plugin = { api.registerMethod("get_methods", { cacheable: false, async handler(): Promise { + await pluginsLoadedPromise; + + console.log("get_methods", api.rpcServer.getMethods()); + return api.rpcServer.getMethods(); }, }); From a003da1606c104a7aa0fd3cfe9018557a4ff1e24 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Wed, 29 Mar 2023 17:16:13 -0400 Subject: [PATCH 4/6] *Remove debug logging --- src/plugins/core.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/plugins/core.ts b/src/plugins/core.ts index e761c89..3854e0f 100644 --- a/src/plugins/core.ts +++ b/src/plugins/core.ts @@ -24,8 +24,6 @@ const plugin: Plugin = { async handler(): Promise { await pluginsLoadedPromise; - console.log("get_methods", api.rpcServer.getMethods()); - return api.rpcServer.getMethods(); }, }); From 556373c5bcd9c2555f7ebfe606833d819803c3be Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Fri, 7 Apr 2023 20:52:23 -0400 Subject: [PATCH 5/6] *Ensure we use Protomux pair for the RPC service --- src/modules/rpc.ts | 14 +++++++++++--- src/modules/rpc/server.ts | 2 +- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/modules/rpc.ts b/src/modules/rpc.ts index 852da9d..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) { diff --git a/src/modules/rpc/server.ts b/src/modules/rpc/server.ts index 1c04b5d..46fce6a 100644 --- a/src/modules/rpc/server.ts +++ b/src/modules/rpc/server.ts @@ -23,7 +23,7 @@ import config from "../../config"; 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 { From 2b12150d71509447eb8968ee875bc205e1855353 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Fri, 7 Apr 2023 20:52:49 -0400 Subject: [PATCH 6/6] *Backwards compat fix to ensure Protomux is stored on the stream --- src/modules/swarm.ts | 3 +++ 1 file changed, 3 insertions(+) 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] },