Adding support for Cloudflare Workers#65
Conversation
…dflare deployment
|
All contributors have signed the CLA ✍️ ✅ |
|
I have read the CLA Document and I hereby sign the CLA |
|
fwiw, the
|
|
@kpwebb what's the status of this PR? I run a few TS services on Cloudflare, but recently I had to use Rust for a few tasks and it would be great if I could run those on CF as well. |
|
As said here https://discord.com/channels/1128210118216007792/1458175817640050922, it would be nice to understand exactly what the current SDK is missing to enable cf workers, and then have a community library that handles the integration with cf workers specifically. I'll be happy to assist with the process :) |
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
CfWorkerServerthat 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
CfWorkerServerin combination with the workers-rs crate:This code needs to be compiled using
wranglerwith additional Rust compiler flags to enable wasm support for dependencies:RUSTFLAGS='--cfg getrandom_backend="wasm_js"' npx wrangler devRUSTFLAGS='--cfg getrandom_backend="wasm_js"' npx wrangler deployI 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.