Skip to content

Support passing custom AbortSignal #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function retryInit(options={}) {
retryOnHttpResponse: ((typeof retryOptions.retryOnHttpResponse === 'function') && retryOptions.retryOnHttpResponse) ||
((response) => { return response.status >= 500; }),
retryOnHttpError: ((typeof retryOptions.retryOnHttpError === 'function') && retryOptions.retryOnHttpError) ||
((error) => { return shouldRetryOnHttpError(error); }),
((error) => { return shouldRetryOnHttpError(error, !!options.signal); }),
socketTimeout: socketTimeoutValue
};
}
Expand Down Expand Up @@ -150,14 +150,14 @@ function checkParameters(retryOptions) {
* @param {Object} error
* @returns Returns true for all FetchError's of type `system`
*/
function shouldRetryOnHttpError(error) {
function shouldRetryOnHttpError(error, ignoreAborted) {
// special handling for known fetch errors: https://github.com/node-fetch/node-fetch/blob/main/docs/ERROR-HANDLING.md
// retry on all errors originating from Node.js core
// retry on AbortError caused by network timeouts
if (error.name === 'FetchError' && error.type === 'system') {
console.error(`FetchError failed with code: ${error.code}; message: ${error.message}`);
return true;
} else if (error.name === 'AbortError') {
} else if (error.name === 'AbortError' && !ignoreAborted) {
console.error(`AbortError failed with type: ${error.type}; message: ${error.message}`);
return true;
}
Expand Down Expand Up @@ -208,11 +208,11 @@ module.exports = async function (url, options) {
const waitTime = getRetryDelay(retryOptions);

let timeoutHandler;
if (retryOptions.socketTimeout) {
if (!options.signal && retryOptions.socketTimeout) {
const controller = new AbortController();
timeoutHandler = setTimeout(() => controller.abort(), retryOptions.socketTimeout);
options.signal = controller.signal;
}
}

try {
const response = await fetch(url, options);
Expand Down