fix(perps): investigate Failed to execute 'dispatchEvent' on 'EventTarget': parameter 1 is not of type 'Event'#30612
Conversation
…rget': parameter 1 is not of type 'Event'.
Replace event-target-shim's Event/EventTarget globals with React Native's own implementations from react-native/src/private/webapis/. This fixes the "parameter 1 is not of type 'Event'" TypeError when @nktkas/rews dispatches CloseEvent on the native WebSocket, because RN's EventTarget.dispatchEvent validates event instanceof using its own Event class.
|
CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes. |
Worker reportReport — TAT-3223: Fix CloseEvent dispatchEvent TypeErrorSummaryThe Root cause
React Native's When Reproduction commitSHA: Metro log excerpt: Changes
Test planAutomated
Manual (Gherkin)Given the app is running with the Hyperliquid SDK connected
When a WebSocket connection is closed while in CONNECTING state
Then no TypeError "parameter 1 is not of type 'Event'" is thrown
And the CloseEvent is dispatched successfully on the native WebSocketEvidence
Ticket |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 4d38898. Configure here.
Switched to RN native Event classes in prior commit, making event-target-shim a dead direct dependency flagged by depcheck.
🔍 Smart E2E Test Selection
click to see 🤖 AI reasoning detailsE2E Test Selection:
Impact Analysis:
Tag Selection:
Other tags (SmokeSwap, SmokeAccounts, SmokeBrowser, etc.) are not directly impacted as the WebSocket event polyfill change is specifically for the Hyperliquid SDK used by Perps. Performance Test Selection: |
|




Description
Fix
TypeError: Failed to execute 'dispatchEvent' on 'EventTarget': parameter 1 is not of type 'Event'crash caused by theCloseEventpolyfill inshim.jsusingevent-target-shim'sEventclass instead of React Native's ownEventclass. When@nktkas/rews(Hyperliquid SDK WebSocket transport) dispatches aCloseEventon the native WebSocket, RN'sdispatchEventvalidatesevent instanceof RNEvent— which failed becauseevent-target-shimprovides a differentEventclass.Replaced
event-target-shimglobals with React Native's ownEvent,EventTarget,CloseEvent, andMessageEventclasses so allinstanceofchecks pass consistently.Changelog
CHANGELOG entry: Fixed a crash caused by CloseEvent dispatch on WebSocket failing instanceof validation
Related issues
Fixes: TAT-3223
Manual testing steps
Screenshots/Recordings
State-only fix: no visual evidence needed. Both ACs proven via CDP eval (CloseEvent dispatch success) and lint:tsc (no TS errors).
Pre-merge author checklist
Pre-merge reviewer checklist
Validation Recipe
recipe.json
{ "pr": "TAT-3223", "title": "CloseEvent dispatchEvent on native WebSocket must not throw TypeError", "jira": "TAT-3223", "acceptance_criteria": [ "CloseEvent dispatched on native WebSocket must not throw TypeError", "No new TypeScript errors introduced by the fix" ], "validate": { "static": ["yarn lint:tsc"], "workflow": { "pre_conditions": ["wallet.unlocked"], "entry": "ac1-eval-closeevent-dispatch", "nodes": { "ac1-eval-closeevent-dispatch": { "action": "eval_sync", "expression": "(function() { try { var ws = new WebSocket('wss://echo.websocket.org'); var ce = new CloseEvent('close', {code: 1006, reason: '', wasClean: false}); ws.dispatchEvent(ce); ws.close(); return JSON.stringify({success: true, error: null}); } catch(e) { return JSON.stringify({success: false, error: e.message}); } })()", "assert": { "operator": "eq", "field": "success", "value": true }, "next": "ac1-eval-closeevent-props" }, "ac1-eval-closeevent-props": { "action": "eval_sync", "expression": "(function() { var ce = new CloseEvent('close', {code: 1006, reason: 'test', wasClean: true}); return JSON.stringify({type: ce.type, code: ce.code, reason: ce.reason, wasClean: ce.wasClean}); })()", "assert": { "all": [ { "operator": "eq", "field": "code", "value": 1006 }, { "operator": "eq", "field": "reason", "value": "test" }, { "operator": "eq", "field": "wasClean", "value": true } ] }, "next": "ac1-eval-messageevent-dispatch" }, "ac1-eval-messageevent-dispatch": { "action": "eval_sync", "expression": "(function() { try { var ws = new WebSocket('wss://echo.websocket.org'); var me = new MessageEvent('message', {data: 'hello'}); ws.dispatchEvent(me); ws.close(); return JSON.stringify({success: true, error: null}); } catch(e) { return JSON.stringify({success: false, error: e.message}); } })()", "assert": { "operator": "eq", "field": "success", "value": true }, "next": "setup-done" }, "setup-done": { "action": "end", "status": "pass" } } } } }Recipe Workflow
workflow.mmd
graph TD ac1-eval-closeevent-dispatch["ac1-eval-closeevent-dispatch<br/>eval_sync: CloseEvent dispatch on native WS"] ac1-eval-closeevent-props["ac1-eval-closeevent-props<br/>eval_sync: Verify CloseEvent properties"] ac1-eval-messageevent-dispatch["ac1-eval-messageevent-dispatch<br/>eval_sync: MessageEvent dispatch on native WS"] setup-done["setup-done<br/>end: pass"] ac1-eval-closeevent-dispatch --> ac1-eval-closeevent-props ac1-eval-closeevent-props --> ac1-eval-messageevent-dispatch ac1-eval-messageevent-dispatch --> setup-doneNote
Medium Risk
Touches app bootstrap polyfills used by Hyperliquid WebSockets; low blast radius but wrong globals could break perps connectivity at runtime.
Overview
Fixes a Hyperliquid / perps WebSocket crash where
dispatchEventrejectedCloseEventbecause polyfilled events did not pass React Native’sinstanceof Eventcheck.shim.jsstops usingevent-target-shimand hand-rolledCloseEvent/MessageEventconstructors. When globals are missing, it assigns React Native’s ownEvent,EventTarget,CloseEvent, andMessageEventfrom RN private web API modules so events dispatched by@nktkas/rewsmatch what RN’s WebSocketEventTargetexpects.event-target-shimis removed frompackage.json/ lockfile.shim.test.jsadds unit coverage for RNCloseEventandMessageEventproperties and inheritance.Reviewed by Cursor Bugbot for commit 5d5cb89. Bugbot is set up for automated code reviews on this repo. Configure here.