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