kernel-libresolver/dist/index.js

41 lines
1.2 KiB
JavaScript
Raw Normal View History

2022-08-18 22:00:26 +00:00
import { addHandler, handleMessage } from "libkmodule";
import { register } from "@lumeweb/kernel-dns-client";
2022-08-20 06:57:45 +00:00
import { RpcNetwork } from "@lumeweb/kernel-rpc-client";
import { ResolverRegistry } from "./resolverRegistry.js";
2022-08-20 16:43:57 +00:00
import { DNS_RECORD_TYPE } from "@lumeweb/resolver-common";
2022-08-18 22:00:26 +00:00
let resolver;
export function setup(rm) {
addHandler("resolve", handleResolve);
addHandler("register", handleRegister);
2022-08-19 15:21:13 +00:00
addHandler("getSupportedTlds", handleGetSupportedTlds);
2022-08-18 22:00:26 +00:00
onmessage = handleMessage;
2022-08-20 06:57:45 +00:00
resolver = new rm(new ResolverRegistry(new RpcNetwork()));
2022-08-18 22:00:26 +00:00
}
async function handleRegister(aq) {
await register();
aq.respond();
}
async function handleResolve(aq) {
if (!("domain" in aq.callerInput)) {
aq.reject("domain required");
return;
}
let ret;
try {
2022-08-20 16:43:57 +00:00
ret = await resolver.resolve(aq.callerInput.domain, aq.callerInput?.options ?? { type: DNS_RECORD_TYPE.CONTENT }, aq.callerInput?.bypassCache || false);
2022-08-18 22:00:26 +00:00
}
catch (e) {
aq.reject(e);
return;
}
if (ret.error) {
aq.reject(ret.error);
return;
}
aq.respond(ret);
}
2022-08-19 15:21:13 +00:00
function handleGetSupportedTlds(aq) {
aq.respond(resolver.getSupportedTlds());
}
2022-08-18 22:28:56 +00:00
export * from "./resolverRegistry.js";