kernel-swarm-client/dist/index.js

91 lines
2.8 KiB
JavaScript
Raw Normal View History

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;
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-01 12:55:30 +00:00
return this.callModuleReturn("ready", { swarm: this.swarm });
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
}
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;
}
on(eventName, listener) {
2023-02-01 12:55:30 +00:00
const [update, promise] = this.connectModule("listenSocketEvent", { id: this.id, event: eventName }, (data) => {
2022-07-20 06:13:07 +00:00
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) {
2023-02-01 12:55:30 +00:00
this.callModule("write", { id: this.id, message });
2022-07-20 06:13:07 +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 12:55:30 +00:00
const MODULE = "_A73ORX4dxSkt7Cv8v6gtbV0W5EsLrdZX6SywPdSTFBPEg";
export const createClient = factory(SwarmClient, MODULE);
const createSocket = factory(Socket, MODULE);