Diffinery

homeflare/celldpublic

celld/docs/wasm.md
101 lines · 3.5 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## Prebuilt Workers
31
32With `no_bundle: true`, celld preserves the entry JavaScript byte for byte.
33It applies Wrangler's default `**/*.wasm` and `**/*.wasm?module` patterns below
34the directory that contains `main`. For example, `main: "./dist/shim.mjs"` can
35import `"./add.wasm"` or `"./lib/add.wasm"` from `dist`. The module names keep
36these relative paths. The files use the same WASM metadata and feature gate as
37a bundled deploy.
38
39The default scan includes WASM files that the JavaScript does not import. Use
40a dedicated build output directory, so celld does not upload unrelated WASM.
41The scan excludes `.git`, `.celld`, `.wrangler`, and symbolic links to
42directories. It refuses a symbolic link whose name matches a WASM pattern.
43Copy the WASM file into the build output instead of linking to it or importing
44it from a parent directory.
45
46This mode matches Wrangler's default WASM discovery, but it does not implement
47all [Wrangler module discovery settings](https://developers.cloudflare.com/workers/wrangler/configuration/#find-additional-modules).
48celld 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. This
51mode does not require an esbuild executable.
52
53## Rust with workers-rs
54
55[workers-rs](https://github.com/cloudflare/workers-rs) compiles a Rust
56crate to a Worker. The tool `worker-build` produces a JavaScript shim
57and a wasm file, and the shim is a normal entry point for
58`celld deploy`.
59
601. Install the build tool: `cargo install worker-build`.
612. Build the crate: `worker-build --release`.
623. Point the config at the shim:
63
64```jsonc
65{
66 "name": "my-app",
67 "main": "./build/worker/shim.mjs",
68 "compatibility_date": "2026-01-01",
69}
70```
71
724. Deploy: `celld deploy`.
73
74The shim wraps its exports in a JavaScript Proxy, and celld resolves
75entrypoint classes and Durable Object classes through that wrapper.
76The runtime surface that the application can use is the surface in
77[Cloudflare compatibility](cloudflare-compat.md); a workers-rs API
78that maps to a missing runtime feature does not work.
79
80## Dynamic Workers
81
82A dynamically loaded worker can also carry wasm. Pass the bytes in the
83`modules` map; a `BufferSource` value becomes a compiled-module import
84in the loaded worker.
85
86```js
87const worker = env.loader.load({
88 mainModule: "main.js",
89 modules: {
90 "main.js": `import m from "./add.wasm"; ...`,
91 "add.wasm": wasmBytes,
92 },
93});
94```
95
96## Limits
97
98The wasm bytes count against the deployment size limits, exactly as
99JavaScript modules do. A wasm module that does not compile fails the
100importing module with a `WebAssembly.CompileError` that names the
101file.