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) {
|
2022-08-03 16:29:53 +00:00
|
|
|
const pkg = await import("libkernel");
|
2022-07-21 04:56:59 +00:00
|
|
|
callModule = pkg.callModule;
|
|
|
|
connectModule = pkg.connectModule;
|
|
|
|
}
|
|
|
|
else {
|
2022-08-03 16:29:53 +00:00
|
|
|
const pkg = await import("libkmodule");
|
2022-07-21 04:56:59 +00:00
|
|
|
callModule = pkg.callModule;
|
|
|
|
connectModule = pkg.connectModule;
|
|
|
|
}
|
2022-07-20 06:13:07 +00:00
|
|
|
}
|
|
|
|
export class DHT {
|
2022-08-03 16:29:53 +00:00
|
|
|
useDefaultDht;
|
2022-08-03 16:32:46 +00:00
|
|
|
id = 0;
|
2022-08-03 16:29:53 +00:00
|
|
|
constructor(useDefaultDht = true) {
|
|
|
|
this.useDefaultDht = useDefaultDht;
|
|
|
|
}
|
2022-07-20 06:13:07 +00:00
|
|
|
async connect(pubkey) {
|
2022-08-03 16:37:15 +00:00
|
|
|
await this.setup();
|
2022-08-04 01:36:52 +00:00
|
|
|
const dht = !this.useDefaultDht ? this.id : undefined;
|
|
|
|
const [resp, err] = await callModule(DHT_MODULE, "connect", {
|
|
|
|
pubkey,
|
|
|
|
dht,
|
|
|
|
});
|
2022-07-20 06:13:07 +00:00
|
|
|
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-08-03 16:37:15 +00:00
|
|
|
await this.setup();
|
2022-08-03 16:29:53 +00:00
|
|
|
const dht = !this.useDefaultDht ? this.id : undefined;
|
|
|
|
return callModule(DHT_MODULE, "ready", { dht });
|
2022-07-20 06:13:07 +00:00
|
|
|
}
|
2022-07-20 07:27:17 +00:00
|
|
|
async addRelay(pubkey) {
|
2022-08-03 16:37:15 +00:00
|
|
|
await this.setup();
|
2022-08-03 16:29:53 +00:00
|
|
|
const dht = !this.useDefaultDht ? this.id : undefined;
|
|
|
|
const [, err] = await callModule(DHT_MODULE, "addRelay", { pubkey, dht });
|
2022-07-20 07:27:17 +00:00
|
|
|
if (err) {
|
|
|
|
throw new Error(err);
|
|
|
|
}
|
|
|
|
}
|
2022-07-20 22:03:37 +00:00
|
|
|
async removeRelay(pubkey) {
|
2022-08-03 16:37:15 +00:00
|
|
|
await this.setup();
|
2022-08-03 16:29:53 +00:00
|
|
|
const dht = !this.useDefaultDht ? this.id : undefined;
|
|
|
|
const [, err] = await callModule(DHT_MODULE, "removeRelay", {
|
|
|
|
pubkey,
|
|
|
|
dht,
|
|
|
|
});
|
2022-07-20 22:03:37 +00:00
|
|
|
if (err) {
|
|
|
|
throw new Error(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
async clearRelays() {
|
2022-08-03 16:37:15 +00:00
|
|
|
await this.setup();
|
2022-08-03 16:29:53 +00:00
|
|
|
const dht = !this.useDefaultDht ? this.id : undefined;
|
|
|
|
await callModule(DHT_MODULE, "clearRelays", { dht });
|
|
|
|
}
|
2022-08-14 11:33:04 +00:00
|
|
|
async getRelays() {
|
|
|
|
await this.setup();
|
|
|
|
const [list, err] = await callModule(DHT_MODULE, "getRelays");
|
|
|
|
if (err) {
|
|
|
|
throw new Error(err);
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
async getRelayServers() {
|
|
|
|
await this.setup();
|
|
|
|
const [list, err] = await callModule(DHT_MODULE, "getRelayServers");
|
|
|
|
if (err) {
|
|
|
|
throw new Error(err);
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
2022-08-03 16:29:53 +00:00
|
|
|
async create() {
|
|
|
|
await loadLibs();
|
2022-08-03 16:37:15 +00:00
|
|
|
if (this.useDefaultDht || this.id > 0) {
|
2022-08-03 16:29:53 +00:00
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
const [dht, err] = await callModule(DHT_MODULE, "openDht");
|
|
|
|
if (err) {
|
|
|
|
throw new Error(err);
|
|
|
|
}
|
2022-08-04 01:09:44 +00:00
|
|
|
this.id = dht.dht;
|
2022-08-03 16:29:53 +00:00
|
|
|
}
|
|
|
|
async close() {
|
2022-08-03 16:37:15 +00:00
|
|
|
await this.setup();
|
2022-08-03 16:29:53 +00:00
|
|
|
if (this.useDefaultDht) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const [, err] = await callModule(DHT_MODULE, "closeDht", { dht: this.id });
|
|
|
|
if (err) {
|
|
|
|
throw new Error(err);
|
|
|
|
}
|
|
|
|
return true;
|
2022-07-20 22:03:37 +00:00
|
|
|
}
|
2022-08-03 16:37:15 +00:00
|
|
|
async setup() {
|
|
|
|
await loadLibs();
|
|
|
|
await this.create();
|
|
|
|
}
|
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 });
|
|
|
|
}
|
2022-09-01 00:38:27 +00:00
|
|
|
end() {
|
2022-08-04 02:26:40 +00:00
|
|
|
callModule(DHT_MODULE, "socketExists", { id: this.id }).then(([exists]) => {
|
|
|
|
if (exists) {
|
|
|
|
callModule(DHT_MODULE, "close", { id: this.id });
|
|
|
|
}
|
|
|
|
});
|
2022-07-20 06:13:07 +00:00
|
|
|
}
|
|
|
|
ensureEvent(event) {
|
|
|
|
if (!(event in this.eventUpdates)) {
|
|
|
|
this.eventUpdates[event] = [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
trackEvent(event, update) {
|
|
|
|
this.ensureEvent(event);
|
|
|
|
this.eventUpdates[event].push(update);
|
|
|
|
}
|
|
|
|
}
|