Skip to content

Commit

Permalink
fix(fetch): fix & optimize progress capturing for cases when the requ…
Browse files Browse the repository at this point in the history
…est data has a nullish value or zero data length (#6400)
  • Loading branch information
DigitalBrainJS committed May 18, 2024
1 parent ad3174a commit 95a3e8e
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions lib/adapters/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ isFetchSupported && (((res) => {
})(new Response));

const getBodyLength = async (body) => {
if (body == null) {
return 0;
}

if(utils.isBlob(body)) {
return body.size;
}
Expand Down Expand Up @@ -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,
Expand All @@ -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)) {
Expand Down

0 comments on commit 95a3e8e

Please sign in to comment.