From 02e260fb16670af32fc0c99fbf01a8492d5dad6b Mon Sep 17 00:00:00 2001 From: Shigma Date: Tue, 26 Mar 2024 03:10:52 +0800 Subject: [PATCH] fix(core): fix AbortController abort stream body, close #4 --- packages/core/src/index.ts | 7 ++++--- packages/file/src/index.ts | 1 + 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index a56d6bf..881ad3d 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -255,12 +255,13 @@ export class HTTP extends Service { const timer = config.timeout && setTimeout(() => { controller.abort(new HTTPError('request timeout', 'ETIMEDOUT')) }, config.timeout) - return () => { + return (done?: boolean) => { clearTimeout(timer) + if (done) return controller.abort(new HTTPError('context disposed', 'ETIMEDOUT')) } }) - controller.signal.addEventListener('abort', dispose) + controller.signal.addEventListener('abort', () => dispose()) try { const headers = new Headers(config.headers) @@ -317,7 +318,7 @@ export class HTTP extends Service { } return response } finally { - controller.abort() + dispose(true) } } diff --git a/packages/file/src/index.ts b/packages/file/src/index.ts index 9ec6a49..4a17240 100644 --- a/packages/file/src/index.ts +++ b/packages/file/src/index.ts @@ -23,6 +23,7 @@ export interface FileResponse { } export const name = 'undios-file' +export const inject = ['http'] export interface Config {}