*Update dist
This commit is contained in:
parent
35e394fec4
commit
a41e162b1d
|
@ -1,20 +1,26 @@
|
||||||
|
/// <reference types="node" />
|
||||||
import DhtNode from "@hyperswarm/dht-relay";
|
import DhtNode from "@hyperswarm/dht-relay";
|
||||||
export default class DHT {
|
import Hyperswarm from "hyperswarm";
|
||||||
|
import EventEmitter from "node:events";
|
||||||
|
export default class HyperswarmWeb extends EventEmitter {
|
||||||
private _options;
|
private _options;
|
||||||
private _relays;
|
private _relays;
|
||||||
private _activeRelays;
|
private _activeRelay;
|
||||||
private _maxConnections;
|
private _discovery;
|
||||||
private _inited;
|
constructor(opts?: any);
|
||||||
constructor(opts?: {});
|
|
||||||
ready(): Promise<void>;
|
ready(): Promise<void>;
|
||||||
get relays(): string[];
|
private ensureConnection;
|
||||||
get relayServers(): string[];
|
|
||||||
addRelay(pubkey: string): Promise<boolean>;
|
|
||||||
removeRelay(pubkey: string): boolean;
|
|
||||||
clearRelays(): void;
|
|
||||||
private isServerAvailable;
|
private isServerAvailable;
|
||||||
connect(pubkey: string, options?: {}): Promise<DhtNode>;
|
connect(pubkey: string, options?: {}): Promise<DhtNode>;
|
||||||
private fillConnections;
|
get relays(): string[];
|
||||||
|
addRelay(pubkey: string): Promise<void>;
|
||||||
|
removeRelay(pubkey: string): boolean;
|
||||||
|
clearRelays(): void;
|
||||||
|
on(eventName: string | symbol, listener: (...args: any[]) => void): Hyperswarm;
|
||||||
|
addListener(eventName: string | symbol, listener: (...args: any[]) => void): this;
|
||||||
|
off(eventName: string | symbol, listener: (...args: any[]) => void): Hyperswarm;
|
||||||
|
removeListener(eventName: string | symbol, listener: (...args: any[]) => void): this;
|
||||||
|
emit(eventName: string | symbol, ...args: any[]): boolean;
|
||||||
|
once(eventName: string | symbol, listener: (...args: any[]) => void): this;
|
||||||
}
|
}
|
||||||
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;AAgB5C,MAAM,CAAC,OAAO,OAAO,GAAG;IACtB,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;IAStB,IAAI,MAAM,IAAI,MAAM,EAAE,CAErB;IAED,IAAI,YAAY,IAAI,MAAM,EAAE,CAE3B;IAEY,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAiChD,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO;IAepC,WAAW,IAAI,IAAI;YAIZ,iBAAiB;IAazB,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC;YAmB/C,eAAe;CAkD9B;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;AAW5C,OAAO,UAAU,MAAM,YAAY,CAAC;AAEpC,OAAO,YAAY,MAAM,aAAa,CAAC;AAEvC,MAAM,CAAC,OAAO,OAAO,aAAc,SAAQ,YAAY;IACrD,OAAO,CAAC,QAAQ,CAAM;IACtB,OAAO,CAAC,OAAO,CAA0B;IACzC,OAAO,CAAC,YAAY,CAAa;IACjC,OAAO,CAAC,UAAU,CAAsB;gBAC5B,IAAI,GAAE,GAAQ;IAO1B,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;YAIR,gBAAgB;YAgDhB,iBAAiB;IAYzB,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC;IAQ7D,IAAI,MAAM,IAAI,MAAM,EAAE,CAErB;IAEY,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAI7C,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO;IAUpC,WAAW,IAAI,IAAI;IAI1B,EAAE,CACA,SAAS,EAAE,MAAM,GAAG,MAAM,EAC1B,QAAQ,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,GACjC,UAAU;IAGb,WAAW,CACT,SAAS,EAAE,MAAM,GAAG,MAAM,EAC1B,QAAQ,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,GACjC,IAAI;IAIP,GAAG,CACD,SAAS,EAAE,MAAM,GAAG,MAAM,EAC1B,QAAQ,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,GACjC,UAAU;IAIb,cAAc,CACZ,SAAS,EAAE,MAAM,GAAG,MAAM,EAC1B,QAAQ,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,GACjC,IAAI;IAGP,IAAI,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,OAAO;IAIzD,IAAI,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,EAAE,QAAQ,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,GAAG,IAAI;CAG3E"}
|
|
@ -2,73 +2,57 @@
|
||||||
import DhtNode from "@hyperswarm/dht-relay";
|
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
|
import { createClient } from "@lumeweb/kernel-peer-discovery-client";
|
||||||
// @ts-ignore
|
import { load } from "@lumeweb/libkernel-universal";
|
||||||
import { blake2b, hexToBuf } from "libskynet";
|
|
||||||
// @ts-ignore
|
|
||||||
import { registryRead } from "libkmodule";
|
|
||||||
import { unpack } from "msgpackr";
|
|
||||||
import randomNumber from "random-number-csprng";
|
import randomNumber from "random-number-csprng";
|
||||||
const REGISTRY_DHT_KEY = "lumeweb-dht-node";
|
import EventEmitter from "node:events";
|
||||||
export default class DHT {
|
export default class HyperswarmWeb extends EventEmitter {
|
||||||
_options;
|
_options;
|
||||||
_relays = new Map();
|
_relays = new Set();
|
||||||
_activeRelays = new Map();
|
_activeRelay;
|
||||||
_maxConnections = 10;
|
_discovery;
|
||||||
_inited = false;
|
|
||||||
constructor(opts = {}) {
|
constructor(opts = {}) {
|
||||||
// @ts-ignore
|
super();
|
||||||
opts.custodial = false;
|
opts.custodial = false;
|
||||||
this._options = opts;
|
this._options = opts;
|
||||||
|
this._discovery = createClient();
|
||||||
}
|
}
|
||||||
ready() {
|
ready() {
|
||||||
if (this._inited) {
|
return this.ensureConnection();
|
||||||
return Promise.resolve();
|
|
||||||
}
|
}
|
||||||
this._inited = true;
|
async ensureConnection() {
|
||||||
return this.fillConnections();
|
const logErr = (await load()).logErr;
|
||||||
|
if (this._activeRelay) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
get relays() {
|
const relays = this.relays;
|
||||||
return [...this._relays.keys()];
|
do {
|
||||||
}
|
const index = await randomNumber(0, relays.length - 1);
|
||||||
get relayServers() {
|
const relay = relays[index];
|
||||||
return [...this._relays.values()];
|
let ret;
|
||||||
}
|
|
||||||
async addRelay(pubkey) {
|
|
||||||
let entry = await registryRead(hexToBuf(pubkey).shift(), hashDataKey(REGISTRY_DHT_KEY));
|
|
||||||
if (entry[1] || !entry[0]?.exists) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
let host;
|
|
||||||
try {
|
try {
|
||||||
host = unpack(entry[0].entryData);
|
ret = await this._discovery.discover(relay);
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
return false;
|
logErr(e);
|
||||||
|
relays.splice(index, 1);
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
const [domain, port] = host.split(":");
|
ret = ret;
|
||||||
if (isNaN(parseInt(port))) {
|
const connection = `wss://${ret.host}:${ret.port}`;
|
||||||
return false;
|
if (!(await this.isServerAvailable(connection))) {
|
||||||
|
relays.splice(index, 1);
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
this._relays.set(pubkey, `wss://${domain}:${port}/`);
|
this._activeRelay = new DhtNode(new Stream(true, new WebSocket(connection)), this._options);
|
||||||
if (this._inited) {
|
this._activeRelay.on("close", () => {
|
||||||
await this.fillConnections();
|
this._activeRelay = undefined;
|
||||||
|
});
|
||||||
|
} while (relays.length > 0);
|
||||||
|
if (!this._activeRelay) {
|
||||||
|
throw new Error("Failed to find an available relay");
|
||||||
}
|
}
|
||||||
return true;
|
await this._activeRelay.dht.ready();
|
||||||
}
|
|
||||||
removeRelay(pubkey) {
|
|
||||||
if (!this._relays.has(pubkey)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (this._activeRelays.has(pubkey)) {
|
|
||||||
this._activeRelays.get(pubkey).destroy();
|
|
||||||
this._activeRelays.delete(pubkey);
|
|
||||||
}
|
|
||||||
this._relays.delete(pubkey);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
clearRelays() {
|
|
||||||
[...this._relays.keys()].forEach(this.removeRelay);
|
|
||||||
}
|
}
|
||||||
async isServerAvailable(connection) {
|
async isServerAvailable(connection) {
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
|
@ -83,71 +67,43 @@ export default class DHT {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
async connect(pubkey, options = {}) {
|
async connect(pubkey, options = {}) {
|
||||||
if (this._activeRelays.size === 0) {
|
if (!this._activeRelay) {
|
||||||
await this.fillConnections();
|
await this.ensureConnection();
|
||||||
}
|
}
|
||||||
if (this._activeRelays.size === 0) {
|
return this._activeRelay.connect(pubkey, options);
|
||||||
throw new Error("Failed to find an available relay");
|
|
||||||
}
|
}
|
||||||
let index = 0;
|
get relays() {
|
||||||
if (this._activeRelays.size > 1) {
|
return [...this._relays.values()];
|
||||||
index = await randomNumber(0, this._activeRelays.size - 1);
|
|
||||||
}
|
}
|
||||||
const node = this._activeRelays.get([...this._activeRelays.keys()][index]);
|
async addRelay(pubkey) {
|
||||||
return node.connect(pubkey, options);
|
this._relays.add(pubkey);
|
||||||
}
|
}
|
||||||
async fillConnections() {
|
removeRelay(pubkey) {
|
||||||
let available = [];
|
if (!this._relays.has(pubkey)) {
|
||||||
const updateAvailable = () => {
|
return false;
|
||||||
available = [...this._relays.keys()].filter((x) => ![...this._activeRelays.keys()].includes(x));
|
|
||||||
};
|
|
||||||
updateAvailable();
|
|
||||||
let relayPromises = [];
|
|
||||||
while (this._activeRelays.size <=
|
|
||||||
Math.min(this._maxConnections, available.length)) {
|
|
||||||
if (0 === available.length) {
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
let relayIndex = 0;
|
this._relays.delete(pubkey);
|
||||||
if (available.length > 1) {
|
return true;
|
||||||
relayIndex = await randomNumber(0, available.length - 1);
|
|
||||||
}
|
}
|
||||||
const pubkey = available[relayIndex];
|
clearRelays() {
|
||||||
const connection = this._relays.get(available[relayIndex]);
|
this._relays.clear();
|
||||||
if (!(await this.isServerAvailable(connection))) {
|
|
||||||
available.splice(relayIndex, 1);
|
|
||||||
this.removeRelay(available[relayIndex]);
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
const node = new DhtNode(new Stream(true, new WebSocket(connection)), this._options);
|
on(eventName, listener) {
|
||||||
this._activeRelays.set(available[relayIndex], node);
|
return this._activeRelay?.on(eventName, listener);
|
||||||
updateAvailable();
|
|
||||||
relayPromises.push(node.ready());
|
|
||||||
node._protocol._stream.on("close", () => {
|
|
||||||
this._activeRelays.delete(pubkey);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
return Promise.allSettled(relayPromises);
|
addListener(eventName, listener) {
|
||||||
|
return this.on(eventName, listener);
|
||||||
|
}
|
||||||
|
off(eventName, listener) {
|
||||||
|
return this._activeRelay?.off(eventName, listener);
|
||||||
|
}
|
||||||
|
removeListener(eventName, listener) {
|
||||||
|
return this.on(eventName, listener);
|
||||||
|
}
|
||||||
|
emit(eventName, ...args) {
|
||||||
|
return this._activeRelay?.emit(eventName, ...args);
|
||||||
|
}
|
||||||
|
once(eventName, listener) {
|
||||||
|
return this._activeRelay?.once(eventName, listener);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
export function hashDataKey(dataKey) {
|
|
||||||
return blake2b(encodeUtf8String(dataKey));
|
|
||||||
}
|
|
||||||
function encodeUtf8String(str) {
|
|
||||||
const byteArray = stringToUint8ArrayUtf8(str);
|
|
||||||
const encoded = new Uint8Array(8 + byteArray.length);
|
|
||||||
encoded.set(encodeNumber(byteArray.length));
|
|
||||||
encoded.set(byteArray, 8);
|
|
||||||
return encoded;
|
|
||||||
}
|
|
||||||
function stringToUint8ArrayUtf8(str) {
|
|
||||||
return new TextEncoder().encode(str);
|
|
||||||
}
|
|
||||||
function encodeNumber(num) {
|
|
||||||
const encoded = new Uint8Array(8);
|
|
||||||
for (let index = 0; index < encoded.length; index++) {
|
|
||||||
encoded[index] = num & 0xff;
|
|
||||||
num = num >> 8;
|
|
||||||
}
|
|
||||||
return encoded;
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue