WebAssembly InstantiateStreaming Wrong MIME type

后端 未结 3 1513
不知归路
不知归路 2021-02-14 11:03

I am attempting to get this tutorial (here: https://www.hellorust.com/demos/add/index.html) to work, and it seems that whatever I do, I cannot get the WebAssembly MDN reserved f

相关标签:
3条回答
  • 2021-02-14 11:44

    there was some error; TypeError: "Response has unsupported MIME type" The Web server you are running does not understands/serves a MIME type application/wasm.

    You can use a rust based http server, it knows about wasm MIME type.

    Installation

    Simply use curl

    curl -SsL https://cdn.rawgit.com/thecoshman/http/master/install.sh | sh
    

    and execute the downloaded script or you can explorer other ways to do the same at https://crates.io/crates/https.


    Running

    Please use the downloaded server to server your Web Application(index.html). e.g

    cd ${YOUR_APPS_PATH}
    
    http
    
    0 讨论(0)
  • 2021-02-14 11:44

    A snippet of code for a workaround has been published on the WebAssembly Git here. Unfortunately, this is a workaround, and this defeats the purpose of instantiateStreaming() which is told here to be "a lot more efficient", since the workaround needs an ArrayBuffer that instantiateStreaming() helps to avoid.

    0 讨论(0)
  • 2021-02-14 11:52

    Considering you can't change the server to properly return application/wasm for .wasm file requests for any reason, you can work around the issue by changing the way you instantiate the WebAssembly module. Instead of doing this:

    WebAssembly.instantiateStreaming(fetch("./add.wasm")).then(obj => /* ... */)
    

    Do this:

    const response = await fetch("add.wasm");
    const buffer = await response.arrayBuffer();
    const obj = await WebAssembly.instantiate(buffer);
    obj.instance.exports.exported_func();
    

    Or the equivalent using then() if you cannot use async/await.

    In practice, what my workaround does is to avoid calling instantiateStreaming(), which must check the MIME type returned by the server before proceeding (according to this specification). Instead, I call instantiate() passing an ArrayBuffer and avoid the check altogether.

    0 讨论(0)
提交回复
热议问题