Diffinery

homeflare/celldpublic

celld/docs/wasm.md
96 lines · 3.1 KBRaw History
1# WebAssembly
2
3A Worker bundle can import a `.wasm` file. As in Wrangler, the import gives
4the compiled module, not the bytes.
5
6```js
7import addModule from "./add.wasm";
8
9const { exports } = new WebAssembly.Instance(addModule);
10
11export default {
12 fetch() {
13 return new Response(String(exports.add(2, 3)));
14 },
15};
16```
17
18`celld deploy` uploads each imported wasm file beside the bundle and marks the
19deployment with the `wasm-v1` feature. A node that predates this feature
20refuses the deployment, so a mixed fleet fails at deploy time, not at request
21time.
22
23celld compiles each wasm module once per process, so later isolates and cell
24activations reuse it.
25
26## Example
27
28The [WebAssembly example](../examples/wasm) compiles a Rust Durable Object and
29imports its WebAssembly module.
30
31<!-- celld-example: wasm -->
32
33## Prebuilt Workers
34
35With `no_bundle: true`, celld preserves the entry JavaScript byte for byte. It
36applies Wrangler's default `**/*.wasm` and `**/*.wasm?module` patterns below
37the directory that contains `main`. For example, `main: "./dist/shim.mjs"` can
38import `"./add.wasm"` or `"./lib/add.wasm"` from `dist`. The module names keep
39these relative paths. This mode does not require esbuild.
40
41The scan uploads WASM files that the JavaScript does not import, so use a
42dedicated build output directory. It skips `.git`, `.celld`, `.wrangler`, and
43symbolic links to directories. It refuses a symbolic link whose name matches a
44WASM pattern, so copy the file into the build output instead.
45
46celld does not implement the other
47[Wrangler module discovery settings](https://developers.cloudflare.com/workers/wrangler/configuration/#find-additional-modules).
48It does not discover additional JavaScript modules and does not accept
49`rules`, `base_dir`, or `find_additional_modules`. The JavaScript must already
50be bundled, and its WASM imports must be relative to the entry directory.
51
52## Rust with workers-rs
53
54[workers-rs](https://github.com/cloudflare/workers-rs) builds a JavaScript shim
55and a wasm file, and the shim is a normal entry point for `celld deploy`.
56
571. Install the build tool: `cargo install worker-build`.
582. Build the crate: `worker-build --release`.
593. Point the config at the shim:
60
61```jsonc
62{
63 "name": "my-app",
64 "main": "./build/worker/shim.mjs",
65 "compatibility_date": "2026-01-01",
66}
67```
68
694. Deploy: `celld deploy`.
70
71celld resolves entrypoint and Durable Object classes through the shim's Proxy
72wrapper. A workers-rs API that needs a runtime feature missing from
73[Cloudflare compatibility](cloudflare-compat.md) does not work.
74
75## Dynamic Workers
76
77Pass wasm to a dynamically loaded worker as `{ wasm: bytes }` in the `modules`
78map, and the worker imports a compiled module. celld refuses bare bytes, as
79workerd does.
80
81```js
82const worker = env.loader.load({
83 compatibilityDate: "2025-01-01",
84 mainModule: "main.js",
85 modules: {
86 "main.js": `import m from "./add.wasm"; ...`,
87 "add.wasm": { wasm: wasmBytes },
88 },
89});
90```
91
92## Limits
93
94Wasm bytes count against the deployment size limits like JavaScript modules. A
95module that does not compile fails the importing module with a
96`WebAssembly.CompileError` that names the file.