kernel-swarm-client/src/index.ts

102 lines
2.5 KiB
TypeScript
Raw Normal View History

2022-07-20 01:38:29 +00:00
import {
2022-07-20 02:58:40 +00:00
callModule as callModuleKernel,
connectModule as connectModuleKernel,
2022-07-20 01:38:29 +00:00
} from "libkernel";
import {
2022-07-20 02:58:40 +00:00
callModule as callModuleModule,
connectModule as connectModuleModule,
2022-07-20 01:38:29 +00:00
} from "libkmodule";
import { EventEmitter } from "events";
import { DataFn, ErrTuple } from "libskynet";
import { Buffer } from "buffer";
const DHT_MODULE = "AQD1IgE4lTZkq1fqdoYGojKRNrSk0YQ_wrHbRtIiHDrnow";
let callModule: typeof callModuleModule,
2022-07-20 02:58:40 +00:00
connectModule: typeof connectModuleModule;
2022-07-20 01:38:29 +00:00
2022-07-20 21:23:48 +00:00
if (typeof window !== "undefined" && window?.document) {
callModule = callModuleKernel;
connectModule = connectModuleKernel;
2022-07-20 01:38:29 +00:00
} else {
2022-07-20 21:23:48 +00:00
callModule = callModuleModule;
connectModule = connectModuleModule;
2022-07-20 01:38:29 +00:00
}
export class DHT {
2022-07-20 02:58:40 +00:00
public async connect(pubkey: string): Promise<Socket> {
const [resp, err] = await callModule(DHT_MODULE, "connect", { pubkey });
if (err) {
2022-07-20 07:26:55 +00:00
throw new Error(err);
2022-07-20 01:38:29 +00:00
}
2022-07-20 02:58:40 +00:00
return new Socket(resp.id);
}
2022-07-20 01:38:29 +00:00
2022-07-20 02:58:40 +00:00
async ready(): Promise<ErrTuple> {
return callModule(DHT_MODULE, "ready");
}
public async addRelay(pubkey: string): Promise<void> {
const [, err] = await callModule(DHT_MODULE, "addRelay", { pubkey });
if (err) {
2022-07-20 07:26:55 +00:00
throw new Error(err);
2022-07-20 01:38:29 +00:00
}
2022-07-20 02:58:40 +00:00
}
2022-07-20 01:38:29 +00:00
}
export class Socket extends EventEmitter {
2022-07-20 02:58:40 +00:00
private id: number;
private eventUpdates: { [event: string]: DataFn[] } = {};
2022-07-20 01:38:29 +00:00
2022-07-20 02:58:40 +00:00
constructor(id: number) {
super();
this.id = id;
}
2022-07-20 01:38:29 +00:00
2022-07-20 02:58:40 +00:00
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);
2022-07-20 01:38:29 +00:00
2022-07-20 02:58:40 +00:00
promise.then(() => {
this.off(eventName, listener);
});
2022-07-20 01:38:29 +00:00
2022-07-20 02:58:40 +00:00
return super.on(eventName, listener);
}
2022-07-20 01:38:29 +00:00
2022-07-20 02:58:40 +00:00
off(type: string, listener: any): this {
const updates = [...this.eventUpdates[type]];
this.eventUpdates[type] = [];
for (const func of updates) {
func({ action: "off" });
2022-07-20 01:38:29 +00:00
}
2022-07-20 02:58:40 +00:00
return super.off(type, listener);
}
2022-07-20 01:38:29 +00:00
2022-07-20 02:58:40 +00:00
write(message: string | Buffer): void {
callModule(DHT_MODULE, "write", { id: this.id, message });
}
2022-07-20 01:38:29 +00:00
2022-07-20 02:58:40 +00:00
end(): void {
callModule(DHT_MODULE, "close", { id: this.id });
}
private ensureEvent(event: string): void {
if (!(event in this.eventUpdates)) {
this.eventUpdates[event] = [];
2022-07-20 01:38:29 +00:00
}
2022-07-20 02:58:40 +00:00
}
private trackEvent(event: string, update: DataFn): void {
this.ensureEvent(event as string);
this.eventUpdates[event].push(update);
}
2022-07-20 01:38:29 +00:00
}