From 4fee7b41288df4be6f532181a0814e373260543b Mon Sep 17 00:00:00 2001 From: Fedor Indutny Date: Thu, 8 Jun 2017 16:49:17 -0400 Subject: [PATCH] lib: fix `resolve4` usage on node < v6.11.0 --- .travis.yml | 6 ++++++ lib/redns.js | 17 +++++++++++++++-- package-lock.json | 5 +++++ package.json | 1 + 4 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..135fa30 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,6 @@ +sudo: false +language: node_js +node_js: + - "4" + - "6" + - "latest" diff --git a/lib/redns.js b/lib/redns.js index f24b227..b33d5e5 100644 --- a/lib/redns.js +++ b/lib/redns.js @@ -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; @@ -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; @@ -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); }; diff --git a/package-lock.json b/package-lock.json index dfa3f23..1a6f783 100644 --- a/package-lock.json +++ b/package-lock.json @@ -168,6 +168,11 @@ "integrity": "sha1-nJkJQXbhIkDLItbFFGCYQA/g99Q=", "dev": true }, + "compare-versions": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/compare-versions/-/compare-versions-3.0.1.tgz", + "integrity": "sha1-5gkVt/spTim7tghpGIdivuuHFEM=" + }, "concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", diff --git a/package.json b/package.json index 287a158..254be67 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ }, "dependencies": { "async": "^2.4.1", + "compare-versions": "^3.0.1", "debug": "^2.6.8" } }