Skip to content

Commit

Permalink
chore: refactored get_ips_by_host using Promise.allSettled
Browse files Browse the repository at this point in the history
  • Loading branch information
msimerson committed Apr 18, 2024
1 parent 92b3217 commit 1b5a7e2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 41 deletions.
14 changes: 2 additions & 12 deletions .eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 } ]
extends: ['@haraka']
8 changes: 5 additions & 3 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 17 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion test/net_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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());
Expand Down

0 comments on commit 1b5a7e2

Please sign in to comment.