2
0
mirror of https://github.com/tenrok/axios.git synced 2026-06-20 20:00: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
+16 -7
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,10 +140,12 @@ export default isFetchSupported && (async (config) => {
headers.setContentType(contentTypeHeader) headers.setContentType(contentTypeHeader)
} }
data = trackStream(_request.body, DEFAULT_CHUNK_SIZE, fetchProgressDecorator( if (_request.body) {
requestContentLength, data = trackStream(_request.body, DEFAULT_CHUNK_SIZE, fetchProgressDecorator(
progressEventReducer(onUploadProgress) requestContentLength,
)); progressEventReducer(onUploadProgress)
));
}
} }
if (!utils.isString(withCredentials)) { if (!utils.isString(withCredentials)) {