parent
428e8b3160
commit
2c197cdeef
106
src/index.ts
106
src/index.ts
|
@ -1,43 +1,45 @@
|
|||
// @ts-ignore
|
||||
import { DhtNode } from "@hyperswarm/dht-relay";
|
||||
import DhtNode from "@hyperswarm/dht-relay";
|
||||
// @ts-ignore
|
||||
import Stream from "@hyperswarm/dht-relay/ws";
|
||||
// @ts-ignore
|
||||
import createPool from "websocket-pool";
|
||||
// @ts-ignore
|
||||
import createRoundRobin from "@derhuerst/round-robin-scheduler";
|
||||
import { Buffer } from "buffer";
|
||||
import { blake2b } from "libskynet";
|
||||
import { registryRead } from "libkernel";
|
||||
import { errTuple } from "libskynet";
|
||||
// @ts-ignore
|
||||
import {Buffer} from "buffer";
|
||||
// @ts-ignore
|
||||
import {blake2b} from "libskynet";
|
||||
// @ts-ignore
|
||||
import {registryRead} from "libkmodule";
|
||||
// @ts-ignore
|
||||
import {errTuple} from "libskynet";
|
||||
|
||||
const REGISTRY_DHT_KEY = "lumeweb-dht-relay";
|
||||
const IP_REGEX =
|
||||
/^(?:(?:2[0-4]\d|25[0-5]|1\d{2}|[1-9]?\d)\.){3}(?:2[0-4]\d|25[0-5]|1\d{2}|[1-9]?\d)$/;
|
||||
import {unpack} from "msgpackr";
|
||||
|
||||
const REGISTRY_DHT_KEY = "lumeweb-dht-node";
|
||||
|
||||
export default class DHT {
|
||||
private _dht: DhtNode;
|
||||
private _wsPool: DhtNode;
|
||||
private _wsPool: createRoundRobin.RoundRobin;
|
||||
private _options: any;
|
||||
private _relays: { [pubkey: string]: string } = {};
|
||||
|
||||
constructor() {
|
||||
this._wsPool = createPool(WebSocket, createRoundRobin);
|
||||
this._dht = new DhtNode(new Stream(true, this._wsPool));
|
||||
return this.setupProxy();
|
||||
constructor(opts = {}) {
|
||||
// @ts-ignore
|
||||
opts.custodial = true;
|
||||
this._options = opts;
|
||||
this._wsPool = createRoundRobin();
|
||||
}
|
||||
|
||||
static get IS_WEB() {
|
||||
return true;
|
||||
ready(): Promise<void> {
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
private _relays: { [pubkey: string]: () => void } = {};
|
||||
|
||||
get relays(): string[] {
|
||||
return Object.keys(this._relays);
|
||||
}
|
||||
|
||||
public async addRelay(pubkey: string): Promise<boolean> {
|
||||
let entry: errTuple = await registryRead(
|
||||
stringToUint8ArrayUtf8(pubkey),
|
||||
Uint8Array.from(Buffer.from(pubkey, "hex")),
|
||||
hashDataKey(REGISTRY_DHT_KEY)
|
||||
);
|
||||
|
||||
|
@ -45,23 +47,24 @@ export default class DHT {
|
|||
return false;
|
||||
}
|
||||
|
||||
const host = Buffer.from(entry[0].entryData).toString("utf8") as string;
|
||||
const [ip, port] = host.split(".");
|
||||
if (!IP_REGEX.test(ip)) {
|
||||
let host;
|
||||
|
||||
try {
|
||||
host = unpack(entry[0].entryData);
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const [domain, port] = host.split(":");
|
||||
|
||||
if (isNaN(parseInt(port))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const connection = `ws://${ip}:${port}/`;
|
||||
const connection = `wss://${domain}:${port}/`;
|
||||
|
||||
if (!(await this.isServerAvailable(connection))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
this._relays[pubkey] = this._wsPool.add(connection);
|
||||
this._wsPool.add(connection);
|
||||
this._relays[pubkey] = connection;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -71,14 +74,14 @@ export default class DHT {
|
|||
return false;
|
||||
}
|
||||
|
||||
this._relays[pubkey]();
|
||||
this._wsPool.remove(this._relays[pubkey]);
|
||||
delete this._relays[pubkey];
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public clearRelays(): void {
|
||||
this._wsPool.close();
|
||||
this._wsPool = createRoundRobin();
|
||||
this._relays = {};
|
||||
}
|
||||
|
||||
|
@ -95,27 +98,28 @@ export default class DHT {
|
|||
});
|
||||
}
|
||||
|
||||
private setupProxy() {
|
||||
return new Proxy(this, {
|
||||
get(target, name: string) {
|
||||
if (!target.hasOwnProperty(name)) {
|
||||
if (!target._dht.hasOwnProperty(name)) {
|
||||
throw new Error(`Cannot access the ${name} property`);
|
||||
}
|
||||
return target._dht[target];
|
||||
} else {
|
||||
// @ts-ignore
|
||||
return target[name];
|
||||
}
|
||||
},
|
||||
has(target, name: string) {
|
||||
if (!target.hasOwnProperty(name)) {
|
||||
return target._dht.hasOwnProperty(name);
|
||||
async connect(pubkey: string, options = {}): Promise<DhtNode> {
|
||||
const relay = await this.getAvailableRelay();
|
||||
|
||||
if (!relay) {
|
||||
throw new Error("Failed to find an available relay");
|
||||
}
|
||||
|
||||
return true;
|
||||
},
|
||||
});
|
||||
const node = new DhtNode(new Stream(true, new WebSocket(relay as string)), this._options);
|
||||
await node.ready();
|
||||
|
||||
return node.connect(pubkey, options)
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue