@endo/captp

中文文档
4.5.1发布于 2026/05/28Apache-2.0ES Module近 30 天下载 2.6万

Capability Transfer Protocol for distributed objects

原始使用文档
文档目录3 节
  1. Usage
  2. Loopback
  3. TrapCaps

@endo/captp

A minimal CapTP implementation leveraging Agoric's published modules.

Usage

NOTE: myconn below is not part of the CapTP library, it represents a connection object that you have created where makeCapTP is called on both sides of the connection, passing in the function to send a JSON-able object on the connection, and returning a dispatch function to receive a decoded JSON object from the connection.

import { E, makeCapTP } from '@endo/captp';

// Create a message dispatcher and bootstrap.
// Messages on myconn are exchanged with JSON-able objects.
const { dispatch, getBootstrap, abort } = makeCapTP('myid', myconn.send, myBootstrap);
myconn.onReceive = obj => dispatch(obj);

// Get the remote's bootstrap object and call a remote method.
E(getBootstrap()).method(args).then(res => console.log('got res', res));

// Tear down the CapTP connection if it fails (e.g. connection is closed).
abort(Error('Connection aborted by user.'));

Loopback

The makeLoopback() function creates an async barrier between "near" and "far" objects. This is useful for testing and isolation within the same address space.

TrapCaps

In addition to the normal CapTP facilities, this library also has the notion of "TrapCaps", which enable a "guest" endpoint to call a "host" object (which may resolve an answer promise at its convenience), but the guest synchronously blocks until it receives the resolved answer.

This is a specialized and advanced use case, not for mutually-suspicious CapTP parties, but instead for clear "guest"/"host" relationship, such as user-space code and synchronous devices.

  1. Supply the trapHost and trapGuest protocol implementation (such as the one based on SharedArrayBuffers in src/atomics.js) to the host and guest makeCapTP calls.
  2. On the host side, use the returned makeTrapHandler(target) to mark a target as synchronous-enabled.
  3. On the guest side, use the returned Trap(target) proxy maker much like E(target), but it will return a synchronous result. Trap will throw an error if target was not marked as a TrapHandler by the host.

To understand how trapHost and trapGuest relate, consider the trapHost as a maker of AsyncIterators which don't return any useful value. These specific iterators are used to drive the transfer of serialized data back to the guest.

trapGuest receives arguments to describe the specific trap request, including startTrap() which sends data to the host to perform the actual work of the trap. The returned (synchronous) iterator from startTrap() drives the async iterator of the host until it fully transfers the trap results to the guest, and the guest unblocks.

The Loopback implementation provides partial support for TrapCaps, except it cannot unwrap promises. Loopback TrapHandlers must return synchronously, or an exception will be thrown.

版本与发布记录

@endo/captp 共发布 52 个版本,首次发布于 2022/01/23;过去 12 个月发布了 2 个版本。

版本发布日期距今
4.5.1latest2026/05/285 个月前
4.5.02026/02/278 个月前
4.4.82025/07/121.3 年前
4.4.72025/06/181.4 年前
4.4.62025/06/031.4 年前
4.4.52025/03/251.6 年前

关于 @endo/captp 的常见问题

@endo/captp 是什么?

Capability Transfer Protocol for distributed objects

如何安装 @endo/captp?

在项目目录运行 npm install @endo/captp,或使用 pnpm add / yarn add / bun add。

@endo/captp 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/endo__captp 是否存在。

@endo/captp 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@endo/captp 可以商用吗?

@endo/captp 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@endo/captp 的下载量有多少?

根据 npm 官方下载统计,@endo/captp 在 2026/10/14 – 2026/11/12 这 30 天内被下载 26,199 次,日均约 873 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@endo/captp 还在维护吗?

最新版本 4.5.1 发布于 2026/05/28(5 个月前);至今共发布 52 个版本,其中过去 12 个月发布了 2 个;首次发布于 2022/01/23。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。