Skip to content

@tank/cloudflare-workers

1.0.0

Description

Cloudflare Workers platform patterns covering runtime basics, Wrangler, KV, D1, R2, Durable Objects, Queues, Workers AI, Hono integration, testing, performance, and deployment.

Triggered by

cloudflare workerswranglerworkers kvd1 databaser2 storagedurable objects
Download
Verified
tank install @tank/cloudflare-workers

Cloudflare Workers

Core Philosophy

  1. Edge-first means Web API-first — Write to the standard Fetch runtime model (Request, Response, fetch, URL) instead of assuming Node APIs exist.
  2. Bindings are the platform contract — KV, D1, R2, Durable Objects, Queues, and AI are not ad hoc SDK calls; they are explicit runtime capabilities passed into your worker.
  3. Latency beats distance — Keep hot request paths short, cache aggressively, and move coordination-heavy logic out of the edge path when possible.
  4. State belongs in the right primitive — KV for simple global reads, D1 for relational queries, R2 for blobs, Durable Objects for coordination, Queues for async work.
  5. Deploy safely with environments — Separate preview, staging, and production config in Wrangler; edge mistakes propagate globally fast.

Quick-Start: Common Problems

"Which Cloudflare storage primitive should I use?"

NeedPrimitive
Small globally replicated key/value readsKV
Relational data and SQLD1
Files and large objectsR2
Coordinated per-key state / rooms / locksDurable Objects
Async background processingQueues
-> See references/platform-primitives.md

"How do I structure a Worker app?"

  1. Keep the fetch handler thin
  2. Parse request, call domain logic, return Response
  3. Keep bindings explicit in env typing
  4. Split route logic from persistence/AI integrations -> See references/runtime-and-routing.md

"How do I deploy safely with Wrangler?"

  1. Define environments in wrangler.toml
  2. Keep secrets per-environment
  3. Test locally with Miniflare / Wrangler dev
  4. Promote through preview/staging before production -> See references/wrangler-and-deployment.md

"When should I use Hono on Workers?"

SignalRecommendation
Very small worker, one routeplain fetch handler
Many routes, middleware, validationHono
Need typed RPC client and route compositionHono
-> See references/runtime-and-routing.md

Decision Trees

Coordination Primitive

SignalUse
Read-heavy, eventually consistent dataKV
Relational queries and transactionsD1
Stateful single-entity coordinationDurable Objects
File/object servingR2
Deferred processingQueues

Compute Surface

SignalSurface
Main request handlingWorker fetch handler
Static site plus edge functionsPages Functions
Scheduled jobsCron Trigger
AI inference pathWorkers AI + Worker orchestration

Testing Strategy

NeedTool
Local runtime simulationWrangler dev / Miniflare
Unit-level handler testsVitest
Binding-integrated testsMiniflare with configured env

Reference Index

FileContents
references/runtime-and-routing.mdFetch runtime, Request/Response patterns, routing, Hono integration, middleware, Pages Functions
references/platform-primitives.mdKV, D1, R2, Durable Objects, Queues, Vectorize, Workers AI selection and usage patterns
references/wrangler-and-deployment.mdWrangler config, environments, secrets, preview/prod rollout, CI/CD, observability hooks
references/testing-and-debugging.mdMiniflare, Wrangler dev, Vitest patterns, logs, traces, debugging runtime mismatches
references/performance-and-security.mdCaching, latency, cold starts, auth, secrets, edge-safe dependencies, common security pitfalls

Command Palette

Search skills, docs, and navigate Tank