Diffinery

homeflare/celldpublic

celld/docs/wasm.md
78 lines · 2.2 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## Rust with workers-rs
31
32[workers-rs](https://github.com/cloudflare/workers-rs) compiles a Rust
33crate to a Worker. The tool `worker-build` produces a JavaScript shim
34and a wasm file, and the shim is a normal entry point for
35`celld deploy`.
36
371. Install the build tool: `cargo install worker-build`.
382. Build the crate: `worker-build --release`.
393. 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
494. Deploy: `celld deploy`.
50
51The shim wraps its exports in a JavaScript Proxy, and celld resolves
52entrypoint classes and Durable Object classes through that wrapper.
53The runtime surface that the application can use is the surface in
54[Cloudflare compatibility](cloudflare-compat.md); a workers-rs API
55that maps to a missing runtime feature does not work.
56
57## The Worker Loader
58
59A dynamically loaded worker can also carry wasm. Pass the bytes in the
60`modules` map; a `BufferSource` value becomes a compiled-module import
61in the loaded worker.
62
63```js
64const 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
75The wasm bytes count against the deployment size limits, exactly as
76JavaScript modules do. A wasm module that does not compile fails the
77importing module with a `WebAssembly.CompileError` that names the
78file.