Chapter 19½: inside jev-worker/src — one file, two adapters

Everything is in lib.rs, top to bottom in the order you would read it.

lib.rs, in reading order

  1. The doc comment: what the crate is and why it has no ledger.
  2. MAX_BODY: 8 MiB, the most of a response body it accepts, the same cap jev-http uses. A bigger body is TooLarge, which the client never retries (the answer was already produced and billed).
  3. FetchTransport: holds one URL. FetchTransport::api() is jev_protocol::ENDPOINT; FetchTransport::new(url) is anything else.
  4. request(url, sent): turns jev-client's HttpRequest into a workers-rs Request: a POST with every header copied and the body as a Uint8Array. A header that is not valid text, or a request workers-rs will not build, is a Config error.
  5. header_map(headers): turns the response's Headers back into an http::HeaderMap, skipping any pair http will not accept.
  6. impl Transport for FetchTransport: send runs Fetch::Request(..) .send(). A failed fetch is Connect; a failure reading the body is Interrupted; an oversized body is TooLarge. distrust_connection is empty, because fetch owns the connections.
  7. WorkerRuntime and impl Runtime: now is jev_client::Instant::now() (web-time's performance.now() clock), wall_clock is Date.now(), sleep is a workers-rs Delay, timeout races the future against a Delay with futures_util::future::select, and jitter is Math.random().
fetch outcomeTransportErrorKindWhat the client does
the request cannot be builtConfigfails at once
fetch itself failsConnectretries with backoff
reading the body failsInterruptedretries with backoff
body over 8 MiBTooLargefails at once

Try it

cargo build -p jev-worker --target wasm32-unknown-unknown

For the people who maintain it

In this folder

FileWhat
lib.rsFetchTransport, WorkerRuntime, MAX_BODY.
CLAUDE.mdInvariants for agents.

← Previous: Chapter 19: jev-worker · Up: jev-worker · Next: Chapter 20: jev-http →