diff --git a/dist/index.d.ts.map b/dist/index.d.ts.map index f68b3d4..4f373a6 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":";;AAQA,OAAO,EAAC,YAAY,EAAC,MAAM,QAAQ,CAAC;AACpC,OAAO,EAAS,QAAQ,EAAC,MAAM,WAAW,CAAC;AAC3C,OAAO,EAAC,MAAM,EAAC,MAAM,QAAQ,CAAC;AAe9B,qBAAa,GAAG;IACC,OAAO,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAQ/C,KAAK,IAAI,OAAO,CAAC,QAAQ,CAAC;IAInB,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAOvC,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAO1C,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC;CAG5C;AAED,qBAAa,MAAO,SAAQ,YAAY;IACpC,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,GAAG,GAAG,IAAI;IAStC,KAAK,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI;IAIrC,GAAG,IAAI,IAAI;IAIX,OAAO,CAAC,WAAW;IAMnB,OAAO,CAAC,UAAU;CAIrB"} \ No newline at end of file +{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAAA,OAAO,EAAC,YAAY,EAAC,MAAM,QAAQ,CAAC;AACpC,OAAO,EAAS,QAAQ,EAAC,MAAM,WAAW,CAAC;AAC3C,OAAO,EAAC,MAAM,EAAC,MAAM,QAAQ,CAAC;AAsB9B,qBAAa,GAAG;IACC,OAAO,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAS/C,KAAK,IAAI,OAAO,CAAC,QAAQ,CAAC;IAKnB,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAQvC,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAQ1C,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC;CAI5C;AAED,qBAAa,MAAO,SAAQ,YAAY;IACpC,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,GAAG,GAAG,IAAI;IAStC,KAAK,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI;IAIrC,GAAG,IAAI,IAAI;IAIX,OAAO,CAAC,WAAW;IAMnB,OAAO,CAAC,UAAU;CAIrB"} \ No newline at end of file diff --git a/dist/index.js b/dist/index.js index 93f437b..138650d 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,18 +1,24 @@ -import { callModule as callModuleKernel, connectModule as connectModuleKernel, } from "libkernel"; -import { callModule as callModuleModule, connectModule as connectModuleModule, } from "libkmodule"; import { EventEmitter } from "events"; const DHT_MODULE = "AQD1IgE4lTZkq1fqdoYGojKRNrSk0YQ_wrHbRtIiHDrnow"; let callModule, connectModule; -if (typeof window !== "undefined" && window?.document) { - callModule = callModuleKernel; - connectModule = connectModuleKernel; -} -else { - callModule = callModuleModule; - connectModule = connectModuleModule; +async function loadLibs() { + if (callModule && connectModule) { + return; + } + if (typeof window !== "undefined" && window?.document) { + const pkg = (await import("libkernel")); + callModule = pkg.callModule; + connectModule = pkg.connectModule; + } + else { + const pkg = (await import("libkmodule")); + callModule = pkg.callModule; + connectModule = pkg.connectModule; + } } export class DHT { async connect(pubkey) { + await loadLibs(); const [resp, err] = await callModule(DHT_MODULE, "connect", { pubkey }); if (err) { throw new Error(err); @@ -20,21 +26,25 @@ export class DHT { return new Socket(resp.id); } async ready() { + await loadLibs(); return callModule(DHT_MODULE, "ready"); } async addRelay(pubkey) { + await loadLibs(); const [, err] = await callModule(DHT_MODULE, "addRelay", { pubkey }); if (err) { throw new Error(err); } } async removeRelay(pubkey) { + await loadLibs(); const [, err] = await callModule(DHT_MODULE, "removeRelay", { pubkey }); if (err) { throw new Error(err); } } async clearRelays() { + await loadLibs(); await callModule(DHT_MODULE, "clearRelays"); } }