Skip to content

Commit

Permalink
dns: minor refactor of dns module
Browse files Browse the repository at this point in the history
Move to the more efficient module.exports = {} pattern.

PR-URL: #11597
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Claudio Rodriguez <cjrodr@yahoo.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
  • Loading branch information
jasnell committed Mar 4, 2017
1 parent 1402fef commit 531de63
Showing 1 changed file with 74 additions and 56 deletions.
130 changes: 74 additions & 56 deletions lib/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function onlookupall(err, addresses) {

// Easy DNS A/AAAA look up
// lookup(hostname, [options,] callback)
exports.lookup = function lookup(hostname, options, callback) {
function lookup(hostname, options, callback) {
var hints = 0;
var family = -1;
var all = false;
Expand All @@ -119,9 +119,9 @@ exports.lookup = function lookup(hostname, options, callback) {
all = options.all === true;

if (hints !== 0 &&
hints !== exports.ADDRCONFIG &&
hints !== exports.V4MAPPED &&
hints !== (exports.ADDRCONFIG | exports.V4MAPPED)) {
hints !== cares.AI_ADDRCONFIG &&
hints !== cares.AI_V4MAPPED &&
hints !== (cares.AI_ADDRCONFIG | cares.AI_V4MAPPED)) {
throw new TypeError('Invalid argument: hints must use valid flags');
}
} else {
Expand Down Expand Up @@ -166,7 +166,7 @@ exports.lookup = function lookup(hostname, options, callback) {

callback.immediately = true;
return req;
};
}


function onlookupservice(err, host, service) {
Expand All @@ -178,7 +178,7 @@ function onlookupservice(err, host, service) {


// lookupService(address, port, callback)
exports.lookupService = function lookupService(host, port, callback) {
function lookupService(host, port, callback) {
if (arguments.length !== 3)
throw new Error('Invalid arguments');

Expand All @@ -205,7 +205,7 @@ exports.lookupService = function lookupService(host, port, callback) {

callback.immediately = true;
return req;
};
}


function onresolve(err, result, ttls) {
Expand Down Expand Up @@ -251,26 +251,25 @@ function resolver(bindingName) {


var resolveMap = Object.create(null);
exports.resolve4 = resolveMap.A = resolver('queryA');
exports.resolve6 = resolveMap.AAAA = resolver('queryAaaa');
exports.resolveCname = resolveMap.CNAME = resolver('queryCname');
exports.resolveMx = resolveMap.MX = resolver('queryMx');
exports.resolveNs = resolveMap.NS = resolver('queryNs');
exports.resolveTxt = resolveMap.TXT = resolver('queryTxt');
exports.resolveSrv = resolveMap.SRV = resolver('querySrv');
exports.resolvePtr = resolveMap.PTR = resolver('queryPtr');
exports.resolveNaptr = resolveMap.NAPTR = resolver('queryNaptr');
exports.resolveSoa = resolveMap.SOA = resolver('querySoa');
exports.reverse = resolver('getHostByAddr');


exports.resolve = function resolve(hostname, type_, callback_) {
resolveMap.A = resolver('queryA');
resolveMap.AAAA = resolver('queryAaaa');
resolveMap.CNAME = resolver('queryCname');
resolveMap.MX = resolver('queryMx');
resolveMap.NS = resolver('queryNs');
resolveMap.TXT = resolver('queryTxt');
resolveMap.SRV = resolver('querySrv');
resolveMap.PTR = resolver('queryPtr');
resolveMap.NAPTR = resolver('queryNaptr');
resolveMap.SOA = resolver('querySoa');


function resolve(hostname, type_, callback_) {
var resolver, callback;
if (typeof type_ === 'string') {
resolver = resolveMap[type_];
callback = callback_;
} else if (typeof type_ === 'function') {
resolver = exports.resolve4;
resolver = resolveMap.A;
callback = type_;
} else {
throw new Error('"type" argument must be a string');
Expand All @@ -281,15 +280,15 @@ exports.resolve = function resolve(hostname, type_, callback_) {
} else {
throw new Error(`Unknown type "${type_}"`);
}
};
}


exports.getServers = function getServers() {
function getServers() {
return cares.getServers();
};
}


exports.setServers = function setServers(servers) {
function setServers(servers) {
// cache the original servers because in the event of an error setting the
// servers cares won't have any servers available for resolution
const orig = cares.getServers();
Expand Down Expand Up @@ -326,34 +325,53 @@ exports.setServers = function setServers(servers) {
var err = cares.strerror(errorNumber);
throw new Error(`c-ares failed to set servers: "${err}" [${servers}]`);
}
};
}

// uv_getaddrinfo flags
exports.ADDRCONFIG = cares.AI_ADDRCONFIG;
exports.V4MAPPED = cares.AI_V4MAPPED;

// ERROR CODES
exports.NODATA = 'ENODATA';
exports.FORMERR = 'EFORMERR';
exports.SERVFAIL = 'ESERVFAIL';
exports.NOTFOUND = 'ENOTFOUND';
exports.NOTIMP = 'ENOTIMP';
exports.REFUSED = 'EREFUSED';
exports.BADQUERY = 'EBADQUERY';
exports.BADNAME = 'EBADNAME';
exports.BADFAMILY = 'EBADFAMILY';
exports.BADRESP = 'EBADRESP';
exports.CONNREFUSED = 'ECONNREFUSED';
exports.TIMEOUT = 'ETIMEOUT';
exports.EOF = 'EOF';
exports.FILE = 'EFILE';
exports.NOMEM = 'ENOMEM';
exports.DESTRUCTION = 'EDESTRUCTION';
exports.BADSTR = 'EBADSTR';
exports.BADFLAGS = 'EBADFLAGS';
exports.NONAME = 'ENONAME';
exports.BADHINTS = 'EBADHINTS';
exports.NOTINITIALIZED = 'ENOTINITIALIZED';
exports.LOADIPHLPAPI = 'ELOADIPHLPAPI';
exports.ADDRGETNETWORKPARAMS = 'EADDRGETNETWORKPARAMS';
exports.CANCELLED = 'ECANCELLED';
module.exports = {
lookup,
lookupService,
getServers,
setServers,
resolve,
resolve4: resolveMap.A,
resolve6: resolveMap.AAAA,
resolveCname: resolveMap.CNAME,
resolveMx: resolveMap.MX,
resolveNs: resolveMap.NS,
resolveTxt: resolveMap.TXT,
resolveSrv: resolveMap.SRV,
resolvePtr: resolveMap.PTR,
resolveNaptr: resolveMap.NAPTR,
resolveSoa: resolveMap.SOA,
reverse: resolver('getHostByAddr'),

// uv_getaddrinfo flags
ADDRCONFIG: cares.AI_ADDRCONFIG,
V4MAPPED: cares.AI_V4MAPPED,

// ERROR CODES
NODATA: 'ENODATA',
FORMERR: 'EFORMERR',
SERVFAIL: 'ESERVFAIL',
NOTFOUND: 'ENOTFOUND',
NOTIMP: 'ENOTIMP',
REFUSED: 'EREFUSED',
BADQUERY: 'EBADQUERY',
BADNAME: 'EBADNAME',
BADFAMILY: 'EBADFAMILY',
BADRESP: 'EBADRESP',
CONNREFUSED: 'ECONNREFUSED',
TIMEOUT: 'ETIMEOUT',
EOF: 'EOF',
FILE: 'EFILE',
NOMEM: 'ENOMEM',
DESTRUCTION: 'EDESTRUCTION',
BADSTR: 'EBADSTR',
BADFLAGS: 'EBADFLAGS',
NONAME: 'ENONAME',
BADHINTS: 'EBADHINTS',
NOTINITIALIZED: 'ENOTINITIALIZED',
LOADIPHLPAPI: 'ELOADIPHLPAPI',
ADDRGETNETWORKPARAMS: 'EADDRGETNETWORKPARAMS',
CANCELLED: 'ECANCELLED'
};

0 comments on commit 531de63

Please sign in to comment.