kernel-libresolver/dist/resolverRegistry.js

60 lines
1.6 KiB
JavaScript
Raw Normal View History

2022-08-18 22:28:56 +00:00
import { DNS_RECORD_TYPE, resolverError, } from "@lumeweb/resolver-common";
2022-08-19 13:57:34 +00:00
import { getResolvers, resolve } from "@lumeweb/kernel-dns-client";
2022-08-18 22:50:54 +00:00
import { RpcNetwork } from "@lumeweb/kernel-rpc-client";
2022-08-19 13:57:34 +00:00
import { callModule } from "libkmodule";
2022-08-18 22:28:56 +00:00
export class ResolverRegistry {
2022-08-18 22:50:54 +00:00
_rpcNetwork;
constructor(network) {
this._rpcNetwork = network;
}
get rpcNetwork() {
return this._rpcNetwork;
}
2022-08-19 13:57:34 +00:00
get resolvers() {
return getResolvers()
.catch(() => {
return new Set();
})
.then((resolvers) => {
return new Set(resolvers.map((resolver) => new ResolverModule(this, resolver)));
});
}
2022-08-18 22:28:56 +00:00
async resolve(domain, options = { type: DNS_RECORD_TYPE.DEFAULT }, bypassCache = false) {
let result;
try {
result = await resolve(domain, options, bypassCache);
}
catch (e) {
return resolverError(e);
}
return result;
}
}
2022-08-19 13:57:34 +00:00
export class ResolverModule {
resolver;
domain;
constructor(resolver, domain) {
this.resolver = resolver;
this.domain = domain;
}
async resolve(domain, options, bypassCache) {
const [ret, err] = await callModule(this.domain, "resolve", {
domain,
options,
bypassCache,
});
if (err) {
return resolverError(err);
}
return ret;
}
2022-08-19 15:21:13 +00:00
async getSupportedTlds() {
const [ret, err] = await callModule(this.domain, "getSupportedTlds");
if (err) {
throw new Error(err);
}
return ret;
}
2022-08-19 13:57:34 +00:00
}
2022-08-18 22:50:54 +00:00
export { RpcNetwork };