kernel-swarm-client/src/index.ts

119 lines
3.1 KiB
TypeScript
Raw Normal View History

2022-08-03 16:05:10 +00:00
import { Buffer } from "buffer";
2023-02-01 12:54:20 +00:00
import { Client, factory } from "@lumeweb/libkernel-universal";
import { hexToBuf, DataFn, ErrTuple } from "@siaweb/libweb";
2022-07-20 01:38:29 +00:00
2023-02-01 12:54:20 +00:00
export class SwarmClient extends Client {
private useDefaultSwarm: boolean;
private id: number = 0;
2022-07-20 01:38:29 +00:00
2023-02-01 12:54:20 +00:00
get swarm(): number | undefined {
return this.useDefaultSwarm ? undefined : this.id;
2022-08-03 16:05:10 +00:00
}
2022-07-20 01:38:29 +00:00
2022-08-03 16:05:10 +00:00
constructor(useDefaultDht = true) {
2023-02-01 12:54:20 +00:00
super();
this.useDefaultSwarm = useDefaultDht;
2022-08-03 16:05:10 +00:00
}
2023-02-01 12:54:20 +00:00
public async connect(pubkey: string | Uint8Array): Promise<Socket> {
if (typeof pubkey === "string") {
const buf = hexToBuf(pubkey);
pubkey = this.handleErrorOrReturn(buf);
}
2023-02-01 12:54:20 +00:00
const resp = this.callModuleReturn("connect", {
pubkey,
swarm: this.swarm,
}) as any;
return createSocket(resp.id);
2022-08-03 16:05:10 +00:00
}
2023-02-01 13:47:19 +00:00
async init(): Promise<ErrTuple> {
return this.callModuleReturn("init", { swarm: this.swarm });
}
2022-08-03 16:05:10 +00:00
async ready(): Promise<ErrTuple> {
2023-02-01 12:54:20 +00:00
return this.callModuleReturn("ready", { swarm: this.swarm });
2022-08-03 16:05:10 +00:00
}
2022-08-03 16:05:10 +00:00
public async addRelay(pubkey: string): Promise<void> {
2023-02-01 12:54:20 +00:00
return this.callModuleReturn("addRelay", { pubkey, swarm: this.swarm });
2022-08-03 16:05:10 +00:00
}
2022-08-03 16:05:10 +00:00
public async removeRelay(pubkey: string): Promise<void> {
2023-02-01 12:54:20 +00:00
return this.callModuleReturn("removeRelay", { pubkey, swarm: this.swarm });
2022-08-03 16:05:10 +00:00
}
2022-08-03 16:05:10 +00:00
public async clearRelays(): Promise<void> {
2023-02-01 12:54:20 +00:00
return this.callModuleReturn("clearRelays", { swarm: this.swarm });
2022-08-03 16:05:10 +00:00
}
public async getRelays(): Promise<string[]> {
2023-02-01 12:54:20 +00:00
return this.callModuleReturn("getRelays", { swarm: this.swarm });
}
2022-08-03 16:05:10 +00:00
}
2023-02-01 12:54:20 +00:00
export class Socket extends Client {
2022-08-03 16:05:10 +00:00
private id: number;
private eventUpdates: { [event: string]: DataFn[] } = {};
constructor(id: number) {
super();
this.id = id;
}
on(eventName: string, listener: (...args: any[]) => void): this {
2023-02-01 12:54:20 +00:00
const [update, promise] = this.connectModule(
2022-08-03 16:05:10 +00:00
"listenSocketEvent",
{ id: this.id, event: eventName },
(data: any) => {
this.emit(eventName, data);
}
);
this.trackEvent(eventName, update);
promise.then(() => {
this.off(eventName, listener);
});
2023-02-01 12:54:20 +00:00
return super.on(eventName, listener) as this;
2022-08-03 16:05:10 +00:00
}
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 {
2023-02-01 12:54:20 +00:00
this.callModule("write", { id: this.id, message });
2022-08-03 16:05:10 +00:00
}
end(): void {
2023-02-01 12:54:20 +00:00
this.callModule("socketExists", { id: this.id }).then(
([exists]: ErrTuple) => {
if (exists) {
2023-02-01 12:54:20 +00:00
this.callModule("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
}
2023-02-01 12:54:20 +00:00
const MODULE = "_A73ORX4dxSkt7Cv8v6gtbV0W5EsLrdZX6SywPdSTFBPEg";
export const createClient = factory<SwarmClient>(SwarmClient, MODULE);
const createSocket = factory<Socket>(Socket, MODULE);