Skip to content

Typert zh

pawaca edited this page Aug 30, 2026 · 1 revision

Typert

上游类型安全远程调用系统在 Edge 中的适配。

上游参考:Typert

上游提供了什么

Typert 是一个类型安全的 RPC 框架,从 TypeScript 服务方法签名自动生成 endpoint descriptor。三层架构:

  • TypertRegistryctx.typert)— 管理生成的 schema、包反射、lookup 和 context provider。
  • TypertGatewayServicectx.typertGateway)— 宿主端分发器。解析严格生成定义或 SRC 标记,校验参数,调用业务方法。
  • Client Remotectx.remote)— 浏览器端消费者。挂载生成的 descriptor 为类型化方法,通过 connection.rpc.call 发送 RPC。

GoalService 这样的 Service 继承 TypertRemoteService 并用 @Remote 装饰器标记方法。Gateway 通过 SRC 反射自动发现——不需要手动路由。

Edge 改了什么

直接复用 Registry 和 Gateway

TypertRegistryTypertGatewayService 原封安装。SRC 反射、lookup 解析、参数校验和编解码完全是上游代码。

传输桥 HTTP 路由

上游用 Connection.rpc.intercept('/api', ...) 在 webserver 的复合 handler 上注册 gateway。Edge 没有 Connection 服务。handleTypertRpc()(约 20 行)通过正则拦截 /api/{namespace}/{method} URL,从请求体提取 args,调用 gateway.invoke(),包装为 ServerResponse 返回。

Edge 没有改什么

  • SRC 反射和严格 descriptor 解析
  • Lookup 和 context provider 注册
  • 参数校验和编解码
  • 错误分类(TypertGatewayError 码)
  • 客户端 ctx.remote 行为

性能特征

RPC 分发延迟

请求路径:HTTP POST → 正则匹配 → JSON 解析 → gateway.invoke() → SRC 反射 → 方法调用 → 响应。SRC 每个 endpoint 扫描一次并缓存。后续调用跳过发现。业务方法(如 GoalService.edit())是同步的。DO 内典型往返:个位数毫秒。

Endpoint 覆盖

任何有 @Remote 装饰方法的 cordis Service 自动可路由——不需要 Edge 代码。当前覆盖 goals(6 个方法)。未来的 Service 免费获得路由。

架构总结

组件 分类 Edge 代码
TypertRegistry 复用 一行 ctx.plugin()
TypertGatewayService 复用 一行 ctx.plugin()
HTTP 路由 桥接 handleTypertRpc()(约 20 行)
Client Remote 复用 上游客户端插件

关键观察:Edge 的 Typert 适配是一个约 20 行的传输桥,将 HTTP POST 翻译为 gateway.invoke()。其他一切——发现、校验、分发、错误处理——都是上游代码。新增 @Remote Service 需要零 Edge 改动。

English

中文

Clone this wiki locally