Skip to content

fix(util): detect Cloudflare Workers in shouldUseGlobalFetchAndWebSocket#11456

Open
Muhammad-Bin-Ali wants to merge 1 commit intodiscordjs:mainfrom
Muhammad-Bin-Ali:fix-shouldUseGlobalFetchAndWebSocket-for-cloudflare-workers
Open

fix(util): detect Cloudflare Workers in shouldUseGlobalFetchAndWebSocket#11456
Muhammad-Bin-Ali wants to merge 1 commit intodiscordjs:mainfrom
Muhammad-Bin-Ali:fix-shouldUseGlobalFetchAndWebSocket-for-cloudflare-workers

Conversation

@Muhammad-Bin-Ali
Copy link

@Muhammad-Bin-Ali Muhammad-Bin-Ali commented Mar 18, 2026

Closes #11455

Problem

shouldUseGlobalFetchAndWebSocket() returns false on Cloudflare Workers with nodejs_compat, causing client.login() to hang indefinitely.

Workers with nodejs_compat polyfills globalThis.process including process.versions.node. The function sees process, checks for deno/bun in versions, finds neither, and returns false (defaulting to the ws npm package (TCP sockets)). Workers doesn't support TCP sockets, so the gateway connection hangs forever with no error.

Fix

Check for globalThis.WebSocketPair (a Workers-only global) before the Deno/Bun branch. This is the same detection approach already used by getUserAgentAppendix() in the same file.

Workers natively supports both globalThis.WebSocket and globalThis.fetch, so returning true is correct for both consumers (@discordjs/ws and @discordjs/rest).

Changes

  • packages/util/src/functions/runtime.ts — added WebSocketPair check (8 lines)
  • packages/util/__tests__/runtime.test.ts — new file, 6 tests covering every branch

@vercel
Copy link

vercel bot commented Mar 18, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

2 Skipped Deployments
Project Deployment Actions Updated (UTC)
discord-js Ignored Ignored Mar 19, 2026 3:18pm
discord-js-guide Ignored Ignored Mar 19, 2026 3:18pm

Request Review

@coderabbitai
Copy link

coderabbitai bot commented Mar 18, 2026

📝 Walkthrough

Walkthrough

This PR enhances the shouldUseGlobalFetchAndWebSocket runtime detection function by adding an early check for WebSocketPair on globalThis to detect Cloudflare Workers environments, and introduces comprehensive test coverage for the function across multiple runtime scenarios (browser, Node.js, Deno, Bun, and Cloudflare Workers).

Changes

Cohort / File(s) Summary
Test Coverage
packages/util/__tests__/runtime.test.ts
Added 45 lines of test cases validating shouldUseGlobalFetchAndWebSocket behavior across six runtime environments: browser with/without fetch/WebSocket, Cloudflare Workers with node compatibility, Node.js, Deno, and Bun.
Runtime Detection Logic
packages/util/src/functions/runtime.ts
Added early return condition checking for WebSocketPair on globalThis to detect Cloudflare Workers before evaluating process versions.

Possibly related issues

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically identifies the main fix: detecting Cloudflare Workers in the shouldUseGlobalFetchAndWebSocket function, which matches the primary change in the changeset.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Description check ✅ Passed The PR description clearly explains the problem, the fix, and the specific changes made, directly aligned with the changeset.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Tip

Migrating from UI to YAML configuration.

Use the @coderabbitai configuration command in a PR comment to get a dump of all your UI settings in YAML format. You can then edit this YAML file and upload it to the root of your repository to configure CodeRabbit programmatically.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/util/__tests__/runtime.test.ts`:
- Around line 16-23: The test mutates globals with delete which
vi.unstubAllGlobals() won't restore; update the test "GIVEN browser env without
fetch or WebSocket THEN returns false" to avoid direct deletion by using
vi.stubGlobal('fetch', undefined) and vi.stubGlobal('WebSocket', undefined) (or
alternatively capture original descriptors for globalThis.fetch and
globalThis.WebSocket and restore them in a try/finally), then assert
shouldUseGlobalFetchAndWebSocket() returns false and ensure cleanup/un-stubbing
occurs so other tests are not affected.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 7fe9722e-9dbb-4d35-8416-f382e808b8f8

📥 Commits

Reviewing files that changed from the base of the PR and between 2a06721 and 40d5344.

📒 Files selected for processing (2)
  • packages/util/__tests__/runtime.test.ts
  • packages/util/src/functions/runtime.ts
📜 Review details
🔇 Additional comments (2)
packages/util/src/functions/runtime.ts (1)

9-15: Cloudflare Workers short-circuit is correctly prioritized.

This check is in the right place (before the process.versions runtime branch) and correctly prevents Workers + nodejs_compat from falling into the Node.js path. It aligns with how runtime selection is consumed in packages/ws/src/ws/WebSocketShard.ts:87-89 and packages/rest/src/index.ts:7.

packages/util/__tests__/runtime.test.ts (1)

25-44: Runtime-branch coverage is solid for the new behavior.

The Workers (WebSocketPair) case plus Node, Deno, and Bun paths are clearly asserted and match the updated runtime detection contract.

@Muhammad-Bin-Ali Muhammad-Bin-Ali force-pushed the fix-shouldUseGlobalFetchAndWebSocket-for-cloudflare-workers branch from 40d5344 to cfab41c Compare March 19, 2026 15:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

shouldUseGlobalFetchAndWebSocket() returns false on Cloudflare Workers with nodejs_compat

2 participants