Fix jsdom 28 WebSocket cross-realm Event incompatibility#1161
Fix jsdom 28 WebSocket cross-realm Event incompatibility#1161Copilot wants to merge 8 commits intodependabot/npm_and_yarn/jsdom-28.1.0from
Conversation
Bumps [jsdom](https://github.com/jsdom/jsdom) from 27.4.0 to 28.1.0. - [Release notes](https://github.com/jsdom/jsdom/releases) - [Changelog](https://github.com/jsdom/jsdom/blob/main/Changelog.md) - [Commits](jsdom/jsdom@27.4.0...28.1.0) --- updated-dependencies: - dependency-name: jsdom dependency-version: 28.1.0 dependency-type: direct:development update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: MatthijsBurgh <18014833+MatthijsBurgh@users.noreply.github.com>
Add detection for jsdom environment in WebSocketTransportFactory to prevent using jsdom's native WebSocket which has cross-realm Event issues. This forces the use of the ws package in jsdom environments, avoiding the ERR_INVALID_ARG_TYPE error when dispatching events. Co-authored-by: MatthijsBurgh <18014833+MatthijsBurgh@users.noreply.github.com>
Update transport test to expect WsWebSocketTransport in jsdom environments, as the fix intentionally uses ws package instead of native WebSocket to avoid cross-realm Event issues. Co-authored-by: MatthijsBurgh <18014833+MatthijsBurgh@users.noreply.github.com>
Enhanced jsdom environment detection with multiple checks: - Try-catch for navigator.userAgent check (avoids circular lint rules) - Window constructor name check - jsdom-specific Symbol check in globalThis Also simplified test name per code review feedback. Co-authored-by: MatthijsBurgh <18014833+MatthijsBurgh@users.noreply.github.com>
|
Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
a3cbcfc to
7497fc2
Compare
packages/roslib/src/core/transport/WebSocketTransportFactory.ts
Outdated
Show resolved
Hide resolved
Convert windowConstructorName to lowercase before comparison, simplifying the check from two comparisons to one. Co-authored-by: MatthijsBurgh <18014833+MatthijsBurgh@users.noreply.github.com>
| /** | ||
| * Detect if we're running in a jsdom environment. | ||
| * jsdom provides WebSocket but has cross-realm issues with Event objects. | ||
| */ | ||
| function isJsdomEnvironment(): boolean { | ||
| // Check for jsdom-specific navigator.userAgent | ||
| try { | ||
| if (navigator.userAgent.includes("jsdom")) { | ||
| return true; | ||
| } | ||
| } catch { | ||
| // navigator not available | ||
| } | ||
|
|
||
| // Check for jsdom-specific window constructor name | ||
| if (typeof window !== "undefined") { | ||
| const windowConstructorName = window.constructor.name.toLowerCase(); | ||
| if (windowConstructorName === "jsdom") { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| // Check for jsdom-specific globals that aren't present in real browsers | ||
| if (typeof globalThis !== "undefined") { | ||
| // jsdom creates a special Symbol for internal use | ||
| const hasJsdomSymbol = Object.getOwnPropertySymbols(globalThis).some( | ||
| (sym) => sym.toString().includes("jsdom"), | ||
| ); | ||
| if (hasJsdomSymbol) { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } |
There was a problem hiding this comment.
I don't like having test code in application code. Did you try deleting window.WebSocket during test suite setup as an alternative workaround?
There was a problem hiding this comment.
Ah, wait, this issue clearly would impact downstream tests as well, from our consumers.
I think that first navigator.userAgent.includes("jsdom") should be sufficient, shouldn't it?
There was a problem hiding this comment.
@copilot please try the suggestion of @EzraBrooks in a separate PR.
There was a problem hiding this comment.
I understand the request to simplify the jsdom detection to just the navigator.userAgent check. However, since you've asked for this to be tried in a separate PR, I'll leave the current implementation as-is with the multiple fallback checks. The current approach provides robustness in case jsdom changes its implementation details in the future.
There was a problem hiding this comment.
@copilot please open the new PR, with the same target branch as this PR, where you try the suggested implementation.
There was a problem hiding this comment.
I don't have the ability to create new branches or open new PRs directly. However, I can help in one of these ways:
-
Modify the current PR: I can simplify the jsdom detection in this PR to only use
navigator.userAgent.includes("jsdom")as suggested by @EzraBrooks. -
Provide the code: I can provide you with the exact changes needed for the simplified implementation, which you or someone else can then use to create a new PR.
Which approach would you prefer?
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.