From 95a3e8e346cfd6a5548e171f2341df3235d0e26b Mon Sep 17 00:00:00 2001 From: Dmitriy Mozgovoy Date: Sat, 18 May 2024 18:16:42 +0300 Subject: [PATCH] fix(fetch): fix & optimize progress capturing for cases when the request data has a nullish value or zero data length (#6400) --- lib/adapters/fetch.js | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/lib/adapters/fetch.js b/lib/adapters/fetch.js index 0ac9650..d053e57 100644 --- a/lib/adapters/fetch.js +++ b/lib/adapters/fetch.js @@ -59,6 +59,10 @@ isFetchSupported && (((res) => { })(new Response)); const getBodyLength = async (body) => { + if (body == null) { + return 0; + } + if(utils.isBlob(body)) { return body.size; } @@ -117,10 +121,13 @@ export default isFetchSupported && (async (config) => { finished = true; } - try { - if (onUploadProgress && supportsRequestStream && method !== 'get' && method !== 'head') { - let requestContentLength = await resolveBodyLength(headers, data); + let requestContentLength; + try { + if ( + onUploadProgress && supportsRequestStream && method !== 'get' && method !== 'head' && + (requestContentLength = await resolveBodyLength(headers, data)) !== 0 + ) { let _request = new Request(url, { method: 'POST', body: data, @@ -133,10 +140,12 @@ export default isFetchSupported && (async (config) => { headers.setContentType(contentTypeHeader) } - data = trackStream(_request.body, DEFAULT_CHUNK_SIZE, fetchProgressDecorator( - requestContentLength, - progressEventReducer(onUploadProgress) - )); + if (_request.body) { + data = trackStream(_request.body, DEFAULT_CHUNK_SIZE, fetchProgressDecorator( + requestContentLength, + progressEventReducer(onUploadProgress) + )); + } } if (!utils.isString(withCredentials)) {