kernel-swarm-client/src/index.ts

190 lines
4.5 KiB
TypeScript
Raw Normal View History

2022-08-03 16:05:10 +00:00
import { EventEmitter } from "events";
import { DataFn, ErrTuple } from "libskynet";
import { Buffer } from "buffer";
2022-07-20 01:38:29 +00:00
const DHT_MODULE = "AQD1IgE4lTZkq1fqdoYGojKRNrSk0YQ_wrHbRtIiHDrnow";
2022-08-03 16:05:10 +00:00
let callModule: any, connectModule: any;
2022-07-20 01:38:29 +00:00
async function loadLibs() {
2022-08-03 16:05:10 +00:00
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;
}
2022-07-20 01:38:29 +00:00
}
export class DHT {
2022-08-03 16:05:10 +00:00
private useDefaultDht: boolean;
2022-08-03 16:27:47 +00:00
private id: number = 0;
2022-07-20 01:38:29 +00:00
2022-08-03 16:05:10 +00:00
constructor(useDefaultDht = true) {
this.useDefaultDht = useDefaultDht;
}
2022-08-03 16:05:10 +00:00
public async connect(pubkey: string): Promise<Socket> {
await this.setup();
2022-08-04 01:36:30 +00:00
const dht = !this.useDefaultDht ? this.id : undefined;
const [resp, err] = await callModule(DHT_MODULE, "connect", {
pubkey,
dht,
});
2022-08-03 16:05:10 +00:00
if (err) {
throw new Error(err);
}
2022-08-03 16:05:10 +00:00
return new Socket(resp.id);
}
2022-07-20 02:58:40 +00:00
2022-08-03 16:05:10 +00:00
async ready(): Promise<ErrTuple> {
await this.setup();
2022-08-03 16:05:10 +00:00
const dht = !this.useDefaultDht ? this.id : undefined;
return callModule(DHT_MODULE, "ready", { dht });
}
2022-08-03 16:05:10 +00:00
public async addRelay(pubkey: string): Promise<void> {
await this.setup();
2022-08-03 16:05:10 +00:00
const dht = !this.useDefaultDht ? this.id : undefined;
const [, err] = await callModule(DHT_MODULE, "addRelay", { pubkey, dht });
if (err) {
throw new Error(err);
}
2022-08-03 16:05:10 +00:00
}
2022-08-03 16:05:10 +00:00
public async removeRelay(pubkey: string): Promise<void> {
await this.setup();
2022-08-03 16:05:10 +00:00
const dht = !this.useDefaultDht ? this.id : undefined;
const [, err] = await callModule(DHT_MODULE, "removeRelay", {
pubkey,
dht,
});
if (err) {
throw new Error(err);
}
2022-08-03 16:05:10 +00:00
}
2022-08-03 16:05:10 +00:00
public async clearRelays(): Promise<void> {
await this.setup();
2022-08-03 16:05:10 +00:00
const dht = !this.useDefaultDht ? this.id : undefined;
await callModule(DHT_MODULE, "clearRelays", { dht });
}
public async getRelays(): Promise<string[]> {
await this.setup();
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;
}
2022-08-03 16:05:10 +00:00
private async create() {
await loadLibs();
if (this.useDefaultDht || this.id > 0) {
2022-08-03 16:05:10 +00:00
return Promise.resolve();
2022-07-20 01:38:29 +00:00
}
2022-08-03 16:05:10 +00:00
const [dht, err] = await callModule(DHT_MODULE, "openDht");
if (err) {
throw new Error(err);
2022-07-20 01:38:29 +00:00
}
2022-08-04 01:09:24 +00:00
this.id = dht.dht;
2022-08-03 16:05:10 +00:00
}
2022-07-20 01:38:29 +00:00
2022-08-03 16:05:10 +00:00
public async close(): Promise<boolean> {
await this.setup();
2022-08-03 16:05:10 +00:00
if (this.useDefaultDht) {
return false;
}
2022-08-03 16:05:10 +00:00
const [, err] = await callModule(DHT_MODULE, "closeDht", { dht: this.id });
if (err) {
throw new Error(err);
2022-07-20 01:38:29 +00:00
}
2022-07-20 02:58:40 +00:00
2022-08-03 16:05:10 +00:00
return true;
}
2022-08-04 01:36:30 +00:00
private async setup() {
await loadLibs();
await this.create();
}
2022-08-03 16:05:10 +00:00
}
2022-08-03 16:05:10 +00:00
export class Socket extends EventEmitter {
private id: number;
private eventUpdates: { [event: string]: DataFn[] } = {};
constructor(id: number) {
super();
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);
}
write(message: string | Buffer): void {
callModule(DHT_MODULE, "write", { id: this.id, message });
}
2022-09-01 00:34:41 +00:00
close(): void {
callModule(DHT_MODULE, "socketExists", { id: this.id }).then(
([exists]: ErrTuple) => {
if (exists) {
callModule(DHT_MODULE, "close", { id: this.id });
}
}
);
2022-08-03 16:05:10 +00:00
}
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);
}
2022-07-20 01:38:29 +00:00
}