mirror of
https://github.com/tenrok/axios.git
synced 2026-06-20 20:00:40 +03:00
fix(fetch): fix stream handling in Safari by fallback to using a stream reader instead of an async iterator; (#6584)
This commit is contained in:
@@ -146,7 +146,7 @@ export default isFetchSupported && (async (config) => {
|
|||||||
progressEventReducer(asyncDecorator(onUploadProgress))
|
progressEventReducer(asyncDecorator(onUploadProgress))
|
||||||
);
|
);
|
||||||
|
|
||||||
data = trackStream(_request.body, DEFAULT_CHUNK_SIZE, onProgress, flush, encodeText);
|
data = trackStream(_request.body, DEFAULT_CHUNK_SIZE, onProgress, flush);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -189,7 +189,7 @@ export default isFetchSupported && (async (config) => {
|
|||||||
trackStream(response.body, DEFAULT_CHUNK_SIZE, onProgress, () => {
|
trackStream(response.body, DEFAULT_CHUNK_SIZE, onProgress, () => {
|
||||||
flush && flush();
|
flush && flush();
|
||||||
unsubscribe && unsubscribe();
|
unsubscribe && unsubscribe();
|
||||||
}, encodeText),
|
}),
|
||||||
options
|
options
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,14 +17,34 @@ export const streamChunk = function* (chunk, chunkSize) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const readBytes = async function* (iterable, chunkSize, encode) {
|
export const readBytes = async function* (iterable, chunkSize) {
|
||||||
for await (const chunk of iterable) {
|
for await (const chunk of readStream(iterable)) {
|
||||||
yield* streamChunk(ArrayBuffer.isView(chunk) ? chunk : (await encode(String(chunk))), chunkSize);
|
yield* streamChunk(chunk, chunkSize);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const trackStream = (stream, chunkSize, onProgress, onFinish, encode) => {
|
const readStream = async function* (stream) {
|
||||||
const iterator = readBytes(stream, chunkSize, encode);
|
if (stream[Symbol.asyncIterator]) {
|
||||||
|
yield* stream;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const reader = stream.getReader();
|
||||||
|
try {
|
||||||
|
for (;;) {
|
||||||
|
const {done, value} = await reader.read();
|
||||||
|
if (done) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
yield value;
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
await reader.cancel();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const trackStream = (stream, chunkSize, onProgress, onFinish) => {
|
||||||
|
const iterator = readBytes(stream, chunkSize);
|
||||||
|
|
||||||
let bytes = 0;
|
let bytes = 0;
|
||||||
let done;
|
let done;
|
||||||
|
|||||||
Reference in New Issue
Block a user