2022-07-20 06:13:07 +00:00
|
|
|
import { EventEmitter } from "events";
|
|
|
|
const DHT_MODULE = "AQD1IgE4lTZkq1fqdoYGojKRNrSk0YQ_wrHbRtIiHDrnow";
|
|
|
|
let callModule, connectModule;
|
2022-07-21 04:56:59 +00:00
|
|
|
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;
|
|
|
|
}
|
2022-07-20 06:13:07 +00:00
|
|
|
}
|
|
|
|
export class DHT {
|
|
|
|
async connect(pubkey) {
|
2022-07-21 04:56:59 +00:00
|
|
|
await loadLibs();
|
2022-07-20 06:13:07 +00:00
|
|
|
const [resp, err] = await callModule(DHT_MODULE, "connect", { pubkey });
|
|
|
|
if (err) {
|
2022-07-20 07:27:17 +00:00
|
|
|
throw new Error(err);
|
2022-07-20 06:13:07 +00:00
|
|
|
}
|
|
|
|
return new Socket(resp.id);
|
|
|
|
}
|
|
|
|
async ready() {
|
2022-07-21 04:56:59 +00:00
|
|
|
await loadLibs();
|
2022-07-20 06:13:07 +00:00
|
|
|
return callModule(DHT_MODULE, "ready");
|
|
|
|
}
|
2022-07-20 07:27:17 +00:00
|
|
|
async addRelay(pubkey) {
|
2022-07-21 04:56:59 +00:00
|
|
|
await loadLibs();
|
2022-07-20 07:27:17 +00:00
|
|
|
const [, err] = await callModule(DHT_MODULE, "addRelay", { pubkey });
|
|
|
|
if (err) {
|
|
|
|
throw new Error(err);
|
|
|
|
}
|
|
|
|
}
|
2022-07-20 22:03:37 +00:00
|
|
|
async removeRelay(pubkey) {
|
2022-07-21 04:56:59 +00:00
|
|
|
await loadLibs();
|
2022-07-20 22:03:37 +00:00
|
|
|
const [, err] = await callModule(DHT_MODULE, "removeRelay", { pubkey });
|
|
|
|
if (err) {
|
|
|
|
throw new Error(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
async clearRelays() {
|
2022-07-21 04:56:59 +00:00
|
|
|
await loadLibs();
|
2022-07-20 22:03:37 +00:00
|
|
|
await callModule(DHT_MODULE, "clearRelays");
|
|
|
|
}
|
2022-07-20 06:13:07 +00:00
|
|
|
}
|
|
|
|
export class Socket extends EventEmitter {
|
|
|
|
id;
|
|
|
|
eventUpdates = {};
|
|
|
|
constructor(id) {
|
|
|
|
super();
|
|
|
|
this.id = id;
|
|
|
|
}
|
|
|
|
on(eventName, listener) {
|
|
|
|
const [update, promise] = connectModule(DHT_MODULE, "listenSocketEvent", { id: this.id, event: eventName }, (data) => {
|
|
|
|
this.emit(eventName, data);
|
|
|
|
});
|
|
|
|
this.trackEvent(eventName, update);
|
|
|
|
promise.then(() => {
|
|
|
|
this.off(eventName, listener);
|
|
|
|
});
|
|
|
|
return super.on(eventName, listener);
|
|
|
|
}
|
|
|
|
off(type, listener) {
|
|
|
|
const updates = [...this.eventUpdates[type]];
|
|
|
|
this.eventUpdates[type] = [];
|
|
|
|
for (const func of updates) {
|
|
|
|
func({ action: "off" });
|
|
|
|
}
|
|
|
|
return super.off(type, listener);
|
|
|
|
}
|
|
|
|
write(message) {
|
|
|
|
callModule(DHT_MODULE, "write", { id: this.id, message });
|
|
|
|
}
|
|
|
|
end() {
|
|
|
|
callModule(DHT_MODULE, "close", { id: this.id });
|
|
|
|
}
|
|
|
|
ensureEvent(event) {
|
|
|
|
if (!(event in this.eventUpdates)) {
|
|
|
|
this.eventUpdates[event] = [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
trackEvent(event, update) {
|
|
|
|
this.ensureEvent(event);
|
|
|
|
this.eventUpdates[event].push(update);
|
|
|
|
}
|
|
|
|
}
|