fetch-event-source icon indicating copy to clipboard operation
fetch-event-source copied to clipboard

fix: fix bug with polyfill fetch

Open Xuan-Yu-San opened this issue 1 year ago • 3 comments

Xuan-Yu-San avatar Oct 08 '23 13:10 Xuan-Yu-San

@microsoft-github-policy-service agree

Xuan-Yu-San avatar Oct 09 '23 11:10 Xuan-Yu-San

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.

Xuan-Yu-San avatar Oct 09 '23 11:10 Xuan-Yu-San

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()));
    }
}

congjinruo avatar Aug 01 '24 02:08 congjinruo