mcpField
E
edgeone-makers-edge-functions

edgeone-makers-edge-functions

github.com/tencentedgeone开发1.8k253 次安装3 周前更新
代码仓库

关于 edgeone-makers-edge-functions

用于编写 Makers Edge Functions 的技能,介绍基于 V8 的 JavaScript 运行时、路由、上下文 API、KV 前置条件、支持的 Web API 与运行限制。

主要功能

  1. 01V8 运行时指导
  2. 02HTTP 方法处理器与动态路由
  3. 03EventContext API 参考
  4. 04KV 存储配置指导
  5. 05支持的 API 与运行限制

适用场景

  1. 01编写 Edge Function API 端点
  2. 02处理动态 URL 参数
  3. 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+)

评论

支持 Markdown