*prettier

This commit is contained in:
Derrick Hammer 2022-07-26 21:39:32 -04:00
parent c7b64064ad
commit 2e71082a59
1 changed files with 130 additions and 116 deletions

View File

@ -5,14 +5,14 @@ import Stream from "@hyperswarm/dht-relay/ws";
// @ts-ignore
import createRoundRobin from "@derhuerst/round-robin-scheduler";
// @ts-ignore
import {Buffer} from "buffer";
import { Buffer } from "buffer";
// @ts-ignore
// @ts-ignore
import {blake2b, errTuple} from "libskynet";
import { blake2b, errTuple } from "libskynet";
// @ts-ignore
import {registryRead} from "libkmodule";
import { registryRead } from "libkmodule";
import {unpack} from "msgpackr";
import { unpack } from "msgpackr";
import randomNumber from "random-number-csprng";
const REGISTRY_DHT_KEY = "lumeweb-dht-node";
@ -81,13 +81,12 @@ export default class DHT {
return false;
}
if (this._activeRelays.has(pubkey)) {
this._activeRelays.get(pubkey).destroy();
this._activeRelays.delete(pubkey)
this._activeRelays.delete(pubkey);
}
this._relays.delete(pubkey)
this._relays.delete(pubkey);
return true;
}
@ -114,18 +113,27 @@ export default class DHT {
throw new Error("Failed to find an available relay");
}
const node = this._activeRelays.get([...this._activeRelays.keys()][await randomNumber(0, this._activeRelays.size - 1)]);
const node = this._activeRelays.get(
[...this._activeRelays.keys()][
await randomNumber(0, this._activeRelays.size - 1)
]
);
return node.connect(pubkey, options)
return node.connect(pubkey, options);
}
private async fillConnections(): Promise<any> {
let available = [...this._relays.keys()].filter(x => [...this._activeRelays.keys()].includes(x));
let available = [...this._relays.keys()].filter((x) =>
[...this._activeRelays.keys()].includes(x)
);
let relayPromises = [];
if (0 > available.length) {
return;
}
while (this._activeRelays.size <= Math.min(this._maxConnections, available.length + this._activeRelays.size)) {
while (
this._activeRelays.size <=
Math.min(this._maxConnections, available.length + this._activeRelays.size)
) {
const relayIndex = await randomNumber(0, available.length - 1);
const connection = available[relayIndex];
@ -134,7 +142,13 @@ export default class DHT {
continue;
}
const node = new DhtNode(new Stream(true, new WebSocket(this._activeRelays.get(connection) as string)), this._options);
const node = new DhtNode(
new Stream(
true,
new WebSocket(this._activeRelays.get(connection) as string)
),
this._options
);
this._activeRelays.set(available[relayIndex], node);
relayPromises.push(node.ready());