Skip to content

Write a WebAssembly worker

A WebAssembly worker is a component targeting virtufin:worker@1.0.0 (reference). Rust is the supported path; Python through componentize-py is a preview.

Rust

You need Rust with the wasm32-wasip2 target and cargo-generate. The crate virtufin-worker comes from the org's cargo registry: add it to ~/.cargo/config.toml once (the template's .cargo/config.toml has it too):

[registries.virtufin]
index = "sparse+https://gitea.haenerconsulting.com/api/packages/virtufin/cargo/"
credential-provider = "cargo:token"

The registry needs a login: once, with a Gitea access token that can read packages (or set CARGO_REGISTRIES_VIRTUFIN_TOKEN to the same value):

cargo login --registry virtufin "Bearer <token>"

Start from worker-devkit's template and build:

cargo generate --git https://git.haenerconsulting.com/virtufin/worker-devkit.git src/rust/templates/worker --name my-worker
cd my-worker
cargo build --release          # the template's config targets wasm32-wasip2
ls target/wasm32-wasip2/release/my_worker.wasm

The worker is a type implementing Worker, exported with export!:

use virtufin_worker::{export, host, CloudEvent, Error, LogLevel, StartContext, Worker};

struct Echo {
    worker_id: String,
}

impl Worker for Echo {
    fn start(ctx: StartContext) -> Result<Self, Error> {
        host::log(LogLevel::Info, "started");
        Ok(Echo { worker_id: ctx.worker_id })
    }

    fn process(&mut self, event: CloudEvent) -> Result<Vec<CloudEvent>, Error> {
        let mut out = CloudEvent::new("com.example.echo");
        out.data_content_type = event.data_content_type.clone();
        out.data = event.data.clone();
        Ok(vec![out])
    }
}

export!(Echo);

Create it like any worker, with MIME type application/wasm and the .wasm as the code (a URL, or the bytes inline). For example with the C# client:

await client.CreateWorkerAsync(new WorkerSpec(
    new CodeSource.Content(File.ReadAllBytes("target/wasm32-wasip2/release/my_worker.wasm")),
    WorkerMimeTypes.Wasm, ["act.exchange.test.thing"]));

What the guest gets, and what it does not (no files, no sockets: call the platform with host::gateway_call), is in the reference.

Python (preview)

worker-devkit's src/rust/templates/python-componentize is a componentize-py project for the same WIT world:

pip install componentize-py==0.25.1
componentize-py --wit-path wit --world worker componentize --stub-wasi app -o worker.wasm

--stub-wasi is required: CPython's component imports wasi:filesystem and wasi:sockets, which the engine refuses; the flag replaces every WASI import with a trapping stub. So in a Python worker print and file or socket access trap (log with host.log), and Python's random repeats the seed baked in at build time (never use it for secrets). componentize-py embeds CPython: the component is 18 MiB and its first load compiles for about 6 s (cached for restarts), against 30 ms for the Rust template (measured on an M-series Mac).

Run it locally

WasmWorkerLoader (Virtufin.WorkManager.Engines.Local) runs a component in this process's WasmEngine, as the Runtime does. It needs the host binary on the PATH:

cargo install --locked --path src/Virtufin.WorkManager.Engine.Wasm.Host   # in a workmanager checkout