@codesandbox/pitcher-client

中文文档
1.1.7发布于 2025/05/12GPL-3.0TypeScriptESM + CJS近 30 天下载 2.2万

How it works

原始使用文档
文档目录3 节
  1. How it works
  2. How to write tests
  3. How does it work?

pitcher-client

How it works

pitcher-client is exposed as a class PitcherClient which is responsible for keeping a connection to a single VM, exposing an imperative API to send requests, listen to notifications etc. and also holds internal state for simpler consumption.

There are really three levels to PitcherClient. The main class is instantiated in a CONNECTED state and will only move between CONNECTING, DISCONNECTED and CONNECTED based on the state of its passed connection. The Connection class holds connection states for the initial VM request and the websocket connection, while the WebsocketClient holds connection states for the websocket connection.

All messages are managed by the PitcherClientMessageHandler and PitcherMssageHandler (Should probably be a single class), where serialisation, deserialisation, queuing and matching of messages are handled.

How to write tests

All tests in pitcher-client can be written as full integration tests, where the only mocking is responses from Pitcher.

describe("Something", () => {
  test("should do something", async () => {
    const { client, pitcher, dispose } = await initPitcherClientMock({
      // This object represents all the responses you want Pitcher to give,
      // typed and ready to roll
      "file/open": ({ params: { id } }) => ({
        method: "file/open",
        status: PitcherResponseStatus.RESOLVED,
        result: {
          clients: {},
          content: "",
          document: { clients: {}, revision: 0 },
          id,
          isBinary: false,
          savedHash: "cc",
        },
      }),
    });

    // Use pitcher client as normal
    client.fs.getFile("someId");

    // Send a notification form Pitcher to the client
    await pitcher.sendNotification({
      method: "file/documentOperation",
      params: {
        id: file.id,
        revision: 1,
        operation: incoming.toJSON(),
        reason: ot.OperationReason.USER,
      },
    });

    // Not strictly required, but nice to clean up tests
    dispose();
  });
});

NOTE! If pitcher-client sends a request you do not have a handler for, it will throw an error and tell you which handler is missing.

How does it work?

  • The initPitcherClientMock returns initPitcherClient by giving it a mocked request for an instance
  • The jest-websocket-mock library is used to set up a new mocked WebSocket server for each use of initPitcherClientMock

版本与发布记录

@codesandbox/pitcher-client 共发布 8 个版本,首次发布于 2024/12/12;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.1.7latest2025/05/121.5 年前
1.1.62025/05/121.5 年前
1.1.52025/01/301.8 年前
1.1.42025/01/301.8 年前
1.1.12025/01/251.8 年前
1.1.02025/01/241.8 年前

关于 @codesandbox/pitcher-client 的常见问题

@codesandbox/pitcher-client 是什么?

How it works

如何安装 @codesandbox/pitcher-client?

在项目目录运行 npm install @codesandbox/pitcher-client,或使用 pnpm add / yarn add / bun add。

@codesandbox/pitcher-client 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@codesandbox/pitcher-client 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@codesandbox/pitcher-client 可以商用吗?

@codesandbox/pitcher-client 使用 GPL-3.0 许可。强 Copyleft:分发包含该代码的软件时,整个作品需以 GPL-3.0 兼容许可开源,并附带专利与反锁定条款。仅内部使用不触发此义务。以上为概括,不构成法律意见,请以许可原文为准。

@codesandbox/pitcher-client 的下载量有多少?

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

@codesandbox/pitcher-client 还在维护吗?

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