forked from tcort/link-check
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
33 lines (26 loc) · 1.03 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
33
"use strict";
const url = require('url');
const protocols = {
file: require('./lib/proto/file'),
http: require('./lib/proto/http'),
https: require('./lib/proto/https'),
mailto: 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 = {};
}
opts.timeout = opts.timeout || 10000;
opts.retryOn429 = opts.retryOn429 || false;
opts.allwaysRetry = opts.allwaysRetry || false;
opts.aliveStatusCodes = opts.aliveStatusCodes || [ 200 ];
const protocol = (url.parse(link, false, true).protocol || url.parse(opts.baseUrl, false, true).protocol || 'unknown:').replace(/:$/, '');
if (!protocols.hasOwnProperty(protocol)) {
callback(new Error('Unsupported Protocol'), null);
return;
}
protocols[protocol].check(link, opts, callback);
};
module.exports.LinkCheckResult = require('./lib/LinkCheckResult');