From 1666b7a611ad1149ff4143b0d4a308eb55b5c549 Mon Sep 17 00:00:00 2001 From: Luciano Martorella Date: Wed, 13 Aug 2025 16:46:43 +0200 Subject: [PATCH] - Using instantiateStreaming when using URL in browser --- lib/read-wasm-browser.js | 2 +- lib/wasm.js | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/read-wasm-browser.js b/lib/read-wasm-browser.js index a4fb29c2..26e6c78e 100644 --- a/lib/read-wasm-browser.js +++ b/lib/read-wasm-browser.js @@ -4,7 +4,7 @@ let mappingsWasm = null; module.exports = function readWasm() { if (typeof mappingsWasm === "string") { - return fetch(mappingsWasm).then(response => response.arrayBuffer()); + return fetch(mappingsWasm); } if (mappingsWasm instanceof ArrayBuffer) { return Promise.resolve(mappingsWasm); diff --git a/lib/wasm.js b/lib/wasm.js index 3091d9ee..c29122f4 100644 --- a/lib/wasm.js +++ b/lib/wasm.js @@ -23,8 +23,8 @@ module.exports = function wasm() { const callbackStack = []; cachedWasm = readWasm() - .then(buffer => { - return WebAssembly.instantiate(buffer, { + .then(bufferOrResponse => { + const opts = { env: { mapping_callback( generatedLine, @@ -114,7 +114,18 @@ module.exports = function wasm() { console.timeEnd("sort_by_original_location"); }, }, - }); + }; + let source; + if (bufferOrResponse instanceof Response) { + if (WebAssembly.instantiateStreaming) { + source = WebAssembly.instantiateStreaming(bufferOrResponse, opts); + } else { + source = WebAssembly.instantiate(bufferOrResponse.arrayBuffer(), opts); + } + } else { + source = WebAssembly.instantiate(bufferOrResponse, opts); + } + return source; }) .then(Wasm => { return {