-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Use fetch()
for loading packages & shared libraries
#22002
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we need a new option at all here or if we should just do this in all cases?
fetch(url) | ||
.then(response => { | ||
if(response.ok) { | ||
return response.arrayBuffer(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not call onload
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
response.arrayBuffer();
returns a promise that resolves to a buffer, not a literal buffer.
src/web_or_worker_shell_read.js
Outdated
if(response.ok) { | ||
return response.arrayBuffer(); | ||
} | ||
throw new Error(response.statusText + ' : ' + response.url); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this not call onerror
instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me check the make sure the error has the correct stack if its not actually thrown, and then I can change it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I remembered why I used this pattern as I was editing: The catch()
at the end of the function will catch errors coming from the fetch()
, but fetch()
won't throw errors by itself if the request fails, i.e. in a 404 or 500 error. For that we have to throw our own error. When we do that, we skip the then()
on line 32 and head right to catch()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about return new Promise.reject(..)
Then I think maybe you can do .then(onload, onerror)
without the extra catch? Not sure which is more idomatic.
I'd like this to be enabled by default, but I don't want to introduce any surprises in cases where people are polyfilling/overriding |
We will also likely need to write a minimal polyfil for cases where we target older browser. (i.e. |
Polyfilling/overriding XMLHttpRequest is not something that I know of anyone doing and we don't document emscripten as supporting this. I think its reasonable to simply make the switch and add a ChangeLog entry for this.. if somebody really is overriding XMLHttpRequest it should be easy enough to also override fetch. That was we can avoid adding a new setting (each time we do this it increases our testing burden). |
I'll go ahead and make those changes. |
fetch
for loading packages & shared libraries
fetch
for loading packages & shared librariesfetch()
for loading packages & shared libraries
This reverts commit 0bf9b71.
If possible, I'd like to see the The |
tools/file_packager.py
Outdated
Module.dataFileDownloads = Module.dataFileDownloads || {}; | ||
const url = packageName; | ||
fetch(url) | ||
.catch(error => { throw new Error(error + ' : ' + url) }) // Do this first so we don't catch errors from then() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of .catch
here is it the same to write the error handler as the second argument to then()
? Is one way preferable over the other?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case, we need to catch()
before then()
so we only handle fetch errors here. If we putt this at the bottom, or in the second param of this(),
then it will also act on the network errors generated by then(),
and I'd need to add logic to skip certain errors.
tools/file_packager.py
Outdated
Module.dataFileDownloads = Module.dataFileDownloads || {}; | ||
const url = packageName; | ||
fetch(url) | ||
.catch(error => { throw new Error(error + ' : ' + url) }) // Do this first so we don't catch errors from then() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think our style uses braces around the single argument to arrow functions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
throw
isn't a function, so it needs to have braces or it counts as a syntax error. I'm switching to Promise.reject
which is a function.
tools/file_packager.py
Outdated
if(response.ok) { | ||
return response.json(); | ||
} | ||
throw new Error(response.statusText + ' : ' + response.url); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would return Promise.reject(new Error(response.statusText + ' : ' + response.url))
instead of throwing?
#22015 - libs Let me know how those look. |
This PR switches AJAX requests for file packages & shared libraries to be loaded with
fetch()
rather thanXmlHttpRequest()
. This allows service workers to make AJAX requests, sinceXmlHttpRequest
is not available in service workers.Previously,
--embed-file
was necessary to load file packages, which embeds the files into the binary and precludes caching and re-use of the package. Loading shared libraries is not possible in a service worker without this patch.Fixes #22003