DNS resolver — resolve domain names with custom logic.
1const dns = require("dns").promises;23class CustomResolver {4 constructor() {5 this.cache = new Map();6 this.cacheTTL = 300000; // 5 min7 }89 async resolve(hostname) {10 const cached = this.cache.get(hostname);11 if (cached && Date.now() - cached.timestamp < this.cacheTTL) {12 return cached.addresses;13 }1415 const addresses = await dns.resolve4(hostname);16 this.cache.set(hostname, {17 addresses,18 timestamp: Date.now(),19 });2021 return addresses;22 }2324 clearCache() {25 this.cache.clear();26 }27}2829// Usage30const resolver = new CustomResolver();31const addresses = await resolver.resolve("example.com");32console.log(addresses);
DNS over HTTPS:
1async function resolveDoH(hostname) {2 const url = `https://dns.google/resolve?name=${hostname}&type=A`;3 const response = await fetch(url);4 const data = await response.json();5 return data.Answer?.map(a => a.data) || [];6}