diff --git a/dist/index.d.ts.map b/dist/index.d.ts.map index 84171dc..1921adb 100644 --- a/dist/index.d.ts.map +++ b/dist/index.d.ts.map @@ -1 +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;IACtB,OAAO,CAAC,QAAQ,CAAM;IACtB,OAAO,CAAC,OAAO,CAAkC;IACjD,OAAO,CAAC,aAAa,CAA0C;IAC/D,OAAO,CAAC,eAAe,CAAM;IAC7B,OAAO,CAAC,OAAO,CAAS;gBAEZ,IAAI,KAAK;IAMrB,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAStB,IAAI,MAAM,IAAI,MAAM,EAAE,CAErB;IAEY,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAiChD,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO;IAepC,WAAW,IAAI,IAAI;YAIZ,iBAAiB;IAazB,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC;YAgB/C,eAAe;CA6C9B;AAED,wBAAgB,WAAW,CAAC,OAAO,EAAE,MAAM,GAAG,UAAU,CAEvD"} \ No newline at end of file +{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,OAAO,MAAM,uBAAuB,CAAC;AAgB5C,MAAM,CAAC,OAAO,OAAO,GAAG;IACtB,OAAO,CAAC,QAAQ,CAAM;IACtB,OAAO,CAAC,OAAO,CAAkC;IACjD,OAAO,CAAC,aAAa,CAA0C;IAC/D,OAAO,CAAC,eAAe,CAAM;IAC7B,OAAO,CAAC,OAAO,CAAS;gBAEZ,IAAI,KAAK;IAMrB,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAStB,IAAI,MAAM,IAAI,MAAM,EAAE,CAErB;IAEY,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAiChD,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO;IAepC,WAAW,IAAI,IAAI;YAIZ,iBAAiB;IAazB,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC;YAmB/C,eAAe;CAkD9B;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 index c60e7ab..1850e4e 100644 --- a/dist/index.js +++ b/dist/index.js @@ -3,10 +3,8 @@ import DhtNode from "@hyperswarm/dht-relay"; // @ts-ignore import Stream from "@hyperswarm/dht-relay/ws"; // @ts-ignore -import { Buffer } from "buffer"; // @ts-ignore -// @ts-ignore -import { blake2b } from "libskynet"; +import { blake2b, hexToBuf } from "libskynet"; // @ts-ignore import { registryRead } from "libkmodule"; import { unpack } from "msgpackr"; @@ -34,7 +32,7 @@ export default class DHT { return [...this._relays.keys()]; } async addRelay(pubkey) { - let entry = await registryRead(Uint8Array.from(Buffer.from(pubkey, "hex")), hashDataKey(REGISTRY_DHT_KEY)); + let entry = await registryRead(hexToBuf(pubkey).shift(), hashDataKey(REGISTRY_DHT_KEY)); if (entry[1] || !entry[0]?.exists) { return false; } @@ -82,6 +80,9 @@ export default class DHT { }); } async connect(pubkey, options = {}) { + if (this._activeRelays.size === 0) { + await this.fillConnections(); + } if (this._activeRelays.size === 0) { throw new Error("Failed to find an available relay"); } @@ -108,6 +109,7 @@ export default class DHT { if (available.length > 1) { relayIndex = await randomNumber(0, available.length - 1); } + const pubkey = available[relayIndex]; const connection = this._relays.get(available[relayIndex]); if (!(await this.isServerAvailable(connection))) { available.splice(relayIndex, 1); @@ -118,6 +120,9 @@ export default class DHT { this._activeRelays.set(available[relayIndex], node); updateAvailable(); relayPromises.push(node.ready()); + node._protocol._stream.on("close", () => { + this._activeRelays.delete(pubkey); + }); } return Promise.allSettled(relayPromises); } @@ -133,7 +138,7 @@ function encodeUtf8String(str) { return encoded; } function stringToUint8ArrayUtf8(str) { - return Uint8Array.from(Buffer.from(str, "utf-8")); + return new TextEncoder().encode(str); } function encodeNumber(num) { const encoded = new Uint8Array(8);