Skip to content

Unable to load wasm in electron. #11671

Closed
@bcleveland1022

Description

@bcleveland1022

I was trying to run my app in electron and got the below warnings and errors during start up. The Strata-wasm.js is the javascript file generated by the em++ build using emscripten 1.39.19.

Strata-wasm.js:2212 failed to asynchronously prepare wasm: RuntimeError: abort(both async and sync fetching of the wasm failed) at Error
at jsStackTrace (file:///C:/Users/clevelandb/opensphere-yarn-workspace/workspace/opensphere/dist/opensphere/v1595010164/vendor/strata/Strata-wasm.js:3124:17)
at stackTrace (file:///C:/Users/clevelandb/opensphere-yarn-workspace/workspace/opensphere/dist/opensphere/v1595010164/vendor/strata/Strata-wasm.js:3141:16)
at abort (file:///C:/Users/clevelandb/opensphere-yarn-workspace/workspace/opensphere/dist/opensphere/v1595010164/vendor/strata/Strata-wasm.js:2064:44)
at getBinary (file:///C:/Users/clevelandb/opensphere-yarn-workspace/workspace/opensphere/dist/opensphere/v1595010164/vendor/strata/Strata-wasm.js:2144:5)
at file:///C:/Users/clevelandb/opensphere-yarn-workspace/workspace/opensphere/dist/opensphere/v1595010164/vendor/strata/Strata-wasm.js:2166:13
at new Promise (<anonymous>)
at getBinaryPromise (file:///C:/Users/clevelandb/opensphere-yarn-workspace/workspace/opensphere/dist/opensphere/v1595010164/vendor/strata/Strata-wasm.js:2165:10)
at instantiateArrayBuffer (file:///C:/Users/clevelandb/opensphere-yarn-workspace/workspace/opensphere/dist/opensphere/v1595010164/vendor/strata/Strata-wasm.js:2209:12)
at instantiateAsync (file:///C:/Users/clevelandb/opensphere-yarn-workspace/workspace/opensphere/dist/opensphere/v1595010164/vendor/strata/Strata-wasm.js:2238:14)
at createWasm (file:///C:/Users/clevelandb/opensphere-yarn-workspace/workspace/opensphere/dist/opensphere/v1595010164/vendor/strata/Strata-wasm.js:2254:3)


Uncaught (in promise) RuntimeError: abort(RuntimeError: abort(both async and sync fetching of the wasm failed) at Error
at jsStackTrace (file:///C:/Users/clevelandb/opensphere-yarn-workspace/workspace/opensphere/dist/opensphere/v1595010164/vendor/strata/Strata-wasm.js:3124:17)
at stackTrace (file:///C:/Users/clevelandb/opensphere-yarn-workspace/workspace/opensphere/dist/opensphere/v1595010164/vendor/strata/Strata-wasm.js:3141:16)
at abort (file:///C:/Users/clevelandb/opensphere-yarn-workspace/workspace/opensphere/dist/opensphere/v1595010164/vendor/strata/Strata-wasm.js:2064:44)
at getBinary (file:///C:/Users/clevelandb/opensphere-yarn-workspace/workspace/opensphere/dist/opensphere/v1595010164/vendor/strata/Strata-wasm.js:2144:5)
at file:///C:/Users/clevelandb/opensphere-yarn-workspace/workspace/opensphere/dist/opensphere/v1595010164/vendor/strata/Strata-wasm.js:2166:13
at new Promise (<anonymous>)
at getBinaryPromise (file:///C:/Users/clevelandb/opensphere-yarn-workspace/workspace/opensphere/dist/opensphere/v1595010164/vendor/strata/Strata-wasm.js:2165:10)
at instantiateArrayBuffer (file:///C:/Users/clevelandb/opensphere-yarn-workspace/workspace/opensphere/dist/opensphere/v1595010164/vendor/strata/Strata-wasm.js:2209:12)
at instantiateAsync (file:///C:/Users/clevelandb/opensphere-yarn-workspace/workspace/opensphere/dist/opensphere/v1595010164/vendor/strata/Strata-wasm.js:2238:14)
at createWasm (file:///C:/Users/clevelandb/opensphere-yarn-workspace/workspace/opensphere/dist/opensphere/v1595010164/vendor/strata/Strata-wasm.js:2254:3)) at Error
at jsStackTrace (file:///C:/Users/clevelandb/opensphere-yarn-workspace/workspace/opensphere/dist/opensphere/v1595010164/vendor/strata/Strata-wasm.js:3124:17)
at stackTrace (file:///C:/Users/clevelandb/opensphere-yarn-workspace/workspace/opensphere/dist/opensphere/v1595010164/vendor/strata/Strata-wasm.js:3141:16)
at abort (file:///C:/Users/clevelandb/opensphere-yarn-workspace/workspace/opensphere/dist/opensphere/v1595010164/vendor/strata/Strata-wasm.js:2064:44)
at file:///C:/Users/clevelandb/opensphere-yarn-workspace/workspace/opensphere/dist/opensphere/v1595010164/vendor/strata/Strata-wasm.js:2215:7
at abort (file:///C:/Users/clevelandb/opensphere-yarn-workspace/workspace/opensphere/dist/opensphere/v1595010164/vendor/strata/Strata-wasm.js:2070:9)
at file:///C:/Users/clevelandb/opensphere-yarn-workspace/workspace/opensphere/dist/opensphere/v1595010164/vendor/strata/Strata-wasm.js:2215:7

After some investigation I noticed it was failing due to the isFileURI check within the auto generated Strata-wasm.js file, because we were running in electron and the wasm file was a local file within the electron application.

function getBinaryPromise() {
   // If we don't have the binary yet, and have the Fetch api, use that;
  // in some environments, like Electron's render process, Fetch api may be present, but have a different context than expected, 
let's only use it on the Web
  if (!wasmBinary && (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER) && typeof fetch === 'function'
      // Let's not use fetch to get objects over file:// as it's most likely Cordova which doesn't support fetch for file://
      && !isFileURI(wasmBinaryFile)
      ) {
    return fetch(wasmBinaryFile, { credentials: 'same-origin' }).then(function(response) {
      if (!response['ok']) {
        throw "failed to load wasm binary file at '" + wasmBinaryFile + "'";
      }
      return response['arrayBuffer']();
    }).catch(function () {
      return getBinary();
    });
  }
  // Otherwise, getBinary should be able to get it synchronously
  return new Promise(function(resolve, reject) {
    resolve(getBinary());
  });
}

    // Prefer streaming instantiation if available.
  function instantiateAsync() {
    if (!wasmBinary &&
        typeof WebAssembly.instantiateStreaming === 'function' &&
        !isDataURI(wasmBinaryFile) &&
        // Don't use streaming for file:// delivered objects in a webview, fetch them synchronously.
        !isFileURI(wasmBinaryFile) &&
        typeof fetch === 'function') {
      fetch(wasmBinaryFile, { credentials: 'same-origin' }).then(function (response) {
        var result = WebAssembly.instantiateStreaming(response, info);
        return result.then(receiveInstantiatedSource, function(reason) {
            // We expect the most common failure cause to be a bad MIME type for the binary,
            // in which case falling back to ArrayBuffer instantiation should work.
            err('wasm streaming compile failed: ' + reason);
            err('falling back to ArrayBuffer instantiation');
            return instantiateArrayBuffer(receiveInstantiatedSource);
          });
      });
    } else {
      return instantiateArrayBuffer(receiveInstantiatedSource);
    }
  }
  // User shell pages can write their own Module.instantiateWasm = function(imports, successCallback) callback
  // to manually instantiate the Wasm module themselves. This allows pages to run the instantiation parallel
  // to any other async startup actions they are performing.
  if (Module['instantiateWasm']) {
    try {
      var exports = Module['instantiateWasm'](info, receiveInstance);
      return exports;
    } catch(e) {
      err('Module.instantiateWasm callback failed with error: ' + e);
      return false;
    }
  }
instantiateAsync();
  return {}; // no exports yet; we'll fill them in later
}

Currently our workaround for this issue is to modify the isFileURI to always return false then everything works as expected in electron.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions