2022-07-20 06:06:55 +00:00
|
|
|
// @ts-ignore
|
2022-07-20 05:59:16 +00:00
|
|
|
import DhtNode from "@hyperswarm/dht-relay";
|
2022-07-20 06:06:55 +00:00
|
|
|
// @ts-ignore
|
2022-06-27 22:22:53 +00:00
|
|
|
import Stream from "@hyperswarm/dht-relay/ws";
|
2023-01-31 10:10:34 +00:00
|
|
|
import { createClient } from "@lumeweb/kernel-peer-discovery-client";
|
|
|
|
import { load } from "@lumeweb/libkernel-universal";
|
2022-07-26 23:28:53 +00:00
|
|
|
import randomNumber from "random-number-csprng";
|
2023-01-31 10:43:58 +00:00
|
|
|
import EventEmitter from "eventemitter2";
|
2023-01-31 10:10:34 +00:00
|
|
|
export default class HyperswarmWeb extends EventEmitter {
|
2022-07-20 06:06:55 +00:00
|
|
|
_options;
|
2023-01-31 10:10:34 +00:00
|
|
|
_relays = new Set();
|
|
|
|
_activeRelay;
|
|
|
|
_discovery;
|
2023-01-31 11:58:39 +00:00
|
|
|
_queuedEmActions = [];
|
2022-07-20 06:06:55 +00:00
|
|
|
constructor(opts = {}) {
|
2023-01-31 10:10:34 +00:00
|
|
|
super();
|
2022-07-21 18:58:32 +00:00
|
|
|
opts.custodial = false;
|
2022-07-20 06:06:55 +00:00
|
|
|
this._options = opts;
|
2023-01-31 10:10:34 +00:00
|
|
|
this._discovery = createClient();
|
2022-06-27 22:22:53 +00:00
|
|
|
}
|
2022-07-20 06:06:55 +00:00
|
|
|
ready() {
|
2023-01-31 10:10:34 +00:00
|
|
|
return this.ensureConnection();
|
2022-06-27 22:22:53 +00:00
|
|
|
}
|
2023-01-31 10:10:34 +00:00
|
|
|
async ensureConnection() {
|
|
|
|
const logErr = (await load()).logErr;
|
|
|
|
if (this._activeRelay) {
|
|
|
|
return;
|
2022-07-20 06:06:55 +00:00
|
|
|
}
|
2023-01-31 10:10:34 +00:00
|
|
|
const relays = this.relays;
|
|
|
|
do {
|
2023-01-31 12:39:11 +00:00
|
|
|
const index = relays.length > 1 ? await randomNumber(0, relays.length - 1) : 0;
|
2023-01-31 10:10:34 +00:00
|
|
|
const relay = relays[index];
|
|
|
|
let ret;
|
|
|
|
try {
|
|
|
|
ret = await this._discovery.discover(relay);
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
logErr(e);
|
|
|
|
relays.splice(index, 1);
|
|
|
|
continue;
|
|
|
|
}
|
2023-01-31 13:49:16 +00:00
|
|
|
if (!ret) {
|
|
|
|
relays.splice(index, 1);
|
|
|
|
continue;
|
|
|
|
}
|
2023-01-31 10:10:34 +00:00
|
|
|
ret = ret;
|
|
|
|
const connection = `wss://${ret.host}:${ret.port}`;
|
|
|
|
if (!(await this.isServerAvailable(connection))) {
|
|
|
|
relays.splice(index, 1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
this._activeRelay = new DhtNode(new Stream(true, new WebSocket(connection)), this._options);
|
|
|
|
this._activeRelay.on("close", () => {
|
|
|
|
this._activeRelay = undefined;
|
|
|
|
});
|
2023-02-01 09:42:29 +00:00
|
|
|
} while (relays.length > 0 && !this._activeRelay);
|
2023-01-31 10:10:34 +00:00
|
|
|
if (!this._activeRelay) {
|
|
|
|
throw new Error("Failed to find an available relay");
|
2022-07-26 23:28:53 +00:00
|
|
|
}
|
2023-01-31 11:58:39 +00:00
|
|
|
this._processQueuedActions();
|
2023-01-31 10:10:34 +00:00
|
|
|
await this._activeRelay.dht.ready();
|
2022-07-20 06:06:55 +00:00
|
|
|
}
|
|
|
|
async isServerAvailable(connection) {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
const ws = new WebSocket(connection);
|
|
|
|
ws.addEventListener("open", () => {
|
|
|
|
ws.close();
|
|
|
|
resolve(true);
|
|
|
|
});
|
|
|
|
ws.addEventListener("error", () => {
|
|
|
|
resolve(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
async connect(pubkey, options = {}) {
|
2023-01-31 10:10:34 +00:00
|
|
|
if (!this._activeRelay) {
|
|
|
|
await this.ensureConnection();
|
2022-07-20 06:06:55 +00:00
|
|
|
}
|
2023-01-31 10:10:34 +00:00
|
|
|
return this._activeRelay.connect(pubkey, options);
|
2022-07-20 06:06:55 +00:00
|
|
|
}
|
2023-01-31 10:10:34 +00:00
|
|
|
get relays() {
|
|
|
|
return [...this._relays.values()];
|
|
|
|
}
|
|
|
|
async addRelay(pubkey) {
|
|
|
|
this._relays.add(pubkey);
|
|
|
|
}
|
|
|
|
removeRelay(pubkey) {
|
|
|
|
if (!this._relays.has(pubkey)) {
|
|
|
|
return false;
|
2022-07-20 06:06:55 +00:00
|
|
|
}
|
2023-01-31 10:10:34 +00:00
|
|
|
this._relays.delete(pubkey);
|
|
|
|
return true;
|
2022-06-27 22:22:53 +00:00
|
|
|
}
|
2023-01-31 10:10:34 +00:00
|
|
|
clearRelays() {
|
|
|
|
this._relays.clear();
|
|
|
|
}
|
|
|
|
on(eventName, listener) {
|
2023-02-01 09:51:05 +00:00
|
|
|
return this._processOrQueueAction("on", ...arguments);
|
2023-01-31 10:10:34 +00:00
|
|
|
}
|
|
|
|
addListener(eventName, listener) {
|
|
|
|
return this.on(eventName, listener);
|
|
|
|
}
|
|
|
|
off(eventName, listener) {
|
2023-02-01 09:51:05 +00:00
|
|
|
return this._processOrQueueAction("off", ...arguments);
|
2023-01-31 10:10:34 +00:00
|
|
|
}
|
|
|
|
removeListener(eventName, listener) {
|
2023-01-31 11:58:39 +00:00
|
|
|
return this.off(eventName, listener);
|
2023-01-31 10:10:34 +00:00
|
|
|
}
|
|
|
|
emit(eventName, ...args) {
|
2023-02-01 09:51:05 +00:00
|
|
|
return this._processOrQueueAction("emit", ...arguments);
|
2023-01-31 10:10:34 +00:00
|
|
|
}
|
|
|
|
once(eventName, listener) {
|
2023-02-01 09:51:05 +00:00
|
|
|
return this._processOrQueueAction("once", ...arguments);
|
2023-01-31 11:58:39 +00:00
|
|
|
}
|
|
|
|
_processOrQueueAction(method, ...args) {
|
|
|
|
if (this._activeRelay) {
|
|
|
|
return this._activeRelay[method](...args);
|
|
|
|
}
|
|
|
|
this._queuedEmActions.push([method, args]);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
_processQueuedActions() {
|
|
|
|
for (const action of this._queuedEmActions) {
|
|
|
|
this._activeRelay[action[0]](...action[1]);
|
|
|
|
}
|
|
|
|
this._queuedEmActions = [];
|
2022-07-20 06:06:55 +00:00
|
|
|
}
|
2022-06-27 22:22:53 +00:00
|
|
|
}
|