satori icon indicating copy to clipboard operation
satori copied to clipboard

Cloudflare Workers WebAssembly error with 0.16+

Open vladshcherbin opened this issue 1 year ago • 13 comments

Bug report

Image

Description / Observed Behavior

Starting with 0.16 there's an error on Cloudflare Workers

Expected Behavior

Expect it to work same as before 0.16

Reproduction

Create any satori call on Cloudflare Workers, check dashboard logs for errors

Additional Context

Satori 0.16+, previous worked with no errors

Wrangler has nodejs_compat flag on:

"compatibility_date": "2025-07-22",
"compatibility_flags": ["nodejs_compat"]

vladshcherbin avatar Jul 23 '25 09:07 vladshcherbin

This is likely a bug/limitation in Cloudflare Workers. Do they have a way to allow this?

shuding avatar Jul 25 '25 19:07 shuding

This is likely a bug/limitation in Cloudflare Workers. Do they have a way to allow this?

No, We can't use WebAssembly.instantiate() in Cloudflare Workers.

Also, Nextjs docs says it doesn't support WebAssembly.instantiate() either in edge runtime.

https://nextjs.org/docs/app/api-reference/edge#unsupported-apis

kumardeo avatar Jul 27 '25 05:07 kumardeo

Forgive me for breaking Cloudflare Workers support. I am fixing that - https://github.com/shuding/yoga-wasm-web/pull/35

pavi2410 avatar Jul 31 '25 07:07 pavi2410

This seems to be working for me on both wrangler dev and wrangler dev --remote. Can you please check this?

import satori from 'satori'
-import initYoga from 'yoga-wasm-web'
-import yogaWasm from '../node_modules/yoga-wasm-web/dist/yoga.wasm'

-const yoga = await initYoga(yogaWasm)
-init(yoga)

// Satori should work now.

wrangler.toml

name = "playbadges"
main = "./src/index.ts"
compatibility_date = "2025-08-01"

pavi2410 avatar Aug 15 '25 16:08 pavi2410

I am still facing this issue in version 1.18.1 when using the following code:

import satori, { init } from 'satori/standalone';

export default {
  async fetch(req, env, ctx) {
    const res = await fetch('https://unpkg.com/satori/yoga.wasm');
    const yogaWasm = await res.arrayBuffer();

    await init(yogaWasm);

    const svg = await satori(
      <div
        style={{
          display: 'flex',
          width: '100%',
          height: '100%',
          alignItems: 'center',
          justifyContent: 'center',
        }}
      >
        Hello
      </div>,
      {
        width: 600,
        height: 600,
        fonts: [
          {
            data: await fetch(
              'https://github.com/JetBrains/JetBrainsMono/raw/cd5227bd1f61dff3bbd6c814ceaf7ffd95e947d9/fonts/ttf/JetBrainsMono-Regular.ttf',
            ).then((res) => res.arrayBuffer()),
            name: 'sans serif',
          },
        ],
      },
    );

    return new Response(svg, {
      headers: {
        'Content-Type': 'image/svg+xml',
      },
    });
  },
} satisfies ExportedHandler<Env>;

▲ [WARNING] failed to asynchronously prepare wasm: CompileError: WebAssembly.instantiate(): Wasm code generation disallowed by embedder


▲ [WARNING] Aborted(CompileError: WebAssembly.instantiate(): Wasm code generation disallowed by embedder)

This is probably because Cloudflare workers does not allow compiling and instantiating WebAssembly code through buffer sources.

Instead it gives WebAssembly.Module when wasm binary is imported using es module syntax

import wasmModule from "./path-to-yoga-wasm.wasm";
//             ^ this is WebAssembly.Module

So, there must be a way to pass this WebAssembly.Module of yoga wasm to initialize yoga.

Before 0.16 we were using the following:

import satori, { init } from 'satori/wasm';
import initYoga from 'yoga-wasm-web';
import yogaWasmModule from './yoga.wasm';

const yoga = await initYoga(yogaWasmModule); // initYoga() accepts both BufferSource and WebAssembly.Module
await init(yoga);

// now we can use satori
await satori(...);

So, I expect that init from satori/standalone accepts WebAssembly.Module also so that usage of WebAssembly.instantiate() with buffer source is prevented.

import satori, { init } from 'satori/standalone';
import yogaWasmModule from 'satori/yoga.wasm';

await init(yogaWasmModule); // should accept WebAssembly.Module as well and not only BufferSource

kumardeo avatar Aug 30 '25 07:08 kumardeo

You should be able to download the wasm file locally from https://unpkg.com/satori/yoga.wasm and import it. I will test it myself later.

pavi2410 avatar Aug 30 '25 07:08 pavi2410

You should be able to download the wasm file locally from https://unpkg.com/satori/yoga.wasm and import it. I will test it myself later.

The signature for the init function exported in satori/standalone is as follows:

declare function init(wasm: ArrayBuffer | Buffer): Promise<void>;

It expects ArrayBuffer | Buffer and later it is passed to WebAssembly.instantiate() call which is disallowed in Cloudflare Workers.

kumardeo avatar Aug 30 '25 20:08 kumardeo

I expect that init from satori/standalone accepts WebAssembly.Module

Good point! @huozhi ran into it too. Will fix!

shuding avatar Sep 02 '25 08:09 shuding

Even with the update of https://github.com/vercel/satori/pull/709, I can't figure out how to pass the WebAssembly.Module available in Cloudflare Workers environment into init() from satori/standalone, nor can I figure out how to instantiate it into a WebAssembly.Instance since that seems to need special functionality in the patched version of yoga-layout inside satori. Though this kind of seems like the fault of yoga-layout for bundling their code in a way that is fundamentally incompatible with Cloudflare Workers

Also I think the yoga.wasm inside [email protected] may be corrupted? This code reproduces an error in Browser and Node.js:

await WebAssembly.compileStreaming(fetch('https://unpkg.com/[email protected]/yoga.wasm'));
// Uncaught: CompileError: WebAssembly.compileStreaming(): section (code 1, "Type") extends past end of the module (length 7299055, remaining bytes 81343) @+8

And same error message when I do import yogaModule from "./yoga.wasm" inside Cloudflare Workers.

patdx avatar Oct 05 '25 05:10 patdx

Any update on this? I am also experiencing the above issue.

kumardeo avatar Nov 16 '25 13:11 kumardeo

Thank you Kumardeo, I look forward to your PR being merged

cendyne avatar Dec 31 '25 21:12 cendyne

this has broken a ton of stuff moving to workers in CF, really hope this gets fixed

acidjazz avatar Feb 05 '26 01:02 acidjazz

:tada: This issue has been resolved in version 0.19.2 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

github-actions[bot] avatar Feb 10 '26 14:02 github-actions[bot]