link-check
Advanced tools
Comparing version 4.4.7 to 4.5.0
# Changes | ||
## Version 4.5.0 | ||
* update dependencies | ||
* add an option to automatically retry on a 429 response (PR #19) | ||
## Version 4.4.7 | ||
@@ -4,0 +9,0 @@ |
@@ -20,3 +20,3 @@ "use strict"; | ||
opts.timeout = opts.timeout || '10s'; | ||
opts.retryOn429 = opts.retryOn429 || false; | ||
@@ -23,0 +23,0 @@ const protocol = (url.parse(link, false, true).protocol || url.parse(opts.baseUrl, false, true).protocol || 'unknown:').replace(/:$/, ''); |
@@ -11,4 +11,7 @@ "use strict"; | ||
check: function (link, opts, callback) { | ||
check: function check(link, opts, callback, attempts) { | ||
if (attempts == null) { | ||
attempts = 0; | ||
} | ||
const options = { | ||
@@ -41,3 +44,30 @@ uri: link, | ||
request.get(options, function (err, res) { | ||
callback(null, new LinkCheckResult(opts, link, res ? res.statusCode : 0, err)); | ||
// If enabled in opts, the response was a 429 (Too Many Requests) and there is a retry-after provided, wait and then retry | ||
if (opts.retryOn429 && res && res.statusCode === 429 && res.headers.hasOwnProperty('retry-after') && attempts < 2) { | ||
const retryStr = res.headers['retry-after']; | ||
// Sites may respond with a retry-after such as '1m30s', so we need to split this into distinct | ||
// time units (separate '1m' and '30s') for zeit/ms to parse them | ||
let retryInMs = 0; | ||
let buf = ''; | ||
let letter = false; | ||
for (let i = 0; i < retryStr.length; i++) { | ||
let c = retryStr[i]; | ||
if (isNaN(c)) { | ||
letter = true; | ||
buf += c; | ||
} else { | ||
if (letter) { | ||
retryInMs += ms(buf.trim()); | ||
buf = ''; | ||
} | ||
letter = false; | ||
buf += c; | ||
} | ||
} | ||
retryInMs += ms(buf.trim()); | ||
// Recurse back after the retry timeout has elapsed (incrementing our attempts to avoid an infinite loop) | ||
setTimeout(check, retryInMs, link, opts, callback, attempts + 1); | ||
} else { | ||
callback(null, new LinkCheckResult(opts, link, res ? res.statusCode : 0, err)); | ||
} | ||
}).pipe(new BlackHole()); | ||
@@ -44,0 +74,0 @@ }); |
{ | ||
"name": "link-check", | ||
"version": "4.4.7", | ||
"version": "4.5.0", | ||
"description": "checks whether a hyperlink is alive (200 OK) or dead", | ||
@@ -34,3 +34,3 @@ "main": "index.js", | ||
"ms": "^2.1.2", | ||
"request": "^2.88.0" | ||
"request": "^2.88.2" | ||
}, | ||
@@ -40,4 +40,4 @@ "devDependencies": { | ||
"express": "^4.17.1", | ||
"jshint": "^2.10.3", | ||
"mocha": "^6.2.2" | ||
"jshint": "^2.11.0", | ||
"mocha": "^7.1.1" | ||
}, | ||
@@ -44,0 +44,0 @@ "jshintConfig": { |
@@ -33,2 +33,3 @@ # link-check | ||
* `headers` a string based attribute value object to send custom HTTP headers. Example: `{ 'Authorization' : 'Basic Zm9vOmJhcg==' }`. | ||
* `retryOn429` a boolean indicating whether to retry on a 429 (Too Many Requests) response. When true, a retry will only be attempted when the response includes a `retry-after` header that indicates how long to wait before retrying. | ||
* `callback` function which accepts `(err, result)`. | ||
@@ -35,0 +36,0 @@ * `err` an Error object when the operation cannot be completed, otherwise `null`. |
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
24083
438
81
Updatedrequest@^2.88.2