Skip to content

feat(framework): Cloudflare Workers adapter #89

@qantrepreneur

Description

@qantrepreneur

What to build

Add a @sundayceo/framework/cloudflare sub-export that adapts the framework's request handler for Cloudflare Workers.

Adapter API

The adapter wraps RequestHandlerOptions into a Workers export default { fetch } module and merges Cloudflare env bindings into the app's request context explicitly — following ecosystem conventions (React Router, Astro, SolidStart all pass env through the request context, none use AsyncLocalStorage).

// src/server.ts
import { cloudflare } from "@sundayceo/framework/cloudflare";
import { app } from "./app";
import { routes, templates } from "./routes.gen";

export default cloudflare({
  app,
  getRoutes: () => routes,
  loadRouteModule: (route) => route.load(),
  loadTemplate: (id) => templates[id]().then((m) => m.default),
}, {
  env: (env) => ({ db: env.DB, cache: env.CACHE }),
});

What the adapter does

  1. Returns a Workers-compatible { fetch(request, env, ctx) } object
  2. Calls the user-provided env mapping function to convert Cloudflare bindings into context properties
  3. Wraps app.context to merge env-derived values into the request context so loaders/handlers see both
  4. Makes ExecutionContext (for waitUntil, etc.) available as cloudflare.ctx in the merged context

Package changes

  • Add src/cloudflare.ts entry point to the framework package
  • Add to tsup config's entry array
  • Add to package.json exports map: "./cloudflare": { "import": "./dist/cloudflare.js", "types": "./dist/cloudflare.d.ts" }

Acceptance criteria

  • @sundayceo/framework/cloudflare is importable
  • cloudflare(handlerOptions) returns a valid Workers { fetch } export
  • env mapping function threads bindings into the request context
  • ExecutionContext available as cloudflare.ctx in the merged context
  • No AsyncLocalStorage — env flows explicitly through the request context
  • Works with wrangler dev and wrangler deploy
  • Tests verify the adapter wraps the handler correctly and maps env bindings

Blocked by

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestready-for-agentTriaged and ready for implementation

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions