hyperswarm-web/src/index.ts

272 lines
6.4 KiB
TypeScript
Raw Normal View History

2022-06-27 22:21:31 +00:00
// @ts-ignore
2022-07-20 05:55:44 +00:00
import DhtNode from "@hyperswarm/dht-relay";
2022-06-27 22:21:31 +00:00
// @ts-ignore
import Stream from "@hyperswarm/dht-relay/ws";
2023-01-31 10:10:12 +00:00
import { createClient } from "@lumeweb/kernel-peer-discovery-client";
import type {
PeerDiscoveryClient,
Peer,
} from "@lumeweb/kernel-peer-discovery-client";
// @ts-ignore
import Hyperswarm from "hyperswarm";
import randomNumber from "random-number-csprng";
import EventEmitter, { OnOptions } from "eventemitter2";
import { Mutex } from "async-mutex";
2023-07-01 06:31:46 +00:00
import { logErr } from "@lumeweb/libkernel";
2022-06-27 22:21:31 +00:00
2023-07-01 06:31:46 +00:00
export default class HyperswarmWeb extends EventEmitter.default {
2022-07-27 01:39:32 +00:00
private _options: any;
2023-01-31 10:10:12 +00:00
private _discovery: PeerDiscoveryClient;
private _queuedEmActions: [string, any][] = [];
private _connectionMutex: Mutex = new Mutex();
2023-02-17 13:04:16 +00:00
2023-01-31 10:10:12 +00:00
constructor(opts: any = {}) {
super();
2022-07-27 01:39:32 +00:00
opts.custodial = false;
this._options = opts;
2023-01-31 10:10:12 +00:00
this._discovery = createClient();
2022-07-27 01:39:32 +00:00
}
2023-02-17 13:04:16 +00:00
private _relays: Set<string> = new Set();
get relays(): string[] {
return [...this._relays.values()];
}
private _activeRelay: Hyperswarm;
2023-02-01 13:36:43 +00:00
get activeRelay(): Hyperswarm {
return this._activeRelay;
}
2023-02-17 13:04:16 +00:00
private _ready = false;
get ready(): boolean {
return this._ready;
}
2023-02-01 13:22:30 +00:00
init(): Promise<void> {
2023-01-31 10:10:12 +00:00
return this.ensureConnection();
2022-08-14 11:25:16 +00:00
}
2023-02-17 13:04:16 +00:00
async connect(pubkey: string, options = {}): Promise<DhtNode> {
if (!this._activeRelay) {
await this.ensureConnection();
}
return this._activeRelay.connect(pubkey, options);
}
public async addRelay(pubkey: string): Promise<void> {
this._relays.add(pubkey);
}
public removeRelay(pubkey: string): boolean {
if (!this._relays.has(pubkey)) {
return false;
}
this._relays.delete(pubkey);
return true;
}
public clearRelays(): void {
this._relays.clear();
}
on(
eventName: string | symbol,
2023-07-01 06:31:46 +00:00
listener: (...args: any[]) => void,
2023-02-17 13:04:16 +00:00
): Hyperswarm {
return this._processOrQueueAction("on", ...arguments);
}
onSelf(
eventName: string | symbol,
listener: (...args: any[]) => void,
2023-07-01 06:31:46 +00:00
options?: boolean | OnOptions,
): Hyperswarm {
return super.on(eventName, listener, options);
}
2023-02-17 13:04:16 +00:00
addListener(
eventName: string | symbol,
2023-07-01 06:31:46 +00:00
listener: (...args: any[]) => void,
2023-02-17 13:04:16 +00:00
): this {
return this.on(eventName, listener);
}
off(
eventName: string | symbol,
2023-07-01 06:31:46 +00:00
listener: (...args: any[]) => void,
2023-02-17 13:04:16 +00:00
): Hyperswarm {
return this._processOrQueueAction("off", ...arguments);
}
2023-03-19 17:23:07 +00:00
offSelf(
eventName: string | symbol,
2023-07-01 06:31:46 +00:00
listener: (...args: any[]) => void,
2023-03-19 17:23:07 +00:00
): Hyperswarm {
return super.off(eventName, listener);
}
2023-02-17 13:04:16 +00:00
removeListener(
eventName: string | symbol,
2023-07-01 06:31:46 +00:00
listener: (...args: any[]) => void,
2023-02-17 13:04:16 +00:00
): this {
return this.off(eventName, listener);
}
emit(eventName: string | symbol, ...args: any[]): boolean {
return this._processOrQueueAction("emit", ...arguments);
}
emitSelf(eventName: string | symbol, ...args: any[]): boolean {
return super.emit(eventName, ...args);
}
2023-02-17 13:04:16 +00:00
once(eventName: string | symbol, listener: (...args: any[]) => void): this {
return this._processOrQueueAction("once", ...arguments);
}
2023-03-19 17:23:07 +00:00
onceSelf(
eventName: string | symbol,
2023-07-01 06:31:46 +00:00
listener: (...args: any[]) => void,
2023-03-19 17:23:07 +00:00
): this {
return this.once(eventName, listener);
}
2023-02-17 13:04:16 +00:00
public join(topic: Uint8Array, opts = {}): void {
return this._processOrQueueAction("join", ...arguments);
}
public joinPeer(publicKey: Uint8Array): void {
return this._processOrQueueAction("joinPeer", ...arguments);
}
public leave(topic: Uint8Array): void {
return this._processOrQueueAction("leave", ...arguments);
}
public leavePeer(publicKey: Uint8Array): void {
return this._processOrQueueAction("leavePeer", ...arguments);
}
public status(publicKey: Uint8Array) {
return this._activeRelay?.status(publicKey);
}
public topics(): string[] {
return this._activeRelay?.topics();
}
public async flush(): Promise<any> {
return this._activeRelay?.flush();
}
public async clear(): Promise<any> {
return this._activeRelay?.clear();
}
2023-01-31 10:10:12 +00:00
private async ensureConnection(): Promise<any> {
2023-03-29 18:57:18 +00:00
await this._connectionMutex.acquire();
2023-01-31 10:10:12 +00:00
if (this._activeRelay) {
this._connectionMutex.release();
2023-01-31 10:10:12 +00:00
return;
2022-07-27 01:39:32 +00:00
}
2022-07-20 05:55:44 +00:00
2023-01-31 10:10:12 +00:00
const relays = this.relays;
2022-06-27 22:21:31 +00:00
if (relays.length > 0) {
do {
const index =
relays.length > 1 ? await randomNumber(0, relays.length - 1) : 0;
const relay = relays[index];
let ret;
try {
ret = await this._discovery.discover(relay);
} catch (e) {
logErr(e);
relays.splice(index, 1);
continue;
}
if (!ret) {
relays.splice(index, 1);
continue;
}
ret = ret as Peer;
const connection = `wss://${ret.host}:${ret.port}`;
if (!(await this.isServerAvailable(connection))) {
relays.splice(index, 1);
continue;
}
this._activeRelay = new Hyperswarm({
dht: new DhtNode(
new Stream(true, new WebSocket(connection)),
2023-07-01 06:31:46 +00:00
this._options,
),
keyPair: this._options.keyPair,
});
2023-02-17 21:32:39 +00:00
this._activeRelay.dht._protocol._stream.once("close", () => {
this._activeRelay = undefined;
2023-02-17 13:04:16 +00:00
this._ready = false;
this.emitSelf("close");
});
} while (relays.length > 0 && !this._activeRelay);
}
2022-06-27 22:21:31 +00:00
2023-01-31 10:10:12 +00:00
if (!this._activeRelay) {
this._connectionMutex.release();
2023-01-31 10:10:12 +00:00
throw new Error("Failed to find an available relay");
2022-07-20 05:55:44 +00:00
}
2022-06-27 22:21:31 +00:00
this.emitSelf("init");
this._processQueuedActions();
await this._activeRelay.dht.ready();
this._connectionMutex.release();
2023-02-17 13:04:16 +00:00
this._ready = true;
this.emit("ready");
2022-07-27 01:39:32 +00:00
}
private async isServerAvailable(connection: string): Promise<boolean> {
return new Promise<boolean>((resolve) => {
const ws = new WebSocket(connection);
ws.addEventListener("open", () => {
ws.close();
resolve(true);
});
ws.addEventListener("error", () => {
resolve(false);
});
});
}
private _processOrQueueAction(method: string, ...args: any[]) {
if (this._activeRelay) {
return this._activeRelay[method](...args);
}
this._queuedEmActions.push([method, args]);
return this;
}
private _processQueuedActions(): void {
for (const action of this._queuedEmActions) {
this._activeRelay[action[0]](...action[1]);
}
this._queuedEmActions = [];
2022-07-27 01:39:32 +00:00
}
2022-06-27 22:21:31 +00:00
}