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

Đang mở Phù hợp với người mới
#444 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
62/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
react-native, typescript
Lĩnh vực
frontend, networking

Hướng nghiên cứu

Lỗi nằm ở generateUUID() trong packages/partysocket/src/index.ts (khoảng dòng 40-61), nơi crypto?.randomUUID và performance?.now tham chiếu đến các biến toàn cục có thể chưa được khai báo. Hãy bắt đầu bằng cách chạy bước tái tạo lỗi từ issue sau Reflect.deleteProperty(globalThis, "crypto"), rồi bảo vệ các truy cập đó bằng kiểm tra typeof để bộ sinh dự phòng được chạy. Hoàn thành khi new PartySocket() không có id thành công khi thiếu crypto, và bộ kiểm thử partysocket vượt qua. Tác giả đã chuẩn bị sẵn bản sửa trên một fork tại commit 5a52347, có thể dùng làm tham chiếu.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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).

Ngôn ngữ chính
TypeScript
Star
1.3k
Fork
86
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của cloudflare/partykit

Tất cả issue của cloudflare/partykit

Issue tương tự

Thêm issue về TypeScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.