From 1b5a7e2bafd24bee14fb50939daa5f66085ded5b Mon Sep 17 00:00:00 2001 From: Matt Simerson Date: Wed, 17 Apr 2024 18:07:33 -0700 Subject: [PATCH] chore: refactored get_ips_by_host using Promise.allSettled --- .eslintrc.yaml | 14 ++------------ Changes.md | 8 +++++--- index.js | 42 +++++++++++++++++------------------------- test/net_utils.js | 4 +++- 4 files changed, 27 insertions(+), 41 deletions(-) diff --git a/.eslintrc.yaml b/.eslintrc.yaml index 6313e7c..035a400 100644 --- a/.eslintrc.yaml +++ b/.eslintrc.yaml @@ -2,16 +2,6 @@ env: node: true es6: true mocha: true - es2020: true + es2022: true -plugins: - - haraka - -extends: - - eslint:recommended - - plugin:haraka/recommended - -root: true - -rules: - indent: [2, 2, { SwitchCase: 1 } ] \ No newline at end of file +extends: ['@haraka'] diff --git a/Changes.md b/Changes.md index 288419c..23c17a6 100644 --- a/Changes.md +++ b/Changes.md @@ -9,19 +9,21 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/). #### Added -- add timeout to DNS Resolver #83 +- feat: add timeout to DNS Resolver #83 - feat: normalizeDomain, for punycode/IDN names - feat: get_mx now *also* returns implicit MX records - feat: added get_implicit_mx - feat: added resolve_mx_hosts - doc(Changes): fixed broken tag version links -- chore: populate [files] in package.json. Delete .npmignore. +- chore: populate [files] in package.json. Delete .npmignore +- chore(lint): remove duplicate / stale rules from .eslintrc +- dep(punycode): punycode -> punycode.js (avoid name collission) +- chore: refactored get_ips_by_host using Promise.allSettled ### [1.5.3] - 2023-12-15 - dep(punycode): override built-in with trailing / - ### [1.5.2] - 2023-12-11 - dep(stun): use updated @msimerson/stun diff --git a/index.js b/index.js index 35fa386..2e60ad9 100644 --- a/index.js +++ b/index.js @@ -367,31 +367,23 @@ exports.get_ipany_re = function (prefix, suffix, modifier) { exports.get_ips_by_host = function (hostname, done) { const ips = new Set() const errors = [] - const promises = [] - - async function resolveAny (ver) { - try { - const addrs = await dns[`resolve${ver}`](hostname) - for (const a of addrs) { - ips.add(a); - } - return addrs - } - catch (err) { - errors.push(err); - } - } - - promises.push(resolveAny('4')) - promises.push(resolveAny('6')) - if (done) { // legacy callback API - Promise.all(promises).then((r) => { done(errors, Array.from(ips)) }) - } - else { // promise API - // if (process.env.DEBUG && errors.length) console.error(errors) - return Promise.all(promises).then(r => { return Array.from(ips) }) - } + return Promise.allSettled([ + dns.resolve6(hostname), + dns.resolve4(hostname), + ]) + .then((res) => { + res + .filter(a => a.status === 'rejected') + .map(a => errors.push(a.reason)) + + res + .filter(a => a.status === 'fulfilled') + .map(a => a.value.map(ip => ips.add(ip))) + + if (done) done(errors, Array.from(ips)) + return Array.from(ips) + }) } exports.ipv6_reverse = function (ipv6) { @@ -506,7 +498,7 @@ function fatal_mx_err (err) { } exports.get_mx = async (raw_domain, cb) => { - let domain = normalizeDomain(raw_domain); + const domain = normalizeDomain(raw_domain); try { const exchanges = await dns.resolveMx(domain) diff --git a/test/net_utils.js b/test/net_utils.js index 0d0bacb..93a36bf 100644 --- a/test/net_utils.js +++ b/test/net_utils.js @@ -1060,7 +1060,8 @@ describe('get_ips_by_host', function () { '192.48.85.149', '2607:f060:b008:feed::2' ], - 'localhost.haraka.tnpi.net': [ '127.0.0.1', '::1' ] + 'localhost.haraka.tnpi.net': [ '127.0.0.1', '::1' ], + // 'non-exist.haraka.tnpi.net': [], } for (const t in tests) { @@ -1070,6 +1071,7 @@ describe('get_ips_by_host', function () { net_utils.get_ips_by_host(t, function (err, res) { if (err && err.length) { console.error(err); + return done() } assert.deepEqual(err, []); assert.deepEqual(res.sort(), tests[t].sort());