Skip to content

Commit

Permalink
options: parse retry-after header
Browse files Browse the repository at this point in the history
and set ignoreInvalidCookies by default
  • Loading branch information
myfreeer committed May 14, 2020
1 parent 4f42ba2 commit 90381be
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@ const logger = {

const MAX_RETRY = 25;
const MAX_RETRY_DELAY = 5000;
const MAX_RETRY_AFTER = 60000;
/**
* @type {Partial<DownloaderOptions>}
*/
const defaultOptions = {
req: {
maxRedirects: 15,
// RequestError: Cookie has domain set to a public suffix
// https://developer.mozilla.org/zh-CN/docs/Mozilla/QA/Marionette/Client
ignoreInvalidCookies: true,
retry: {
decompress: true,
limit: MAX_RETRY,
maxRetryAfter: MAX_RETRY_AFTER,
/**
* if you would like to implement it yourself,
* set error.retryLimitExceeded to 1 or true if
Expand Down Expand Up @@ -55,6 +60,25 @@ const defaultOptions = {
error.response && error.response.statusCode === 429) {
// add random delay
delay += 3000 + Math.random() * 3000;
if (error.response.headers &&
error.response.headers['retry-after']) {
let retryAfter = parseInt(error.response.headers['retry-after']);
if (Number.isNaN(retryAfter)) {
retryAfter = Date.parse(error.response.headers['retry-after']) - Date.now();
} else {
retryAfter *= 1000;
}
if (!isNaN(retryAfter)) {
retryAfter |= 0;
if (retryOptions.maxRetryAfter) {
if (retryAfter <= retryOptions.maxRetryAfter) {
delay = retryAfter;
}
} else {
delay = retryAfter;
}
}
}
}
delay |= 0;
return delay;
Expand Down

0 comments on commit 90381be

Please sign in to comment.