-
Notifications
You must be signed in to change notification settings - Fork 19
fix: be defensive around use of window.WebSocket #24
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,8 @@ | |
| } | ||
| })(function(global, navigator) { | ||
|
|
||
| var WebSocket = global.WebSocket | ||
|
|
||
| var RobustWebSocket = function(url, protocols, userOptions) { | ||
| var realWs = { close: function() {} }, | ||
| connectTimeout, | ||
|
|
@@ -75,6 +77,12 @@ | |
| } | ||
|
|
||
| self.send = function() { | ||
| if (realWs.readyState !== self.OPEN) { | ||
| throw Object.assign( | ||
| new Error('An attempt was made to use a WebSocket that is not usable'), | ||
| { name: 'InvalidStateError' } | ||
| ) | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why would we try to re-implement the WebSocket standard? calling
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As previously stated -
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But that's not what this code is checking. If you want to check for th3 existence of send and throw an Error, that is acceptible. |
||
| return realWs.send.apply(realWs, arguments) | ||
| } | ||
|
|
||
|
|
@@ -93,7 +101,7 @@ | |
| } | ||
|
|
||
| self.open = function() { | ||
| if (realWs.readyState !== WebSocket.OPEN && realWs.readyState !== WebSocket.CONNECTING) { | ||
| if (realWs.readyState !== self.OPEN && realWs.readyState !== self.CONNECTING) { | ||
| clearPendingReconnectIfNeeded() | ||
| reconnectWhenOnlineAgain = false | ||
| explicitlyClosed = false | ||
|
|
@@ -239,5 +247,16 @@ | |
| } | ||
| } | ||
|
|
||
| ['CONNECTING' , 'OPEN', 'CLOSING', 'CLOSED'].forEach(function (name) { | ||
| const descriptor = { | ||
| writable: false, | ||
| enumerable: true, | ||
| configurable: false, | ||
| value: WebSocket[name], | ||
| } | ||
| Object.defineProperty(RobustWebSocket, name, descriptor) | ||
| Object.defineProperty(RobustWebSocket.prototype, name, descriptor) | ||
| }) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd be glad to take this change at |
||
|
|
||
| return RobustWebSocket | ||
| }, typeof window != 'undefined' ? window : (typeof global != 'undefined' ? global : this)); | ||
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.
This doesn't make sense. either a)
WebSocketis native and supported and never replaced, or b) it is polyfilled and we shouldn't close over and force the users to understand ordering of this library and the polyfill.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.
The reason for this change is for another option: WebSocket has been overriden by browser extensions and users want to ensure that RobustWebSocket's implementation doesn't change from under them.