5.5.0 • Published 5 years ago
@boillodmanuel/link-check v5.5.0
ℹ️ Fork of tcort repository (version 4.5.2), with several improvements:
- completely rewritten in typescript
- see CHANGELOG.md since v4.5.2 to get full list of improvements
link-check
Checks whether a hyperlink is alive (200 OK) or dead.
Installation
npm install --save link-checkSpecification
A link is said to be 'alive' if an HTTP HEAD or HTTP GET for the given URL
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.
API
linkCheck(link, opts, callback)
Given a link and a callback, attempt an HTTP HEAD and possibly an HTTP GET.
Parameters:
urlstring containing a URL.optsoptional options object containing any of the following optional fields:baseUrlthe base URL for relative links.timeouttimeout in zeit/ms format. (e.g."2000ms",20s,1m). Default10s.aliveStatusCodesan array of numeric HTTP Response codes which indicate that the link is alive. Entries in this array may also be regular expressions. Example:[ 200, /^[45][0-9]{2}$/ ]. Default[ 200 ].headersa string based attribute value object to send custom HTTP headers. Example:{ 'Authorization' : 'Basic Zm9vOmJhcg==' }.retryOn429a boolean indicating whether to retry on a 429 (Too Many Requests) response. When true, if the response has a 429 HTTP code and includes an optionalretry-afterheader, a retry will be attempted after the delay indicated in theretry-afterheader. If noretry-afterheader is present in the response or theretry-afterheader value is not valid according to RFC7231 (value must be in seconds), a default retry delay of 60 seconds will apply. This default can be overriden by thefallbackRetryDelayparameter.retryOnErrora boolean indicating whether to retry on error (not an error status code). Default isfalse.retryCountthe number of retries to be made on a 429 response. Default2.fallbackRetryDelaythe delay in zeit/ms format. (e.g."2000ms",20s,1m) for retries on a 429 response when noretry-afterheader is returned or when it has an invalid value. Default is60s.debugenable debugdebugToStdErrwrite debug message to standard error output
callbackfunction which accepts(err, result).erran Error object when the operation cannot be completed, otherwisenull.resultan object with the following properties orundefined:linkthelinkprovided as inputstatusa string set to eitheraliveordead.statusCodethe HTTP status code. Set to0if no HTTP status code was returned (e.g. when the server is down).errany connection error that occurred, otherwiseundefined.additionalMessagesa list of additional messages that occured, otherwiseundefined.
Examples
Javascript:
'use strict';
const linkChecker = require('link-check');
linkCheck.linkCheck('http://example.com', function (err, result) {
if (err) {
console.error(err);
return;
}
console.log(`${result.link} is ${result.status}`);
});Typescript:
import { linkCheck, Options, LinkCheckResult } from 'link-check'
const options: Options = {
timeout: '5s',
}
linkCheck('http://example.com', options, (err: any, result: LinkCheckResult) => {
if (err) {
console.error(err)
return
}
console.log(`${result.link} is ${result.status}`)
})One-liner test from shell
node -e 'require("link-check").linkCheck("https://www.shellcheck.net", {debug: true, timeout: "5s"}, function (err, result) { console.log(result)})'With basic authentication:
'use strict';
const linkChecker = require('link-check');
linkChecker.linkCheck('http://example.com', { headers: { 'Authorization': 'Basic Zm9vOmJhcg==' } }, function (err, result) {
if (err) {
console.error(err);
return;
}
console.log(`${result.link} is ${result.status}`);
});Testing
npm testLicense
See LICENSE.md