emscripten icon indicating copy to clipboard operation
emscripten copied to clipboard

[fetch] Support streaming fetch requests.

Open brendandahl opened this issue 3 months ago • 2 comments

Add a new FETCH_BACKEND setting that can either be 'xhr' or 'fetch' to use the corresponding DOM API. To keep the emscripten Fetch.js code the same I've implemented a polyfill of XMLHttpRequest using Fetch. To support streaming I wired up the fetch code to use the old onprogress code that old versions of Firefox supported.

Most of the current API is supported with some notable exceptions:

  • synchronous requests
  • overriding the mime type

I also changed a few of the tests to support both sync and async so I could test them with the fetch backend.

brendandahl avatar Sep 24 '25 23:09 brendandahl

Just adding a comment here to say I'd appreciate this change.

I had just hacked up Fetch.js to also do this, and then found this PR while searching for issues (and this looks much better than my hack and slash).

RE: overrideMimeType, it's effectively supported in that it doesn't matter - you don't need the XHR to reinterpret whatever mime type the server specified, you treat it as a big binary buffer regardless thanks to the readable stream.

inolen avatar Nov 07 '25 20:11 inolen

I've been meaning to come back to this. I was thinking of changing it so that the DOM fetch is only used when streaming is requested. That way I don't have to try and replicate all the old XHR behavior when streaming isn't enabled.

brendandahl avatar Nov 07 '25 23:11 brendandahl

I've changed how it works from original PR, now when FETCH_STREAMING is enabled we'll only use the DOM fetch API when a streaming data is requested. This way we can worry less about fully polyfilling XHR and hopefully have less breakage.

brendandahl avatar Nov 19 '25 23:11 brendandahl