关于 edgeone-makers-edge-functions
用于编写 Makers Edge Functions 的技能,介绍基于 V8 的 JavaScript 运行时、路由、上下文 API、KV 前置条件、支持的 Web API 与运行限制。
主要功能
- 01V8 运行时指导
- 02HTTP 方法处理器与动态路由
- 03EventContext API 参考
- 04KV 存储配置指导
- 05支持的 API 与运行限制
适用场景
- 01编写 Edge Function API 端点
- 02处理动态 URL 参数
- 03在边缘函数中使用 KV 存储
安装
复制下面的命令,在终端中运行。
npx skills add https:/ / github.com/ tencentedgeone/ edgeone-makers-tools --skill edgeone-makers-edge-functions安装后在 Claude Code 中输入/edgeone-makers-edge-functions确认已加载。
详细介绍
Edge Functions
基于 V8 的轻量级函数,运行于边缘节点。适用于简单 API、KV 存储访问和超低延迟响应。
运行时: V8(类似 Cloudflare Workers)——不是 Node.js。请勿使用 Node.js 内置模块或 npm 包。
⚠️ 此 V8 运行时不支持
Response.json()。请始终使用new Response(JSON.stringify(data), { headers: { 'Content-Type': 'application/json' } })。
基本函数
文件:edge-functions/api/hello.js
export default function onRequest(context) {
return new Response('Hello from Edge Functions!');
}
访问路径:GET /api/hello
HTTP 方法处理程序
// edge-functions/api/users.js
// Handle all methods
export function onRequest(context) {
return new Response('Any method');
}
// Or use specific method handlers:
export function onRequestGet(context) {
return new Response(JSON.stringify({ users: [] }), {
headers: { 'Content-Type': 'application/json' },
});
}
export async function onRequestPost(context) {
const body = await context.request.json();
return new Response(JSON.stringify({ created: true }), {
status: 201,
headers: { 'Content-Type': 'application/json' },
});
}
EventContext 对象
export function onRequest(context) {
const {
request, // Standard Request object
params, // Dynamic route params, e.g. { id: "123" }
env, // Environment variables from Makers console
waitUntil, // Extend function lifetime for async tasks
} = context;
// GEO info available via:
const geo = context.request.eo;
// geo.geo.countryName, geo.geo.cityName, geo.geo.latitude, etc.
return new Response('OK');
}
动态路由
// edge-functions/api/users/[id].js
// Matches: /api/users/123, /api/users/abc
export function onRequestGet(context) {
const userId = context.params.id;
return new Response(JSON.stringify({ userId }), {
headers: { 'Content-Type': 'application/json' },
});
}
// edge-functions/api/[[default]].js
// Catches all unmatched routes under /api/
export function onRequest(context) {
return new Response('Catch-all route', { status: 404 });
}
KV 存储(仅限 Edge Functions)
交叉参考:如果代码使用
context.store或 KV API,请同时阅读../makers-storage/SKILL.md。
⚠️ 前置条件:使用 KV 前,必须在 EdgeOne Makers 控制台中启用 KV Storage、创建命名空间,并将其绑定到项目。完整设置说明请参阅 ../makers-storage/references/kv.md。
KV 命名空间是一个全局变量(绑定时在控制台中设置名称),不在 context.env 上。
// edge-functions/api/counter.js
export async function onRequest(context) {
// ⚠️ my_kv is a GLOBAL variable (name set when binding namespace in console)
// Do NOT use context.env.KV ❌
// Read
const count = await my_kv.get('page_views') || '0';
const newCount = parseInt(count) + 1;
// Write
await my_kv.put('page_views', String(newCount));
return new Response(JSON.stringify({ views: newCount }), {
headers: { 'Content-Type': 'application/json' },
});
}
KV Storage API 的完整参考和使用指南,请参阅:../makers-storage/references/kv.md。
支持的运行时 API
Edge Functions 运行于 V8,并支持以下 Web 标准 API:
- Fetch API — 使用
fetch()发起出站 HTTP 请求 - Cache API —
caches.open()、cache.match()、cache.put() - Headers / Request / Response — 标准 Web API 对象
- Streams —
ReadableStream、WritableStream、TransformStream - Web Crypto — 使用
crypto.subtle进行加密和签名 - Encoding —
TextEncoder、TextDecoder - URL / URLSearchParams — URL 解析
⚠️ 不支持:Node.js 内置模块(Node 的 fs、path、http、crypto)、require()、npm 包。
限制
| 资源 | 限制 |
|---|---|
| 代码包大小 | 5 MB |
| 请求正文 | 1 MB |
| 每次调用的 CPU 时间 | 200 ms |
| 语言 | 仅支持 JavaScript (ES2023+) |