From ec33e40c74b300076c36e7a6f9f45320da8a52f1 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Sat, 26 Nov 2022 17:13:02 -0500 Subject: [PATCH] *Export swarm start *Make swarm get non async to prevent race conditions --- src/modules/dns.ts | 2 +- src/modules/relay.ts | 2 +- src/modules/rpc.ts | 4 ++-- src/modules/rpc/cache.ts | 12 ++++-------- src/modules/swarm.ts | 8 ++------ 5 files changed, 10 insertions(+), 18 deletions(-) diff --git a/src/modules/dns.ts b/src/modules/dns.ts index f5aa561..770cdca 100644 --- a/src/modules/dns.ts +++ b/src/modules/dns.ts @@ -36,7 +36,7 @@ async function ipUpdate() { } export async function start() { - const swarm = (await getSwarm()) as any; + const swarm = getSwarm(); await ipUpdate(); diff --git a/src/modules/relay.ts b/src/modules/relay.ts index 4f2328c..68e4ec4 100644 --- a/src/modules/relay.ts +++ b/src/modules/relay.ts @@ -20,7 +20,7 @@ import { getSslContext } from "./ssl.js"; export async function start() { const relayPort = config.uint("port"); - const dht = await getSwarm(); + const dht = getSwarm(); const statusCodeServer = http.createServer(function (req, res) { // @ts-ignore diff --git a/src/modules/rpc.ts b/src/modules/rpc.ts index b173b50..d527d07 100644 --- a/src/modules/rpc.ts +++ b/src/modules/rpc.ts @@ -13,13 +13,13 @@ export async function start() { errorExit("Please set pocket-app-id and pocket-app-key config options."); } - (await getSwarm()).on("connection", (stream: SecretStream) => + getSwarm().on("connection", (stream: SecretStream) => getRpcServer().setup(stream) ); } export async function getRpcByPeer(peer: string) { - const swarm = await getSwarm(); + const swarm = getSwarm(); if (swarm._allConnections.has(peer)) { return swarm._allConnections.get(peer)[RPC_PROTOCOL_SYMBOL]; diff --git a/src/modules/rpc/cache.ts b/src/modules/rpc/cache.ts index 8661454..c22a094 100644 --- a/src/modules/rpc/cache.ts +++ b/src/modules/rpc/cache.ts @@ -34,7 +34,10 @@ export class RPCCache extends EventEmitter { constructor(server: RPCServer) { super(); this.server = server; - this.init(); + this._swarm = getSwarm(); + this.dhtCache = new DHTCache(this._swarm, { + protocol: "lumeweb.rpccache", + }); } public async getNodeQuery( @@ -125,11 +128,4 @@ export class RPCCache extends EventEmitter { return true; } - - private async init() { - this.dhtCache = new DHTCache(await getSwarm(), { - protocol: "lumeweb.rpccache", - }); - this._swarm = await getSwarm(); - } } diff --git a/src/modules/swarm.ts b/src/modules/swarm.ts index 6f190b8..4f3766c 100644 --- a/src/modules/swarm.ts +++ b/src/modules/swarm.ts @@ -34,7 +34,7 @@ export function getKeyPair() { return deriveMyskyRootKeypair(seedPhraseToSeed(seed)[0]); } -async function start() { +export async function start() { const keyPair = getKeyPair(); node = new Hyperswarm({ keyPair, dht: new DHT({ keyPair }) }); @@ -49,10 +49,6 @@ async function start() { return node; } -export async function get(): Promise { - if (!node) { - await start(); - } - +export function get(): Hyperswarm { return node; }