*Update dist

This commit is contained in:
Derrick Hammer 2023-02-01 07:55:30 -05:00
parent 5974956e48
commit ebe2a18d91
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
3 changed files with 40 additions and 108 deletions

19
dist/index.d.ts vendored
View File

@ -1,24 +1,20 @@
/// <reference types="node" /> /// <reference types="node" />
/// <reference types="node" />
import { EventEmitter } from "events";
import { ErrTuple } from "libskynet";
import { Buffer } from "buffer"; import { Buffer } from "buffer";
export declare class DHT { import { Client } from "@lumeweb/libkernel-universal";
private useDefaultDht; import { ErrTuple } from "@siaweb/libweb";
export declare class SwarmClient extends Client {
private useDefaultSwarm;
private id; private id;
get swarm(): number | undefined;
constructor(useDefaultDht?: boolean); constructor(useDefaultDht?: boolean);
connect(pubkey: string): Promise<Socket>; connect(pubkey: string | Uint8Array): Promise<Socket>;
ready(): Promise<ErrTuple>; ready(): Promise<ErrTuple>;
addRelay(pubkey: string): Promise<void>; addRelay(pubkey: string): Promise<void>;
removeRelay(pubkey: string): Promise<void>; removeRelay(pubkey: string): Promise<void>;
clearRelays(): Promise<void>; clearRelays(): Promise<void>;
getRelays(): Promise<string[]>; getRelays(): Promise<string[]>;
getRelayServers(): Promise<string[]>;
private create;
close(): Promise<boolean>;
private setup;
} }
export declare class Socket extends EventEmitter { export declare class Socket extends Client {
private id; private id;
private eventUpdates; private eventUpdates;
constructor(id: number); constructor(id: number);
@ -29,4 +25,5 @@ export declare class Socket extends EventEmitter {
private ensureEvent; private ensureEvent;
private trackEvent; private trackEvent;
} }
export declare const createClient: (...args: any) => SwarmClient;
//# sourceMappingURL=index.d.ts.map //# sourceMappingURL=index.d.ts.map

2
dist/index.d.ts.map vendored
View File

@ -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,M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Q,EAAE,MAAM,gBAAgB,CAAC;AAE5D,qBAAa,WAAY,SAAQ,MAAM;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"}

127
dist/index.js vendored
View File

@ -1,112 +1,44 @@
import { EventEmitter } from "events"; import { Client, factory } from "@lumeweb/libkernel-universal";
const DHT_MODULE = "AQD1IgE4lTZkq1fqdoYGojKRNrSk0YQ_wrHbRtIiHDrnow"; import { hexToBuf } from "@siaweb/libweb";
let callModule, connectModule; export class SwarmClient extends Client {
async function loadLibs() { useDefaultSwarm;
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;
id = 0; id = 0;
get swarm() {
return this.useDefaultSwarm ? undefined : this.id;
}
constructor(useDefaultDht = true) { constructor(useDefaultDht = true) {
this.useDefaultDht = useDefaultDht; super();
this.useDefaultSwarm = useDefaultDht;
} }
async connect(pubkey) { async connect(pubkey) {
await this.setup(); if (typeof pubkey === "string") {
const dht = !this.useDefaultDht ? this.id : undefined; const buf = hexToBuf(pubkey);
const [resp, err] = await callModule(DHT_MODULE, "connect", { pubkey = this.handleErrorOrReturn(buf);
pubkey,
dht,
});
if (err) {
throw new Error(err);
} }
return new Socket(resp.id); const resp = this.callModuleReturn("connect", {
pubkey,
swarm: this.swarm,
});
return createSocket(resp.id);
} }
async ready() { async ready() {
await this.setup(); const dht = !this.useDefaultSwarm ? this.id : undefined;
const dht = !this.useDefaultDht ? this.id : undefined; return this.callModuleReturn("ready", { swarm: this.swarm });
return callModule(DHT_MODULE, "ready", { dht });
} }
async addRelay(pubkey) { async addRelay(pubkey) {
await this.setup(); return this.callModuleReturn("addRelay", { pubkey, swarm: this.swarm });
const dht = !this.useDefaultDht ? this.id : undefined;
const [, err] = await callModule(DHT_MODULE, "addRelay", { pubkey, dht });
if (err) {
throw new Error(err);
}
} }
async removeRelay(pubkey) { async removeRelay(pubkey) {
await this.setup(); return this.callModuleReturn("removeRelay", { pubkey, swarm: this.swarm });
const dht = !this.useDefaultDht ? this.id : undefined;
const [, err] = await callModule(DHT_MODULE, "removeRelay", {
pubkey,
dht,
});
if (err) {
throw new Error(err);
}
} }
async clearRelays() { async clearRelays() {
await this.setup(); return this.callModuleReturn("clearRelays", { swarm: this.swarm });
const dht = !this.useDefaultDht ? this.id : undefined;
await callModule(DHT_MODULE, "clearRelays", { dht });
} }
async getRelays() { async getRelays() {
await this.setup(); return this.callModuleReturn("getRelays", { swarm: this.swarm });
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();
} }
} }
export class Socket extends EventEmitter { export class Socket extends Client {
id; id;
eventUpdates = {}; eventUpdates = {};
constructor(id) { constructor(id) {
@ -114,7 +46,7 @@ export class Socket extends EventEmitter {
this.id = id; this.id = id;
} }
on(eventName, listener) { 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.emit(eventName, data);
}); });
this.trackEvent(eventName, update); this.trackEvent(eventName, update);
@ -132,12 +64,12 @@ export class Socket extends EventEmitter {
return super.off(type, listener); return super.off(type, listener);
} }
write(message) { write(message) {
callModule(DHT_MODULE, "write", { id: this.id, message }); this.callModule("write", { id: this.id, message });
} }
end() { end() {
callModule(DHT_MODULE, "socketExists", { id: this.id }).then(([exists]) => { this.callModule("socketExists", { id: this.id }).then(([exists]) => {
if (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); this.eventUpdates[event].push(update);
} }
} }
const MODULE = "_A73ORX4dxSkt7Cv8v6gtbV0W5EsLrdZX6SywPdSTFBPEg";
export const createClient = factory(SwarmClient, MODULE);
const createSocket = factory(Socket, MODULE);