Adding support for Cloudflare Workers #65
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi, as discussed on Discord, this pr adds Cloudflare Worker Rust support via their WASM bindings. Cloudflare's build tools compile workers into a WASM binary that's wrapped in a Javascript shim. To work in this environment I'm creating an optional
CfWorkerServer
that replaces the existing hyper HTTP endpoint and proxies Worker fetch requests into the existing Restate endpoint resolver.One of the main challenges involves replacing bidi streams, which aren't supported in the Cloudflare Worker environment, with RequestResponse calls that move data across the WASM boundary. Most of what CfWorkerServer currently does is drain input and output streams into buffers that can be sent between the Restate core/handler and the JS request shim.
There might be ways to overcome this by using the JS API's ReadableStream directly. I'm exploring if Cloudflare's V8-based runtime environment supports this (in theory it should) but as an initial pass buffering the input and output streams should work as long as payload doesn't exhaust the container's 128MB memory limit.
In addition to adding the CfWorkerServer, I also added a
with_protocol_mode()
function to the Endpoint builder to enable optional support for the RequestResponse protocol mode as required by Cloudflare WorkersThis is a WIP/request for discussion but I'm happy to make changes if you think this code should get merged into the SDK.
Here's an example service using
CfWorkerServer
in combination with the workers-rs crate:This code needs to be compiled using
wrangler
with additional Rust compiler flags to enable wasm support for dependencies:RUSTFLAGS='--cfg getrandom_backend="wasm_js"' npx wrangler dev
RUSTFLAGS='--cfg getrandom_backend="wasm_js"' npx wrangler deploy
I haven't found a way to build Rust workers via Cloudflare's CD build automation, as the rust compiler requires clang, which isn't currently installed and I wasn't able to find a way to add it at build time.