link-check
Advanced tools
Comparing version 2.0.2 to 2.1.0
24
index.js
@@ -19,4 +19,17 @@ "use strict"; | ||
module.exports = function linkCheck(link, callback) { | ||
var absoluteLinkMatcher = new RegExp('^(?:[a-z]+:)?//', 'i'); | ||
function isRelativeLink(link) { | ||
return !absoluteLinkMatcher.test(link); | ||
} | ||
module.exports = function linkCheck(link, opts, callback) { | ||
if (arguments.length === 2 && typeof opts === 'function') { | ||
// optional 'opts' not supplied. | ||
callback = opts; | ||
opts = {}; | ||
} | ||
var options = { | ||
uri: link, | ||
headers: { | ||
@@ -28,3 +41,8 @@ // override 'User-Agent' (some sites return `401` when the user-agent isn't a web browser) | ||
}; | ||
request.head(link, options, function (err, res, body) { | ||
if (opts.baseUrl && isRelativeLink(link)) { | ||
options.baseUrl = opts.baseUrl; | ||
} | ||
request.head(options, function (err, res, body) { | ||
if (!err && res.statusCode === 200) { | ||
@@ -36,3 +54,3 @@ callback(null, new LinkCheckResult(link, res ? res.statusCode : 0, err)); // alive, returned 200 OK | ||
// if HEAD fails (405 Method Not Allowed, etc), try GET | ||
request.get(link, options, function (err, res) { | ||
request.get(options, function (err, res) { | ||
callback(null, new LinkCheckResult(link, res ? res.statusCode : 0, err)); | ||
@@ -39,0 +57,0 @@ }).pipe(new BlackHole()); |
{ | ||
"name": "link-check", | ||
"version": "2.0.2", | ||
"version": "2.1.0", | ||
"description": "checks whether a hyperlink is alive (200 OK) or dead", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -18,3 +18,3 @@ # link-check | ||
### linkCheck(link, callback) | ||
### linkCheck(link, [opts,] callback) | ||
@@ -26,2 +26,4 @@ Given a `link` and a `callback`, attempt an HTTP HEAD and possibly an HTTP GET. | ||
* `url` string containing a URL. | ||
* `opts` optional options object containing any of the following optional fields: | ||
* `baseUrl` the base URL for relative links. | ||
* `callback` function which accepts `(err, result)`. | ||
@@ -28,0 +30,0 @@ * `err` an Error object when the operation cannot be completed, otherwise `null`. |
@@ -51,2 +51,13 @@ 'use strict'; | ||
it('should find that a valid relative link is alive', function (done) { | ||
linkCheck('/foo/bar', { baseUrl: baseUrl }, function (err, result) { | ||
expect(err).to.be(null); | ||
expect(result.link).to.be('/foo/bar'); | ||
expect(result.status).to.be('alive'); | ||
expect(result.statusCode).to.be(200); | ||
expect(result.err).to.be(null); | ||
done(); | ||
}); | ||
}); | ||
it('should find that an invalid link is dead', function (done) { | ||
@@ -63,2 +74,13 @@ linkCheck(baseUrl + '/foo/dead', function (err, result) { | ||
it('should find that an invalid relative link is dead', function (done) { | ||
linkCheck('/foo/dead', { baseUrl: baseUrl }, function (err, result) { | ||
expect(err).to.be(null); | ||
expect(result.link).to.be('/foo/dead'); | ||
expect(result.status).to.be('dead'); | ||
expect(result.statusCode).to.be(404); | ||
expect(result.err).to.be(null); | ||
done(); | ||
}); | ||
}); | ||
it('should report no DNS entry as a dead link', function (done) { | ||
@@ -65,0 +87,0 @@ linkCheck('http://example.example.example.com/', function (err, result) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10248
163
56