gateway-workers
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGateway Workers
Gateway Workers
Read the current Forward guide before implementing a Worker. It defines how Gateway invokes Workers on permitted HTTP traffic. Retrieve the relevant page below rather than copying setup, API payloads, or limits into the project from memory.
在实现 Worker 前,请阅读当前的 Forward 指南。该指南定义了 Gateway 如何在允许的 HTTP 流量上调用 Worker。请从下方获取相关页面,不要凭记忆将配置、API 载荷或限制复制到项目中。
Workflow
工作流
- Inspect the existing Worker and choose a Worker template for the required transformation, synthetic response, or background task.
- Export a named class extending
GatewayMiddlewarefromWorkerEntrypoint. Implementcloudflare:workersand return ahandle(request, context, next). Gateway calls this method through RPC. A defaultResponsehandler alone does not implement the Forward contract.fetch() - For TypeScript, follow the chosen template's current package installation and type-import instructions.
- Use the second argument for Gateway context. It describes the original request and stays unchanged when the Worker modifies the request. Keep it distinct from (Workers execution context) and
this.ctx(bindings).this.env - Continue the user's request with at most once, or return a synthetic response without calling it.
next(request)always targets the original destination. Changes to the destination URL or host are ignored, and it cannot redirect, retry, or fan out. Usenext()to send a request to a different destination. It creates an independent subrequest that does not inherit the user's Gateway policies or egress configuration.fetch()
- 检查现有 Worker,并为所需的转换、合成响应或后台任务选择一个 Worker 模板。
- 导出一个命名的 类,该类继承
GatewayMiddleware中的cloudflare:workers。实现WorkerEntrypoint并返回handle(request, context, next)。Gateway 通过 RPC 调用此方法。仅使用默认的Response处理器无法实现 Forward 契约。fetch() - 对于 TypeScript,请遵循所选模板当前的软件包安装和类型导入说明。
- 使用第二个参数获取 Gateway 上下文。它描述原始请求,并且当 Worker 修改请求时仍保持不变。务必将其与 (Workers 执行上下文)和
this.ctx(绑定项)区分开来。this.env - 最多调用一次 以继续处理用户请求,或者在不调用它的情况下返回合成响应。
next(request)始终指向原始目标位置。对目标 URL 或主机所做的更改会被忽略,并且它无法重定向、重试或进行扇出。若要向其他目标发送请求,请使用next()。它会创建一个独立的子请求,不会继承用户的 Gateway 策略或出口配置。fetch()
Context
上下文
Use the published types and context reference for field names and availability. Treat a falsy identity () as anonymous and guard optional posture values. Call or only when needed. Both getters can reject. Catch lookup failures separately from missing values and decide how to handle each when the Worker depends on them. Use Gateway-provided identity rather than client-supplied identity headers.
!identityawait context.identity()await context.devicePosture()请使用已发布的类型和上下文参考来确认字段名称和可用性。将假值的身份()视为匿名身份,并对可选的姿态值进行判空保护。仅在需要时调用 或 。两个 getter 都可能拒绝(reject)。应将查询失败与值不存在分开捕获,并在 Worker 依赖这些值时决定各自的处理方式。使用 Gateway 提供的身份信息,而不是客户端提供的身份标头。
!identityawait context.identity()await context.devicePosture()Gateway behavior
Gateway 行为
- Only the first matching Forward policy invokes a Worker. Worker invocation does not confirm successful body inspection. Follow the Forward guide's inspection order.
- Gateway inspects the final response, including synthetic responses. Modified request bytes are not inspected again before reaching the origin.
- Review the Forward guide's limitations before choosing traffic: HTTPS requires decryption, WebSockets bypass the Worker, and matching Workers VPC traffic is dropped. Worker invocation failures fail closed.
Validation
验证
Type-check against the installed Gateway types and test the named RPC entrypoint, including zero or one continuation call and the context values the Worker uses. Then test a permitted request through a scoped Gateway Forward policy. A direct request to a default handler does not validate the Gateway path. Follow the chosen template's test procedure and the Forward guide's troubleshooting steps.
fetch()Check the response and streaming rules: return a final status from to , return no body for HEAD requests or statuses , , and , and stream bodies rather than buffering them.
200599204205304根据已安装的 Gateway 类型进行类型检查,并测试命名的 RPC 入口点,包括零次或一次续接调用,以及 Worker 使用的上下文值。然后,通过限定范围的 Gateway Forward 策略测试一个允许的请求。直接向默认的 处理器发起请求无法验证 Gateway 路径。请遵循所选模板的测试流程和 Forward 指南中的故障排查步骤。
fetch()Related skills
相关 skill
- cloudflare-one: Gateway policy design, traffic paths, identity, TLS, and rollout.
- workers-best-practices: streaming, bindings, generated environment types, background work, observability, and runtime tests.
- CLI selection: choose the existing or
cf-cliskill for project setup and deployment.wrangler
- cloudflare-one:Gateway 策略设计、流量路径、身份、TLS 和 rollout。
- workers-best-practices:流式传输、绑定、生成的环境类型、后台工作、可观测性和运行时测试。
- CLI selection:选择现有的 或
cf-cliskill 进行项目初始化和部署。wrangler