From ebe2a18d91de8c5beba9920719ec8a72043d20be Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Wed, 1 Feb 2023 07:55:30 -0500 Subject: [PATCH] *Update dist --- dist/index.d.ts | 19 +++---- dist/index.d.ts.map | 2 +- dist/index.js | 127 +++++++++++--------------------------------- 3 files changed, 40 insertions(+), 108 deletions(-) diff --git a/dist/index.d.ts b/dist/index.d.ts index 72ae164..99e5211 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -1,24 +1,20 @@ /// -/// -import { EventEmitter } from "events"; -import { ErrTuple } from "libskynet"; import { Buffer } from "buffer"; -export declare class DHT { - private useDefaultDht; +import { Client } from "@lumeweb/libkernel-universal"; +import { ErrTuple } from "@siaweb/libweb"; +export declare class SwarmClient extends Client { + private useDefaultSwarm; private id; + get swarm(): number | undefined; constructor(useDefaultDht?: boolean); - connect(pubkey: string): Promise; + connect(pubkey: string | Uint8Array): Promise; ready(): Promise; addRelay(pubkey: string): Promise; removeRelay(pubkey: string): Promise; clearRelays(): Promise; getRelays(): Promise; - getRelayServers(): Promise; - private create; - close(): Promise; - private setup; } -export declare class Socket extends EventEmitter { +export declare class Socket extends Client { private id; private eventUpdates; constructor(id: number); @@ -29,4 +25,5 @@ export declare class Socket extends EventEmitter { private ensureEvent; private trackEvent; } +export declare const createClient: (...args: any) => SwarmClient; //# sourceMappingURL=index.d.ts.map \ No newline at end of file diff --git a/dist/index.d.ts.map b/dist/index.d.ts.map index 2b81a99..66a69ae 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;AAqBhC,qBAAa,GAAG;IACd,OAAO,CAAC,aAAa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M;IACrC,OAAO,CAAC,eAAe,CAAU;IACjC,OAAO,CAAC,EAAE,CAAa;IAEvB,IAAI,KAAK,IAAI,MAAM,GAAG,SAAS,CAE9B;gBAEW,aAAa,UAAO;IAKnB,OAAO,CAAC,MAAM,EAAE,MAAM,GAAG,UAAU,GAAG,OAAO,CAAC,MAAM,CAAC;IAc5D,KAAK,IAAI,OAAO,CAAC,QAAQ,CAAC;IAKnB,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAIvC,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAI1C,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC;IAG5B,SAAS,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;CAG5C;AAED,qBAAa,MAAO,SAAQ,MAAM;IAChC,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;IAiB/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;IAUX,OAAO,CAAC,WAAW;IAMnB,OAAO,CAAC,UAAU;CAInB;AAID,eAAO,MAAM,YAAY,+BAA4C,CAAC"} \ No newline at end of file diff --git a/dist/index.js b/dist/index.js index 2ec8d9e..1fbbd24 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,112 +1,44 @@ -import { EventEmitter } from "events"; -const DHT_MODULE = "AQD1IgE4lTZkq1fqdoYGojKRNrSk0YQ_wrHbRtIiHDrnow"; -let callModule, connectModule; -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 { - useDefaultDht; +import { Client, factory } from "@lumeweb/libkernel-universal"; +import { hexToBuf } from "@siaweb/libweb"; +export class SwarmClient extends Client { + useDefaultSwarm; id = 0; + get swarm() { + return this.useDefaultSwarm ? undefined : this.id; + } constructor(useDefaultDht = true) { - this.useDefaultDht = useDefaultDht; + super(); + this.useDefaultSwarm = useDefaultDht; } async connect(pubkey) { - await this.setup(); - const dht = !this.useDefaultDht ? this.id : undefined; - const [resp, err] = await callModule(DHT_MODULE, "connect", { - pubkey, - dht, - }); - if (err) { - throw new Error(err); + if (typeof pubkey === "string") { + const buf = hexToBuf(pubkey); + pubkey = this.handleErrorOrReturn(buf); } - return new Socket(resp.id); + const resp = this.callModuleReturn("connect", { + pubkey, + swarm: this.swarm, + }); + return createSocket(resp.id); } async ready() { - await this.setup(); - const dht = !this.useDefaultDht ? this.id : undefined; - return callModule(DHT_MODULE, "ready", { dht }); + const dht = !this.useDefaultSwarm ? this.id : undefined; + return this.callModuleReturn("ready", { swarm: this.swarm }); } async addRelay(pubkey) { - await this.setup(); - const dht = !this.useDefaultDht ? this.id : undefined; - const [, err] = await callModule(DHT_MODULE, "addRelay", { pubkey, dht }); - if (err) { - throw new Error(err); - } + return this.callModuleReturn("addRelay", { pubkey, swarm: this.swarm }); } async removeRelay(pubkey) { - await this.setup(); - const dht = !this.useDefaultDht ? this.id : undefined; - const [, err] = await callModule(DHT_MODULE, "removeRelay", { - pubkey, - dht, - }); - if (err) { - throw new Error(err); - } + return this.callModuleReturn("removeRelay", { pubkey, swarm: this.swarm }); } async clearRelays() { - await this.setup(); - const dht = !this.useDefaultDht ? this.id : undefined; - await callModule(DHT_MODULE, "clearRelays", { dht }); + return this.callModuleReturn("clearRelays", { swarm: this.swarm }); } async getRelays() { - await this.setup(); - const [list, err] = await callModule(DHT_MODULE, "getRelays"); - if (err) { - throw new Error(err); - } - return list; - } - async getRelayServers() { - await this.setup(); - const [list, err] = await callModule(DHT_MODULE, "getRelayServers"); - if (err) { - throw new Error(err); - } - return list; - } - async create() { - await loadLibs(); - if (this.useDefaultDht || this.id > 0) { - return Promise.resolve(); - } - const [dht, err] = await callModule(DHT_MODULE, "openDht"); - if (err) { - throw new Error(err); - } - this.id = dht.dht; - } - async close() { - await this.setup(); - if (this.useDefaultDht) { - return false; - } - const [, err] = await callModule(DHT_MODULE, "closeDht", { dht: this.id }); - if (err) { - throw new Error(err); - } - return true; - } - async setup() { - await loadLibs(); - await this.create(); + return this.callModuleReturn("getRelays", { swarm: this.swarm }); } } -export class Socket extends EventEmitter { +export class Socket extends Client { id; eventUpdates = {}; constructor(id) { @@ -114,7 +46,7 @@ export class Socket extends EventEmitter { this.id = id; } on(eventName, listener) { - const [update, promise] = connectModule(DHT_MODULE, "listenSocketEvent", { id: this.id, event: eventName }, (data) => { + const [update, promise] = this.connectModule("listenSocketEvent", { id: this.id, event: eventName }, (data) => { this.emit(eventName, data); }); this.trackEvent(eventName, update); @@ -132,12 +64,12 @@ export class Socket extends EventEmitter { return super.off(type, listener); } write(message) { - callModule(DHT_MODULE, "write", { id: this.id, message }); + this.callModule("write", { id: this.id, message }); } end() { - callModule(DHT_MODULE, "socketExists", { id: this.id }).then(([exists]) => { + this.callModule("socketExists", { id: this.id }).then(([exists]) => { if (exists) { - callModule(DHT_MODULE, "close", { id: this.id }); + this.callModule("close", { id: this.id }); } }); } @@ -151,3 +83,6 @@ export class Socket extends EventEmitter { this.eventUpdates[event].push(update); } } +const MODULE = "_A73ORX4dxSkt7Cv8v6gtbV0W5EsLrdZX6SywPdSTFBPEg"; +export const createClient = factory(SwarmClient, MODULE); +const createSocket = factory(Socket, MODULE);