关于 edge-computing
用于编写边缘计算代码的技能资料,面向需要将计算部署到靠近用户位置的开发者。涵盖多个边缘平台,以及路由、缓存和有状态连接模式。
主要功能
- 01Cloudflare Workers、Deno Deploy 等平台参考
- 02按地理位置路由请求
- 03使用 KV 缓存响应
- 04A/B 测试与机器人检测
- 05使用 Durable Objects 管理有状态连接
- 06边缘运行时约束说明
适用场景
- 01按请求所在国家选择 API
- 02缓存源站响应以减少重复请求
- 03为用户分配 A/B 测试变体
- 04通过 WebSocket 实现聊天会话
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ thebeardedbearsas/ claude-craft --skill edge-computing安装后在 Claude Code 中输入/edge-computing确认已加载。
详细介绍
边缘计算 — Cloudflare Workers、Deno Deploy
全球低延迟计算,贴近用户。
平台
Cloudflare Workers — V8,300+ 个 POP,KV/Durable
Deno Deploy — Deno,30+ 个 POP,TypeScript
Vercel Edge — V8,20+ 个 POP,Next.js
Fastly Compute@Edge — WASM,70+ 个 POP
地理路由
export default {
async fetch(req) {
const api = req.cf.country === 'US' ? 'us-api' : 'eu-api';
return fetch(`https://${api}.example.com${req.url}`);
}
};
边缘缓存(KV)
const key = new URL(req.url).pathname;
let cached = await env.KV.get(key);
if (cached) return new Response(cached, { headers: { 'X-Cache': 'HIT' } });
const resp = await fetch('https://origin.com' + key);
const body = await resp.text();
await env.KV.put(key, body, { expirationTtl: 3600 });
return new Response(body, { headers: { 'X-Cache': 'MISS' } });
A/B 测试
const variant = Math.random() < 0.5 ? 'A' : 'B';
resp.cookies.set('ab_test', variant);
Bot 检测
if (/bot|crawler/i.test(req.headers.get('User-Agent'))) {
return new Response('Forbidden', { status: 403 });
}
Durable Objects(有状态)
export class ChatRoom {
async fetch(req) {
const [client, server] = Object.values(new WebSocketPair());
this.sessions.push(server); server.accept();
server.on('message', e => this.broadcast(e.data));
return new Response(null, { status: 101, webSocket: client });
}
}
限制
CPU 10-50ms → 卸载繁重任务
内存 128MB → 流式处理
无 FS → KV/R2
参见 @.claude/skills/wasm/SKILL.md
评论
支持 Markdown