DEV Community

moose
moose

Posted on

Deno DNS Resolver

I make a DNS resolver in Deno using type script with Deno's built in Deno.resolveDns functionality. The following snippit grabs an array of record types and builds a return object with them. Deno is still new so there is some "novel" usage here, but I believe that more functionality will come for sure.

create dns_resolver.ts file

Copypaste the following:


let records = ["MX", "AAAA", "A", "SRV","ANAME"]
export async function dns_call(domain:string, records_types:Array<string>):Promise<Object>{
    let promise:Promise<any> = new Promise(function(resolve,reject) {

        let ret_object = {}
        for (let i = 0; i < records_types.length;i++){
            // @ts-ignore
            Deno.resolveDns(domain, records_types[i]).then(function (dns_resolution) {
                // @ts-ignore
                ret_object[records_types[i]] = dns_resolution
                if(i === records_types.length -1){
                    resolve(ret_object)
                }
            }).catch(function () {
                // @ts-ignore
                ret_object[records_types[i]] = "not present"
                if(i === records_types.length -1){
                    resolve(ret_object)
                }
            })
        }
    })
    return promise
}
let smoke = await dns_call("example.com", records)
console.log(smoke)
Enter fullscreen mode Exit fullscreen mode

run

$ > deno run --allow-net --unstable dns_resolver.ts
Enter fullscreen mode Exit fullscreen mode

Play around with it.

Top comments (0)