kernel-discovery-irc/src/index.ts

59 lines
1.3 KiB
TypeScript
Raw Normal View History

2023-07-01 21:31:05 +00:00
import type { ActiveQuery } from "@lumeweb/libkernel/module";
import { addHandler, handleMessage } from "@lumeweb/libkernel/module";
2023-01-18 08:19:39 +00:00
import DiscoveryIRC from "@lumeweb/peer-discovery-irc";
import { createClient } from "@lumeweb/kernel-peer-discovery-client";
2023-01-18 08:19:39 +00:00
onmessage = handleMessage;
const client = createClient();
2023-01-18 08:19:39 +00:00
async function handleRegister(aq: ActiveQuery) {
try {
await client.registerSelf();
} catch (e) {
aq.reject((e as Error).message);
2023-01-18 08:19:39 +00:00
return;
}
aq.respond();
2023-01-18 08:19:39 +00:00
}
async function handleName(aq: ActiveQuery): Promise<void> {
aq.respond("irc");
}
async function handleDiscover(aq: ActiveQuery): Promise<void> {
if (!("pubkey" in aq.callerInput)) {
aq.reject("pubkey required");
return;
}
if (aq.callerInput.pubkey.length !== 32) {
aq.reject("pubkey must be 32 bytes");
return;
}
if (
"options" in aq.callerInput &&
typeof aq.callerInput.options !== "object"
) {
aq.reject(`options must be an object`);
return;
}
2023-07-20 16:56:05 +00:00
try {
const ret = await DiscoveryIRC(aq.callerInput.pubkey, {
host: "liberta.casa",
...aq.callerInput?.options,
});
2023-01-18 08:19:39 +00:00
2023-07-20 16:56:05 +00:00
aq.respond(ret);
} catch (e) {
aq.reject(e);
}
2023-01-18 08:19:39 +00:00
}
addHandler("register", handleRegister);
addHandler("name", handleName);
addHandler("discover", handleDiscover);