JS Runtime 外部注入能力
注入入口:server/src/js_runtime/mod.rs 的 init_js_runtime_globals。
自定义注入
全局函数
globalThis.nodeget(json)— 调用 NodeGet JSON-RPC API,支持以下调用方式:nodeget(json)— 传入完整的 JSON-RPC 请求(string 或 object),返回解析后的 JS 对象nodeget(method, params)— 快捷方式,自动构造{ jsonrpc: "2.0", method, params, id: randomUUID() }nodeget(method, params, id)— 同上,但指定请求 id
globalThis.inlineCall(js_worker_name, params, timeout_sec?)— 调用其他 JS WorkerglobalThis.randomUUID()— 生成随机 UUID v4 字符串
runtimeCtx(handler 第三参数)
脚本 handler 签名为 handler(input, env, runtimeCtx),其中 runtimeCtx 包含以下属性:
runtimeCtx.runType— 当前运行类型字符串:"onCall"/"onCron"/"onRoute"/"onInlineCall"runtimeCtx.workerName— 当前 Worker 的名字runtimeCtx.inlineCall(js_worker_name, params, timeout_sec?)— 等价于globalThis.inlineCallruntimeCtx.inlineCaller— 调用当前脚本的调用者脚本名;顶层调用时为null
llrt_* 模块支持
llrt_fetch::initfetch、Request、Response、Headers、FormData
llrt_buffer::initBuffer、Blob、File、atob、btoa
llrt_stream_web::initReadableStream、WritableStream、TransformStream
llrt_url::initURL、URLSearchParams
llrt_util::initTextEncoder、TextDecoder
llrt_timers::initsetTimeout、clearTimeout、setInterval、clearInterval、setImmediate、queueMicrotask
