From 428e8b31607c289e423697896efc932be443004c Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Mon, 27 Jun 2022 18:22:53 -0400 Subject: [PATCH] *add dist --- dist/index.d.ts | 15 ++++++ dist/index.d.ts.map | 1 + dist/index.js | 114 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 130 insertions(+) create mode 100644 dist/index.d.ts create mode 100644 dist/index.d.ts.map create mode 100644 dist/index.js diff --git a/dist/index.d.ts b/dist/index.d.ts new file mode 100644 index 0000000..ccb1d5f --- /dev/null +++ b/dist/index.d.ts @@ -0,0 +1,15 @@ +export default class DHT { + private _dht; + private _wsPool; + constructor(); + static get IS_WEB(): boolean; + private _relays; + get relays(): string[]; + addRelay(pubkey: string): Promise; + removeRelay(pubkey: string): boolean; + clearRelays(): void; + private isServerAvailable; + private setupProxy; +} +export declare function hashDataKey(dataKey: string): Uint8Array; +//# sourceMappingURL=index.d.ts.map \ No newline at end of file diff --git a/dist/index.d.ts.map b/dist/index.d.ts.map new file mode 100644 index 0000000..0515006 --- /dev/null +++ b/dist/index.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAiBA,MAAM,CAAC,OAAO,OAAO,GAAG;IACtB,OAAO,CAAC,IAAI,CAAU;IACtB,OAAO,CAAC,OAAO,CAAU;;IAQzB,MAAM,KAAK,MAAM,YAEhB;IAED,OAAO,CAAC,OAAO,CAAwC;IAEvD,IAAI,MAAM,IAAI,MAAM,EAAE,CAErB;IAEY,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IA+BhD,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO;IAWpC,WAAW,IAAI,IAAI;YAKZ,iBAAiB;IAa/B,OAAO,CAAC,UAAU;CAsBnB;AAED,wBAAgB,WAAW,CAAC,OAAO,EAAE,MAAM,GAAG,UAAU,CAEvD"} \ No newline at end of file diff --git a/dist/index.js b/dist/index.js new file mode 100644 index 0000000..a1849fd --- /dev/null +++ b/dist/index.js @@ -0,0 +1,114 @@ +// @ts-ignore +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"; +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)$/; +export default class DHT { + constructor() { + this._relays = {}; + this._wsPool = createPool(WebSocket, createRoundRobin); + this._dht = new DhtNode(new Stream(true, this._wsPool)); + return this.setupProxy(); + } + static get IS_WEB() { + return true; + } + get relays() { + return Object.keys(this._relays); + } + async addRelay(pubkey) { + let entry = await registryRead(stringToUint8ArrayUtf8(pubkey), hashDataKey(REGISTRY_DHT_KEY)); + if (entry[1] || !entry[0]?.exists) { + return false; + } + const host = Buffer.from(entry[0].entryData).toString("utf8"); + const [ip, port] = host.split("."); + if (!IP_REGEX.test(ip)) { + return false; + } + if (isNaN(parseInt(port))) { + return false; + } + const connection = `ws://${ip}:${port}/`; + if (!(await this.isServerAvailable(connection))) { + return false; + } + this._relays[pubkey] = this._wsPool.add(connection); + return true; + } + removeRelay(pubkey) { + if (!(pubkey in this._relays)) { + return false; + } + this._relays[pubkey](); + delete this._relays[pubkey]; + return true; + } + clearRelays() { + this._wsPool.close(); + this._relays = {}; + } + async isServerAvailable(connection) { + return new Promise((resolve) => { + const ws = new WebSocket(connection); + ws.addEventListener("open", () => { + ws.close(); + resolve(true); + }); + ws.addEventListener("error", () => { + resolve(false); + }); + }); + } + setupProxy() { + return new Proxy(this, { + get(target, name) { + 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) { + if (!target.hasOwnProperty(name)) { + return target._dht.hasOwnProperty(name); + } + return true; + }, + }); + } +} +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 Uint8Array.from(Buffer.from(str, "utf-8")); +} +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; +}