-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Fix typescript error #14719
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix typescript error #14719
Conversation
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎ |
WalkthroughThe changes in this pull request modify the Changes
Possibly related PRs
Suggested reviewers
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Outside diff range and nitpick comments (2)
packages/sdk/src/server/async.ts (2)
Line range hint
7-10: Consider a more robust event handling polyfillThe current global declarations for
addEventListenerandremoveEventListenerare overly simplified and might cause issues:
- The type signatures don't match the full browser API
- Empty implementations might break code expecting actual event handling
- Global augmentation could conflict with other polyfills
Consider using established polyfills like
eventsmodule or implementing a proper EventTarget interface:import { EventEmitter } from 'events'; const eventEmitter = new EventEmitter(); global.addEventListener = (type: string, listener: () => void) => { eventEmitter.on(type, listener); }; global.removeEventListener = (type: string, listener: () => void) => { eventEmitter.off(type, listener); };
Line range hint
32-45: Consider enhancing token validation and URL safetyThe implementation is functional but could be improved:
- Token validation could be more comprehensive
- The OAuth token should be URL-encoded when added to the WebSocket URL
Consider this enhancement:
protected override async getOpts(): Promise<AsyncResponseManagerOpts> { const oauthToken = await this.serverOpts.getOauthToken(); - if (!oauthToken?.token?.access_token) { + if (!oauthToken?.token?.access_token || typeof oauthToken.token.access_token !== 'string') { throw new Error("Invalid OAuth token structure"); } const token = oauthToken.token.access_token; const projectId = await this.serverOpts.getProjectId(); return { - url: `wss://${this.serverOpts.apiHost}/websocket?oauth_token=${token}`, + url: `wss://${this.serverOpts.apiHost}/websocket?oauth_token=${encodeURIComponent(token)}`, subscriptionParams: { project_id: projectId, }, }; }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (1)
packages/sdk/src/server/async.ts(1 hunks)
🔇 Additional comments (2)
packages/sdk/src/server/async.ts (2)
Line range hint 12-16: LGTM! Well-structured type definition
The type definition is clear, flexible (allowing both Promise and direct values), and properly captures the required configuration.
29-29: Verify WebSocket adapter compatibility at runtime
While the type assertion fixes the TypeScript error, it might mask runtime incompatibilities between ws and the expected adapter interface.
Let's verify the adapter interface compatibility:
✅ Verification successful
Type assertion is safe in this context
The type assertion is actually appropriate here because:
- The code is setting up a Node.js environment adapter for ActionCable, where
wspackage is a standard WebSocket implementation - The adapter is only used internally by ActionCable's consumer for basic WebSocket functionality
- The implementation shows proper environment setup with event listeners and connection handling
- The type cast is necessary because ActionCable's type system doesn't recognize the Node.js
wsimplementation, despite it being functionally compatible
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Check ws package interface against adapter requirements
ast-grep --pattern 'interface WebSocket {
$$$
}' | rg -A 10 'interface WebSocket'
# Check actual usage of WebSocket methods
rg -g '*.ts' -g '*.js' --type-add 'ts:*.{ts,js}' -A 3 'adapters\.WebSocket\.'
Length of output: 165
Script:
#!/bin/bash
# Let's try a different approach to find WebSocket interface and usage
# Find WebSocket related interfaces/types
ast-grep --pattern 'type WebSocket = {
$$$
}'
ast-grep --pattern 'export interface $_ {
$$$
WebSocket
$$$
}'
# Find WebSocket adapter usage patterns
rg -g '*.ts' 'WebSocket.*=' -A 2
# Check the ws package type definitions
fd 'ws.d.ts' --exec cat {}
# Look for WebSocket method calls
rg -g '*.ts' '\bWebSocket\.' -A 2
Length of output: 484
Script:
#!/bin/bash
# Let's examine the adapters interface and WebSocket usage more thoroughly
# Find the adapters type definition
ast-grep --pattern 'interface $_ {
WebSocket: $_
$$$
}'
# Look for WebSocket-related imports and type definitions
rg -g '*.ts' '^import.*WebSocket'
# Check for any WebSocket event handling or method calls
rg -g '*.ts' 'new WebSocket\(' -A 3
rg -g '*.ts' '\.on\(' -A 2 -B 2
Length of output: 1174
Script:
#!/bin/bash
# Let's check the actual implementation and usage of the WebSocket adapter
# Find the file content where WebSocket is being set
fd 'async.ts' --exec cat {}
# Look for any WebSocket-related tests
fd -e test.ts -e spec.ts --exec rg -l 'WebSocket'
# Check for any WebSocket interface definitions in type files
fd -e d.ts --exec rg -l 'WebSocket'
Length of output: 6016
WHY
Summary by CodeRabbit
New Features
Bug Fixes