JS 脚本编写规范
js-worker 的脚本应使用 ES Module 形式,export default 一个对象。
必要导出
建议始终导出:
js
export default {
async onCall(params, env, ctx) {
return { ok: true, from: "onCall", params, env };
},
async onInlineCall(params, env, ctx) {
return { ok: true, from: "onInlineCall", params, env };
},
async onCron(params, env, ctx) {
return { ok: true, from: "onCron", params, env };
},
async onRoute(request, env, ctx) {
return new Response("ok", { status: 200 });
}
};运行时根据 run_type 调用:
call->export default.onCall(...)inline_call->export default.onInlineCall(...)cron->export default.onCron(...)route->export default.onRoute(...)
参数约定
onCall / onInlineCall / onCron 入口签名:
js
async function handler(params, env, ctx) {}params:来自js-worker_run的paramsenv:来自js-worker_run.env或数据库保存的envctx:运行时上下文,当前包含:ctx.runType:当前入口名("onCall"/"onInlineCall"/"onCron"/"onRoute")ctx.workerName:当前 Worker 的名字ctx.inlineCall(js_worker_name, params, timeout_sec?):调用另一个 JS Worker 的onInlineCall,返回其结果;会写入js_resultctx.inlineCaller:调用者脚本名(如 A 通过inlineCall调 B,则 B 中该值为"A";顶层调用为null)
onRoute 入口签名:
js
async function onRoute(request, env, ctx) {}request:运行时直接传入的 Fetch 标准Request对象 可通过request.headers.get("NG-Connecting-IP")获取 TCP 对端 IP(头名大小写不敏感)。env:来自数据库保存的envctx:与其他入口一致
返回值约束
- 必须返回可 JSON 序列化的数据(对象/数组/字符串/数字/布尔/null)。
- 不允许返回
undefined。 onRoute必须返回Response对象。
可用能力
fetch:已注入,可直接发 HTTP 请求。globalThis.nodeget(...):已注入,支持三种调用方式:nodeget(json)— 传入完整 JSON-RPC 请求(string 或 object)nodeget(method, params)— 快捷方式,自动生成 idnodeget(method, params, id)— 快捷方式,指定 id 返回解析后的 JS 对象。
globalThis.randomUUID():已注入,生成随机 UUID v4 字符串。ctx.inlineCall:已注入,可await调用指定js_worker的onInlineCall。- 更多注入函数/对象见 injected。
推荐示例(同时使用 nodeget + fetch)
js
export default {
async onCall(params, env, ctx) {
// 快捷方式:nodeget(method, params)
const hello = await nodeget("nodeget-server_hello", []);
const resp = await fetch("https://httpbin.org/get");
const text = await resp.text();
return {
ok: true,
hello: hello.result,
fetch_status: resp.status,
body_preview: text.slice(0, 120),
params,
env
};
},
async onCron(params, env, ctx) {
return { ok: true, from: "cron", params, env };
},
async onRoute(request, env, ctx) {
const text = await request.text();
return new Response(
JSON.stringify({
ok: true,
method: request.method,
url: request.url,
text,
env
}),
{
status: 200,
headers: { "content-type": "application/json; charset=utf-8" }
}
);
}
};提交脚本时的编码
js-worker_create/js-worker_update传的是js_script_base64。- Base64 原文必须是 UTF-8 编码的 JS 源码。
预编译说明
- 创建/更新时会进行"仅编译"预检查,不会执行业务逻辑。
- 真正执行发生在
js-worker_run。 - HTTP 路由调用发生在
/worker-route/{route_name}。
