UCD.js Docs

Worker Utils

Architecture notes for @ucdjs-internal/worker-utils

@ucdjs-internal/worker-utils centralizes shared request-handling behavior for Hono and H3 workers.

Role

  • Provides common error responses, handler glue, CORS, and rate-limiting setup.
  • Normalizes Cloudflare/H3 environment access and request handling across worker apps.
  • Keeps API-like apps consistent instead of re-implementing platform plumbing.

Mental Model

There are two main surfaces:

  • framework-agnostic response helpers and task helpers
  • framework-specific setup helpers for Hono and H3

The point is policy reuse:

  • same CORS rules
  • same rate-limit strategy
  • same normalized error payload shape

Method Flows

H3 request setup

app routesetupH3Ratelimit()setupH3Cors()H3 appapp routesetupH3Ratelimit()setupH3Cors()H3 appalt[rate limit exceeded][allowed]alt[disallowed origin / preflight handling][allowed]Requestincoming requestapply allowed-origin policyCORS responselimit by client IP429 custom errorcontinue to route handlerroute responseRequest

Error normalization

customError()/internalServerError()errorHandler()/h3ErrorHandler()customError()/internalServerError()errorHandler()/h3ErrorHandler()alt[known HTTP exception][unknown error]Frameworkuncaught error + request contextbuild custom error responsenormalized responseinternalServerError()500 responseFramework

Cloudflare env access

event.runtime.cloudflare.envgetCloudflareEnv()event.runtime.cloudflare.envgetCloudflareEnv()alt[env missing][env present]RoutegetCloudflareEnv(event)read worker bindingsthrow missing env errortyped bindingsRoute

Design Notes

  • Worker apps should compose these helpers early in startup so every route shares the same policy.
  • H3 and Hono have separate adapters because the request lifecycle is different even when the policy is the same.
  • Normalized errors are more important than perfect framework parity.
  • Environment access is strict so misconfigured workers fail clearly.

Testing Use

  • CORS origin allow/deny cases
  • rate-limiter success vs rejection
  • typed environment access for worker bindings
  • error normalization for expected and unexpected failures

On this page