partysocket: new PartySocket() throws ReferenceError where there is no global crypto

未关闭 适合新手
#444 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
62/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
react-native, typescript

调研方向

问题出在 packages/partysocket/src/index.ts 中 generateUUID()(约第 40-61 行),其中 crypto?.randomUUID 和 performance?.now 引用了可能未声明的全局变量。先在 Reflect.deleteProperty(globalThis, "crypto") 之后运行 issue 中的复现步骤,然后用 typeof 检查保护这些访问,使回退生成器能够运行。完成标准是:在缺少 crypto 时,不带 id 的 new PartySocket() 能成功,并且 partysocket 测试套件通过。作者在一个 fork 的提交 5a52347 中已准备好修复,可作为参考。

由索引模型根据 Issue 内容生成。

描述

generateUUID() checks crypto?.randomUUID. Optional chaining does not protect a global name that is not declared. So where there is no global crypto (for example React Native without a polyfill), new PartySocket() without an id throws ReferenceError: crypto is not defined, and the fallback generator below is never used. performance?.now has the same problem.

https://github.com/cloudflare/partykit/blob/partysocket%401.3.0/packages/partysocket/src/index.ts#L40-L61

Repro

The packages: [email protected]. Install them with npm i [email protected].

// npm install, then: node repro.ts (Node.js 22.18 or later runs TypeScript directly)
//
// React Native (Hermes) has no global crypto unless a polyfill adds one. This removes it to get the same
// environment.
Reflect.deleteProperty(globalThis, "crypto");
console.log("typeof crypto:", typeof crypto);

const { default: PartySocket } = await import("partysocket");
try {
  const socket = new PartySocket({ host: "example.com", room: "room-1", startClosed: true });
  console.log("id:", socket.id);
} catch (e) {
  console.log(`new PartySocket() threw: ${(e as Error).name}: ${(e as Error).message}`);
}

// The file is a module, so top-level await works.
export {};

node repro.ts (Node.js 22.18 or later runs TypeScript directly)

Actual

typeof crypto: undefined
new PartySocket() threw: ReferenceError: crypto is not defined

Expected

The fallback generator makes an id, and the socket is made.

A fix

A fix with a new test is on the branch fix/partysocket-no-global-crypto of my fork, at 5a52347. On a GitHub Actions runner, the new test fails before the change and passes after it, and the whole partysocket suite passes. This repository accepts pull requests only from collaborators, so I did not open a pull request. I can open one if you allow it, or you can take the commit.

I found this with AI assistance (Claude) during a review of WebSocket client libraries. The repro above was run with partysocket 1.3.0 on Node.js 24.22.2 on Linux (a GitHub Actions runner), and the output above is from that run. The fix was also prepared with AI assistance (Claude).

主要语言
TypeScript
星标
1.3k
派生
86
PR 合并指标
30 天内没有已合并 PR

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

cloudflare/partykit 的其他 Issue

查看 cloudflare/partykit 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。