diff --git a/dist/index.d.ts b/dist/index.d.ts index b99d525..1b2af00 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -14,6 +14,7 @@ export declare class DHT { clearRelays(): Promise; private create; close(): Promise; + private setup; } export declare class Socket extends EventEmitter { private id; diff --git a/dist/index.d.ts.map b/dist/index.d.ts.map index 947f471..d44fd35 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":";;AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AACtC,OAAO,EAAU,QAAQ,EAAE,MAAM,WAAW,CAAC;AAC7C,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAsBhC,qBAAa,GAAG;IACd,OAAO,CAAC,aAAa,CAAU;IAC/B,OAAO,CAAC,EAAE,CAAa;gBAEX,aAAa,UAAO;IAKnB,OAAO,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAS/C,KAAK,IAAI,OAAO,CAAC,QAAQ,CAAC;IAMnB,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IASvC,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAY1C,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC;YAM3B,MAAM;IAaP,KAAK,IAAI,OAAO,CAAC,OAAO,CAAC;CAavC;AAED,qBAAa,MAAO,SAAQ,YAAY;IACtC,OAAO,CAAC,EAAE,CAAS;IACnB,OAAO,CAAC,YAAY,CAAqC;gBAE7C,EAAE,EAAE,MAAM;IAKtB,EAAE,CAAC,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,GAAG,IAAI;IAkB/D,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,GA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AAI,IAAI;IAIX,OAAO,CAAC,WAAW;IAMnB,OAAO,CAAC,UAAU;CAInB"} \ No newline at end of file diff --git a/dist/index.js b/dist/index.js index f497fc8..78e33ea 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,5 +1,4 @@ import { EventEmitter } from "events"; -import { create } from "domain"; const DHT_MODULE = "AQD1IgE4lTZkq1fqdoYGojKRNrSk0YQ_wrHbRtIiHDrnow"; let callModule, connectModule; async function loadLibs() { @@ -22,10 +21,9 @@ export class DHT { id = 0; constructor(useDefaultDht = true) { this.useDefaultDht = useDefaultDht; - return create(); } async connect(pubkey) { - await loadLibs(); + await this.setup(); const [resp, err] = await callModule(DHT_MODULE, "connect", { pubkey }); if (err) { throw new Error(err); @@ -33,12 +31,12 @@ export class DHT { return new Socket(resp.id); } async ready() { - await loadLibs(); + await this.setup(); const dht = !this.useDefaultDht ? this.id : undefined; return callModule(DHT_MODULE, "ready", { dht }); } async addRelay(pubkey) { - await loadLibs(); + await this.setup(); const dht = !this.useDefaultDht ? this.id : undefined; const [, err] = await callModule(DHT_MODULE, "addRelay", { pubkey, dht }); if (err) { @@ -46,7 +44,7 @@ export class DHT { } } async removeRelay(pubkey) { - await loadLibs(); + await this.setup(); const dht = !this.useDefaultDht ? this.id : undefined; const [, err] = await callModule(DHT_MODULE, "removeRelay", { pubkey, @@ -57,13 +55,13 @@ export class DHT { } } async clearRelays() { - await loadLibs(); + await this.setup(); const dht = !this.useDefaultDht ? this.id : undefined; await callModule(DHT_MODULE, "clearRelays", { dht }); } async create() { await loadLibs(); - if (this.useDefaultDht) { + if (this.useDefaultDht || this.id > 0) { return Promise.resolve(); } const [dht, err] = await callModule(DHT_MODULE, "openDht"); @@ -73,7 +71,7 @@ export class DHT { this.id = dht; } async close() { - await loadLibs(); + await this.setup(); if (this.useDefaultDht) { return false; } @@ -83,6 +81,10 @@ export class DHT { } return true; } + async setup() { + await loadLibs(); + await this.create(); + } } export class Socket extends EventEmitter { id;