code-review by cloudflare/cloudflare-docs
npx skills add https://github.com/cloudflare/cloudflare-docs --skill code-review您对 Cloudflare Workers API、类型和 wrangler 配置的了解可能已过时。对于任何 Workers 代码审查任务,优先检索而非依赖预训练知识。
使用仓库的本地副本 — 请不要运行 npm pack 或安装包来获取类型。
| 来源 | 查找位置 | 用途 |
|---|---|---|
| Wrangler 配置模式 | node_modules/wrangler/config-schema.json | 配置字段、绑定结构、允许的值 |
| Workers 类型 | node_modules/@cloudflare/workers-types/index.d.ts | API 使用方式、处理程序签名、绑定类型 |
| Cloudflare 文档搜索 | 使用 cloudflare-docs 搜索工具或阅读此仓库中的文件 |
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
| API 参考、兼容性日期/标志、绑定文档 |
当需要验证类型、配置字段或 API 签名时,请直接读取这些文件。references/ 目录中的参考指南描述了需要验证的内容 — 而不是如何获取包。
阅读完整文件,而不仅仅是差异或孤立的代码片段。孤立看可能错误的代码,在给定周围逻辑的情况下可能是正确的。
git log --oneline -5 -- <file>每个代码块都属于以下三个类别之一。请在其所属类别的上下文中进行审查。
| 类别 | 定义 | 期望 |
|---|---|---|
| 说明性 | 演示一个概念;大部分逻辑使用注释 | API 名称正确,签名符合实际 |
| 演示性 | 功能上完整但代码不完整;如果放在正确的上下文中可以运行 | 语法有效,API 和绑定访问正确 |
| 可执行 | 独立且完整;无需修改即可运行 | 可编译、可运行,包含导入和配置 |
运行类型检查和代码检查。工具输出是证据,而非意见。
npx tsc --noEmit # TypeScript 错误
npx eslint <files> # 代码检查问题
对于配置文件,请根据最新的 wrangler 配置模式进行验证(检索方法见 references/wrangler-config.md),并检查所有字段、绑定类型和值是否符合要求。
关于类型系统规则,请参阅 references/workers-types.md;关于配置验证,请参阅 references/wrangler-config.md;关于正确的 API 模式,请参阅 references/common-patterns.md。
快速参考规则:
| 规则 | 详情 |
|---|---|
| 绑定访问 | 模块导出处理程序中使用 env.X;在继承平台基类的类中使用 this.env.X。参见 references/common-patterns.md。 |
禁止使用 any | 切勿对绑定类型、处理程序参数或 API 响应使用 any。应使用适当的泛型。 |
| 禁止类型系统作弊 | 标记 as unknown as T、无理由的 @ts-ignore、不安全的断言。参见 references/workers-types.md。 |
| 配置与代码一致性 | wrangler 配置中的绑定名称必须与代码中的 env.X 使用相匹配。参见 references/wrangler-config.md。 |
| 必需的配置字段 | 根据 wrangler 配置模式进行验证 — 不要假设哪些字段是必需的。 |
| 简洁的示例 | 示例应专注于核心逻辑。尽量减少分散代码教学注意力的样板代码。 |
| 未处理的 Promise | 每个 Promise 都必须被 await、return、void 或传递给 ctx.waitUntil()。参见 references/common-patterns.md。 |
| 序列化 | 跨越 Queue、Workflow 步骤或 DO 存储边界的数据必须是结构化克隆可序列化的。参见 references/common-patterns.md。 |
| 流式处理 | 大型/未知负载必须使用流式处理,而非缓冲。标记对无界数据使用 await response.text() 的情况。 |
| 错误处理 | 应最小化但必须存在 — 对可空返回值进行空值检查,基本的 fetch 错误处理。不要使用冗长的 try/catch 分散注意力。 |
| 风险 | 触发条件 |
|---|---|
| 高 | 身份验证、加密、外部调用、价值转移、验证移除、访问控制、绑定配置错误 |
| 中 | 业务逻辑、状态更改、新的公共 API、错误处理、配置更改 |
| 低 | 注释、日志记录、格式化、次要样式 |
将更深入的分析集中在高风险上。对于关键路径,检查影响范围:有多少其他文件引用了此代码?
安全逻辑升级:对于加密、身份验证和时序敏感代码,不要仅仅停留在验证 API 调用是否正确。检查周围逻辑是否存在破坏安全属性的缺陷(例如,timingSafeEqual 调用正确,但在长度不匹配时提前返回)。参见 references/common-patterns.md 安全部分。
| 反模式 | 为何重要 |
|---|---|
在 Env 或处理程序参数上使用 any | 破坏了每个下游绑定访问的类型安全性 |
as unknown as T 双重转换 | 隐藏了真实的类型不兼容性 — 应修复底层设计 |
无解释的 @ts-ignore / @ts-expect-error | 静默掩盖错误;要求每个抑制都附带注释说明理由 |
缓冲无界数据(对流使用 await res.text()、await res.json()) | 大负载导致内存耗尽;应使用流式处理 |
| 硬编码的密钥或 API 密钥 | 应使用 env 绑定和 wrangler secret |
在每个请求上使用 blockConcurrencyWhile | 仅用于初始化;会阻塞所有并发请求 |
| 单一全局 Durable Object | 造成瓶颈;应按协调原子进行分片 |
| DO 中仅使用内存状态 | 驱逐时丢失;应持久化到 SQLite 存储 |
| 配置中缺少 DO 迁移 | 新的 DO 类需要迁移条目,否则部署失败 |
未处理的 Promise(step.do()、fetch() 不带 await) | 静默错误 — 丢弃结果、破坏 Workflow 持久性、忽略错误 |
跨边界传递不可序列化的值(步骤/队列中的 Response、Error) | 可编译但运行时失败;应在跨越边界前提取纯数据 |
在平台基类上使用 implements 而非 extends | 遗留模式 — 会丢失基类中的 this.ctx、this.env 访问 |
**[严重性]** 简要描述
`file.ts:42` — 附带证据的解释(工具输出、类型错误、配置不匹配)
建议修复:`code`(如果适用)
严重性:严重(安全、数据丢失、崩溃) | 高(类型错误、API 错误、配置损坏) | 中(缺少验证、边缘情况、过时模式) | 低(样式、次要改进)
最后按严重性统计数量。如果未发现问题,请直接说明。
每周安装量
56
仓库
GitHub 星标
4.5K
首次出现
2026年2月12日
安全审计
安装于
gemini-cli54
opencode54
codex54
kimi-cli53
github-copilot53
amp52
Your knowledge of Cloudflare Workers APIs, types, and wrangler configuration may be outdated. Prefer retrieval over pre-training for any Workers code review task.
Use the repo's local copies — do not run npm pack or install packages to fetch types.
| Source | Where to find it | Use for |
|---|---|---|
| Wrangler config schema | node_modules/wrangler/config-schema.json | Config fields, binding shapes, allowed values |
| Workers types | node_modules/@cloudflare/workers-types/index.d.ts | API usage, handler signatures, binding types |
| Cloudflare docs search | Use the cloudflare-docs search tool or read files in this repo | API reference, compatibility dates/flags, binding docs |
Read these files directly when you need to verify a type, config field, or API signature. The reference guides in references/ describe what to validate — not how to fetch packages.
Read full files, not just diffs or isolated snippets. Code that looks wrong in isolation may be correct given surrounding logic.
git log --oneline -5 -- <file>Every code block falls into one of three categories. Review in the context of its category.
| Category | Definition | Expectations |
|---|---|---|
| Illustrative | Demonstrates a concept; uses comments for most logic | Correct API names, realistic signatures |
| Demonstrative | Functional but incomplete; would work if placed in the right context | Syntactically valid, correct APIs and binding access |
| Executable | Standalone and complete; runs without modification | Compiles, runs, includes imports and config |
Run type-checking and linting. Tool output is evidence, not opinion.
npx tsc --noEmit # TypeScript errors
npx eslint <files> # Lint issues
For config files, validate against the latest wrangler config schema (see references/wrangler-config.md for retrieval) and check that all fields, binding types, and values conform.
See references/workers-types.md for type system rules, references/wrangler-config.md for config validation, and references/common-patterns.md for correct API patterns.
Quick-reference rules:
| Rule | Detail |
|---|---|
| Binding access | env.X in module export handlers; this.env.X in classes extending platform base classes. See references/common-patterns.md. |
No any | Never use any for binding types, handler params, or API responses. Use proper generics. |
| No type-system cheats | Flag as unknown as T, unjustified @ts-ignore, unsafe assertions. See references/workers-types.md. |
| Risk | Triggers |
|---|---|
| HIGH | Auth, crypto, external calls, value transfer, validation removal, access control, binding misconfiguration |
| MEDIUM | Business logic, state changes, new public APIs, error handling, config changes |
| LOW | Comments, logging, formatting, minor style |
Focus deeper analysis on HIGH risk. For critical paths, check blast radius: how many other files reference this code?
Security logic escalation : for crypto, auth, and timing-sensitive code, do not stop at verifying API calls are correct. Examine the surrounding logic for flaws that undermine the security property (e.g., correct timingSafeEqual call but early return on length mismatch). See references/common-patterns.md Security section.
| Anti-pattern | Why it matters |
|---|---|
any on Env or handler params | Defeats type safety for every binding access downstream |
as unknown as T double-cast | Hides real type incompatibilities — fix the underlying design |
@ts-ignore / @ts-expect-error without explanation | Masks errors silently; require a comment justifying each suppression |
Buffering unbounded data (await res.text(), await res.json() on streams) |
**[SEVERITY]** Brief description
`file.ts:42` — explanation with evidence (tool output, type error, config mismatch)
Suggested fix: `code` (if applicable)
Severity: CRITICAL (security, data loss, crash) | HIGH (type error, wrong API, broken config) | MEDIUM (missing validation, edge case, outdated pattern) | LOW (style, minor improvement)
End with a summary count by severity. If no issues found, say so directly.
Weekly Installs
56
Repository
GitHub Stars
4.5K
First Seen
Feb 12, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
gemini-cli54
opencode54
codex54
kimi-cli53
github-copilot53
amp52
Supabase Postgres 最佳实践指南 - 8大类别性能优化规则与SQL示例
78,800 周安装
| Config-code consistency | Binding names in wrangler config must match env.X usage in code. See references/wrangler-config.md. |
| Required config fields | Verify against the wrangler config schema — do not assume which fields are required. |
| Concise examples | Examples should focus on core logic. Minimize boilerplate that distracts from what the code teaches. |
| Floating promises | Every Promise must be awaited, returned, voided, or passed to ctx.waitUntil(). See references/common-patterns.md. |
| Serialization | Data crossing Queue, Workflow step, or DO storage boundaries must be structured-clone serializable. See references/common-patterns.md. |
| Streaming | Large/unknown payloads must stream, not buffer. Flag await response.text() on unbounded data. |
| Error handling | Minimal but present — null checks on nullable returns, basic fetch error handling. Do not distract with verbose try/catch. |
| Memory exhaustion on large payloads; use streaming |
| Hardcoded secrets or API keys | Use env bindings and wrangler secret |
blockConcurrencyWhile on every request | Only for initialization; blocks all concurrent requests |
| Single global Durable Object | Creates a bottleneck; shard by coordination atom |
| In-memory-only state in DOs | Lost on eviction; persist to SQLite storage |
| Missing DO migrations in config | New DO classes require migration entries or deployment fails |
Floating promises (step.do(), fetch() without await) | Silent bugs — drops results, breaks Workflow durability, ignores errors |
Non-serializable values across boundaries (Response, Error in step/queue) | Compiles but fails at runtime; extract plain data before crossing boundary |
implements instead of extends on platform base classes | Legacy pattern — loses this.ctx, this.env access from base class |