diff --git a/source/core/Ky.ts b/source/core/Ky.ts index d69c03ef..0b01d822 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,11 @@ export class Ky { throw error; } + // Now, it can be determined that a retry is not needed, + // close the ReadableStream of the ky.request. + if (!ky.request.bodyUsed){ + await ky.request.arrayBuffer(); + } // If `onDownloadProgress` is passed, it uses the stream API internally /* istanbul ignore next */ if (ky._options.onDownloadProgress) {