2023-02-01 12:55:30 +00:00
|
|
|
import { Client, factory } from "@lumeweb/libkernel-universal";
|
|
|
|
import { hexToBuf } from "@siaweb/libweb";
|
|
|
|
export class SwarmClient extends Client {
|
|
|
|
useDefaultSwarm;
|
2022-08-03 16:32:46 +00:00
|
|
|
id = 0;
|
2023-02-01 12:55:30 +00:00
|
|
|
get swarm() {
|
|
|
|
return this.useDefaultSwarm ? undefined : this.id;
|
|
|
|
}
|
2022-08-03 16:29:53 +00:00
|
|
|
constructor(useDefaultDht = true) {
|
2023-02-01 12:55:30 +00:00
|
|
|
super();
|
|
|
|
this.useDefaultSwarm = useDefaultDht;
|
2022-08-03 16:29:53 +00:00
|
|
|
}
|
2022-07-20 06:13:07 +00:00
|
|
|
async connect(pubkey) {
|
2023-02-01 12:55:30 +00:00
|
|
|
if (typeof pubkey === "string") {
|
|
|
|
const buf = hexToBuf(pubkey);
|
|
|
|
pubkey = this.handleErrorOrReturn(buf);
|
|
|
|
}
|
|
|
|
const resp = this.callModuleReturn("connect", {
|
2022-08-04 01:36:52 +00:00
|
|
|
pubkey,
|
2023-02-01 12:55:30 +00:00
|
|
|
swarm: this.swarm,
|
2022-08-04 01:36:52 +00:00
|
|
|
});
|
2023-02-01 12:55:30 +00:00
|
|
|
return createSocket(resp.id);
|
2022-07-20 06:13:07 +00:00
|
|
|
}
|
2023-02-01 13:47:57 +00:00
|
|
|
async init() {
|
|
|
|
return this.callModuleReturn("init", { swarm: this.swarm });
|
|
|
|
}
|
2022-07-20 06:13:07 +00:00
|
|
|
async ready() {
|
2023-02-06 08:26:19 +00:00
|
|
|
await this.callModuleReturn("ready", { swarm: this.swarm });
|
|
|
|
this.connectModule("listenConnections", { swarm: this.swarm }, (socketId) => {
|
|
|
|
this.emit("connection", createSocket(socketId));
|
|
|
|
});
|
2022-07-20 06:13:07 +00:00
|
|
|
}
|
2022-07-20 07:27:17 +00:00
|
|
|
async addRelay(pubkey) {
|
2023-02-01 12:55:30 +00:00
|
|
|
return this.callModuleReturn("addRelay", { pubkey, swarm: this.swarm });
|
2022-07-20 07:27:17 +00:00
|
|
|
}
|
2022-07-20 22:03:37 +00:00
|
|
|
async removeRelay(pubkey) {
|
2023-02-01 12:55:30 +00:00
|
|
|
return this.callModuleReturn("removeRelay", { pubkey, swarm: this.swarm });
|
2022-07-20 22:03:37 +00:00
|
|
|
}
|
|
|
|
async clearRelays() {
|
2023-02-01 12:55:30 +00:00
|
|
|
return this.callModuleReturn("clearRelays", { swarm: this.swarm });
|
2022-08-03 16:29:53 +00:00
|
|
|
}
|
2022-08-14 11:33:04 +00:00
|
|
|
async getRelays() {
|
2023-02-01 12:55:30 +00:00
|
|
|
return this.callModuleReturn("getRelays", { swarm: this.swarm });
|
2022-08-03 16:37:15 +00:00
|
|
|
}
|
2023-02-01 19:08:26 +00:00
|
|
|
async join(topic) {
|
2023-02-01 19:06:25 +00:00
|
|
|
this.callModule("join", { id: this.id, topic });
|
|
|
|
}
|
2022-07-20 06:13:07 +00:00
|
|
|
}
|
2023-02-01 12:55:30 +00:00
|
|
|
export class Socket extends Client {
|
2022-07-20 06:13:07 +00:00
|
|
|
id;
|
|
|
|
eventUpdates = {};
|
|
|
|
constructor(id) {
|
|
|
|
super();
|
|
|
|
this.id = id;
|
|
|
|
}
|
2023-02-06 08:26:19 +00:00
|
|
|
on(event, fn, context) {
|
|
|
|
const [update, promise] = this.connectModule("listenSocketEvent", { id: this.id, event: event }, (data) => {
|
|
|
|
this.emit(event, data);
|
2022-07-20 06:13:07 +00:00
|
|
|
});
|
2023-02-06 08:26:19 +00:00
|
|
|
this.trackEvent(event, update);
|
2022-07-20 06:13:07 +00:00
|
|
|
promise.then(() => {
|
2023-02-06 08:26:19 +00:00
|
|
|
this.off(event, fn);
|
2022-07-20 06:13:07 +00:00
|
|
|
});
|
2023-02-06 08:26:19 +00:00
|
|
|
return super.on(event, fn, context);
|
2022-07-20 06:13:07 +00:00
|
|
|
}
|
2023-02-06 08:26:19 +00:00
|
|
|
off(event, fn, context, once) {
|
|
|
|
const updates = [...this.eventUpdates[event]];
|
|
|
|
this.eventUpdates[event] = [];
|
2022-07-20 06:13:07 +00:00
|
|
|
for (const func of updates) {
|
2023-02-06 08:26:19 +00:00
|
|
|
func();
|
2022-07-20 06:13:07 +00:00
|
|
|
}
|
2023-02-06 08:26:19 +00:00
|
|
|
return super.off(event, fn, context, once);
|
2022-07-20 06:13:07 +00:00
|
|
|
}
|
|
|
|
write(message) {
|
2023-02-01 12:55:30 +00:00
|
|
|
this.callModule("write", { id: this.id, message });
|
2022-07-20 06:13:07 +00:00
|
|
|
}
|
2022-09-01 00:38:27 +00:00
|
|
|
end() {
|
2023-02-01 12:55:30 +00:00
|
|
|
this.callModule("socketExists", { id: this.id }).then(([exists]) => {
|
2022-08-04 02:26:40 +00:00
|
|
|
if (exists) {
|
2023-02-01 12:55:30 +00:00
|
|
|
this.callModule("close", { id: this.id });
|
2022-08-04 02:26:40 +00:00
|
|
|
}
|
|
|
|
});
|
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);
|
|
|
|
}
|
|
|
|
}
|
2023-02-01 17:33:30 +00:00
|
|
|
const MODULE = "_A7ClA0mSa1-Pg5c4V3C0H_fnhAFjgccITYT83Euc7t_9A";
|
2023-02-01 12:55:30 +00:00
|
|
|
export const createClient = factory(SwarmClient, MODULE);
|
|
|
|
const createSocket = factory(Socket, MODULE);
|