*prettier

This commit is contained in:
Derrick Hammer 2022-08-03 12:05:10 -04:00
parent 9071ed0592
commit f76391d46e
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 132 additions and 130 deletions

View File

@ -1,154 +1,156 @@
import {EventEmitter} from "events"; import { EventEmitter } from "events";
import {DataFn, ErrTuple} from "libskynet"; import { DataFn, ErrTuple } from "libskynet";
import {Buffer} from "buffer"; import { Buffer } from "buffer";
const DHT_MODULE = "AQD1IgE4lTZkq1fqdoYGojKRNrSk0YQ_wrHbRtIiHDrnow"; const DHT_MODULE = "AQD1IgE4lTZkq1fqdoYGojKRNrSk0YQ_wrHbRtIiHDrnow";
let callModule: any, let callModule: any, connectModule: any;
connectModule: any;
async function loadLibs() { async function loadLibs() {
if (callModule && connectModule) { if (callModule && connectModule) {
return; return;
} }
if (typeof window !== "undefined" && window?.document) { if (typeof window !== "undefined" && window?.document) {
const pkg = (await import("libkernel")); const pkg = await import("libkernel");
callModule = pkg.callModule; callModule = pkg.callModule;
connectModule = pkg.connectModule; connectModule = pkg.connectModule;
} else { } else {
const pkg = (await import("libkmodule")); const pkg = await import("libkmodule");
callModule = pkg.callModule; callModule = pkg.callModule;
connectModule = pkg.connectModule; connectModule = pkg.connectModule;
} }
} }
export class DHT { export class DHT {
private useDefaultDht: boolean; private useDefaultDht: boolean;
private id: number; private id: number;
constructor(useDefaultDht = true) { constructor(useDefaultDht = true) {
this.useDefaultDht = useDefaultDht; this.useDefaultDht = useDefaultDht;
}
public async connect(pubkey: string): Promise<Socket> {
await loadLibs();
const [resp, err] = await callModule(DHT_MODULE, "connect", { pubkey });
if (err) {
throw new Error(err);
}
return new Socket(resp.id);
}
async ready(): Promise<ErrTuple> {
await loadLibs();
const dht = !this.useDefaultDht ? this.id : undefined;
return callModule(DHT_MODULE, "ready", { dht });
}
public async addRelay(pubkey: string): Promise<void> {
await loadLibs();
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> {
await loadLibs();
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> {
await loadLibs();
const dht = !this.useDefaultDht ? this.id : undefined;
await callModule(DHT_MODULE, "clearRelays", { dht });
}
private async create() {
await loadLibs();
if (this.useDefaultDht) {
return Promise.resolve();
}
const [dht, err] = await callModule(DHT_MODULE, "openDht");
if (err) {
throw new Error(err);
} }
public async connect(pubkey: string): Promise<Socket> { this.id = dht;
await loadLibs(); }
const [resp, err] = await callModule(DHT_MODULE, "connect", {pubkey});
if (err) { public async close(): Promise<boolean> {
throw new Error(err); await loadLibs();
}
return new Socket(resp.id); if (this.useDefaultDht) {
return false;
}
const [, err] = await callModule(DHT_MODULE, "closeDht", { dht: this.id });
if (err) {
throw new Error(err);
} }
async ready(): Promise<ErrTuple> { return true;
await loadLibs(); }
const dht = !this.useDefaultDht ? this.id : undefined;
return callModule(DHT_MODULE, "ready", {dht});
}
public async addRelay(pubkey: string): Promise<void> {
await loadLibs();
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> {
await loadLibs();
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> {
await loadLibs();
const dht = !this.useDefaultDht ? this.id : undefined;
await callModule(DHT_MODULE, "clearRelays", {dht});
}
private async create() {
await loadLibs();
if (this.useDefaultDht) {
return Promise.resolve();
}
const [dht, err] = await callModule(DHT_MODULE, "openDht");
if (err) {
throw new Error(err);
}
this.id = dht;
}
public async close(): Promise<boolean> {
await loadLibs();
if (this.useDefaultDht) {
return false;
}
const [, err] = await callModule(DHT_MODULE, "closeDht", {dht: this.id});
if (err) {
throw new Error(err);
}
return true;
}
} }
export class Socket extends EventEmitter { export class Socket extends EventEmitter {
private id: number; private id: number;
private eventUpdates: { [event: string]: DataFn[] } = {}; private eventUpdates: { [event: string]: DataFn[] } = {};
constructor(id: number) { constructor(id: number) {
super(); super();
this.id = id; this.id = id;
}
on(eventName: string, listener: (...args: any[]) => void): this {
const [update, promise] = connectModule(
DHT_MODULE,
"listenSocketEvent",
{ id: this.id, event: eventName },
(data: any) => {
this.emit(eventName, data);
}
);
this.trackEvent(eventName, update);
promise.then(() => {
this.off(eventName, listener);
});
return super.on(eventName, listener);
}
off(type: string, listener: any): this {
const updates = [...this.eventUpdates[type]];
this.eventUpdates[type] = [];
for (const func of updates) {
func({ action: "off" });
} }
return super.off(type, listener);
}
on(eventName: string, listener: (...args: any[]) => void): this { write(message: string | Buffer): void {
const [update, promise] = connectModule( callModule(DHT_MODULE, "write", { id: this.id, message });
DHT_MODULE, }
"listenSocketEvent",
{id: this.id, event: eventName},
(data: any) => {
this.emit(eventName, data);
}
);
this.trackEvent(eventName, update);
promise.then(() => { end(): void {
this.off(eventName, listener); callModule(DHT_MODULE, "close", { id: this.id });
}); }
return super.on(eventName, listener); private ensureEvent(event: string): void {
if (!(event in this.eventUpdates)) {
this.eventUpdates[event] = [];
} }
}
off(type: string, listener: any): this { private trackEvent(event: string, update: DataFn): void {
const updates = [...this.eventUpdates[type]]; this.ensureEvent(event as string);
this.eventUpdates[type] = []; this.eventUpdates[event].push(update);
for (const func of updates) { }
func({action: "off"});
}
return super.off(type, listener);
}
write(message: string | Buffer): void {
callModule(DHT_MODULE, "write", {id: this.id, message});
}
end(): void {
callModule(DHT_MODULE, "close", {id: this.id});
}
private ensureEvent(event: string): void {
if (!(event in this.eventUpdates)) {
this.eventUpdates[event] = [];
}
}
private trackEvent(event: string, update: DataFn): void {
this.ensureEvent(event as string);
this.eventUpdates[event].push(update);
}
} }