Promise-based wrappers for Node.js core APIs that modernize callback-based methods to work with async/await patterns
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Perform DNS lookups and resolve various record types with promise support. Provides modern async/await compatibility for Node.js dns operations.
Resolve hostnames to IP addresses and vice versa.
/**
* Resolve hostname to IP address
* @param hostname - Hostname to resolve
* @param options - Lookup options or address family
* @returns Promise resolving to lookup result
*/
function lookup(hostname, options): Promise<string | {address: string, family: number}>;Resolve various types of DNS records.
/**
* Resolve hostname using specified record type
* @param hostname - Hostname to resolve
* @param rrtype - Record type (A, AAAA, CNAME, MX, NS, SRV, TXT, PTR)
* @returns Promise resolving to array of records
*/
function resolve(hostname, rrtype): Promise<string[] | object[]>;Resolve specific types of DNS records with optimized interfaces.
/**
* Resolve IPv4 addresses (A records)
* @param hostname - Hostname to resolve
* @param options - Resolution options
* @returns Promise resolving to array of IPv4 addresses
*/
function resolve4(hostname, options): Promise<string[]>;
/**
* Resolve IPv6 addresses (AAAA records)
* @param hostname - Hostname to resolve
* @param options - Resolution options
* @returns Promise resolving to array of IPv6 addresses
*/
function resolve6(hostname, options): Promise<string[]>;
/**
* Resolve canonical name records (CNAME)
* @param hostname - Hostname to resolve
* @returns Promise resolving to array of canonical names
*/
function resolveCname(hostname): Promise<string[]>;
/**
* Resolve mail exchange records (MX)
* @param hostname - Hostname to resolve
* @returns Promise resolving to array of MX record objects
*/
function resolveMx(hostname): Promise<{priority: number, exchange: string}[]>;
/**
* Resolve name server records (NS)
* @param hostname - Hostname to resolve
* @returns Promise resolving to array of name server hostnames
*/
function resolveNs(hostname): Promise<string[]>;
/**
* Resolve service records (SRV)
* @param hostname - Hostname to resolve
* @returns Promise resolving to array of SRV record objects
*/
function resolveSrv(hostname): Promise<{priority: number, weight: number, port: number, name: string}[]>;
/**
* Resolve text records (TXT)
* @param hostname - Hostname to resolve
* @returns Promise resolving to array of text record arrays
*/
function resolveTxt(hostname): Promise<string[][]>;
/**
* Reverse DNS lookup (PTR records)
* @param ip - IP address to reverse lookup
* @returns Promise resolving to array of hostnames
*/
function reverse(ip): Promise<string[]>;Usage Examples:
const dns = require('mz/dns');
// Basic hostname lookup
async function lookupHost() {
try {
// Simple lookup (returns first IP)
const address = await dns.lookup('google.com');
console.log('Google IP:', address);
// Lookup with options
const result = await dns.lookup('google.com', { family: 4, all: false });
console.log('IPv4 address:', result);
// Get all addresses
const all = await dns.lookup('google.com', { all: true });
console.log('All addresses:', all);
} catch (error) {
console.error('Lookup failed:', error);
}
}
// Resolve specific record types
async function resolveRecords() {
try {
// IPv4 addresses
const ipv4 = await dns.resolve4('google.com');
console.log('IPv4 addresses:', ipv4);
// IPv6 addresses
const ipv6 = await dns.resolve6('google.com');
console.log('IPv6 addresses:', ipv6);
// MX records
const mx = await dns.resolveMx('google.com');
console.log('Mail servers:', mx);
// Output: [{priority: 10, exchange: 'smtp.google.com'}, ...]
// NS records
const ns = await dns.resolveNs('google.com');
console.log('Name servers:', ns);
// TXT records
const txt = await dns.resolveTxt('google.com');
console.log('TXT records:', txt);
} catch (error) {
console.error('Resolution failed:', error);
}
}
// Reverse DNS lookup
async function reverseLoookup() {
try {
const hostnames = await dns.reverse('8.8.8.8');
console.log('8.8.8.8 resolves to:', hostnames);
} catch (error) {
console.error('Reverse lookup failed:', error);
}
}
// Generic resolve with different record types
async function genericResolve() {
try {
// A records
const a = await dns.resolve('example.com', 'A');
console.log('A records:', a);
// CNAME records
const cname = await dns.resolve('www.example.com', 'CNAME');
console.log('CNAME records:', cname);
// SRV records
const srv = await dns.resolve('_http._tcp.example.com', 'SRV');
console.log('SRV records:', srv);
} catch (error) {
console.error('Generic resolve failed:', error);
}
}
// Callback support is still available
dns.lookup('example.com', (err, address, family) => {
if (err) {
console.error('Error:', err);
} else {
console.log('Address:', address, 'Family:', family);
}
});
// Practical example: Check if domain has mail servers
async function hasMailServers(domain) {
try {
const mx = await dns.resolveMx(domain);
return mx.length > 0;
} catch (error) {
// DNS resolution failed, assume no mail servers
return false;
}
}
// Practical example: Get fastest responding IP
async function getFastestIP(hostname) {
try {
const addresses = await dns.resolve4(hostname);
// In practice, you might ping each to find the fastest
return addresses[0]; // Return first for simplicity
} catch (error) {
throw new Error(`Failed to resolve ${hostname}: ${error.message}`);
}
}The lookup() function accepts an options object:
interface LookupOptions {
/** Address family (4 for IPv4, 6 for IPv6, 0 for both) */
family?: number;
/** Return all addresses instead of just the first */
all?: boolean;
/** Custom hints for getaddrinfo */
hints?: number;
/** Whether to use numeric host */
verbatim?: boolean;
}DNS functions will reject with errors for various conditions:
ENOTFOUND: Domain not foundENODATA: No data for the requested record typeESERVFAIL: DNS server failureETIMEOUT: DNS query timeoutECONNREFUSED: Connection to DNS server refusedconst dns = require('mz/dns');
async function handleDNSErrors() {
try {
await dns.lookup('nonexistent-domain-12345.com');
} catch (error) {
if (error.code === 'ENOTFOUND') {
console.log('Domain not found');
} else if (error.code === 'ETIMEOUT') {
console.log('DNS query timed out');
} else {
console.log('DNS error:', error.code, error.message);
}
}
}Different DNS record types return different data structures:
{priority: number, exchange: string} objects{priority: number, weight: number, port: number, name: string} objectsthenify-all to wrap native dns methods