-
Notifications
You must be signed in to change notification settings - Fork 29
/
index.js
32 lines (24 loc) · 1 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
"use strict";
const { URL } = require('url');
const protocols = {
hash: function () { return require('./lib/proto/hash'); },
file: function () { return require('./lib/proto/file'); },
http: function () { return require('./lib/proto/http'); },
https: function () { return require('./lib/proto/https'); },
mailto: function () { return require('./lib/proto/mailto'); },
};
module.exports = function linkCheck(link, opts, callback) {
if (arguments.length === 2 && typeof opts === 'function') {
// optional 'opts' not supplied.
callback = opts;
opts = {};
}
const url = link.startsWith('#') ? link : new URL(link, opts.baseUrl);
const protocol = link.startsWith('#') ? 'hash' : url.protocol.replace(/:$/, '');
if (!protocols.hasOwnProperty(protocol)) {
callback(new Error('Unsupported Protocol'), null);
return;
}
protocols[protocol]().check(link, opts, callback);
};
module.exports.LinkCheckResult = require('./lib/LinkCheckResult');