Skip to content

Commit

Permalink
websocket: use isClosed and isClosing in websocket.close()
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak committed Mar 1, 2024
1 parent 5600aa1 commit 4f18b15
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lib/web/websocket/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const { MessageEvent, ErrorEvent } = require('./events')

/**
* @param {import('./websocket').WebSocket} ws
* @returns {boolean}
*/
function isEstablished (ws) {
// If the server's response is validated as provided for above, it is
Expand All @@ -18,6 +19,7 @@ function isEstablished (ws) {

/**
* @param {import('./websocket').WebSocket} ws
* @returns {boolean}
*/
function isClosing (ws) {
// Upon either sending or receiving a Close control frame, it is said
Expand All @@ -28,6 +30,7 @@ function isClosing (ws) {

/**
* @param {import('./websocket').WebSocket} ws
* @returns {boolean}
*/
function isClosed (ws) {
return ws[kReadyState] === states.CLOSED
Expand Down
4 changes: 2 additions & 2 deletions lib/web/websocket/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const {
kSentClose,
kByteParser
} = require('./symbols')
const { isEstablished, isClosing, isValidSubprotocol, failWebsocketConnection, fireEvent } = require('./util')
const { isEstablished, isClosing, isValidSubprotocol, failWebsocketConnection, fireEvent, isClosed } = require('./util')
const { establishWebSocketConnection } = require('./connection')
const { WebsocketFrameSend } = require('./frame')
const { ByteParser } = require('./receiver')
Expand Down Expand Up @@ -184,7 +184,7 @@ class WebSocket extends EventTarget {
}

// 3. Run the first matching steps from the following list:
if (this[kReadyState] === WebSocket.CLOSING || this[kReadyState] === WebSocket.CLOSED) {
if (isClosing(this) || isClosed(this)) {
// If this's ready state is CLOSING (2) or CLOSED (3)
// Do nothing.
} else if (!isEstablished(this)) {
Expand Down

0 comments on commit 4f18b15

Please sign in to comment.