2
0
mirror of https://github.com/tenrok/axios.git synced 2026-06-23 20:40:40 +03:00

fix(fetch): fix & optimize progress capturing for cases when the request data has a nullish value or zero data length (#6400)

This commit is contained in:
Dmitriy Mozgovoy
2024-05-18 18:16:42 +03:00
committed by GitHub
parent ad3174a351
commit 95a3e8e346
+12 -3
View File
@@ -59,6 +59,10 @@ isFetchSupported && (((res) => {
})(new Response)); })(new Response));
const getBodyLength = async (body) => { const getBodyLength = async (body) => {
if (body == null) {
return 0;
}
if(utils.isBlob(body)) { if(utils.isBlob(body)) {
return body.size; return body.size;
} }
@@ -117,10 +121,13 @@ export default isFetchSupported && (async (config) => {
finished = true; finished = true;
} }
try { let requestContentLength;
if (onUploadProgress && supportsRequestStream && method !== 'get' && method !== 'head') {
let requestContentLength = await resolveBodyLength(headers, data);
try {
if (
onUploadProgress && supportsRequestStream && method !== 'get' && method !== 'head' &&
(requestContentLength = await resolveBodyLength(headers, data)) !== 0
) {
let _request = new Request(url, { let _request = new Request(url, {
method: 'POST', method: 'POST',
body: data, body: data,
@@ -133,11 +140,13 @@ export default isFetchSupported && (async (config) => {
headers.setContentType(contentTypeHeader) headers.setContentType(contentTypeHeader)
} }
if (_request.body) {
data = trackStream(_request.body, DEFAULT_CHUNK_SIZE, fetchProgressDecorator( data = trackStream(_request.body, DEFAULT_CHUNK_SIZE, fetchProgressDecorator(
requestContentLength, requestContentLength,
progressEventReducer(onUploadProgress) progressEventReducer(onUploadProgress)
)); ));
} }
}
if (!utils.isString(withCredentials)) { if (!utils.isString(withCredentials)) {
withCredentials = withCredentials ? 'cors' : 'omit'; withCredentials = withCredentials ? 'cors' : 'omit';