Cloudflare Workers 应用 — 上下文包
可复制的上下文包,用于包含 D1、KV、R2 和 Hono 的 Cloudflare Workers 应用,让你的 AI 代理从第一个提示开始就了解边缘运行时约束。
CursorClaude CodeCodexWindsurf CloudflareTypeScript
将此粘贴到任务顶部,以便代理在编写任何代码之前了解 Cloudflare Workers 的运行时约束、绑定和项目布局。
项目背景
An API and web application running entirely on Cloudflare Workers — no Node.jsserver, no container. The HTTP router is Hono. Persistent state uses CloudflareD1 (SQLite at the edge) for relational data, KV for session/cache, and R2 forfile storage. Deployed and configured via Wrangler. TypeScript compiled withesbuild through Wrangler's bundler.技术栈
Cloudflare Workers (V8 isolates — NOT Node.js)Hono v4 (lightweight router, runs on Workers)Cloudflare D1 (SQLite — accessed via env.DB binding)Cloudflare KV (key-value — accessed via env.KV binding)Cloudflare R2 (object storage — accessed via env.BUCKET binding)Drizzle ORM with drizzle-orm/d1 adapterTypeScript (strict)Wrangler v3 (CLI: dev, deploy, d1, kv, r2)目录结构
src/ index.ts # Worker entry point — exports default { fetch } routes/ # Hono route handlers, one file per resource middleware/ # Auth, CORS, rate-limit middleware lib/ db.ts # Drizzle D1 client factory (receives env.DB) schema.ts # Drizzle table definitions kv.ts # KV helpers (typed get/put wrappers) r2.ts # R2 helpers (upload, signed URL, delete) types/ env.d.ts # Cloudflare Env interface with all bindings typedmigrations/ # D1 SQL migrations (generated by drizzle-kit)wrangler.toml # Worker config — bindings, routes, compatibility datedrizzle.config.ts编码约定
- The Workers runtime is NOT Node.js. Never import Node built-ins (fs, path, crypto from "node:crypto" is allowed but most others are not).- All bindings (D1, KV, R2, secrets) are accessed via the typed Env interface — never use global variables or process.env.- D1 queries use Drizzle ORM. Raw SQL is only acceptable in migrations.- KV is eventually consistent — do not use it as a lock or transaction.- Secrets are set with `wrangler secret put SECRET_NAME`, never in wrangler.toml.- Hono routes return Response objects or use c.json() / c.text() helpers.- Wrangler compatibility_date must stay at its current pinned value unless explicitly upgrading and testing all affected APIs.- D1 schema changes require: `npx drizzle-kit generate` then `wrangler d1 migrations apply DB --remote`.AI 任务边界
- Do not use setTimeout, setInterval, or long-lived connections — Workers have a 30-second CPU time limit per request.- Do not spawn child processes or access the file system.- Do not import packages that depend on Node.js internals without confirming they have a Workers-compatible build.- Do not add environment variables via wrangler.toml [vars] for secrets — use `wrangler secret put` instead.- Do not change compatibility_date without updating compatibility_flags and running the full test suite.- D1 does not support all SQLite features — avoid triggers, virtual tables, and RETURNING on UPDATE in older compatibility dates.- Workers have a 1 MB script size limit (after compression). Keep dependencies minimal; check bundle size with `wrangler deploy --dry-run`.llms.txt
# Cloudflare Workers AppRuntime: Cloudflare Workers (V8 isolates, not Node.js)Router: Hono v4Database: D1 via Drizzle ORM (src/lib/db.ts, src/lib/schema.ts)Cache/Session: KV (src/lib/kv.ts)File storage: R2 (src/lib/r2.ts)Config: wrangler.toml — all bindings declared hereSecrets: wrangler secret put (never wrangler.toml)Deploy: wrangler deployDev: wrangler dev (local D1 + KV emulation)