edge-runtime icon indicating copy to clipboard operation
edge-runtime copied to clipboard

"Using Wasm modules" edge function documentation code does not work

Open lewinpauli opened this issue 2 months ago • 9 comments

Improve documentation

Link

https://supabase.com/docs/guides/functions/wasm

Describe the problem

when I follow the exact documentation the edgefunction with wasm module throws the following error:

Error occurs locally as well as deployed to supabase cloud

supabase functions serve wasm-add                                                  
WARN: no SMS provider is enabled. Disabling phone login
Setting up Edge Functions runtime...
2025-10-27T18:56:53.703493794Z Serving functions on http://127.0.0.1:54321/functions/v1/<function-name>
2025-10-27T18:56:53.703586127Z  - http://127.0.0.1:54321/functions/v1/wasm-add
2025-10-27T18:56:53.703590211Z Using supabase-edge-runtime-1.69.14 (compatible with Deno v2.1.4)
2025-10-27T18:57:06.033104008Z serving the request with supabase/functions/wasm-add
2025-10-27T18:57:06.078594800Z runtime has escaped from the event loop unexpectedly: event loop error: TypeError: NetworkError when attempting to fetch resource
2025-10-27T18:57:06.078617550Z     at opFetchSend (ext:deno_fetch/26_fetch.js:92:10)
2025-10-27T18:57:06.078620675Z     at mainFetch (ext:deno_fetch/26_fetch.js:191:18)
2025-10-27T18:57:06.078624508Z     at ext:deno_fetch/26_fetch.js:419:11
2025-10-27T18:57:06.078625758Z     at new Promise (<anonymous>)
2025-10-27T18:57:06.078627466Z     at fetch (ext:deno_fetch/26_fetch.js:369:20)
2025-10-27T18:57:06.078628758Z     at file:///var/tmp/sb-compile-edge-runtime/wasm-add/add-wasm/pkg/add_wasm.js:28:54
2025-10-27T18:57:06.078754091Z failed to send request to user worker: event loop error: TypeError: NetworkError when attempting to fetch resource
2025-10-27T18:57:06.078787841Z     at opFetchSend (ext:deno_fetch/26_fetch.js:92:10)
2025-10-27T18:57:06.078791133Z     at mainFetch (ext:deno_fetch/26_fetch.js:191:18)
2025-10-27T18:57:06.078793675Z     at ext:deno_fetch/26_fetch.js:419:11
2025-10-27T18:57:06.078795716Z     at new Promise (<anonymous>)
2025-10-27T18:57:06.078798425Z     at fetch (ext:deno_fetch/26_fetch.js:369:20)
2025-10-27T18:57:06.078800341Z     at file:///var/tmp/sb-compile-edge-runtime/wasm-add/add-wasm/pkg/add_wasm.js:28:54
2025-10-27T18:57:06.078967175Z user worker failed to respond: event loop error: TypeError: NetworkError when attempting to fetch resource
2025-10-27T18:57:06.078979175Z     at opFetchSend (ext:deno_fetch/26_fetch.js:92:10)
2025-10-27T18:57:06.078980800Z     at mainFetch (ext:deno_fetch/26_fetch.js:191:18)
2025-10-27T18:57:06.078982133Z     at ext:deno_fetch/26_fetch.js:419:11
2025-10-27T18:57:06.078983383Z     at new Promise (<anonymous>)
2025-10-27T18:57:06.078984883Z     at fetch (ext:deno_fetch/26_fetch.js:369:20)
2025-10-27T18:57:06.078986175Z     at file:///var/tmp/sb-compile-edge-runtime/wasm-add/add-wasm/pkg/add_wasm.js:28:54
2025-10-27T18:57:06.082367425Z InvalidWorkerResponse: event loop error: TypeError: NetworkError when attempting to fetch resource
2025-10-27T18:57:06.082399716Z     at opFetchSend (ext:deno_fetch/26_fetch.js:92:10)
2025-10-27T18:57:06.082401925Z     at mainFetch (ext:deno_fetch/26_fetch.js:191:18)
2025-10-27T18:57:06.082403133Z     at ext:deno_fetch/26_fetch.js:419:11
2025-10-27T18:57:06.082404300Z     at new Promise (<anonymous>)
2025-10-27T18:57:06.082405591Z     at fetch (ext:deno_fetch/26_fetch.js:369:20)
2025-10-27T18:57:06.082406800Z     at file:///var/tmp/sb-compile-edge-runtime/wasm-add/add-wasm/pkg/add_wasm.js:28:54
2025-10-27T18:57:06.082408008Z     at async Function.allSettled (<anonymous>)
2025-10-27T18:57:06.082409216Z     at async UserWorker.fetch (ext:user_workers/user_workers.js:84:63)
2025-10-27T18:57:06.082410425Z     at async Object.handler (file:///var/tmp/sb-compile-edge-runtime/root/index.ts:176:14)
2025-10-27T18:57:06.082411633Z     at async mapped (ext:runtime/http.js:231:18) {
2025-10-27T18:57:06.082412800Z   name: "InvalidWorkerResponse"
2025-10-27T18:57:06.082414133Z }

Describe the improvement

please provide a documentation with code that works

in the documentation it says "You will need update Supabase CLI to 2.7.0 or higher for the static_files support."

but the currently highest supabase cli version available is 2.53.6

it would also be nice to mention that the "wasm-pack" need to be installed first and that the user needs to go into add-wasm folder first before executing the "wasm-pack build --target deno" command

lewinpauli avatar Oct 27 '25 19:10 lewinpauli

This seems related to https://github.com/denoland/deno/issues/28129, exposed due to changes made here: https://github.com/wasm-bindgen/wasm-bindgen/commit/77c3fe7413460ba6b9009ecf207247c5f3828e9c#diff-9053f2d612dff76d9d1e67267e89e2cc95fba8b7791a6b1b47c8380a8c703e6f

Downgrading your cargo dependency to [email protected] is a workaround for now: wasm-bindgen = "=0.2.103"

seurimas avatar Nov 02 '25 18:11 seurimas

Hi guys, Maybe this one should be moved to edge-runtime repo?

cc: @Hallidayo

kallebysantos avatar Nov 07 '25 15:11 kallebysantos

@kallebysantos ahh yes, moved across from the supabase repo

Hallidayo avatar Nov 07 '25 15:11 Hallidayo

The problem is inside the generated add_wasm.js file. Like @seurimas mentioned, seems that importing a .wasm file using fetch() is not working.

But if you read the file and pass it as Response object directly it does:

// functions/wasm-add/add-wasm/pkg/add_wasm.js

const wasmUrl = new URL('add_wasm_bg.wasm', import.meta.url);

+const wasmBytes = await Deno.readFile(wasmUrl);
+const wasmResponse = new Response(wasmBytes, {
+  headers: [["Content-Type", "application/wasm"]],
+});

- const wasm = (await WebAssembly.instantiateStreaming(fetch(wasmUrl), imports)).instance.exports;
+ const wasm =  (await WebAssembly.instantiateStreaming(wasmResponse, imports)).instance.exports;

export { wasm as __wasm };

wasm.__wbindgen_start();

kallebysantos avatar Nov 07 '25 15:11 kallebysantos

Hey @nyannyacha 💚

Since https://github.com/supabase/edge-runtime/pull/519 is now possible to import the .wasm file directly? Could you teach us how to generate a compatible .wasm file? the one from this docs fails while importing like that.

Trying to import the docs example wasm file directly
import { add } from "./add-wasm/pkg/add_wasm_bg.wasm";

Deno.serve(async (req) => {
  const { a, b } = await req.json();

  return Response.json({ result: add(a, b) });
});
2025-11-07T16:01:04.974444333Z  - http://127.0.0.1:54321/functions/v1/wasm-add
2025-11-07T16:01:04.974595457Z Using supabase-edge-runtime-1.69.15 (compatible with Deno v2.1.4)
2025-11-07T16:01:08.571340880Z serving the request with supabase/functions/wasm-add
2025-11-07T16:01:08.615804300Z worker boot error: Relative import path "__wbindgen_placeholder__" not prefixed with / or ./ or ../ and not in import map from "file:///var/tmp/sb-compile-edge-runtime/wasm-add/add-wasm/pkg/add_wasm_bg.wasm"
2025-11-07T16:01:08.615866508Z worker boot error: Relative import path "__wbindgen_placeholder__" not prefixed with / or ./ or ../ and not in import map from "file:///var/tmp/sb-compile-edge-runtime/wasm-add/add-wasm/pkg/add_wasm_bg.wasm"
2025-11-07T16:01:08.618081540Z InvalidWorkerCreation: worker boot error: Relative import path "__wbindgen_placeholder__" not prefixed with / or ./ or ../ and not in import map from "file:///var/tmp/sb-compile-edge-runtime/wasm-add/add-wasm/pkg/add_wasm_bg.wasm"
2025-11-07T16:01:08.618091415Z     at async Function.create (ext:user_workers/user_workers.js:155:29)
2025-11-07T16:01:08.618093456Z     at async Object.handler (file:///var/tmp/sb-compile-edge-runtime/root/index.ts:158:22)
2025-11-07T16:01:08.618095165Z     at async mapped (ext:runtime/http.js:231:18) {
2025-11-07T16:01:08.618097081Z   name: "InvalidWorkerCreation"
2025-11-07T16:01:08.618099165Z }

kallebysantos avatar Nov 07 '25 16:11 kallebysantos

Hello @kallebysantos 😁 Most bundles generated by wasm-bindgen include polyfills that provide access to the JS interface, making them unsuitable for straight import via import { x } from "./blah.wasm".

The WASM file used in that PR is a very pure wasm binary compiled directly from WAT, not generated by wasm-bindgen. Please note that the WASM file originated from https://docs.deno.com/runtime/reference/wasm/. Hope this helps.

FYI, looking at the original issue, it seems like a bug introduced by changes in wasm-bindgen. Should we close this?

nyannyacha avatar Nov 10 '25 01:11 nyannyacha

I believe you could update it to make this point clearer in the doc.

Downgrading your cargo dependency to [email protected] is a workaround for now: wasm-bindgen = "=0.2.103"

nyannyacha avatar Nov 10 '25 01:11 nyannyacha

Hi @nyannyacha 💚

Yes, seems they did change the js template to use this fetch style instead of file loading, like the workarround I'd paste.

Its ok for me, to close this issue. I'll probably open one inside wasm-bindgen repo, reporting this problem.

kallebysantos avatar Nov 10 '25 01:11 kallebysantos

@kallebysantos Well, I think it'd be better to leave it open for visibility for others for now. If you update the doc and report this issue upstream, it should be fine to close it then.

Have a good night!

nyannyacha avatar Nov 10 '25 01:11 nyannyacha