*prettier
This commit is contained in:
parent
c7b64064ad
commit
2e71082a59
38
src/index.ts
38
src/index.ts
|
@ -5,14 +5,14 @@ import Stream from "@hyperswarm/dht-relay/ws";
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import createRoundRobin from "@derhuerst/round-robin-scheduler";
|
import createRoundRobin from "@derhuerst/round-robin-scheduler";
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import {Buffer} from "buffer";
|
import { Buffer } from "buffer";
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import {blake2b, errTuple} from "libskynet";
|
import { blake2b, errTuple } from "libskynet";
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import {registryRead} from "libkmodule";
|
import { registryRead } from "libkmodule";
|
||||||
|
|
||||||
import {unpack} from "msgpackr";
|
import { unpack } from "msgpackr";
|
||||||
import randomNumber from "random-number-csprng";
|
import randomNumber from "random-number-csprng";
|
||||||
|
|
||||||
const REGISTRY_DHT_KEY = "lumeweb-dht-node";
|
const REGISTRY_DHT_KEY = "lumeweb-dht-node";
|
||||||
|
@ -81,13 +81,12 @@ export default class DHT {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (this._activeRelays.has(pubkey)) {
|
if (this._activeRelays.has(pubkey)) {
|
||||||
this._activeRelays.get(pubkey).destroy();
|
this._activeRelays.get(pubkey).destroy();
|
||||||
this._activeRelays.delete(pubkey)
|
this._activeRelays.delete(pubkey);
|
||||||
}
|
}
|
||||||
|
|
||||||
this._relays.delete(pubkey)
|
this._relays.delete(pubkey);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -114,18 +113,27 @@ export default class DHT {
|
||||||
throw new Error("Failed to find an available relay");
|
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> {
|
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 = [];
|
let relayPromises = [];
|
||||||
if (0 > available.length) {
|
if (0 > available.length) {
|
||||||
return;
|
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 relayIndex = await randomNumber(0, available.length - 1);
|
||||||
|
|
||||||
const connection = available[relayIndex];
|
const connection = available[relayIndex];
|
||||||
|
@ -134,7 +142,13 @@ export default class DHT {
|
||||||
continue;
|
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);
|
this._activeRelays.set(available[relayIndex], node);
|
||||||
|
|
||||||
relayPromises.push(node.ready());
|
relayPromises.push(node.ready());
|
||||||
|
|
Loading…
Reference in New Issue