diff --git a/lib/internal/dns/utils.js b/lib/internal/dns/utils.js index 26949a028d8..511be2bb457 100644 --- a/lib/internal/dns/utils.js +++ b/lib/internal/dns/utils.js @@ -61,7 +61,7 @@ class Resolver { } getServers() { - return ArrayPrototypeMap(this._handle.getServers(), (val) => { + return ArrayPrototypeMap(this._handle.getServers() || [], (val) => { if (!val[1] || val[1] === IANA_DNS_PORT) return val[0]; @@ -76,7 +76,7 @@ class Resolver { // Cache the original servers because in the event of an error while // setting the servers, c-ares won't have any servers available for // resolution. - const orig = this._handle.getServers(); + const orig = this._handle.getServers() || []; const newSet = []; ArrayPrototypeForEach(servers, (serv, index) => { diff --git a/test/parallel/test-dns-get-server.js b/test/parallel/test-dns-get-server.js new file mode 100644 index 00000000000..24c01218fad --- /dev/null +++ b/test/parallel/test-dns-get-server.js @@ -0,0 +1,11 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); + +const { Resolver } = require('dns'); + +const resolver = new Resolver(); +assert(resolver.getServers().length > 0); +// return undefined +resolver._handle.getServers = common.mustCall(() => {}); +assert.strictEqual(resolver.getServers().length, 0);