Skip to content

Commit d198085

Browse files
authoredAug 31, 2024
fix(fetch): fix stream handling in Safari by fallback to using a stream reader instead of an async iterator; (#6584)
1 parent d584fcf commit d198085

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed
 

‎lib/adapters/fetch.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ export default isFetchSupported && (async (config) => {
146146
progressEventReducer(asyncDecorator(onUploadProgress))
147147
);
148148

149-
data = trackStream(_request.body, DEFAULT_CHUNK_SIZE, onProgress, flush, encodeText);
149+
data = trackStream(_request.body, DEFAULT_CHUNK_SIZE, onProgress, flush);
150150
}
151151
}
152152

@@ -189,7 +189,7 @@ export default isFetchSupported && (async (config) => {
189189
trackStream(response.body, DEFAULT_CHUNK_SIZE, onProgress, () => {
190190
flush && flush();
191191
unsubscribe && unsubscribe();
192-
}, encodeText),
192+
}),
193193
options
194194
);
195195
}

‎lib/helpers/trackStream.js

+25-5
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,34 @@ export const streamChunk = function* (chunk, chunkSize) {
1717
}
1818
}
1919

20-
export const readBytes = async function* (iterable, chunkSize, encode) {
21-
for await (const chunk of iterable) {
22-
yield* streamChunk(ArrayBuffer.isView(chunk) ? chunk : (await encode(String(chunk))), chunkSize);
20+
export const readBytes = async function* (iterable, chunkSize) {
21+
for await (const chunk of readStream(iterable)) {
22+
yield* streamChunk(chunk, chunkSize);
2323
}
2424
}
2525

26-
export const trackStream = (stream, chunkSize, onProgress, onFinish, encode) => {
27-
const iterator = readBytes(stream, chunkSize, encode);
26+
const readStream = async function* (stream) {
27+
if (stream[Symbol.asyncIterator]) {
28+
yield* stream;
29+
return;
30+
}
31+
32+
const reader = stream.getReader();
33+
try {
34+
for (;;) {
35+
const {done, value} = await reader.read();
36+
if (done) {
37+
break;
38+
}
39+
yield value;
40+
}
41+
} finally {
42+
await reader.cancel();
43+
}
44+
}
45+
46+
export const trackStream = (stream, chunkSize, onProgress, onFinish) => {
47+
const iterator = readBytes(stream, chunkSize);
2848

2949
let bytes = 0;
3050
let done;

0 commit comments

Comments
 (0)