2023-07-01 05:58:15 +00:00
|
|
|
import { Client, factory } from "@lumeweb/libkernel/module";
|
|
|
|
import type { Peer } from "@lumeweb/libpeerdiscovery";
|
2023-09-09 11:49:55 +00:00
|
|
|
import { hexToBytes } from "@lumeweb/libweb";
|
2023-07-01 05:58:15 +00:00
|
|
|
|
2023-09-03 04:26:59 +00:00
|
|
|
const MODULE = "zdiLW9MtAAMssP5vLBgd1FitouiVXzNUYZszFYG44uVKqCPDqUQox9aq1y";
|
2023-07-01 05:58:15 +00:00
|
|
|
|
2023-01-31 10:07:23 +00:00
|
|
|
export class PeerDiscoveryClient extends Client {
|
|
|
|
public async register(source: string): Promise<void> {
|
2023-07-01 05:58:15 +00:00
|
|
|
const bag = this.getBound(source);
|
2023-01-31 10:07:23 +00:00
|
|
|
const ret = await bag.callModule("register");
|
|
|
|
this.handleError(ret);
|
|
|
|
}
|
|
|
|
|
2023-01-31 14:01:30 +00:00
|
|
|
public async registerSelf(): Promise<void> {
|
|
|
|
return await this.callModuleReturn("register");
|
|
|
|
}
|
|
|
|
|
2023-01-31 10:07:23 +00:00
|
|
|
public async remove(name: string): Promise<boolean> {
|
|
|
|
return await this.callModuleReturn("remove", { name });
|
|
|
|
}
|
|
|
|
|
|
|
|
public async removeAll(): Promise<void> {
|
|
|
|
await this.callModuleReturn("removeAll");
|
|
|
|
}
|
|
|
|
|
|
|
|
public async exists(name: string): Promise<boolean> {
|
|
|
|
return await this.callModuleReturn("remove", { name });
|
|
|
|
}
|
2023-07-01 05:58:15 +00:00
|
|
|
|
2023-01-31 12:18:45 +00:00
|
|
|
public async discover(pubkey: string | Uint8Array): Promise<Peer | boolean> {
|
|
|
|
if (typeof pubkey === "string") {
|
2023-09-09 11:49:55 +00:00
|
|
|
pubkey = hexToBytes(pubkey);
|
2023-01-31 12:18:45 +00:00
|
|
|
}
|
2023-09-09 11:49:55 +00:00
|
|
|
|
2023-01-31 10:07:23 +00:00
|
|
|
return await this.callModuleReturn("discover", { pubkey });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const createClient = factory<PeerDiscoveryClient>(
|
|
|
|
PeerDiscoveryClient,
|
2023-07-01 05:58:15 +00:00
|
|
|
MODULE,
|
2023-01-31 10:07:23 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
export { Peer };
|