Deploy on Workers
Keep the routes platform-free, build their dependencies from bindings in one entry file, and switch optional infrastructure on with flags.
A Worker built on edgetime has two parts: a module of routes that imports nothing from Cloudflare,
and an entry file that turns bindings into what those routes ask for. The repository’s
examples/api is a complete
Worker built this way.
Describe what the routes need
Section titled “Describe what the routes need”Declare the routes’ dependencies by what they call, not by Cloudflare’s types:
import type { RateLimiter } from '@edgetime/middleware';
export interface AppEnv { JWT_ISSUER: string; JWT_AUDIENCE: string; JWT_SECRET: string; clientAddress(request: Request): string | undefined; apiLimit?: RateLimiter; files: FileStore; filesReplica?: FileStore;}
export interface FileStore { get(key: string): Promise<{ body: ReadableStream; httpEtag: string } | null>; put(key: string, value: Uint8Array): Promise<unknown>;}An R2 bucket satisfies FileStore, and so does a Map wrapped in two methods, which is what the
Node example and the tests use.
Build them once per isolate
Section titled “Build them once per isolate”The entry file is the composition root. perEnv() builds the dependencies once per env object,
which is the same for every request an isolate serves. required() and flag() from
@edgetime/config read variables strictly, so a missing or misspelt one
stops the Worker instead of quietly changing what it does.
import { connectingIp } from '@edgetime/cloudflare';import { flag, required } from '@edgetime/config';import { memoryRateLimiter, withFallback } from '@edgetime/middleware';import { perEnv } from '@edgetime/router';import { type AppEnv, app } from './app.ts';
const dependencies = perEnv((env: Env): AppEnv => { const replica = flag(env.REPLICA_ENABLED) ? env.FILES_REPLICA : undefined; return { JWT_ISSUER: required(env, 'JWT_ISSUER'), JWT_AUDIENCE: required(env, 'JWT_AUDIENCE'), JWT_SECRET: required(env, 'JWT_SECRET'), clientAddress: connectingIp, // If the binding fails, each isolate keeps a rough limit of its own until it recovers. apiLimit: withFallback(env.API_LIMIT, memoryRateLimiter({ limit: 300, period: 60 })), files: env.FILES, ...(replica ? { filesReplica: replica } : {}), };});
export default { fetch: (request, env, ctx) => app.fetch(request, dependencies(env), ctx),} satisfies ExportedHandler<Env>;Optional infrastructure behind flags
Section titled “Optional infrastructure behind flags”Redundancy is a deployment choice, not a code path. Bind the second resource and set a text
variable to true only where you want it; the routes see an optional dependency and treat its
absence as “off”.
- Replicated storage. With
filesReplicapresent, uploads go throughwriteAll()to both stores and reads go throughfirstAvailable(), falling back to the replica when the first store is missing the object or fails. See@edgetime/resilience. - Exact rate limits. A Rate Limiting binding counts per location. Where a limit must hold
exactly, such as sign-in or one-time codes, use the Durable Object limiter from
@edgetime/cloudflare/durable. - Limiter outages.
withFallback()keeps a rough in-memory limit in place when the shared limiter fails, and each route decides withonLimiterErrorwhether an outage refuses or allows. SeerateLimit().
Client addresses
Section titled “Client addresses”Use connectingIp(request) as the rate-limit key. It reads CF-Connecting-IP, which Cloudflare’s
edge always sets, and returns undefined for requests that did not come through it, such as
service-binding calls and local runs. rateLimit() refuses a request without a key unless told
onMissingKey: 'skip'.
Testing
Section titled “Testing”Because the routes take ports, unit tests call app.fetch() with in-memory implementations and
no runtime at all. Run the Worker’s own entry file in workerd with @cloudflare/vitest-plugin to
check the bindings are wired as the routes expect.