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

オープン 初心者向け
#444 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
62/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
react-native, typescript

調査の方向性

バグは packages/partysocket/src/index.ts の generateUUID()(40〜61行目付近)にあります。ここでは crypto?.randomUUID と performance?.now が、宣言されていない可能性のあるグローバルを参照しています。まず issue の再現手順を Reflect.deleteProperty(globalThis, "crypto") の後で実行し、次にこれらの参照を 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 はありません

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

cloudflare/partykit のほかの issue

cloudflare/partykit の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。