Cloudflare DevelopersCD
Cloudflare Developers3y ago
4 replies
Tin Cap

Invalid URL when initiating AssemblyScript generated WASM

I'm writing code in AssemblyScript and compiling it to WASM. AssemblyScript also generates JS bindings that work great in Node, but I'm having trouble getting them working in Cloudflare Workers.

In the generated JS bindings, there is code like this this initiates the WebAssembly runtime:
export const {
  memory,
  your_function,
} = await (async url => instantiate(
  await (async () => {
    try { return await globalThis.WebAssembly.compileStreaming(globalThis.fetch(url)); }
    catch { return globalThis.WebAssembly.compile(await (await import("node:fs/promises")).readFile(url)); }
  })(), {
  }
))(new URL("release.wasm", import.meta.url));


Inside my worker, that relative URL does not play nicely:

new URL("release.wasm", import.meta.url)

generates this error:

TypeError: Invalid URL
    at new NodeError (node:internal/errors:399:5)
    at new URL (node:internal/url:560:13)


This works in Node in general. Is there a difference in Cloudflare that makes this not work?
Was this page helpful?