| 1 | # WebAssembly |
| 2 | |
| 3 | A Worker bundle can import a `.wasm` file. The import gives the |
| 4 | compiled module, not the bytes. This is the same rule that Wrangler |
| 5 | applies, so a bundle that runs on Cloudflare runs on celld without a |
| 6 | change. |
| 7 | |
| 8 | ```js |
| 9 | import addModule from "./add.wasm"; |
| 10 | |
| 11 | const { exports } = new WebAssembly.Instance(addModule); |
| 12 | |
| 13 | export default { |
| 14 | fetch() { |
| 15 | return new Response(String(exports.add(2, 3))); |
| 16 | }, |
| 17 | }; |
| 18 | ``` |
| 19 | |
| 20 | `celld deploy` finds each wasm import, uploads the file beside the |
| 21 | bundle, and marks the deployment with the `wasm-v1` feature. A node |
| 22 | that predates this feature refuses the deployment with a clear |
| 23 | message, so a mixed fleet fails at deploy time and not at request |
| 24 | time. |
| 25 | |
| 26 | celld compiles each wasm module once for the whole process. Every |
| 27 | isolate after the first one reuses the compiled module, so a cell |
| 28 | activation does not pay the compilation again. |
| 29 | |
| 30 | ## Rust with workers-rs |
| 31 | |
| 32 | [workers-rs](https://github.com/cloudflare/workers-rs) compiles a Rust |
| 33 | crate to a Worker. The tool `worker-build` produces a JavaScript shim |
| 34 | and a wasm file, and the shim is a normal entry point for |
| 35 | `celld deploy`. |
| 36 | |
| 37 | 1. Install the build tool: `cargo install worker-build`. |
| 38 | 2. Build the crate: `worker-build --release`. |
| 39 | 3. Point the config at the shim: |
| 40 | |
| 41 | ```jsonc |
| 42 | { |
| 43 | "name": "my-app", |
| 44 | "main": "./build/worker/shim.mjs", |
| 45 | "compatibility_date": "2026-01-01", |
| 46 | } |
| 47 | ``` |
| 48 | |
| 49 | 4. Deploy: `celld deploy`. |
| 50 | |
| 51 | The shim wraps its exports in a JavaScript Proxy, and celld resolves |
| 52 | entrypoint classes and Durable Object classes through that wrapper. |
| 53 | The runtime surface that the application can use is the surface in |
| 54 | [Cloudflare compatibility](cloudflare-compat.md); a workers-rs API |
| 55 | that maps to a missing runtime feature does not work. |
| 56 | |
| 57 | ## Dynamic Workers |
| 58 | |
| 59 | A dynamically loaded worker can also carry wasm. Pass the bytes in the |
| 60 | `modules` map; a `BufferSource` value becomes a compiled-module import |
| 61 | in the loaded worker. |
| 62 | |
| 63 | ```js |
| 64 | const worker = env.loader.load({ |
| 65 | mainModule: "main.js", |
| 66 | modules: { |
| 67 | "main.js": `import m from "./add.wasm"; ...`, |
| 68 | "add.wasm": wasmBytes, |
| 69 | }, |
| 70 | }); |
| 71 | ``` |
| 72 | |
| 73 | ## Limits |
| 74 | |
| 75 | The wasm bytes count against the deployment size limits, exactly as |
| 76 | JavaScript modules do. A wasm module that does not compile fails the |
| 77 | importing module with a `WebAssembly.CompileError` that names the |
| 78 | file. |