Skip to content

Commit 95a3e8e

Browse files
fix(fetch): fix & optimize progress capturing for cases when the request data has a nullish value or zero data length (#6400)
1 parent ad3174a commit 95a3e8e

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

lib/adapters/fetch.js

+16-7
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ isFetchSupported && (((res) => {
5959
})(new Response));
6060

6161
const getBodyLength = async (body) => {
62+
if (body == null) {
63+
return 0;
64+
}
65+
6266
if(utils.isBlob(body)) {
6367
return body.size;
6468
}
@@ -117,10 +121,13 @@ export default isFetchSupported && (async (config) => {
117121
finished = true;
118122
}
119123

120-
try {
121-
if (onUploadProgress && supportsRequestStream && method !== 'get' && method !== 'head') {
122-
let requestContentLength = await resolveBodyLength(headers, data);
124+
let requestContentLength;
123125

126+
try {
127+
if (
128+
onUploadProgress && supportsRequestStream && method !== 'get' && method !== 'head' &&
129+
(requestContentLength = await resolveBodyLength(headers, data)) !== 0
130+
) {
124131
let _request = new Request(url, {
125132
method: 'POST',
126133
body: data,
@@ -133,10 +140,12 @@ export default isFetchSupported && (async (config) => {
133140
headers.setContentType(contentTypeHeader)
134141
}
135142

136-
data = trackStream(_request.body, DEFAULT_CHUNK_SIZE, fetchProgressDecorator(
137-
requestContentLength,
138-
progressEventReducer(onUploadProgress)
139-
));
143+
if (_request.body) {
144+
data = trackStream(_request.body, DEFAULT_CHUNK_SIZE, fetchProgressDecorator(
145+
requestContentLength,
146+
progressEventReducer(onUploadProgress)
147+
));
148+
}
140149
}
141150

142151
if (!utils.isString(withCredentials)) {

0 commit comments

Comments
 (0)