haraka-net-utils
Advanced tools
Comparing version 1.3.7 to 1.4.0
@@ -5,2 +5,10 @@ | ||
### [1.4.0] - 2022-07-22 | ||
- feat(get_mx): use async/await | ||
- feat(get_mx): call w/o callback for promise API | ||
- test(get_mx): expand and improve test coverage | ||
- chore(ci): use more shared haraka/.github workflows | ||
### [1.3.7] - 2022-06-03 | ||
@@ -14,4 +22,4 @@ | ||
- chore: replace .release with submodule | ||
- ci: populate test matrix with Node.js LTS versions | ||
- ci: limit dependabot updates to production deps | ||
- chore(ci): populate test matrix with Node.js LTS versions | ||
- chore(ci): limit dependabot updates to production deps | ||
@@ -175,1 +183,2 @@ | ||
[1.3.7]: https://github.com/haraka/haraka-net-utils/releases/tag/1.3.7 | ||
[1.4.0]: https://github.com/haraka/haraka-net-utils/releases/tag/1.4.0 |
28
index.js
@@ -347,2 +347,3 @@ 'use strict'; | ||
// promise API | ||
// if (process.env.DEBUG && errors.length) console.error(errors) | ||
return Promise.all(promises).then(r => { return Array.from(ips) }) | ||
@@ -418,3 +419,3 @@ } | ||
exports.get_mx = function get_mx (raw_domain, cb) { | ||
exports.get_mx = async function get_mx (raw_domain, cb) { | ||
let domain = raw_domain; | ||
@@ -452,16 +453,19 @@ const mxs = []; | ||
const wrap_mx = a => a; | ||
let err = null | ||
dns.resolveMx(domain) | ||
.then(addresses => { | ||
if (addresses && addresses.length) { | ||
for (const addr of addresses) { | ||
mxs.push(wrap_mx(addr)); | ||
} | ||
try { | ||
const addresses = await dns.resolveMx(domain) | ||
if (addresses?.length) { | ||
for (const addr of addresses) { | ||
mxs.push(wrap_mx(addr)); | ||
} | ||
} | ||
} | ||
catch (e) { | ||
err = e | ||
} | ||
cb(null, mxs); | ||
}) | ||
.catch(err => { | ||
cb(err) | ||
}) | ||
if (cb) return cb(err, mxs) | ||
if (err) throw err | ||
return mxs | ||
} |
{ | ||
"name": "haraka-net-utils", | ||
"version": "1.3.7", | ||
"version": "1.4.0", | ||
"description": "haraka network utilities", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -1144,34 +1144,73 @@ | ||
it('gets MX records for a domain', function (done) { | ||
this.timeout(3000) | ||
this.net_utils.get_mx('tnpi.net', (err, mxlist) => { | ||
assert.ifError(err); | ||
assert.ok(mxlist.length); | ||
assert.equal(mxlist[0].exchange, 'mail.theartfarm.com'); | ||
done(); | ||
const validCases = { | ||
'tnpi.net' : 'mail.theartfarm.com', | ||
'matt@tnpi.net': 'mail.theartfarm.com', | ||
'matt.simerson@gmail.com': /google.com/, | ||
'example.com' : '', | ||
} | ||
function checkValid (c, mxlist) { | ||
if ('string' === typeof c) { | ||
assert.equal(mxlist[0].exchange, c); | ||
} | ||
else { | ||
assert.ok(c.test(mxlist[0].exchange)) | ||
} | ||
} | ||
for (const c in validCases) { | ||
it(`gets MX records for ${c}`, function (done) { | ||
this.timeout(3000) | ||
this.net_utils.get_mx(c, (err, mxlist) => { | ||
if (err) console.error(err) | ||
assert.ifError(err); | ||
// assert.ok(mxlist.length); | ||
checkValid(validCases[c], mxlist) | ||
done() | ||
}) | ||
}) | ||
}) | ||
it('gets MX records for an email address', function (done) { | ||
this.net_utils.get_mx('matt@tnpi.net', (err, mxlist) => { | ||
assert.ifError(err); | ||
assert.ok(mxlist.length); | ||
done(); | ||
it(`awaits MX records for ${c}`, async function () { | ||
this.timeout(3000) | ||
const mxlist = await this.net_utils.get_mx(c) | ||
// assert.ok(mxlist.length); | ||
checkValid(validCases[c], mxlist) | ||
}) | ||
}) | ||
} | ||
it('gets MX records for example.com', function (done) { | ||
this.net_utils.get_mx('example.com', (err, mxlist) => { | ||
assert.equal(mxlist.length, 1); | ||
assert.equal(mxlist[0].exchange, ''); | ||
done(); | ||
// macOS: ENODATA, win: ENOTOUND, ubuntu: ESERVFAIL | ||
const invalidCases = { | ||
'invalid': /queryMx (ENODATA|ENOTFOUND|ESERVFAIL) invalid/, | ||
'gmail.xn--com-0da': 'Cannot convert name to ASCII', | ||
} | ||
function checkInvalid (expected, actual) { | ||
if ('string' === typeof expected) { | ||
assert.strictEqual(actual, expected) | ||
} | ||
else { | ||
assert.equal(expected.test(actual), true) | ||
} | ||
} | ||
for (const c in invalidCases) { | ||
it(`cb does not crash on invalid name: ${c}`, function () { | ||
this.net_utils.get_mx(c, (err, mxlist) => { | ||
// console.error(err) | ||
assert.equal(mxlist.length, 0) | ||
checkInvalid(invalidCases[c], err.message) | ||
}) | ||
}) | ||
}) | ||
it('does not crash on invalid IDN name', function (done) { | ||
this.net_utils.get_mx('gmail.xn--com-0da', (err, mxlist) => { | ||
assert.strictEqual(err.message, 'Cannot convert name to ASCII') | ||
done() | ||
it(`async does not crash on invalid name: ${c}`, async function () { | ||
try { | ||
const mxlist = await this.net_utils.get_mx(c) | ||
assert.equal(mxlist.length, 0) | ||
} | ||
catch (err) { | ||
// console.error(err) | ||
checkInvalid(invalidCases[c], err.message) | ||
} | ||
}) | ||
}) | ||
} | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
80867
1484
14