*Update dist

This commit is contained in:
Derrick Hammer 2022-07-20 02:06:55 -04:00
parent f2e6e36783
commit b2f2c7e9f0
3 changed files with 96 additions and 95 deletions

1
dist/index.d.ts vendored
View File

@ -5,7 +5,6 @@ export default class DHT {
private _relays;
constructor(opts?: {});
ready(): Promise<void>;
static get IS_WEB(): boolean;
get relays(): string[];
addRelay(pubkey: string): Promise<boolean>;
removeRelay(pubkey: string): boolean;

1
dist/index.d.ts.map vendored Normal file
View File

@ -0,0 +1 @@
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,OAAO,MAAM,uBAAuB,CAAC;AAkB5C,MAAM,CAAC,OAAO,OAAO,GAAG;IACpB,OAAO,CAAC,OAAO,CAA8B;IAC7C,OAAO,CAAC,QAAQ,CAAM;IACtB,OAAO,CAAC,OAAO,CAAoC;gBAEvC,IAAI,KAAK;IAOrB,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAItB,IAAI,MAAM,IAAI,MAAM,EAAE,CAErB;IAEY,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAgChD,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO;IAWpC,WAAW,IAAI,IAAI;YAKZ,iBAAiB;IAazB,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC;IAavD,iBAAiB,IAAI,OAAO,CAAC,MAAM,GAAG,OAAO,CAAC;CAUvD;AAED,wBAAgB,WAAW,CAAC,OAAO,EAAE,MAAM,GAAG,UAAU,CAEvD"}

189
dist/index.js vendored
View File

@ -1,114 +1,115 @@
// src/index.ts
// @ts-ignore
import DhtNode from "@hyperswarm/dht-relay";
// @ts-ignore
import Stream from "@hyperswarm/dht-relay/ws";
// @ts-ignore
import createRoundRobin from "@derhuerst/round-robin-scheduler";
// @ts-ignore
import { Buffer } from "buffer";
// @ts-ignore
import { blake2b } from "libskynet";
// @ts-ignore
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();
}
ready() {
return Promise.resolve();
}
get relays() {
return Object.keys(this._relays);
}
async addRelay(pubkey) {
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 REGISTRY_DHT_KEY = "lumeweb-dht-node";
export default class DHT {
_wsPool;
_options;
_relays = {};
constructor(opts = {}) {
// @ts-ignore
opts.custodial = true;
this._options = opts;
this._wsPool = createRoundRobin();
}
let host;
try {
host = unpack(entry[0].entryData);
} catch (e) {
return false;
ready() {
return Promise.resolve();
}
const [domain, port] = host.split(":");
if (isNaN(parseInt(port))) {
return false;
get relays() {
return Object.keys(this._relays);
}
const connection = `wss://${domain}:${port}/`;
this._wsPool.add(connection);
this._relays[pubkey] = connection;
return true;
}
removeRelay(pubkey) {
if (!(pubkey in this._relays)) {
return false;
async addRelay(pubkey) {
let entry = await registryRead(Uint8Array.from(Buffer.from(pubkey, "hex")), hashDataKey(REGISTRY_DHT_KEY));
if (entry[1] || !entry[0]?.exists) {
return false;
}
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 = `wss://${domain}:${port}/`;
this._wsPool.add(connection);
this._relays[pubkey] = connection;
return true;
}
this._wsPool.remove(this._relays[pubkey]);
delete this._relays[pubkey];
return true;
}
clearRelays() {
this._wsPool = createRoundRobin();
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);
});
});
}
async connect(pubkey, options = {}) {
const relay = await this.getAvailableRelay();
if (!relay) {
throw new Error("Failed to find an available relay");
removeRelay(pubkey) {
if (!(pubkey in this._relays)) {
return false;
}
this._wsPool.remove(this._relays[pubkey]);
delete this._relays[pubkey];
return true;
}
const node = new DhtNode(new Stream(true, new WebSocket(relay)), this._options);
await node.ready();
return node.connect(pubkey, options);
}
async getAvailableRelay() {
for (let i = 0; i < this._wsPool.length; i++) {
const relay = this._wsPool.get();
if (await this.isServerAvailable(relay)) {
return relay;
}
clearRelays() {
this._wsPool = createRoundRobin();
this._relays = {};
}
return false;
}
};
function hashDataKey(dataKey) {
return blake2b(encodeUtf8String(dataKey));
async isServerAvailable(connection) {
return new Promise((resolve) => {
const ws = new WebSocket(connection);
ws.addEventListener("open", () => {
ws.close();
resolve(true);
});
ws.addEventListener("error", () => {
resolve(false);
});
});
}
async connect(pubkey, options = {}) {
const relay = await this.getAvailableRelay();
if (!relay) {
throw new Error("Failed to find an available relay");
}
const node = new DhtNode(new Stream(true, new WebSocket(relay)), this._options);
await node.ready();
return node.connect(pubkey, options);
}
async getAvailableRelay() {
for (let i = 0; i < this._wsPool.length; i++) {
const relay = this._wsPool.get();
if (await this.isServerAvailable(relay)) {
return relay;
}
}
return false;
}
}
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;
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"));
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 & 255;
num = num >> 8;
}
return encoded;
const encoded = new Uint8Array(8);
for (let index = 0; index < encoded.length; index++) {
encoded[index] = num & 0xff;
num = num >> 8;
}
return encoded;
}
export {
DHT as default,
hashDataKey
};
//# sourceMappingURL=index.js.map