适配器
选择适配器
为 Chrome 扩展、iframe 或本地 Node 进程选择适配器。
在各个上下文中安装 @nexus-js/core,并根据通信方式选择适配器。上下文的启动和连接目标的发现由应用负责。
| 通信场景 | 包 | 指南 |
|---|---|---|
| Chrome 扩展上下文 | @nexus-js/chrome | Chrome 扩展 |
| 父页面和浏览器 iframe | @nexus-js/iframe | Iframe |
| 通过 Unix 套接字通信的本地守护进程和 Node 客户端 | @nexus-js/node-ipc | Node IPC |
| 受控的浏览器、WebView 和 Node 客户端 | @nexus-js/websocket | WebSocket |
适配器提供初始化辅助函数、精确目标类型、平台通信通道和连接元数据。 连接目标与服务提供者的通用行为见核心概念。
Worker 与自定义运行时
在其他运行环境中,可以定义对应的 AdapterModel 并实现 IEndpoint<M>。以下片段假定应用已实现 createWorkerEndpoint():
import { Nexus } from "@nexus-js/core";
import type { AdapterModel, IEndpoint } from "@nexus-js/core";
interface WorkerModel extends AdapterModel {
contextMeta: { context: "worker" | "host" };
connectionMeta: { readonly transport: "worker-port" };
connectionTarget: { context: "worker"; workerId: string };
}
const workerNexus = new Nexus<WorkerModel>();
const endpoint: IEndpoint<WorkerModel> = createWorkerEndpoint();
workerNexus.configure({
endpoint: {
implementation: endpoint,
meta: { context: "host" },
},
});适配器负责接受 Port、连接和匹配精确目标,以及关闭平台资源。Core 负责服务调用、授权策略和会话生命周期。
多个通信网络
为各个独立的通信网络创建不同的具名 Nexus<M> 实例。
测试边界
使用应用测试替身测试调用 NexusInstance 的代码。真实通道、授权、页面重载和断开行为则通过适配器集成测试验证。