2022-08-18 22:00:26 +00:00
|
|
|
import { addHandler, handleMessage } from "libkmodule";
|
2023-02-19 22:17:14 +00:00
|
|
|
import { ResolverRegistry, } from "./resolverRegistry.js";
|
2022-08-31 03:53:26 +00:00
|
|
|
import { DNS_RECORD_TYPE } from "@lumeweb/libresolver";
|
2023-02-19 22:17:14 +00:00
|
|
|
import { dnsClient } from "./client.js";
|
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;
|
2023-02-19 22:17:14 +00:00
|
|
|
resolver = new rm(new ResolverRegistry());
|
2022-08-18 22:00:26 +00:00
|
|
|
}
|
|
|
|
async function handleRegister(aq) {
|
2023-02-19 22:17:14 +00:00
|
|
|
await dnsClient.register();
|
2022-08-18 22:00:26 +00:00
|
|
|
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";
|