From 7398edb787aba8e229e00a81dbcadb519f5b0baf Mon Sep 17 00:00:00 2001 From: jcaesar Date: Tue, 31 Mar 2020 02:28:34 +0900 Subject: [PATCH] Fall back to WebAssembly.compile on mime type error (#628) --- src/pyodide.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/pyodide.js b/src/pyodide.js index ecde23db1..80e2a9b3b 100644 --- a/src/pyodide.js +++ b/src/pyodide.js @@ -331,13 +331,22 @@ var languagePluginLoader = new Promise((resolve, reject) => { Module.preloadedWasm = {}; let isFirefox = navigator.userAgent.toLowerCase().indexOf('firefox') > -1; - let wasm_promise; + let wasm_promise, wasm_fetch = fetch(wasmURL); + const compileBuffer = () => + wasm_fetch.then(response => response.arrayBuffer()) + .then(bytes => WebAssembly.compile(bytes)); if (WebAssembly.compileStreaming === undefined) { - wasm_promise = fetch(wasmURL) - .then(response => response.arrayBuffer()) - .then(bytes => WebAssembly.compile(bytes)); + wasm_promise = compileBuffer(); } else { - wasm_promise = WebAssembly.compileStreaming(fetch(wasmURL)); + wasm_promise = WebAssembly.compileStreaming(wasm_fetch); + wasm_promise = wasm_promise.catch(e => { + if (e instanceof TypeError) { + console.error("pyodide streaming compilation failed:", e, + "- falling back to buffered compilation"); + return compileBuffer() + } + throw e; + }); } Module.instantiateWasm = (info, receiveInstance) => {