*Update dist
This commit is contained in:
parent
0325d4488f
commit
83a7dcd64f
|
@ -1,15 +1,18 @@
|
|||
import DhtNode from "@hyperswarm/dht-relay";
|
||||
export default class DHT {
|
||||
private _dht;
|
||||
private _wsPool;
|
||||
constructor();
|
||||
static get IS_WEB(): boolean;
|
||||
private _options;
|
||||
private _relays;
|
||||
constructor(opts?: {});
|
||||
ready(): Promise<void>;
|
||||
static get IS_WEB(): boolean;
|
||||
get relays(): string[];
|
||||
addRelay(pubkey: string): Promise<boolean>;
|
||||
removeRelay(pubkey: string): boolean;
|
||||
clearRelays(): void;
|
||||
private isServerAvailable;
|
||||
private setupProxy;
|
||||
connect(pubkey: string, options?: {}): Promise<DhtNode>;
|
||||
getAvailableRelay(): Promise<string | boolean>;
|
||||
}
|
||||
export declare function hashDataKey(dataKey: string): Uint8Array;
|
||||
//# sourceMappingURL=index.d.ts.map
|
|
@ -1 +0,0 @@
|
|||
{"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"}
|
|
@ -1,59 +1,58 @@
|
|||
// @ts-ignore
|
||||
import { DhtNode } from "@hyperswarm/dht-relay";
|
||||
// @ts-ignore
|
||||
// src/index.ts
|
||||
import DhtNode from "@hyperswarm/dht-relay";
|
||||
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();
|
||||
import { registryRead } from "libkmodule";
|
||||
import { unpack } from "msgpackr";
|
||||
var REGISTRY_DHT_KEY = "lumeweb-dht-node";
|
||||
var DHT = class {
|
||||
_wsPool;
|
||||
_options;
|
||||
_relays = {};
|
||||
constructor(opts = {}) {
|
||||
opts.custodial = true;
|
||||
this._options = opts;
|
||||
this._wsPool = createRoundRobin();
|
||||
}
|
||||
static get IS_WEB() {
|
||||
return true;
|
||||
ready() {
|
||||
return Promise.resolve();
|
||||
}
|
||||
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) {
|
||||
var _a;
|
||||
let entry = await registryRead(Uint8Array.from(Buffer.from(pubkey, "hex")), hashDataKey(REGISTRY_DHT_KEY));
|
||||
if (entry[1] || !((_a = entry[0]) == null ? void 0 : _a.exists)) {
|
||||
return false;
|
||||
}
|
||||
const host = Buffer.from(entry[0].entryData).toString("utf8");
|
||||
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}/`;
|
||||
if (!(await this.isServerAvailable(connection))) {
|
||||
return false;
|
||||
}
|
||||
this._relays[pubkey] = this._wsPool.add(connection);
|
||||
const connection = `wss://${domain}:${port}/`;
|
||||
this._wsPool.add(connection);
|
||||
this._relays[pubkey] = connection;
|
||||
return true;
|
||||
}
|
||||
removeRelay(pubkey) {
|
||||
if (!(pubkey in this._relays)) {
|
||||
return false;
|
||||
}
|
||||
this._relays[pubkey]();
|
||||
this._wsPool.remove(this._relays[pubkey]);
|
||||
delete this._relays[pubkey];
|
||||
return true;
|
||||
}
|
||||
clearRelays() {
|
||||
this._wsPool.close();
|
||||
this._wsPool = createRoundRobin();
|
||||
this._relays = {};
|
||||
}
|
||||
async isServerAvailable(connection) {
|
||||
|
@ -68,30 +67,26 @@ export default class DHT {
|
|||
});
|
||||
});
|
||||
}
|
||||
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`);
|
||||
async connect(pubkey, options = {}) {
|
||||
const relay = await this.getAvailableRelay();
|
||||
if (!relay) {
|
||||
throw new Error("Failed to find an available relay");
|
||||
}
|
||||
return target._dht[target];
|
||||
const node = new DhtNode(new Stream(true, new WebSocket(relay)), this._options);
|
||||
await node.ready();
|
||||
return node.connect(pubkey, options);
|
||||
}
|
||||
else {
|
||||
// @ts-ignore
|
||||
return target[name];
|
||||
async getAvailableRelay() {
|
||||
for (let i = 0; i < this._wsPool.length; i++) {
|
||||
const relay = this._wsPool.get();
|
||||
if (await this.isServerAvailable(relay)) {
|
||||
return relay;
|
||||
}
|
||||
},
|
||||
has(target, name) {
|
||||
if (!target.hasOwnProperty(name)) {
|
||||
return target._dht.hasOwnProperty(name);
|
||||
}
|
||||
return true;
|
||||
},
|
||||
});
|
||||
return false;
|
||||
}
|
||||
}
|
||||
export function hashDataKey(dataKey) {
|
||||
};
|
||||
function hashDataKey(dataKey) {
|
||||
return blake2b(encodeUtf8String(dataKey));
|
||||
}
|
||||
function encodeUtf8String(str) {
|
||||
|
@ -107,8 +102,13 @@ function stringToUint8ArrayUtf8(str) {
|
|||
function encodeNumber(num) {
|
||||
const encoded = new Uint8Array(8);
|
||||
for (let index = 0; index < encoded.length; index++) {
|
||||
encoded[index] = num & 0xff;
|
||||
encoded[index] = num & 255;
|
||||
num = num >> 8;
|
||||
}
|
||||
return encoded;
|
||||
}
|
||||
export {
|
||||
DHT as default,
|
||||
hashDataKey
|
||||
};
|
||||
//# sourceMappingURL=index.js.map
|
Loading…
Reference in New Issue