Skip to content
Docs
Changelog

http

The outbound HTTP call module and the bounded stream reader: the only code on the Worker that calls fetch or holds a stream reader.

Markdown

Purpose

Two modules that every other package uses instead of calling the platform directly.

outbound is the outbound HTTP call module. A call to another host (payment providers, RevenueCat, Expo push, webhook endpoints, email providers, the error tracker, Cloudflare's GraphQL API, GitHub, the S3 control plane) is one outboundCallEffect(request). Behind that interface: the deadline, the caller's cancellation, redirect: "manual" with a 3xx as a failure, a bounded read of the answer, and one classification of what went wrong (network, timeout, status with Retry-After, redirect, invalid-body, cancelled). fetch is the seam: the platform's in production, a fake in tests. A caller keeps its domain mapping (what a refusal means for a payment, an email or a delivery) and, for an idempotent read, its own retry.

read-bounded is the one bounded stream reader: readBounded (all bytes, or null past the bound), boundedChunks (chunk by chunk, for text that is processed as it arrives) and readBoundedJson. Request bodies of webhooks, stored objects and provider answers all go through it.

Rule and reason: external I/O has a deadline; only reads retry.

Public exports

SubpathTarget
./outbound./src/outbound.ts
./read-bounded./src/read-bounded.ts

These paths come from packages/http/package.json. outbound imports Effect and runs on the Worker; the client bundle check refuses Effect in browser assets. read-bounded is a LEAF module (no imports).

How to use

Inside a program (a job, a transport, a migrated procedure), yield the Effect and map its one failure type:

const answer =
  yield *
  outboundCallEffect({
    url: "https://api.provider.example/v1/things",
    method: "POST",
    headers: { authorization: `Bearer ${key}` },
    body: JSON.stringify(payload),
    read: { as: "json", maxBytes: 256 * 1024 },
    timeoutMs: 10_000,
    fetch: options.fetch,
  }).pipe(Effect.mapError((failure) => new ThingFailed({ status: failure.status })));
const parsed = thingSchema.safeParse(answer.body);

Promise callers such as route handlers and S3 signers use outboundCall. It shares the HTTP exchange and uses a platform timer without starting an Effect runtime. It resolves with { ok: true, status, headers, body } or { ok: false, reason, status, retryable, retryAfterMs, message } and does not reject for an expected failure.

  • read says what to do with an accepted answer: { as: "json", maxBytes }, { as: "bytes", maxBytes }, or { as: "nothing" } when the status is the answer (webhook deliveries).
  • accept chooses the statuses whose body is read (default 2xx). The error tunnel passes the tracker's 429 on, so it accepts every status. A 3xx is never offered to it.
  • signal is the caller's cancellation and comes back as cancelled. Interrupting the fiber needs nothing.
  • onDispatch runs immediately before the request leaves, for a send that must tell "never sent" from "acceptance unknown" (push).
  • retryable means the same request could succeed later, never that it is safe to repeat. Only an idempotent read retries, with idempotentRead from packages/api; the failure already has the shape that schedule reads.

Test a caller by passing a fake fetch and asserting its domain mapping. Redirects, deadlines, body bounds and cancellation are tested once, in packages/http/src/outbound.test.ts.

How to extend

A new runtime quirk or a new rule for every outbound call (a header, a metric, another failure reason) lands in outbound.ts with a case in its suite. Do not add a second fetch call next to it: scripts/outbound-http.test.mjs fails on a fetch( or a .getReader() anywhere else in Worker-side code.

Key files

Edit on GitHubDocumentation is in English.
Cookie settings
We use essential cookies to make our app work. With your consent, we also use optional cookies to improve your experience.

Read our Cookie Policy and Privacy Policy.