From dfb96ae2181e7f973f465dcb579b5d29a229bb4c Mon Sep 17 00:00:00 2001 From: daniluk4000 Date: Fri, 1 Mar 2024 17:13:04 +0300 Subject: [PATCH 1/3] fix: clear abort timeout after response was received --- src/fetch.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/fetch.ts b/src/fetch.ts index c0021c08..ecfb7e94 100644 --- a/src/fetch.ts +++ b/src/fetch.ts @@ -149,10 +149,12 @@ export function createFetch(globalOptions: CreateFetchOptions = {}): $Fetch { } } + let abortTimeout: number | 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; } @@ -161,7 +163,9 @@ export function createFetch(globalOptions: CreateFetchOptions = {}): $Fetch { context.request, context.options as RequestInit ); + clearTimeout(abortTimeout); } catch (error) { + clearTimeout(abortTimeout); context.error = error as Error; if (context.options.onRequestError) { await context.options.onRequestError(context as any); From 35a3ddbc512f8f00a83e56f9a950c6d2f240c80d Mon Sep 17 00:00:00 2001 From: daniluk4000 Date: Fri, 1 Mar 2024 17:35:50 +0300 Subject: [PATCH 2/3] Fix abortTimeout type --- src/fetch.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/fetch.ts b/src/fetch.ts index ecfb7e94..287bdea8 100644 --- a/src/fetch.ts +++ b/src/fetch.ts @@ -149,7 +149,7 @@ export function createFetch(globalOptions: CreateFetchOptions = {}): $Fetch { } } - let abortTimeout: number | undefined; + let abortTimeout: NodeJS.Timeout | undefined; // TODO: Can we merge signals? if (!context.options.signal && context.options.timeout) { From 70e3a01b11d142d14ed010d92cd107c29d0fa8fb Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Thu, 21 Mar 2024 13:30:40 +0100 Subject: [PATCH 3/3] refactor: move to finally --- src/fetch.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/fetch.ts b/src/fetch.ts index 287bdea8..e2cb5d2f 100644 --- a/src/fetch.ts +++ b/src/fetch.ts @@ -154,7 +154,10 @@ export function createFetch(globalOptions: CreateFetchOptions = {}): $Fetch { // TODO: Can we merge signals? if (!context.options.signal && context.options.timeout) { const controller = new AbortController(); - abortTimeout = setTimeout(() => controller.abort(), context.options.timeout); + abortTimeout = setTimeout( + () => controller.abort(), + context.options.timeout + ); context.options.signal = controller.signal; } @@ -163,14 +166,16 @@ export function createFetch(globalOptions: CreateFetchOptions = {}): $Fetch { context.request, context.options as RequestInit ); - clearTimeout(abortTimeout); } catch (error) { - clearTimeout(abortTimeout); context.error = error as Error; if (context.options.onRequestError) { await context.options.onRequestError(context as any); } return await onError(context); + } finally { + if (abortTimeout) { + clearTimeout(abortTimeout); + } } const hasBody =