Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dns: Refactor forEach to map #5803

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 16 additions & 20 deletions lib/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,41 +285,37 @@ exports.getServers = function() {
exports.setServers = function(servers) {
// cache the original servers because in the event of an error setting the
// servers cares won't have any servers available for resolution
var orig = cares.getServers();
const orig = cares.getServers();

var newSet = [];

servers.forEach(function(serv) {
var ver = isIP(serv);

if (ver)
return newSet.push([ver, serv]);

var match = serv.match(/\[(.*)\](:\d+)?/);
const newSet = servers.map((serv) => {
var ipVersion = isIP(serv);
if (ipVersion !== 0)
return [ipVersion, serv];

const match = serv.match(/\[(.*)\](:\d+)?/);
// we have an IPv6 in brackets
if (match) {
ver = isIP(match[1]);
if (ver)
return newSet.push([ver, match[1]]);
ipVersion = isIP(match[1]);
if (ipVersion !== 0)
return [ipVersion, match[1]];
}

var s = serv.split(/:\d+$/)[0];
ver = isIP(s);
const s = serv.split(/:\d+$/)[0];
ipVersion = isIP(s);

if (ver)
return newSet.push([ver, s]);
if (ipVersion !== 0)
return [ipVersion, s];

throw new Error(`IP address is not properly formatted: ${serv}`);
});

var r = cares.setServers(newSet);
const errorNumber = cares.setServers(newSet);

if (r) {
if (errorNumber !== 0) {
// reset the servers to the old servers, because ares probably unset them
cares.setServers(orig.join(','));

var err = cares.strerror(r);
var err = cares.strerror(errorNumber);
throw new Error(`c-ares failed to set servers: "${err}" [${servers}]`);
}
};
Expand Down