*Add resolvers getter

*Add ResolverModule mock class
This commit is contained in:
Derrick Hammer 2022-08-19 09:57:05 -04:00
parent cf4df9dc91
commit 0204e4d5e3
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 41 additions and 1 deletions

View File

@ -4,8 +4,9 @@ import {
DNS_RECORD_TYPE,
resolverError,
} from "@lumeweb/resolver-common";
import { resolve } from "@lumeweb/kernel-dns-client";
import { getResolvers, resolve } from "@lumeweb/kernel-dns-client";
import { RpcNetwork } from "@lumeweb/kernel-rpc-client";
import { callModule } from "libkmodule";
export class ResolverRegistry {
private _rpcNetwork: RpcNetwork;
@ -18,6 +19,18 @@ export class ResolverRegistry {
return this._rpcNetwork;
}
get resolvers(): Promise<Set<ResolverModule>> {
return getResolvers()
.catch(() => {
return new Set();
})
.then((resolvers: string[]) => {
return new Set(
resolvers.map((resolver) => new ResolverModule(this, resolver))
);
});
}
public async resolve(
domain: string,
options: ResolverOptions = { type: DNS_RECORD_TYPE.DEFAULT },
@ -35,4 +48,31 @@ export class ResolverRegistry {
}
}
export class ResolverModule {
private resolver: ResolverRegistry;
private domain: string;
constructor(resolver: ResolverRegistry, domain: string) {
this.resolver = resolver;
this.domain = domain;
}
async resolve(
domain: string,
options: ResolverOptions,
bypassCache: boolean
): Promise<DNSResult> {
const [ret, err] = await callModule(this.domain, "resolve", {
domain,
options,
bypassCache,
});
if (err) {
return resolverError(err);
}
return ret;
}
}
export { RpcNetwork };