Skip to content
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

fix: clear abort timeout after response was received #369

Merged
merged 4 commits into from Mar 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/fetch.ts
Expand Up @@ -149,10 +149,15 @@
}
}

let abortTimeout: NodeJS.Timeout | undefined;

// TODO: Can we merge signals?
if (!context.options.signal && context.options.timeout) {
const controller = new AbortController();
setTimeout(() => controller.abort(), context.options.timeout);
abortTimeout = setTimeout(
() => controller.abort(),
context.options.timeout
);
context.options.signal = controller.signal;
}

Expand All @@ -167,6 +172,10 @@
await context.options.onRequestError(context as any);
}
return await onError(context);
} finally {
if (abortTimeout) {
clearTimeout(abortTimeout);
}
}

const hasBody =
Expand Down Expand Up @@ -223,7 +232,7 @@

$fetch.raw = $fetchRaw;

// @ts-expect-error TODO: Fix conflicting types with undici

Check failure on line 235 in src/fetch.ts

View workflow job for this annotation

GitHub Actions / ci (16)

Unused '@ts-expect-error' directive.

Check failure on line 235 in src/fetch.ts

View workflow job for this annotation

GitHub Actions / ci (18)

Unused '@ts-expect-error' directive.

Check failure on line 235 in src/fetch.ts

View workflow job for this annotation

GitHub Actions / ci (20)

Unused '@ts-expect-error' directive.
$fetch.native = (...args) => fetch(...args);

$fetch.create = (defaultOptions = {}) =>
Expand Down