*Update dist
This commit is contained in:
parent
685bc59894
commit
a0ecea1dc9
|
@ -1,8 +1,10 @@
|
||||||
import DhtNode from "@hyperswarm/dht-relay";
|
import DhtNode from "@hyperswarm/dht-relay";
|
||||||
export default class DHT {
|
export default class DHT {
|
||||||
private _wsPool;
|
|
||||||
private _options;
|
private _options;
|
||||||
private _relays;
|
private _relays;
|
||||||
|
private _activeRelays;
|
||||||
|
private _maxConnections;
|
||||||
|
private _inited;
|
||||||
constructor(opts?: {});
|
constructor(opts?: {});
|
||||||
ready(): Promise<void>;
|
ready(): Promise<void>;
|
||||||
get relays(): string[];
|
get relays(): string[];
|
||||||
|
@ -11,7 +13,7 @@ export default class DHT {
|
||||||
clearRelays(): void;
|
clearRelays(): void;
|
||||||
private isServerAvailable;
|
private isServerAvailable;
|
||||||
connect(pubkey: string, options?: {}): Promise<DhtNode>;
|
connect(pubkey: string, options?: {}): Promise<DhtNode>;
|
||||||
getAvailableRelay(): Promise<string | boolean>;
|
private fillConnections;
|
||||||
}
|
}
|
||||||
export declare function hashDataKey(dataKey: string): Uint8Array;
|
export declare function hashDataKey(dataKey: string): Uint8Array;
|
||||||
//# sourceMappingURL=index.d.ts.map
|
//# sourceMappingURL=index.d.ts.map
|
|
@ -1 +1 @@
|
||||||
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,OAAO,MAAM,uBAAuB,CAAC;AAkB5C,MAAM,CAAC,OAAO,OAAO,GAAG;IACpB,OAAO,CAAC,OAAO,CAA8B;IAC7C,OAAO,CAAC,QAAQ,CAAM;IACtB,OAAO,CAAC,OAAO,CAAoC;gBAEvC,IAAI,KAAK;IAOrB,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAItB,IAAI,MAAM,IAAI,MAAM,EAAE,CAErB;IAEY,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAgChD,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO;IAWpC,WAAW,IAAI,IAAI;YAKZ,iBAAiB;IAazB,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC;IAavD,iBAAiB,IAAI,OAAO,CAAC,MAAM,GAAG,OAAO,CAAC;CAUvD;AAED,wBAAgB,WAAW,CAAC,OAAO,EAAE,MAAM,GAAG,UAAU,CAEvD"}
|
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,OAAO,MAAM,uBAAuB,CAAC;AAkB5C,MAAM,CAAC,OAAO,OAAO,GAAG;IACpB,OAAO,CAAC,QAAQ,CAAM;IACtB,OAAO,CAAC,OAAO,CAAkC;IACjD,OAAO,CAAC,aAAa,CAA0C;IAC/D,OAAO,CAAC,eAAe,CAAM;IAC7B,OAAO,CAAC,OAAO,CAAS;gBAEZ,IAAI,KAAK;IAMrB,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAKtB,IAAI,MAAM,IAAI,MAAM,EAAE,CAErB;IAEY,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAiChD,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO;IAgBpC,WAAW,IAAI,IAAI;YAIZ,iBAAiB;IAazB,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC;YAU/C,eAAe;CAoBhC;AAED,wBAAgB,WAAW,CAAC,OAAO,EAAE,MAAM,GAAG,UAAU,CAEvD"}
|
|
@ -3,30 +3,32 @@ import DhtNode from "@hyperswarm/dht-relay";
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import Stream from "@hyperswarm/dht-relay/ws";
|
import Stream from "@hyperswarm/dht-relay/ws";
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import createRoundRobin from "@derhuerst/round-robin-scheduler";
|
|
||||||
// @ts-ignore
|
|
||||||
import { Buffer } from "buffer";
|
import { Buffer } from "buffer";
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
|
// @ts-ignore
|
||||||
import { blake2b } from "libskynet";
|
import { blake2b } 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";
|
||||||
const REGISTRY_DHT_KEY = "lumeweb-dht-node";
|
const REGISTRY_DHT_KEY = "lumeweb-dht-node";
|
||||||
export default class DHT {
|
export default class DHT {
|
||||||
_wsPool;
|
|
||||||
_options;
|
_options;
|
||||||
_relays = {};
|
_relays = new Map();
|
||||||
|
_activeRelays = new Map();
|
||||||
|
_maxConnections = 10;
|
||||||
|
_inited = false;
|
||||||
constructor(opts = {}) {
|
constructor(opts = {}) {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
opts.custodial = false;
|
opts.custodial = false;
|
||||||
this._options = opts;
|
this._options = opts;
|
||||||
this._wsPool = createRoundRobin();
|
|
||||||
}
|
}
|
||||||
ready() {
|
ready() {
|
||||||
return Promise.resolve();
|
this._inited = true;
|
||||||
|
return this.fillConnections();
|
||||||
}
|
}
|
||||||
get relays() {
|
get relays() {
|
||||||
return Object.keys(this._relays);
|
return [...this._relays.keys()];
|
||||||
}
|
}
|
||||||
async addRelay(pubkey) {
|
async addRelay(pubkey) {
|
||||||
let entry = await registryRead(Uint8Array.from(Buffer.from(pubkey, "hex")), hashDataKey(REGISTRY_DHT_KEY));
|
let entry = await registryRead(Uint8Array.from(Buffer.from(pubkey, "hex")), hashDataKey(REGISTRY_DHT_KEY));
|
||||||
|
@ -44,22 +46,25 @@ export default class DHT {
|
||||||
if (isNaN(parseInt(port))) {
|
if (isNaN(parseInt(port))) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
const connection = `wss://${domain}:${port}/`;
|
this._relays[pubkey] = `wss://${domain}:${port}/`;
|
||||||
this._wsPool.add(connection);
|
if (this._inited) {
|
||||||
this._relays[pubkey] = connection;
|
await this.fillConnections();
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
removeRelay(pubkey) {
|
removeRelay(pubkey) {
|
||||||
if (!(pubkey in this._relays)) {
|
if (!this._relays.has(pubkey)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
this._wsPool.remove(this._relays[pubkey]);
|
if (this._activeRelays.has(pubkey)) {
|
||||||
delete this._relays[pubkey];
|
this._activeRelays.get(pubkey).destroy();
|
||||||
|
this._activeRelays.delete(pubkey);
|
||||||
|
}
|
||||||
|
this._relays.delete(pubkey);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
clearRelays() {
|
clearRelays() {
|
||||||
this._wsPool = createRoundRobin();
|
[...this._relays.keys()].forEach(this.removeRelay);
|
||||||
this._relays = {};
|
|
||||||
}
|
}
|
||||||
async isServerAvailable(connection) {
|
async isServerAvailable(connection) {
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
|
@ -74,22 +79,26 @@ export default class DHT {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
async connect(pubkey, options = {}) {
|
async connect(pubkey, options = {}) {
|
||||||
const relay = await this.getAvailableRelay();
|
if (this._activeRelays.size === 0) {
|
||||||
if (!relay) {
|
|
||||||
throw new Error("Failed to find an available relay");
|
throw new Error("Failed to find an available relay");
|
||||||
}
|
}
|
||||||
const node = new DhtNode(new Stream(true, new WebSocket(relay)), this._options);
|
const node = this._activeRelays.get([...this._activeRelays.keys()][await randomNumber(0, this._activeRelays.size - 1)]);
|
||||||
await node.ready();
|
|
||||||
return node.connect(pubkey, options);
|
return node.connect(pubkey, options);
|
||||||
}
|
}
|
||||||
async getAvailableRelay() {
|
async fillConnections() {
|
||||||
for (let i = 0; i < this._wsPool.length; i++) {
|
let available = [...this._relays.keys()].filter(x => [...this._activeRelays.keys()].includes(x));
|
||||||
const relay = this._wsPool.get();
|
let relayPromises = [];
|
||||||
if (await this.isServerAvailable(relay)) {
|
while (this._activeRelays.size <= Math.min(this._maxConnections, available.length + this._activeRelays.size)) {
|
||||||
return relay;
|
const relayIndex = await randomNumber(0, available.length - 1);
|
||||||
|
const connection = available[relayIndex];
|
||||||
|
if (!this.isServerAvailable(connection)) {
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
const node = new DhtNode(new Stream(true, new WebSocket(this._activeRelays.get(connection))), this._options);
|
||||||
|
this._activeRelays.set(available[relayIndex], node);
|
||||||
|
relayPromises.push(node.ready());
|
||||||
}
|
}
|
||||||
return false;
|
return Promise.allSettled(relayPromises);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
export function hashDataKey(dataKey) {
|
export function hashDataKey(dataKey) {
|
||||||
|
|
Loading…
Reference in New Issue