Skip to content

Commit

Permalink
lib: fix resolve4 usage on node < v6.11.0
Browse files Browse the repository at this point in the history
  • Loading branch information
indutny committed Jun 8, 2017
1 parent ee41c8c commit 4fee7b4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
sudo: false
language: node_js
node_js:
- "4"
- "6"
- "latest"
17 changes: 15 additions & 2 deletions lib/redns.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ const dns = require('dns');
const async = require('async');
const debug = require('debug')('redns');

// See: https://github.com/nodejs/node/pull/9296
const isLegacy = require('compare-versions')(process.version, 'v6.11.0') < 0;

// For later monkey-patching in user code
const dnsLookup = dns.lookup;

Expand Down Expand Up @@ -169,7 +172,7 @@ ReDNS.prototype._resolve = function _resolve(hostname, family, callback) {
callback(new Error('DNS query timed out'));
}, timeout);

resolver(hostname, options, (err, results) => {
const onResults = (err, results) => {
// Timed out
if (done)
return;
Expand All @@ -183,9 +186,19 @@ ReDNS.prototype._resolve = function _resolve(hostname, family, callback) {
if (typeof result === 'string')
return { address: result, ttl: Infinity, family };

// Node.js before v6.11.0 has no way to get record's TTL
// fallback to `maxTTL` option
if (isLegacy)
return { address: result.address, ttl: Infinity, family };

return { address: result.address, ttl: result.ttl, family };
}));
});
};

if (isLegacy)
resolver(hostname, onResults);
else
resolver(hostname, options, onResults);
}, callback);
};

Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
},
"dependencies": {
"async": "^2.4.1",
"compare-versions": "^3.0.1",
"debug": "^2.6.8"
}
}

0 comments on commit 4fee7b4

Please sign in to comment.