Skip to main content

Crate connectrpc_workers

Crate connectrpc_workers 

Source
Expand description

ConnectRPC ClientTransport implementations backed by the Cloudflare Workers fetch APIs.

Two transports are provided:

  • FetcherTransport wraps a worker::Fetcher (a [[services]] binding). Use it for inter-service calls within the same Cloudflare zone. The runtime short-circuits these requests, so there’s no DNS lookup, no TLS handshake, and no trip out to the public internet.
  • FetchTransport wraps the global worker::Fetch for arbitrary http:// / https:// URLs.

§Sendness

ClientTransport requires Send + Sync + 'static on the type and a Send + 'static future. Workers’ fetch is !Send (everything in JS-land is !Send). We use worker::send::SendFuture / worker::send::SendWrapper to satisfy the bound. workers-rs ships these specifically because the Workers isolate is single-threaded, so nothing is ever actually moved across threads.

§Protocol

Use connectrpc::Protocol::Connect (or GrpcWeb). Workers fetch subrequests don’t expose raw HTTP/2, so gRPC’s trailer requirement won’t survive. Connect over HTTP/1.1 and GrpcWeb (trailers in body) both work.

§Example

use connectrpc::client::ClientConfig;
use connectrpc::Protocol;
use connectrpc_workers::FetcherTransport;

// Inside a #[event(fetch)] handler:
let echo = env.service("ECHO")?;
let transport = FetcherTransport::new(echo);
let config = ClientConfig::new("http://echo/".parse()?).protocol(Protocol::Connect);
let client = EchoServiceClient::new(transport, config);
let resp = client.echo(EchoRequest { message: "hi".into() }).await?;

Structs§

FetchTransport
ClientTransport backed by the global Workers fetch (arbitrary URL).
FetcherTransport
ClientTransport backed by a Workers service binding.