How Do Redis and Cloudflare Workers Relay Functions Work in OmniRoute?

OmniRoute uses a unified HTTP relay architecture where Cloudflare Workers and Vercel Edge Functions share the same request proxying schema, with authentication validation and header building handled through shared utilities in the relay subsystem.

The OmniRoute repository (diegosouzapw/OmniRoute) implements embedded service relays as a centralized proxy layer. Rather than each service defining unique protocols, the codebase converges on a common pattern: requests are forwarded to worker endpoints that then proxy to upstream targets. This design minimizes configuration surface area and maximizes code reuse across deployment targets.

Cloudflare Workers Relay Implementation

The Cloudflare Workers integration lives in src/lib/proxyRelay/cloudflareWorkerScript.ts. It exposes two primary exports that mirror the Vercel implementation for consistency.

Core Request Building Function

The buildCloudflareWorkerUploadRequest() function constructs the proxied HTTP request:

export async function buildCloudflareWorkerUploadRequest(
  ctx: RelayContext,
): Promise<Request> {
  if (isRelayAuthMissing(ctx.relayAuth, "cloudflare")) {
    throw new RelayError("relay_auth_missing", "Missing auth for Cloudflare relay");
  }

  const url = `https://${ctx.host}`; // Cloudflare workers expose an HTTP endpoint
  const headers = buildVercelRelayHeaders(ctx.relayAuth!);
  const request = await buildFetchRequest({ url, method: "POST", headers, body: ctx.body });
  return request;
}

Key behaviors to note:

  • Authentication gate: The function validates ctx.relayAuth using isRelayAuthMissing() from ../relay/auth.ts before proceeding
  • URL construction: Cloudflare Workers expose standard HTTPS endpoints, constructed from ctx.host
  • Header delegation: Headers are built via buildVercelRelayHeaders()—deliberate reuse across platforms
  • Request assembly: Final request construction delegates to buildFetchRequest() in ../relay/fetchRequest.ts

Shared Header Utilities

Since Cloudflare and Vercel share the same relay schema, the file re-exports Vercel's header builder:

export { buildVercelRelayHeaders as buildCloudflareRelayHeaders };

This alias preserves API clarity while eliminating duplicate code.

Relay Context and Type System

The RelayContext type imported from ../relay/types.ts provides the contract for all relay operations. Based on usage patterns in the Cloudflare implementation, this context includes:

  • host: Target hostname for the worker endpoint
  • relayAuth: Authentication credentials keyed by service type
  • body: Request payload for upstream proxying

Error Handling Pattern

Relay failures use the centralized RelayError class from ../relay/error.ts. The Cloudflare implementation specifically throws "relay_auth_missing" when credentials are absent, enabling upstream catch blocks to handle auth flows consistently across Redis, Cloudflare, and other embedded services.

Architecture Benefits

OmniRoute's relay design prioritizes protocol uniformity over service-specific optimizations. By funneling Cloudflare Workers through the same path as Vercel Edge Functions, the codebase achieves:

  • Single authentication validation logic in isRelayAuthMissing()
  • One header construction utility for multiple platforms
  • Unified error taxonomy across all embedded services
  • Simplified testing through shared request builders

Summary

  • OmniRoute's Cloudflare Workers relay uses the same HTTP schema as Vercel, implemented in src/lib/proxyRelay/cloudflareWorkerScript.ts
  • The buildCloudflareWorkerUploadRequest() function validates auth, constructs the HTTPS URL, builds headers via buildVercelRelayHeaders(), and assembles the final request
  • Header utilities are re-exported from Vercel's implementation to avoid duplication
  • All relay errors flow through the RelayError class with service-specific error codes like "relay_auth_missing"

Frequently Asked Questions

How does OmniRoute handle authentication for Cloudflare Workers?

OmniRoute validates Cloudflare relay authentication through isRelayAuthMissing() in src/lib/relay/auth.ts. The function checks the relayAuth field on the RelayContext for a "cloudflare" key. If missing, it throws a RelayError with code "relay_auth_missing" before any network request is attempted.

Why does OmniRoute reuse Vercel's header builder for Cloudflare?

The codebase explicitly states that "Cloudflare Workers use the same HTTP relay schema as Vercel." Rather than maintaining parallel implementations, buildVercelRelayHeaders() is re-exported as buildCloudflareRelayHeaders(). This architectural decision reduces bug surface area and ensures consistent header formatting across edge platforms.

What happens if relay auth is misconfigured?

The buildCloudflareWorkerUploadRequest() function gates all execution behind an auth check. When isRelayAuthMissing() returns true, the function immediately throws:

throw new RelayError("relay_auth_missing", "Missing auth for Cloudflare relay");

This prevents partial request construction and provides a clear, catchable error type for upstream handling.

Where does the actual HTTP request construction happen?

While buildCloudflareWorkerUploadRequest() orchestrates the process, the final Request object is assembled by buildFetchRequest() imported from ../relay/fetchRequest.ts. This utility handles the low-level fetch()-compatible request creation, accepting URL, method, headers, and body parameters.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →