[Fixed] Problem serving up wasm

What I’m trying to do:
Use a js library that wraps a wasm module.

What I’ve tried and what’s not working:
I’ve added the wasm and its js wrapper to my theme assets with an import map in native libraries. However, when I try to call anything from within the wasm, I get:

`WebAssembly.instantiateStreaming` failed because your server does not serve wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:
 TypeError: WebAssembly: Response has unsupported MIME type '' expected 'application/wasm'

I’ve hit this locally when using the built in python http server and had to add the mime type for .wasm files to its config.

1 Like

This seems like a bug to me - moved to bug reports

1 Like

Does

Perhaps need to become:

 {"wasm" "application/wasm"} {"woff2" "application/font-woff2"})

almost, you’ll see the theme assets are served in server.clj,
and the mime-type is generated in this line:

There is an optional argument to this function which can take a map of additional mime-types.

Also a clojure map is like a python dict, but without any commas or colons so this is probably better :wink:
{"wasm" "application/wasm" "woff2" "application/font-woff2"})

There’s a fix in the queue and we’ll update this thread when it goes live.

1 Like

This is for something we’re hoping to use at EthDam this weekend so, if there’s any chance of having it by then, that would be much appreciated!

Noted.

As a workaround you could do something like:

import requests

@anvil.server.http_endpoint("/wasm/:path")
def get_wasm_file(path):
    request = requests.get(anvil.server.get_app_origin() + "/_/theme/" + path)
    headers = dict(request.headers)
    headers["Content-Type"] = "application/wasm"
    # we're not sending it gzipped so remove this header
    headers.pop('Content-Encoding', None)
    body = BlobMedia("application/wasm", request.content, path)
    response = anvil.server.HttpResponse(200, body, headers)
    return response

then adjust the import from "_/theme/foo.wasm" to "_/api/wasm/foo.wasm" as appropriate.


edit: removed the content-encoding from the headers
It would probably also work fine with just {"Content-Type": "application/wasm"} as the headers argument to anvil.server.HttpResponse

1 Like

Very nice!! Thank you kindly.

Thanks for the (very speedy) fix!

1 Like