Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ if (typeof process !== 'undefined' && process) {
module.exports = globalThis.WebSocket
} else {
// Node.js or Electron browser process
// eslint-disable-next-line unicorn/no-typeof-undefined
Copy link

Choose a reason for hiding this comment

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

Is there a good reason for typeof? Why not just globalThis.WebSocket === undefined?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the reason is If WebSocket is not defined (like in older browsers versions, server-side runtimes etc), directly accessing globalThis.WebSocket might throw a ReferenceError if WebSocket is not even declared.

Using typeof ensures the check does not break, as typeof safely returns 'undefined' instead of throwing an error.

if (typeof globalThis.WebSocket === 'undefined') {
// Fall back to ws
module.exports = require('ws')
Expand Down