*rewrite for new design

This commit is contained in:
Derrick Hammer 2023-02-01 07:54:20 -05:00
parent b5a81d51ab
commit b567d70dda
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 43 additions and 118 deletions

View File

@ -4,14 +4,11 @@
"type": "module", "type": "module",
"main": "dist/index.js", "main": "dist/index.js",
"dependencies": { "dependencies": {
"buffer": "^6.0.3", "@lumeweb/libkernel-universal": "git+https://git.lumeweb.com/LumeWeb/libkernel-universal.git",
"events": "^3.3.0", "@siaweb/libweb": "git+https://git.lumeweb.com/LumeWeb/libsiaweb.git"
"libkernel": "^0.1.43",
"libkmodule": "^0.2.44",
"libskynet": "^0.0.62"
}, },
"devDependencies": { "devDependencies": {
"@types/node": "^18.0.6", "@types/node": "^18.11.18",
"prettier": "^2.7.1" "prettier": "^2.8.3"
} }
} }

View File

@ -1,132 +1,56 @@
import { EventEmitter } from "events";
import { DataFn, ErrTuple } from "libskynet";
import { Buffer } from "buffer"; import { Buffer } from "buffer";
import { Client, factory } from "@lumeweb/libkernel-universal";
import { hexToBuf, DataFn, ErrTuple } from "@siaweb/libweb";
const DHT_MODULE = "AQD1IgE4lTZkq1fqdoYGojKRNrSk0YQ_wrHbRtIiHDrnow"; export class SwarmClient extends Client {
private useDefaultSwarm: boolean;
let callModule: any, connectModule: any;
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 {
private useDefaultDht: boolean;
private id: number = 0; private id: number = 0;
constructor(useDefaultDht = true) { get swarm(): number | undefined {
this.useDefaultDht = useDefaultDht; return this.useDefaultSwarm ? undefined : this.id;
} }
public async connect(pubkey: string): Promise<Socket> { constructor(useDefaultDht = true) {
await this.setup(); super();
const dht = !this.useDefaultDht ? this.id : undefined; this.useDefaultSwarm = useDefaultDht;
const [resp, err] = await callModule(DHT_MODULE, "connect", { }
pubkey,
dht, public async connect(pubkey: string | Uint8Array): Promise<Socket> {
}); if (typeof pubkey === "string") {
if (err) { const buf = hexToBuf(pubkey);
throw new Error(err); pubkey = this.handleErrorOrReturn(buf);
} }
return new Socket(resp.id);
const resp = this.callModuleReturn("connect", {
pubkey,
swarm: this.swarm,
}) as any;
return createSocket(resp.id);
} }
async ready(): Promise<ErrTuple> { async ready(): Promise<ErrTuple> {
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 });
} }
public async addRelay(pubkey: string): Promise<void> { public async addRelay(pubkey: string): Promise<void> {
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);
}
} }
public async removeRelay(pubkey: string): Promise<void> { public async removeRelay(pubkey: string): Promise<void> {
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);
}
} }
public async clearRelays(): Promise<void> { public async clearRelays(): Promise<void> {
await this.setup(); return this.callModuleReturn("clearRelays", { swarm: this.swarm });
const dht = !this.useDefaultDht ? this.id : undefined;
await callModule(DHT_MODULE, "clearRelays", { dht });
} }
public async getRelays(): Promise<string[]> { public async getRelays(): Promise<string[]> {
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;
}
public async getRelayServers(): Promise<string[]> {
await this.setup();
const [list, err] = await callModule(DHT_MODULE, "getRelayServers");
if (err) {
throw new Error(err);
}
return list;
}
private 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;
}
public async close(): Promise<boolean> {
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;
}
private async setup() {
await loadLibs();
await this.create();
} }
} }
export class Socket extends EventEmitter { export class Socket extends Client {
private id: number; private id: number;
private eventUpdates: { [event: string]: DataFn[] } = {}; private eventUpdates: { [event: string]: DataFn[] } = {};
@ -136,8 +60,7 @@ export class Socket extends EventEmitter {
} }
on(eventName: string, listener: (...args: any[]) => void): this { on(eventName: string, listener: (...args: any[]) => void): this {
const [update, promise] = connectModule( const [update, promise] = this.connectModule(
DHT_MODULE,
"listenSocketEvent", "listenSocketEvent",
{ id: this.id, event: eventName }, { id: this.id, event: eventName },
(data: any) => { (data: any) => {
@ -150,7 +73,7 @@ export class Socket extends EventEmitter {
this.off(eventName, listener); this.off(eventName, listener);
}); });
return super.on(eventName, listener); return super.on(eventName, listener) as this;
} }
off(type: string, listener: any): this { off(type: string, listener: any): this {
@ -163,14 +86,14 @@ export class Socket extends EventEmitter {
} }
write(message: string | Buffer): void { write(message: string | Buffer): void {
callModule(DHT_MODULE, "write", { id: this.id, message }); this.callModule("write", { id: this.id, message });
} }
end(): void { end(): void {
callModule(DHT_MODULE, "socketExists", { id: this.id }).then( this.callModule("socketExists", { id: this.id }).then(
([exists]: ErrTuple) => { ([exists]: ErrTuple) => {
if (exists) { if (exists) {
callModule(DHT_MODULE, "close", { id: this.id }); this.callModule("close", { id: this.id });
} }
} }
); );
@ -187,3 +110,8 @@ export class Socket extends EventEmitter {
this.eventUpdates[event].push(update); this.eventUpdates[event].push(update);
} }
} }
const MODULE = "_A73ORX4dxSkt7Cv8v6gtbV0W5EsLrdZX6SywPdSTFBPEg";
export const createClient = factory<SwarmClient>(SwarmClient, MODULE);
const createSocket = factory<Socket>(Socket, MODULE);