diff --git a/lib/dns-store.js b/lib/dns-store.js index 143ef4d..547385c 100644 --- a/lib/dns-store.js +++ b/lib/dns-store.js @@ -422,14 +422,37 @@ module.exports.query = function (input, query, cb) { }); myRecords = someRecords; - if (255 !== query.question[0].type && 'ANY' !== query.question[0].typeName) { - myRecords = myRecords.filter(function (r) { - return ((r.type && r.type === query.question[0].type) + // If we had an ANY query then we don't need to filter out results + if (255 !== query.question[0].type && 'ANY' !== query.question[0].typeName) { + var hasA = false; + var hasCname = false; + + // We should only return the records that match the query, + // except in the case of A/AAAA in which case we should also collect the CNAME + myRecords = myRecords.filter(function (r) { + var passCnames = false; + + if (!hasA && ('A' === query.question[0].typeName || 'AAAA' === query.question[0].typeName)) { + passCnames = ('CNAME' === r.type ||'CNAME' === r.typeName); + hasCname = hasCname || passCnames; + } + + hasA = hasA || ('A' === r.type || 'A' === r.typeName || 'AAAA' === r.type || 'AAAA' === r.typeName); + + return passCnames || ((r.type && r.type === query.question[0].type) || (r.type && r.type === query.question[0].typeName) || (r.typeName && r.typeName === query.question[0].typeName) ); }); + + // A and AAAA will also return CNAME records + // but we filter out the CNAME records unless there are no A / AAAA records + if (hasA && hasCname && ('A' === query.question[0].typeName || 'AAAA' === query.question[0].typeName)) { + myRecords = myRecords.forEach(function (r) { + return 'CNAME' !== r.type && 'CNAME' !== r.typeName; + }); + } } if (myRecords.length) {