Skip to content

Conversation

@brendandahl
Copy link
Collaborator

@brendandahl brendandahl commented Sep 24, 2025

Add a new FETCH_STREAMING setting that enables using the DOM fetch API to stream data when paired with EMSCRIPTEN_FETCH_STREAM_DATA. When EMSCRIPTEN_FETCH_STREAM_DATA is not used we will default to a regular XMLHttpRequest. 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.

@inolen
Copy link
Collaborator

inolen commented Nov 7, 2025

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.

@brendandahl
Copy link
Collaborator Author

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 brendandahl force-pushed the fetch-xhr branch 3 times, most recently from fe93793 to 536b320 Compare November 19, 2025 22:35
@brendandahl
Copy link
Collaborator Author

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.

Copy link
Collaborator

@sbc100 sbc100 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice to see this working out!

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.
is only used for streaming requests otherwise it defaults to a regular
XHR request.
@brendandahl brendandahl merged commit af9ee5b into emscripten-core:main Nov 20, 2025
3 of 16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants