From 67b4edde37d84203f6a1e6e4531fe7c9ae294134 Mon Sep 17 00:00:00 2001 From: "jiahao.si" Date: Sun, 24 Jul 2022 18:18:20 +0800 Subject: [PATCH] dns: fix getServers return undefined PR-URL: https://github.com/nodejs/node/pull/43922 Reviewed-By: Paolo Insogna Reviewed-By: Matteo Collina --- lib/internal/dns/utils.js | 4 ++-- test/parallel/test-dns-get-server.js | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-dns-get-server.js diff --git a/lib/internal/dns/utils.js b/lib/internal/dns/utils.js index 35ac2b32570922..adc50def9d6058 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 00000000000000..24c01218fad519 --- /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);