hyperswarm-web/src/index.ts

150 lines
3.8 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";
// @ts-ignore
import createRoundRobin from "@derhuerst/round-robin-scheduler";
2022-07-20 05:55:44 +00:00
// @ts-ignore
import {Buffer} from "buffer";
// @ts-ignore
import {blake2b} from "libskynet";
// @ts-ignore
import {registryRead} from "libkmodule";
// @ts-ignore
import {errTuple} from "libskynet";
import {unpack} from "msgpackr";
2022-06-27 22:21:31 +00:00
2022-07-20 05:55:44 +00:00
const REGISTRY_DHT_KEY = "lumeweb-dht-node";
2022-06-27 22:21:31 +00:00
export default class DHT {
2022-07-20 05:55:44 +00:00
private _wsPool: createRoundRobin.RoundRobin;
private _options: any;
private _relays: { [pubkey: string]: string } = {};
constructor(opts = {}) {
// @ts-ignore
opts.custodial = true;
this._options = opts;
this._wsPool = createRoundRobin();
}
2022-06-27 22:21:31 +00:00
2022-07-20 05:55:44 +00:00
ready(): Promise<void> {
return Promise.resolve();
}
2022-06-27 22:21:31 +00:00
2022-07-20 05:55:44 +00:00
get relays(): string[] {
return Object.keys(this._relays);
}
2022-06-27 22:21:31 +00:00
2022-07-20 05:55:44 +00:00
public async addRelay(pubkey: string): Promise<boolean> {
let entry: errTuple = await registryRead(
Uint8Array.from(Buffer.from(pubkey, "hex")),
hashDataKey(REGISTRY_DHT_KEY)
);
2022-06-27 22:21:31 +00:00
2022-07-20 05:55:44 +00:00
if (entry[1] || !entry[0]?.exists) {
return false;
}
2022-06-27 22:21:31 +00:00
2022-07-20 05:55:44 +00:00
let host;
2022-06-27 22:21:31 +00:00
2022-07-20 05:55:44 +00:00
try {
host = unpack(entry[0].entryData);
} catch (e) {
return false;
}
const [domain, port] = host.split(":");
if (isNaN(parseInt(port))) {
return false;
}
2022-06-27 22:21:31 +00:00
2022-07-20 05:55:44 +00:00
const connection = `wss://${domain}:${port}/`;
this._wsPool.add(connection);
this._relays[pubkey] = connection;
return true;
2022-06-27 22:21:31 +00:00
}
2022-07-20 05:55:44 +00:00
public removeRelay(pubkey: string): boolean {
if (!(pubkey in this._relays)) {
return false;
}
this._wsPool.remove(this._relays[pubkey]);
delete this._relays[pubkey];
return true;
2022-06-27 22:21:31 +00:00
}
2022-07-20 05:55:44 +00:00
public clearRelays(): void {
this._wsPool = createRoundRobin();
this._relays = {};
}
2022-06-27 22:21:31 +00:00
2022-07-20 05:55:44 +00:00
private async isServerAvailable(connection: string): Promise<boolean> {
return new Promise((resolve) => {
const ws = new WebSocket(connection);
ws.addEventListener("open", () => {
ws.close();
resolve(true);
});
ws.addEventListener("error", () => {
resolve(false);
});
});
2022-06-27 22:21:31 +00:00
}
2022-07-20 05:55:44 +00:00
async connect(pubkey: string, options = {}): Promise<DhtNode> {
const relay = await this.getAvailableRelay();
if (!relay) {
throw new Error("Failed to find an available relay");
}
2022-06-27 22:21:31 +00:00
2022-07-20 05:55:44 +00:00
const node = new DhtNode(new Stream(true, new WebSocket(relay as string)), this._options);
await node.ready();
2022-06-27 22:21:31 +00:00
2022-07-20 05:55:44 +00:00
return node.connect(pubkey, options)
2022-06-27 22:21:31 +00:00
}
2022-07-20 05:55:44 +00:00
async getAvailableRelay(): Promise<string | boolean> {
for (let i = 0; i < this._wsPool.length; i++) {
const relay = this._wsPool.get();
if (await this.isServerAvailable(relay)) {
return relay;
}
2022-06-27 22:21:31 +00:00
}
2022-07-20 05:55:44 +00:00
return false;
}
2022-06-27 22:21:31 +00:00
}
export function hashDataKey(dataKey: string): Uint8Array {
2022-07-20 05:55:44 +00:00
return blake2b(encodeUtf8String(dataKey));
2022-06-27 22:21:31 +00:00
}
function encodeUtf8String(str: string): Uint8Array {
2022-07-20 05:55:44 +00:00
const byteArray = stringToUint8ArrayUtf8(str);
const encoded = new Uint8Array(8 + byteArray.length);
encoded.set(encodeNumber(byteArray.length));
encoded.set(byteArray, 8);
return encoded;
2022-06-27 22:21:31 +00:00
}
function stringToUint8ArrayUtf8(str: string): Uint8Array {
2022-07-20 05:55:44 +00:00
return Uint8Array.from(Buffer.from(str, "utf-8"));
2022-06-27 22:21:31 +00:00
}
function encodeNumber(num: number): Uint8Array {
2022-07-20 05:55:44 +00:00
const encoded = new Uint8Array(8);
for (let index = 0; index < encoded.length; index++) {
encoded[index] = num & 0xff;
num = num >> 8;
}
return encoded;
2022-06-27 22:21:31 +00:00
}