2022-08-18 22:28:36 +00:00
|
|
|
import {
|
|
|
|
DNSResult,
|
|
|
|
ResolverOptions,
|
|
|
|
DNS_RECORD_TYPE,
|
|
|
|
resolverError,
|
|
|
|
} from "@lumeweb/resolver-common";
|
2022-08-19 13:57:05 +00:00
|
|
|
import { getResolvers, resolve } from "@lumeweb/kernel-dns-client";
|
2022-08-18 22:50:35 +00:00
|
|
|
import { RpcNetwork } from "@lumeweb/kernel-rpc-client";
|
2022-08-19 13:57:05 +00:00
|
|
|
import { callModule } from "libkmodule";
|
2022-08-18 22:28:36 +00:00
|
|
|
|
|
|
|
export class ResolverRegistry {
|
2022-08-18 22:50:35 +00:00
|
|
|
private _rpcNetwork: RpcNetwork;
|
|
|
|
|
|
|
|
constructor(network: RpcNetwork) {
|
|
|
|
this._rpcNetwork = network;
|
|
|
|
}
|
|
|
|
|
|
|
|
get rpcNetwork(): RpcNetwork {
|
|
|
|
return this._rpcNetwork;
|
|
|
|
}
|
|
|
|
|
2022-08-19 13:57:05 +00:00
|
|
|
get resolvers(): Promise<Set<ResolverModule>> {
|
|
|
|
return getResolvers()
|
|
|
|
.catch(() => {
|
|
|
|
return new Set();
|
|
|
|
})
|
|
|
|
.then((resolvers: string[]) => {
|
|
|
|
return new Set(
|
|
|
|
resolvers.map((resolver) => new ResolverModule(this, resolver))
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-08-18 22:28:36 +00:00
|
|
|
public async resolve(
|
|
|
|
domain: string,
|
|
|
|
options: ResolverOptions = { type: DNS_RECORD_TYPE.DEFAULT },
|
|
|
|
bypassCache: boolean = false
|
|
|
|
): Promise<DNSResult> {
|
|
|
|
let result: DNSResult;
|
|
|
|
|
|
|
|
try {
|
|
|
|
result = await resolve(domain, options, bypassCache);
|
|
|
|
} catch (e: any) {
|
|
|
|
return resolverError(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
2022-08-18 22:50:35 +00:00
|
|
|
|
2022-08-19 13:57:05 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-08-19 15:20:33 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
async getSupportedTlds(): Promise<string[]> {
|
|
|
|
const [ret, err] = await callModule(this.domain, "getSupportedTlds");
|
|
|
|
if (err) {
|
|
|
|
throw new Error(err);
|
|
|
|
}
|
|
|
|
|
2022-08-19 13:57:05 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-18 22:50:35 +00:00
|
|
|
export { RpcNetwork };
|