fetch-event-source
fetch-event-source copied to clipboard
fix: fix bug with polyfill fetch
@microsoft-github-policy-service agree
When using fetch-event-source in the webview of iOS or Android, it is important to note that the fetch method is not implemented as native code. Instead, it is a polyfill that provides functionality similar to the native fetch method. One specific difference is that the polyfill does not have a response with a body of type ReadableStream
. Considering this limitation is therefore crucial in such cases.
your code may cause repeated fetch requests. I adjusted it.
export async function getBytes(response: Response, onChunk: (arr: Uint8Array) => void) {
const reader = response.body?.getReader();
if (reader) {
let result;
while (!(result = await reader.read()).done) {
onChunk(result.value);
}
} else {
onChunk(new Uint8Array(await response.arrayBuffer()));
}
}