Skip to content

Commit

Permalink
Re-enable XHR as fallback for fetch on webview (#22026)
Browse files Browse the repository at this point in the history
In #22015 we switched `readAsync` to be based on fetch rather than
XHR.  However that broke the assumption in `getBinaryenPromise` that was
assuming `readAsync` was XHR-based.

This change reinstates the XHR fallback in the case of webviews where
loading from `file://` URLs is not only possible but (apparently)
common.

Sadly we don't have any webview tests so I have no way to actually test
this within our current frameworks.
  • Loading branch information
sbc100 authored May 31, 2024
1 parent b0ec4e1 commit 67fa4c1
Show file tree
Hide file tree
Showing 81 changed files with 116 additions and 108 deletions.
39 changes: 12 additions & 27 deletions src/preamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -642,36 +642,21 @@ function getBinarySync(file) {
function getBinaryPromise(binaryFile) {
#if !SINGLE_FILE
// If we don't have the binary yet, try to load it asynchronously.
// Fetch has some additional restrictions over XHR, like it can't be used on a file:// url.
// See https://github.com/github/fetch/pull/92#issuecomment-140665932
// Cordova or Electron apps are typically loaded from a file:// url.
// So use fetch if it is available and the url is not a file, otherwise fall back to XHR.
// If we don't have the binary yet, load it asynchronously using readAsync.
if (!wasmBinary
#if SUPPORT_BASE64_EMBEDDING
&& !isDataURI(binaryFile)
#endif
&& (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER)) {
if (typeof fetch == 'function'
#if ENVIRONMENT_MAY_BE_WEBVIEW
&& !isFileURI(binaryFile)
#endif
) {
return fetch(binaryFile, {{{ makeModuleReceiveExpr('fetchSettings', "{ credentials: 'same-origin' }") }}}).then((response) => {
if (!response['ok']) {
throw `failed to load wasm binary file at '${binaryFile}'`;
}
return response['arrayBuffer']();
}).catch(() => getBinarySync(binaryFile));
}
#if ENVIRONMENT_MAY_BE_WEBVIEW
else if (readAsync) {
// fetch is not available or url is file => try XHR (readAsync uses XHR internally)
return new Promise((resolve, reject) => {
readAsync(binaryFile, (response) => resolve(new Uint8Array(/** @type{!ArrayBuffer} */(response))), reject)
});
}
|| isDataURI(binaryFile)
#endif
) {
// Fetch the binary use readAsync
return new Promise((resolve, reject) => {
readAsync(binaryFile,
(response) => resolve(new Uint8Array(/** @type{!ArrayBuffer} */(response))),
(error) => {
try { resolve(getBinarySync(binaryFile)); }
catch (e) { reject(e); }
});
});
}
#endif
Expand Down
27 changes: 25 additions & 2 deletions src/web_or_worker_shell_read.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,35 @@
}

readAsync = (url, onload, onerror) => {
fetch(url)
#if ENVIRONMENT_MAY_BE_WEBVIEW
// Fetch has some additional restrictions over XHR, like it can't be used on a file:// url.
// See https://github.com/github/fetch/pull/92#issuecomment-140665932
// Cordova or Electron apps are typically loaded from a file:// url.
// So use XHR on webview if URL is a file URL.
if (isFileURI(url)) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'arraybuffer';
xhr.onload = () => {
if (xhr.status == 200 || (xhr.status == 0 && xhr.response)) { // file URLs can return 0
onload(xhr.response);
return;
}
onerror();
};
xhr.onerror = onerror;
xhr.send(null);
return;
}
#elif ASSERTIONS
assert(!isFileURI(url), "readAsync does not work with file:// URLs");
#endif
fetch(url, {{{ makeModuleReceiveExpr('fetchSettings', "{ credentials: 'same-origin' }") }}})
.then((response) => {
if (response.ok) {
return response.arrayBuffer();
}
return Promise.reject(new Error(response.statusText + ' : ' + response.url));
return Promise.reject(new Error(response.status + ' : ' + response.url));
})
.then(onload, onerror)
};
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_ctors1.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
9813
9817
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_ctors1.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
24096
24102
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_ctors2.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
9797
9801
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_ctors2.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
24064
24070
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_except.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
10897
10895
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_except.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
28002
28008
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_except_wasm.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
9777
9774
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_except_wasm.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
23989
23995
Original file line number Diff line number Diff line change
@@ -1 +1 @@
9777
9774
Original file line number Diff line number Diff line change
@@ -1 +1 @@
23989
23995
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_mangle.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
10904
10900
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_mangle.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
28002
28008
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_noexcept.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
9813
9817
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_noexcept.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
24096
24102
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_wasmfs.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
11922
11930
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_files_js_fs.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7774
7766
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_files_js_fs.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
19120
19126
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_files_wasmfs.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3095
3088
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_files_wasmfs.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6672
6680
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_hello_O0.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8122
8111
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_hello_O0.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
21887
21927
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_hello_O1.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2938
2937
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_hello_O1.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7529
7568
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_hello_O2.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2594
2589
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_hello_O2.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5362
5370
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_hello_O3.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2509
2505
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_hello_O3.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5208
5216
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_hello_Os.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2509
2505
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_hello_Os.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5208
5216
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_hello_Oz.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2495
2487
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_hello_Oz.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5175
5183
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_hello_dylink.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6405
6409
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_hello_dylink.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
14186
14194
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1862
1858
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3999
4008
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_hello_wasmfs.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2509
2505
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_hello_wasmfs.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5208
5216
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2074
2071
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4433
4442
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2113
2106
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4480
4489
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_mem_O3.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2541
2537
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_mem_O3.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5352
5359
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_mem_O3_grow.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2687
2685
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_mem_O3_grow.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5649
5657
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2379
2374
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5038
5047
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_mem_O3_standalone.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2346
2344
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_mem_O3_standalone.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4969
4977
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2096
2092
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4484
4493
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2113
2106
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4480
4489
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2113
2106
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4480
4489
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_minimal_64.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1627
1608
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_minimal_64.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3419
3384
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_minimal_O0.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6706
6678
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_minimal_O0.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
17995
18019
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_minimal_O1.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1728
1705
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_minimal_O1.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4100
4130
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_minimal_O2.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1559
1540
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_minimal_O2.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3124
3089
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_minimal_O3.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1521
1504
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_minimal_O3.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3074
3039
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_minimal_Os.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1521
1504
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_minimal_Os.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3074
3039
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_minimal_Oz-ctors.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1514
1496
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_minimal_Oz-ctors.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3059
3024
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_minimal_Oz.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1521
1504
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_minimal_Oz.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3074
3039
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_minimal_pthreads.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5237
5223
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_minimal_pthreads.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
11387
11396
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_minimal_wasmfs.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1521
1504
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_minimal_wasmfs.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3074
3039
2 changes: 1 addition & 1 deletion test/other/test_unoptimized_code_size.js.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
55401
55440
2 changes: 1 addition & 1 deletion test/other/test_unoptimized_code_size_no_asserts.js.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
31301
31340
2 changes: 1 addition & 1 deletion test/other/test_unoptimized_code_size_strict.js.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
54264
54303
2 changes: 1 addition & 1 deletion test/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4331,7 +4331,7 @@ def test_small_js_flags(self):
print('size:', size)
# Note that this size includes test harness additions (for reporting the result, etc.).
if not self.is_wasm64() and not self.is_2gb():
self.assertLess(abs(size - 4510), 100)
self.assertLess(abs(size - 4477), 100)

# Tests that it is possible to initialize and render WebGL content in a
# pthread by using OffscreenCanvas.
Expand Down

0 comments on commit 67fa4c1

Please sign in to comment.