|
21 | 21 | };
|
22 | 22 | }
|
23 | 23 |
|
24 |
| - readAsync = (url, onload, onerror) => { |
| 24 | + readAsync = (url) => { |
25 | 25 | #if ENVIRONMENT_MAY_BE_WEBVIEW
|
26 | 26 | // Fetch has some additional restrictions over XHR, like it can't be used on a file:// url.
|
27 | 27 | // See https://github.com/github/fetch/pull/92#issuecomment-140665932
|
28 | 28 | // Cordova or Electron apps are typically loaded from a file:// url.
|
29 | 29 | // So use XHR on webview if URL is a file URL.
|
30 | 30 | if (isFileURI(url)) {
|
31 |
| - var xhr = new XMLHttpRequest(); |
32 |
| - xhr.open('GET', url, true); |
33 |
| - xhr.responseType = 'arraybuffer'; |
34 |
| - xhr.onload = () => { |
35 |
| - if (xhr.status == 200 || (xhr.status == 0 && xhr.response)) { // file URLs can return 0 |
36 |
| - onload(xhr.response); |
37 |
| - return; |
38 |
| - } |
39 |
| - onerror(); |
40 |
| - }; |
41 |
| - xhr.onerror = onerror; |
42 |
| - xhr.send(null); |
43 |
| - return; |
| 31 | + return new Promise((reject, resolve) => { |
| 32 | + var xhr = new XMLHttpRequest(); |
| 33 | + xhr.open('GET', url, true); |
| 34 | + xhr.responseType = 'arraybuffer'; |
| 35 | + xhr.onload = () => { |
| 36 | + if (xhr.status == 200 || (xhr.status == 0 && xhr.response)) { // file URLs can return 0 |
| 37 | + resolve(xhr.response); |
| 38 | + } |
| 39 | + reject(xhr.status); |
| 40 | + }; |
| 41 | + xhr.onerror = reject; |
| 42 | + xhr.send(null); |
| 43 | + }); |
44 | 44 | }
|
45 | 45 | #elif ASSERTIONS
|
46 | 46 | assert(!isFileURI(url), "readAsync does not work with file:// URLs");
|
47 | 47 | #endif
|
48 |
| - fetch(url, {{{ makeModuleReceiveExpr('fetchSettings', "{ credentials: 'same-origin' }") }}}) |
49 |
| - .then((response) => { |
50 |
| - if (response.ok) { |
51 |
| - return response.arrayBuffer(); |
52 |
| - } |
53 |
| - return Promise.reject(new Error(response.status + ' : ' + response.url)); |
54 |
| - }) |
55 |
| - .then(onload, onerror) |
| 48 | + return fetch(url, {{{ makeModuleReceiveExpr('fetchSettings', "{ credentials: 'same-origin' }") }}}) |
| 49 | + .then((response) => { |
| 50 | + if (response.ok) { |
| 51 | + return response.arrayBuffer(); |
| 52 | + } |
| 53 | + return Promise.reject(new Error(response.status + ' : ' + response.url)); |
| 54 | + }) |
56 | 55 | };
|
0 commit comments