From 3efef4a01c149995029505835d7a5abef1e621a0 Mon Sep 17 00:00:00 2001 From: HaidongPang Date: Thu, 7 Nov 2024 15:21:31 +0800 Subject: [PATCH] Clean up orphan ReadableStream remaining after retry (#650) --- source/core/Ky.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/source/core/Ky.ts b/source/core/Ky.ts index d69c03ef..e2d6212b 100644 --- a/source/core/Ky.ts +++ b/source/core/Ky.ts @@ -35,6 +35,8 @@ export class Ky { // Delay the fetch so that body method shortcuts can set the Accept header await Promise.resolve(); + // Before using ky.request, _fetch clones a new Request for retries preparation. + // If retry is not needed, close the original Request ReadableStream for memory safety. let response = await ky._fetch(); for (const hook of ky._options.hooks.afterResponse) { @@ -63,6 +65,10 @@ export class Ky { throw error; } + // Now, it can be determined that a retry is not needed, + // close the ReadableStream of the ky.request. + await ky.request.body?.cancel(); + // If `onDownloadProgress` is passed, it uses the stream API internally /* istanbul ignore next */ if (ky._options.onDownloadProgress) {