Skip to content

Commit

Permalink
Merge pull request #94 from rkitover/replace-isemail
Browse files Browse the repository at this point in the history
Replace isemail to fix deprecation warning
  • Loading branch information
tcort authored Nov 5, 2024
2 parents 4c21b13 + 78afb46 commit 0e5d251
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 43 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ eventually ends in a `200 OK` response. To minimize bandwidth, an HTTP HEAD
is performed. If that fails (e.g. with a `405 Method Not Allowed`), an HTTP
GET is performed. Redirects are followed.

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

## API

Expand Down Expand Up @@ -52,7 +52,7 @@ Parameters:
```js
'use strict';

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

linkCheck('http://example.com', function (err, result) {
if (err) {
Expand All @@ -68,7 +68,7 @@ linkCheck('http://example.com', function (err, result) {
```js
'use strict';

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

linkCheck('http://example.com', { headers: { 'Authorization': 'Basic Zm9vOmJhcg==' } }, function (err, result) {
if (err) {
Expand Down
20 changes: 15 additions & 5 deletions lib/proto/mailto.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"use strict";

const Isemail = require('isemail');
const LinkCheckResult = require('../LinkCheckResult');

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

callback(null, new LinkCheckResult(opts, link, Isemail.validate(address) ? 200 : 400, null));
}
emailValidator(address, { checkMx: true, timeout: opts.timeout || '10s' }).then((emailValid) => {
if (!emailValid) {
return callback(null, new LinkCheckResult(opts, link, 400, null));
}
return callback(null, new LinkCheckResult(opts, link, 200, null));
}).catch((error) => {
if (error.message.match(/timed out/)) {
return callback(null, new LinkCheckResult(opts, link, 0, { message: 'Domain MX lookup timed out', code: 'ECONNRESET' }));
}
return callback(null, new LinkCheckResult(opts, link, 0, error));
});
});
};
69 changes: 36 additions & 33 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
"homepage": "https://github.com/tcort/link-check#readme",
"dependencies": {
"is-relative-url": "^4.0.0",
"isemail": "^3.2.0",
"ms": "^2.1.3",
"needle": "^3.3.1",
"node-email-verifier": "^2.0.0",
"proxy-agent": "^6.4.0"
},
"devDependencies": {
Expand Down
21 changes: 21 additions & 0 deletions test/link-check.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,18 @@ describe('link-check', function () {
});
});

it('should handle timeout for mailto validation', function (done) {
linkCheck('mailto:[email protected]', { timeout: '1ms' }, function (err, result) {
expect(err).to.be(null);
expect(result.link).to.be('mailto:[email protected]');
expect(result.status).to.be('dead');
expect(result.statusCode).to.be(0);
expect(result.err.code).to.be('ECONNRESET');
expect(result.err.message).to.be('Domain MX lookup timed out');
done();
});
});

it('should handle valid mailto with encoded characters in address', function (done) {
linkCheck('mailto:foo%[email protected]', function (err, result) {
expect(err).to.be(null);
Expand All @@ -286,6 +298,15 @@ describe('link-check', function () {
});
});

it('should handle valid mailto with invalid domain without MX record', function (done) {
linkCheck('mailto:[email protected]', function (err, result) {
expect(err).to.be(null);
expect(result.link).to.be('mailto:[email protected]');
expect(result.status).to.be('dead');
done();
});
});

it('should handle invalid mailto', function (done) {
linkCheck('mailto:foo@@bar@@baz', function (err, result) {
expect(err).to.be(null);
Expand Down

0 comments on commit 0e5d251

Please sign in to comment.