Diffinery

homeflare/celldpublic

celld/docs/wasm.md
108 lines · 3.7 KBRaw History
1# WebAssembly
2
3A Worker bundle can import a `.wasm` file. The import gives the
4compiled module, not the bytes. This is the same rule that Wrangler
5applies, so a bundle that runs on Cloudflare runs on celld without a
6change.
7
8```js
9import addModule from "./add.wasm";
10
11const { exports } = new WebAssembly.Instance(addModule);
12
13export 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
21bundle, and marks the deployment with the `wasm-v1` feature. A node
22that predates this feature refuses the deployment with a clear
23message, so a mixed fleet fails at deploy time and not at request
24time.
25
26celld compiles each wasm module once for the whole process. Every
27isolate after the first one reuses the compiled module, so a cell
28activation does not pay the compilation again.
29
30## Example
31
32The [WebAssembly example](../examples/wasm) compiles a Rust Durable Object and
33imports its WebAssembly module.
34
35<!-- celld-example: wasm -->
36
37## Prebuilt Workers
38
39With `no_bundle: true`, celld preserves the entry JavaScript byte for byte.
40It applies Wrangler's default `**/*.wasm` and `**/*.wasm?module` patterns below
41the directory that contains `main`. For example, `main: "./dist/shim.mjs"` can
42import `"./add.wasm"` or `"./lib/add.wasm"` from `dist`. The module names keep
43these relative paths. The files use the same WASM metadata and feature gate as
44a bundled deploy.
45
46The default scan includes WASM files that the JavaScript does not import. Use
47a dedicated build output directory, so celld does not upload unrelated WASM.
48The scan excludes `.git`, `.celld`, `.wrangler`, and symbolic links to
49directories. It refuses a symbolic link whose name matches a WASM pattern.
50Copy the WASM file into the build output instead of linking to it or importing
51it from a parent directory.
52
53This mode matches Wrangler's default WASM discovery, but it does not implement
54all [Wrangler module discovery settings](https://developers.cloudflare.com/workers/wrangler/configuration/#find-additional-modules).
55celld does not discover additional JavaScript modules and does not accept
56`rules`, `base_dir`, or `find_additional_modules`. The JavaScript must already
57be bundled, and its WASM imports must be relative to the entry directory. This
58mode does not require an esbuild executable.
59
60## Rust with workers-rs
61
62[workers-rs](https://github.com/cloudflare/workers-rs) compiles a Rust
63crate to a Worker. The tool `worker-build` produces a JavaScript shim
64and a wasm file, and the shim is a normal entry point for
65`celld deploy`.
66
671. Install the build tool: `cargo install worker-build`.
682. Build the crate: `worker-build --release`.
693. Point the config at the shim:
70
71```jsonc
72{
73 "name": "my-app",
74 "main": "./build/worker/shim.mjs",
75 "compatibility_date": "2026-01-01",
76}
77```
78
794. Deploy: `celld deploy`.
80
81The shim wraps its exports in a JavaScript Proxy, and celld resolves
82entrypoint classes and Durable Object classes through that wrapper.
83The runtime surface that the application can use is the surface in
84[Cloudflare compatibility](cloudflare-compat.md); a workers-rs API
85that maps to a missing runtime feature does not work.
86
87## Dynamic Workers
88
89A dynamically loaded worker can also carry wasm. Pass the bytes in the
90`modules` map; a `BufferSource` value becomes a compiled-module import
91in the loaded worker.
92
93```js
94const worker = env.loader.load({
95 mainModule: "main.js",
96 modules: {
97 "main.js": `import m from "./add.wasm"; ...`,
98 "add.wasm": wasmBytes,
99 },
100});
101```
102
103## Limits
104
105The wasm bytes count against the deployment size limits, exactly as
106JavaScript modules do. A wasm module that does not compile fails the
107importing module with a `WebAssembly.CompileError` that names the
108file.