crossbind
GitHub

Edge

Two builds reach the edge. -e edge makes a single-threaded WebAssembly module and loader for a Cloudflare Worker. -e http makes a WASI HTTP component, a C++ request handler with no JavaScript, for wasmtime serve, Spin and wasmCloud.

Cloudflare Workers

Workers run JavaScript in V8 isolates, with no Web Workers and no SharedArrayBuffer, and there is no bundler plugin: the Worker imports the build output itself. Install the CLI and name the project, which names the output files:

shell
npm install --save-dev crossbind@beta
crossbind.config.mjs
export default {
general: { name: 'my-worker' },
paths: { config: import.meta.url, output: 'dist' },
};

Without a bundler, the build binds every .h file in src/native, so a port is reached through a header of your own. Build; the loader and its wasm land in dist:

shell
npx crossbind build -p wasm -a wasm32 -r st -e edge -b release
index.js
import initNative from './dist/my-worker-wasm-wasm32-st-release.edge.js';
import wasm from './dist/my-worker-wasm-wasm32-st-release.edge.wasm';
 
export default {
async fetch() {
const { Native } = await initNative({ getWasmFunction: () => wasm });
return new Response(Native.sample());
},
};

Native is a class in src/native/native.h. Workers cannot compile WebAssembly from bytes, so the Worker imports the .wasm as a module and getWasmFunction (Runtime API) hands it to the loader. initNative() starts once per isolate.

Start it in the handler, not with a top-level await: Workers forbid random numbers in global scope, and Rust's standard library asks for some as the module starts. The build also writes dist/edge/wasm.mjs, which does both imports and exports initNative and every bound name; a package publishes it as its edge entry (Publishing packages).

Wrangler bundles the .wasm import with no extra rules:

wrangler.toml
name = "my-worker"
main = "index.js"
compatibility_date = "2024-09-08"
shell
npx wrangler dev
npx wrangler deploy

To start from a working Worker instead, npm create crossbind@beta -- my-worker Cloud "Cloudflare Worker" scaffolds one.

Limits

  • Single-threaded only. -r mt and useWorker need Web Workers, which an isolate does not have.
  • Files live in memory. m.FS keeps them only as long as the isolate, and there is no OPFS. Keep durable data in a store such as R2 or KV and pass it in from JavaScript.
  • No header constants. #define and const values bind only for names an app imports, and a Worker imports no header.
  • Platform caps. A free Worker gets 10 ms of CPU per request, and every plan caps a Worker's size; check your .wasm against yours.

WASI HTTP components

-e http links your C++ into a component that exports wasi:http/[email protected] (the wasi:http/service world). There is no main() and no JavaScript: src/native defines the one function <crossbind/http.h> declares, and the host calls it for each request.

src/native/handler.cpp
#include <crossbind/http.h>
 
namespace crossbind::http {
 
// Called once per request, after its whole body has arrived.
Response handle(Request request) {
return {
200,
{ { "content-type", "text/plain" } },
"hello " + request.path + "\n",
};
}
 
}
crossbind.config.mjs
export default {
general: { name: 'my-service' },
paths: { config: import.meta.url, output: 'dist' },
};
shell
npx crossbind build -p wasi -e http -b release
wasmtime serve dist/my-service-wasi-wasm32-st-release.http.wasm

Spin 4 serves the same file from a manifest, with spin up:

spin.toml
spin_manifest_version = 2
 
[application]
name = "my-service"
 
[[trigger.http]]
route = "/..."
component = "my-service"
 
[component.my-service]
source = "dist/my-service-wasi-wasm32-st-release.http.wasm"

wasmCloud's wash dev takes it as build.component_path in .wash/config.yaml. The build compiles like a WASI command, and ports link from their -wasi packages.

  • Request carries method, authority, path and query as the client sent them, headers with lowercase names, and body, read whole before handle runs. Response is status (200 unless set), headers and body.
  • An exception out of handle answers 500 and logs crossbind: <method> <path> threw: <what> to stderr.
  • A host can reuse one instance for many requests, so static state outlives a request. Calls of handle never overlap, but one can run while an earlier response body is still being written.
  • Each request runs on a 1 MiB stack of its own; -Wl,-z,stack-size=<bytes> in binary.wasiFlags changes it.

Not there yet: outgoing requests (wasi:http/client), streamed request and response bodies, trailers, threads, Rust, and the data files and environment of dependencies such as PROJ, GDAL and OpenSSL, which no host passes to a component. The component also imports the wasi:cli environment, exit and terminal interfaces, so a host that offers only the wasi:http/service imports rejects it.

examples/cloud-cloudflare-worker is a complete Worker; e2e/cloud-wasi-http builds a component and tests it under wasmtime serve and Spin.

Type to search every guide page and section.
↑↓ navigate↵ openesc close