From c3eb2953b2c875e0c030a3f6ba6e39ce31376ee0 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Mon, 27 Feb 2017 17:54:16 -0800 Subject: [PATCH] dns: minor refactor of dns module Move to the more efficient module.exports = {} pattern. --- lib/dns.js | 130 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 74 insertions(+), 56 deletions(-) diff --git a/lib/dns.js b/lib/dns.js index cfa04ed192050f..1f22c91c78c27a 100644 --- a/lib/dns.js +++ b/lib/dns.js @@ -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; @@ -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 { @@ -166,7 +166,7 @@ exports.lookup = function lookup(hostname, options, callback) { callback.immediately = true; return req; -}; +} function onlookupservice(err, host, service) { @@ -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'); @@ -205,7 +205,7 @@ exports.lookupService = function lookupService(host, port, callback) { callback.immediately = true; return req; -}; +} function onresolve(err, result, ttls) { @@ -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'); @@ -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(); @@ -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' +};