Skip to content

Commit 0e5d251

Browse files
authored
Merge pull request #94 from rkitover/replace-isemail
Replace isemail to fix deprecation warning
2 parents 4c21b13 + 78afb46 commit 0e5d251

File tree

5 files changed

+77
-43
lines changed

5 files changed

+77
-43
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ eventually ends in a `200 OK` response. To minimize bandwidth, an HTTP HEAD
1717
is performed. If that fails (e.g. with a `405 Method Not Allowed`), an HTTP
1818
GET is performed. Redirects are followed.
1919

20-
In the case of `mailto:` links, this module validates the e-mail address
21-
using [isemail](https://www.npmjs.com/package/isemail).
20+
In the case of `mailto:` links, this module validates the e-mail address using
21+
[node-email-verifier](https://www.npmjs.com/package/node-email-verifier).
2222

2323
## API
2424

@@ -52,7 +52,7 @@ Parameters:
5252
```js
5353
'use strict';
5454

55-
const linkCheck = require('link-check');
55+
import linkCheck from 'link-check';
5656

5757
linkCheck('http://example.com', function (err, result) {
5858
if (err) {
@@ -68,7 +68,7 @@ linkCheck('http://example.com', function (err, result) {
6868
```js
6969
'use strict';
7070

71-
const linkCheck = require('link-check');
71+
import linkCheck from 'link-check';
7272

7373
linkCheck('http://example.com', { headers: { 'Authorization': 'Basic Zm9vOmJhcg==' } }, function (err, result) {
7474
if (err) {

lib/proto/mailto.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
"use strict";
22

3-
const Isemail = require('isemail');
43
const LinkCheckResult = require('../LinkCheckResult');
54

6-
module.exports = {
7-
check: function (link, opts, callback) {
5+
module.exports.check = (link, opts, callback) => {
6+
import('node-email-verifier').then((mod) => {
7+
const emailValidator = mod.default;
88
const address = link
99
.substr(7) // strip "mailto:"
1010
.split('?')[0]; // trim ?subject=blah hfields
@@ -13,6 +13,16 @@ module.exports = {
1313
* so it's safe to split on '?' and pick [0].
1414
*/
1515

16-
callback(null, new LinkCheckResult(opts, link, Isemail.validate(address) ? 200 : 400, null));
17-
}
16+
emailValidator(address, { checkMx: true, timeout: opts.timeout || '10s' }).then((emailValid) => {
17+
if (!emailValid) {
18+
return callback(null, new LinkCheckResult(opts, link, 400, null));
19+
}
20+
return callback(null, new LinkCheckResult(opts, link, 200, null));
21+
}).catch((error) => {
22+
if (error.message.match(/timed out/)) {
23+
return callback(null, new LinkCheckResult(opts, link, 0, { message: 'Domain MX lookup timed out', code: 'ECONNRESET' }));
24+
}
25+
return callback(null, new LinkCheckResult(opts, link, 0, error));
26+
});
27+
});
1828
};

package-lock.json

Lines changed: 36 additions & 33 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
"homepage": "https://github.com/tcort/link-check#readme",
3030
"dependencies": {
3131
"is-relative-url": "^4.0.0",
32-
"isemail": "^3.2.0",
3332
"ms": "^2.1.3",
3433
"needle": "^3.3.1",
34+
"node-email-verifier": "^2.0.0",
3535
"proxy-agent": "^6.4.0"
3636
},
3737
"devDependencies": {

test/link-check.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,18 @@ describe('link-check', function () {
268268
});
269269
});
270270

271+
it('should handle timeout for mailto validation', function (done) {
272+
linkCheck('mailto:[email protected]', { timeout: '1ms' }, function (err, result) {
273+
expect(err).to.be(null);
274+
expect(result.link).to.be('mailto:[email protected]');
275+
expect(result.status).to.be('dead');
276+
expect(result.statusCode).to.be(0);
277+
expect(result.err.code).to.be('ECONNRESET');
278+
expect(result.err.message).to.be('Domain MX lookup timed out');
279+
done();
280+
});
281+
});
282+
271283
it('should handle valid mailto with encoded characters in address', function (done) {
272284
linkCheck('mailto:foo%[email protected]', function (err, result) {
273285
expect(err).to.be(null);
@@ -286,6 +298,15 @@ describe('link-check', function () {
286298
});
287299
});
288300

301+
it('should handle valid mailto with invalid domain without MX record', function (done) {
302+
linkCheck('mailto:[email protected]', function (err, result) {
303+
expect(err).to.be(null);
304+
expect(result.link).to.be('mailto:[email protected]');
305+
expect(result.status).to.be('dead');
306+
done();
307+
});
308+
});
309+
289310
it('should handle invalid mailto', function (done) {
290311
linkCheck('mailto:foo@@bar@@baz', function (err, result) {
291312
expect(err).to.be(null);

0 commit comments

Comments
 (0)