agentsclimarketplace

Cloudflare workers edge

Skill findscripter/everything-skills/10-platform/cloudflare-workers-edge

当用 Cloudflare Workers 把逻辑部署到边缘、接入 KV/D1/R2/Durable Objects 存储、做边缘缓存与请求改写、降低延迟时使用;产出 wrangler.toml 配置、绑定接入与 fetch 处理器代码;不适用于传统 Node/Express、AWS Lambda、GCP Functions 或不用边缘特性的纯前端。触发词:Cloudflare Workers、Wrangler、KV/D1/R2、边缘From its SKILL.md

Install
npx -y skills add findscripter/everything-skills --skill cloudflare-workers-edge

Assembled from the repository path, not quoted from the project. Check it against their README if it does not work.

2 things to look at

  • no licenseNo license file was found in the repository. Code published without one is not open source by default, so using it at work is a question for whoever answers licensing questions where you are.
  • 1 stars1 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.

What its file declares

Copied from the file, not written here

The file declares its own license as MIT. That is the author’s claim about this one file, and it is not the same thing as the license GitHub reports for the repository, which is listed with the other numbers below.

SKILL.md

4.5 KB, ~1.1k tokens by cl100k_base, as published. Nobody here has run it

何时使用

适用:

  • 把无服务器函数部署到 Cloudflare 边缘网络,靠近用户降低延迟。
  • 接入边缘存储:KV(键值/读多写少)、D1(边缘 SQLite)、R2(对象存储)、Durable Objects(有状态协调、高并发)。
  • 在边缘改写请求/响应、注入安全头、做边缘缓存与重定向。
  • 用 Cloudflare Pages + Workers 搭建全栈应用。

不该用(负边界):

  • 任务面向传统 Node.js/Express 服务器应用。
  • 目标平台是 AWS Lambda 或 Google Cloud Functions(请用对应技能)。
  • 不涉及边缘特性的纯前端开发。

步骤 / 指令

  1. Wrangler 生态:用 wrangler.toml 做配置,本地测试用 npx wrangler dev
  2. Fetch API:Workers 运行在 Web 标准 Fetch API 之上,不存在 Node.js 全局对象;用到 fs/path 等需开启 Node.js 兼容模式(compatibility flags)。
  3. 绑定(Bindings):所有 KV、D1、密钥等绑定都在 wrangler.toml 中声明,并通过 fetch 处理器的 env 参数访问,如 env.MY_KV_NAMESPACE
  4. 冷启动:Workers 是 0ms 冷启动,但要控制打包体积,免费版上限 1MB,不要引入大型库。
  5. Durable Objects:需要有状态协调、强一致或高并发时使用。
  6. 非阻塞任务:响应发出后才需执行的日志、分析等异步任务,用 ctx.waitUntil() 包裹,避免阻塞响应。

示例

示例 1:带 KV 绑定的基础 Worker(TypeScript)

export interface Env {
  MY_KV_NAMESPACE: KVNamespace;
}

export default {
  async fetch(
    request: Request,
    env: Env,
    ctx: ExecutionContext,
  ): Promise<Response> {
    const value = await env.MY_KV_NAMESPACE.get("my-key");
    if (!value) {
      return new Response("Not Found", { status: 404 });
    }
    return new Response(`Stored Value: ${value}`);
  },
};

示例 2:在边缘改写响应、注入安全头(JavaScript)

export default {
  async fetch(request, env, ctx) {
    const response = await fetch(request);
    const newResponse = new Response(response.body, response);

    // 在边缘添加安全头
    newResponse.headers.set("X-Content-Type-Options", "nosniff");
    newResponse.headers.set(
      "Content-Security-Policy",
      "upgrade-insecure-requests",
    );

    return newResponse;
  },
};

注意事项

最佳实践:

  • env.VAR_NAME 读取密钥与环境变量。
  • Response.redirect() 做干净的边缘重定向。
  • wrangler tail 实时调试生产环境。
  • 不要引入大型库;Workers 的内存与 CPU 时间有限。
  • 不要直接用 fspath 等 Node.js 专属库,除非开启 Node.js 兼容模式。

排错:

  • 问题:请求超出 CPU 时间限制。
  • 解决:优化循环、减少 await 调用数量、把同步重计算移出请求/响应路径;不阻塞响应的任务交给 ctx.waitUntil()

约束提醒:

  • 仅在任务明确落在上述边缘计算范围内时使用本技能。
  • 输出不能替代针对具体环境的验证、测试或专家评审。
  • 缺少必要输入、权限、安全边界或验收标准时,先停下并询问澄清。

互见

  • AWS Lambda / Google Cloud Functions:换用各自平台的 Serverless 技能。
  • 传统 Node.js/Express 服务端:换用对应后端技能。

采编自 sickn33/antigravity-awesome-skills(MIT 许可)。

What ships with it

Read from the repository

Just SKILL.md. No reference files, no scripts.

Keep looking

Skills are one crate of 326,834. Ordering is by how many stacks a row turns up in, so the top of any crate is what has actually been picked rather than what has the most stars.