|
| 1 | +/** |
| 2 | + * Reject on timeout |
| 3 | + * @param maxTimeoutMS |
| 4 | + * @param reject |
| 5 | + * @returns {number} timerID |
| 6 | + */ |
| 7 | +function rejectOnTimeout(maxTimeoutMS, reject) { |
| 8 | + return setTimeout(() => { |
| 9 | + reject({ |
| 10 | + status: 'error', |
| 11 | + message: 'Timeout loading Blob URL' |
| 12 | + }); |
| 13 | + }, maxTimeoutMS); |
| 14 | +} |
| 15 | + |
1 | 16 | /** |
2 | 17 | * @description Converts a URL to a BLOB URL |
3 | 18 | * @param {string} urlToLoad |
|
10 | 25 | * } |
11 | 26 | * }>} |
12 | 27 | */ |
13 | | -function getBlobFromURL(urlToLoad, max_timeout_ms) { |
| 28 | +function getBlobFromURL(urlToLoad, maxTimeoutMS) { |
14 | 29 | return new Promise((resolve, reject) => { |
15 | | - let timerID = setTimeout(() => { |
16 | | - reject({ |
17 | | - status: 'error', |
18 | | - message: 'Timeout loading Blob URL' |
19 | | - }); |
20 | | - }, max_timeout_ms); |
| 30 | + const timerID = rejectOnTimeout(maxTimeoutMS, reject); |
21 | 31 |
|
22 | | - let xhr = new XMLHttpRequest(); |
23 | | - xhr.responseType = 'blob'; |
24 | | - xhr.onload = function (response) { |
25 | | - clearTimeout(timerID); // clear timeout reject error |
| 32 | + // If fetch exists, use it to fetch blob, otherwise use XHR. |
| 33 | + // XHR causes issues on safari 14.1 so we prefer fetch |
| 34 | + const fetchBlob = (typeof fetch !== 'undefined' && fetch) ? loadUrlUsingFetch : loadUrlUsingXhr; |
| 35 | + |
| 36 | + fetchBlob(urlToLoad).then((blob) => { |
26 | 37 | resolve({ |
27 | 38 | status: 'success', |
28 | 39 | payload: { |
29 | | - blobURL: URL.createObjectURL(xhr.response) |
| 40 | + blobURL: URL.createObjectURL(blob) |
30 | 41 | } |
31 | 42 | }); |
32 | | - }; |
33 | | - |
34 | | - xhr.onerror = function () { |
35 | | - clearTimeout(timerID); // clear timeout reject error |
| 43 | + }).catch(() => { |
36 | 44 | reject({ |
37 | 45 | status: 'error', |
38 | 46 | message: 'Error loading Blob URL' |
39 | 47 | }); |
| 48 | + }).finally(() => { |
| 49 | + // Clear the timeout timer on fail or success. |
| 50 | + clearTimeout(timerID); |
| 51 | + }); |
| 52 | + }); |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * Use fetch function to fetch file |
| 57 | + * @param urlToLoad |
| 58 | + * @returns {Promise<unknown>} |
| 59 | + */ |
| 60 | +function loadUrlUsingFetch(urlToLoad) { |
| 61 | + return new Promise((resolve, reject) => { |
| 62 | + fetch(urlToLoad).then((response) => { |
| 63 | + response.blob().then((blob) => { |
| 64 | + resolve(blob); |
| 65 | + }); |
| 66 | + }).catch(() => { |
| 67 | + reject('error'); |
| 68 | + }); |
| 69 | + }); |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * Use XHR to fetch file |
| 74 | + * @param urlToLoad |
| 75 | + * @returns {Promise<unknown>} |
| 76 | + */ |
| 77 | +function loadUrlUsingXhr(urlToLoad) { |
| 78 | + return new Promise((resolve, reject) => { |
| 79 | + const xhr = new XMLHttpRequest(); |
| 80 | + xhr.responseType = 'blob'; |
| 81 | + xhr.onload = function (response) { |
| 82 | + resolve(xhr.response); |
40 | 83 | }; |
| 84 | + |
| 85 | + xhr.onerror = function () { |
| 86 | + reject('error'); |
| 87 | + }; |
| 88 | + |
41 | 89 | xhr.open('GET', urlToLoad, true); |
42 | 90 | xhr.send(); |
43 | 91 | }); |
|
0 commit comments