*Export swarm start

*Make swarm get non async to prevent race conditions
This commit is contained in:
Derrick Hammer 2022-11-26 17:13:02 -05:00
parent ebd09f9a52
commit ec33e40c74
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
5 changed files with 10 additions and 18 deletions

View File

@ -36,7 +36,7 @@ async function ipUpdate() {
}
export async function start() {
const swarm = (await getSwarm()) as any;
const swarm = getSwarm();
await ipUpdate();

View File

@ -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

View File

@ -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];

View File

@ -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();
}
}

View File

@ -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<Hyperswarm> {
if (!node) {
await start();
}
export function get(): Hyperswarm {
return node;
}