@waku/discovery
Version:
Contains various discovery mechanisms: DNS Discovery (EIP-1459, Peer Exchange, Local Peer Cache Discovery.
58 lines • 1.63 kB
JavaScript
import { Logger } from "@waku/utils";
import { bytesToUtf8 } from "@waku/utils/bytes";
import DnsOverHttpResolver from "dns-over-http-resolver";
const log = new Logger("dns-over-https");
export class DnsOverHttps {
resolver;
/**
* Create new Dns-Over-Http DNS client.
*
* @throws {code: string} If DNS query fails.
*/
static async create() {
return new DnsOverHttps();
}
constructor(resolver = new DnsOverHttpResolver()) {
this.resolver = resolver;
}
/**
* Resolves a TXT record
*
* @param domain The domain name
*
* @throws if the query fails
*/
async resolveTXT(domain) {
let answers;
try {
answers = await this.resolver.resolveTxt(domain);
}
catch (error) {
log.error("query failed: ", error);
throw new Error("DNS query failed");
}
if (!answers)
throw new Error(`Could not resolve ${domain}`);
const result = [];
answers.forEach((d) => {
if (typeof d === "string") {
result.push(d);
}
else if (Array.isArray(d)) {
d.forEach((sd) => {
if (typeof sd === "string") {
result.push(sd);
}
else {
result.push(bytesToUtf8(sd));
}
});
}
else {
result.push(bytesToUtf8(d));
}
});
return result;
}
}
//# sourceMappingURL=dns_over_https.js.map