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

Open Beginner friendly
#444 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
62/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
react-native, typescript

Research direction

The bug is in generateUUID() in packages/partysocket/src/index.ts (around lines 40-61), where crypto?.randomUUID and performance?.now reference globals that may be undeclared. Start by running the repro from the issue after Reflect.deleteProperty(globalThis, "crypto"), then guard those lookups with typeof checks so the fallback generator runs. Done means new PartySocket() without an id succeeds when crypto is absent, and the partysocket test suite passes. The author has a prepared fix on a fork at commit 5a52347 that can be used as a reference.

Written by the indexing model from the issue text.

Description

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

Dominant language
TypeScript
Stars
1.3k
Forks
86
PR merge metrics
No merged PRs in 30d

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from cloudflare/partykit

All issues in cloudflare/partykit

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.