|
|
@@ -9,7 +9,23 @@
|
|
9
|
9
|
* @returns {void}
|
|
10
|
10
|
*/
|
|
11
|
11
|
export function loadScript(url) {
|
|
12
|
|
- return (
|
|
|
12
|
+ return new Promise((resolve, reject) => {
|
|
|
13
|
+ // XXX The implementation of fetch on Android will throw an Exception on
|
|
|
14
|
+ // the Java side which will break the app if the URL is invalid (which
|
|
|
15
|
+ // the implementation of fetch on Android calls 'unexpected url'). In
|
|
|
16
|
+ // order to try to prevent the breakage of the app, try to fail on an
|
|
|
17
|
+ // invalid URL as soon as possible.
|
|
|
18
|
+ const { hostname, pathname, protocol } = new URL(url);
|
|
|
19
|
+
|
|
|
20
|
+ // XXX The standard URL implementation should throw an Error if the
|
|
|
21
|
+ // specified URL is relative. Unfortunately, the polyfill used on
|
|
|
22
|
+ // react-native does not.
|
|
|
23
|
+ if (!hostname || !pathname || !protocol) {
|
|
|
24
|
+ reject(`unexpected url: ${url}`);
|
|
|
25
|
+
|
|
|
26
|
+ return;
|
|
|
27
|
+ }
|
|
|
28
|
+
|
|
13
|
29
|
fetch(url, { method: 'GET' })
|
|
14
|
30
|
.then(response => {
|
|
15
|
31
|
switch (response.status) {
|
|
|
@@ -22,5 +38,7 @@ export function loadScript(url) {
|
|
22
|
38
|
})
|
|
23
|
39
|
.then(responseText => {
|
|
24
|
40
|
eval.call(window, responseText); // eslint-disable-line no-eval
|
|
25
|
|
- }));
|
|
|
41
|
+ })
|
|
|
42
|
+ .then(resolve, reject);
|
|
|
43
|
+ });
|
|
26
|
44
|
}
|