Re-factor the fetchData helper function, in src/display/display_utils.js to be asynchronous

This commit is contained in:
Jonas Jenwald 2021-06-11 17:12:55 +02:00
parent b05a22d01b
commit 7b17dc8bfd

View File

@ -46,23 +46,18 @@ class DOMCanvasFactory extends BaseCanvasFactory {
} }
} }
function fetchData(url, asTypedArray) { async function fetchData(url, asTypedArray = false) {
if ( if (
(typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) || (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) ||
(isFetchSupported() && isValidFetchUrl(url, document.baseURI)) (isFetchSupported() && isValidFetchUrl(url, document.baseURI))
) { ) {
return fetch(url).then(async response => { const response = await fetch(url);
if (!response.ok) { if (!response.ok) {
throw new Error(response.statusText); throw new Error(response.statusText);
} }
let data; return asTypedArray
if (asTypedArray) { ? new Uint8Array(await response.arrayBuffer())
data = new Uint8Array(await response.arrayBuffer()); : stringToBytes(await response.text());
} else {
data = stringToBytes(await response.text());
}
return data;
});
} }
// The Fetch API is not supported. // The Fetch API is not supported.