2
0
mirror of https://github.com/tenrok/axios.git synced 2026-06-11 18:02:32 +03:00

fix(fetch): optimize signals composing logic; (#6582)

This commit is contained in:
Dmitriy Mozgovoy
2024-08-30 21:26:12 +03:00
committed by GitHub
parent ee208cfcae
commit df9889b83c
4 changed files with 101 additions and 49 deletions
+10 -17
View File
@@ -113,18 +113,13 @@ export default isFetchSupported && (async (config) => {
responseType = responseType ? (responseType + '').toLowerCase() : 'text';
let [composedSignal, stopTimeout] = (signal || cancelToken || timeout) ?
composeSignals([signal, cancelToken], timeout) : [];
let composedSignal = composeSignals([signal, cancelToken && cancelToken.toAbortSignal()], timeout);
let finished, request;
let request;
const onFinish = () => {
!finished && setTimeout(() => {
composedSignal && composedSignal.unsubscribe();
});
finished = true;
}
const unsubscribe = composedSignal && composedSignal.unsubscribe && (() => {
composedSignal.unsubscribe();
});
let requestContentLength;
@@ -161,7 +156,7 @@ export default isFetchSupported && (async (config) => {
// Cloudflare Workers throws when credentials are defined
// see https://github.com/cloudflare/workerd/issues/902
const isCredentialsSupported = "credentials" in Request.prototype;
const isCredentialsSupported = "credentials" in Request.prototype;
request = new Request(url, {
...fetchOptions,
signal: composedSignal,
@@ -176,7 +171,7 @@ export default isFetchSupported && (async (config) => {
const isStreamResponse = supportsResponseStream && (responseType === 'stream' || responseType === 'response');
if (supportsResponseStream && (onDownloadProgress || isStreamResponse)) {
if (supportsResponseStream && (onDownloadProgress || (isStreamResponse && unsubscribe))) {
const options = {};
['status', 'statusText', 'headers'].forEach(prop => {
@@ -193,7 +188,7 @@ export default isFetchSupported && (async (config) => {
response = new Response(
trackStream(response.body, DEFAULT_CHUNK_SIZE, onProgress, () => {
flush && flush();
isStreamResponse && onFinish();
unsubscribe && unsubscribe();
}, encodeText),
options
);
@@ -203,9 +198,7 @@ export default isFetchSupported && (async (config) => {
let responseData = await resolvers[utils.findKey(resolvers, responseType) || 'text'](response, config);
!isStreamResponse && onFinish();
stopTimeout && stopTimeout();
!isStreamResponse && unsubscribe && unsubscribe();
return await new Promise((resolve, reject) => {
settle(resolve, reject, {
@@ -218,7 +211,7 @@ export default isFetchSupported && (async (config) => {
})
})
} catch (err) {
onFinish();
unsubscribe && unsubscribe();
if (err && err.name === 'TypeError' && /fetch/i.test(err.message)) {
throw Object.assign(