From 1fabdc7e4dd458077bf6045762eaa1ab383c6e82 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Wed, 20 Jul 2022 02:13:07 -0400 Subject: [PATCH] *add dist --- dist/index.d.ts | 21 +++++++++++++++ dist/index.d.ts.map | 1 + dist/index.js | 66 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 dist/index.d.ts create mode 100644 dist/index.d.ts.map create mode 100644 dist/index.js diff --git a/dist/index.d.ts b/dist/index.d.ts new file mode 100644 index 0000000..f47ee4e --- /dev/null +++ b/dist/index.d.ts @@ -0,0 +1,21 @@ +/// +/// +import { EventEmitter } from "events"; +import { ErrTuple } from "libskynet"; +import { Buffer } from "buffer"; +export declare class DHT { + connect(pubkey: string): Promise; + ready(): Promise; +} +export declare class Socket extends EventEmitter { + private id; + private eventUpdates; + constructor(id: number); + on(eventName: string, listener: (...args: any[]) => void): this; + off(type: string, listener: any): this; + write(message: string | Buffer): void; + end(): void; + private ensureEvent; + private trackEvent; +} +//# 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 new file mode 100644 index 0000000..7f9279d --- /dev/null +++ b/dist/index.d.ts.map @@ -0,0 +1 @@ +{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAQA,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AACtC,OAAO,EAAU,QAAQ,EAAE,MAAM,WAAW,CAAC;AAC7C,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAehC,qBAAa,GAAG;IACC,OAAO,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAQ/C,KAAK,IAAI,OAAO,CAAC,QAAQ,CAAC;CAGnC;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 new file mode 100644 index 0000000..e29aa8c --- /dev/null +++ b/dist/index.js @@ -0,0 +1,66 @@ +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 (window.document) { + callModule = callModuleKernel; + connectModule = connectModuleKernel; +} +else { + callModule = callModuleModule; + connectModule = connectModuleModule; +} +export class DHT { + async connect(pubkey) { + const [resp, err] = await callModule(DHT_MODULE, "connect", { pubkey }); + if (err) { + throw err; + } + return new Socket(resp.id); + } + async ready() { + return callModule(DHT_MODULE, "ready"); + } +} +export class Socket extends EventEmitter { + id; + eventUpdates = {}; + constructor(id) { + super(); + this.id = id; + } + on(eventName, listener) { + const [update, promise] = connectModule(DHT_MODULE, "listenSocketEvent", { id: this.id, event: eventName }, (data) => { + this.emit(eventName, data); + }); + this.trackEvent(eventName, update); + promise.then(() => { + this.off(eventName, listener); + }); + return super.on(eventName, listener); + } + off(type, listener) { + const updates = [...this.eventUpdates[type]]; + this.eventUpdates[type] = []; + for (const func of updates) { + func({ action: "off" }); + } + return super.off(type, listener); + } + write(message) { + callModule(DHT_MODULE, "write", { id: this.id, message }); + } + end() { + callModule(DHT_MODULE, "close", { id: this.id }); + } + ensureEvent(event) { + if (!(event in this.eventUpdates)) { + this.eventUpdates[event] = []; + } + } + trackEvent(event, update) { + this.ensureEvent(event); + this.eventUpdates[event].push(update); + } +}