Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev committed Sep 25, 2024
1 parent d45cac7 commit 1d8507a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
9 changes: 5 additions & 4 deletions lib/web/websocket/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,10 @@ function closeWebSocketConnection (object, code, reason, validate = false) {
// Fail the WebSocket connection and set object’s ready state to CLOSING (2). [WSP]
failWebsocketConnection(object)
object.readyState = states.CLOSING
} else if (object.closeState === sentCloseFrameState.NOT_SENT) {
// Start the WebSocket closing handshake and set object’s ready state to CLOSING (2). [WSP]
object.closeState = sentCloseFrameState.PROCESSING
} else if (!object.closeState.has(sentCloseFrameState.SENT) && !object.closeState.has(sentCloseFrameState.RECEIVED)) {
// Upon either sending or receiving a Close control frame, it is said
// that _The WebSocket Closing Handshake is Started_ and that the
// WebSocket connection is in the CLOSING state.

const frame = new WebsocketFrameSend()

Expand Down Expand Up @@ -280,7 +281,7 @@ function closeWebSocketConnection (object, code, reason, validate = false) {

object.socket.write(frame.createFrame(opcodes.CLOSE))

object.closeState = sentCloseFrameState.SENT
object.closeState.add(sentCloseFrameState.SENT)

// Upon either sending or receiving a Close control frame, it is said
// that _The WebSocket Closing Handshake is Started_ and that the
Expand Down
2 changes: 1 addition & 1 deletion lib/web/websocket/receiver.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ class ByteParser extends Writable {

// Upon receiving such a frame, the other peer sends a
// Close frame in response, if it hasn't already sent one.
if (!this.#handler.closeState.has(sentCloseFrameState.SENT)) {
if (!this.#handler.closeState.has(sentCloseFrameState.SENT) && !this.#handler.closeState.has(sentCloseFrameState.RECEIVED)) {
// If an endpoint receives a Close frame and did not previously send a
// Close frame, the endpoint MUST send a Close frame in response. (When
// sending a Close frame in response, the endpoint typically echos the
Expand Down
10 changes: 6 additions & 4 deletions lib/web/websocket/stream/websocketstream.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class WebSocketStream {

readyState: states.CONNECTING,
socket: null,
closeState: sentCloseFrameState.NOT_SENT,
closeState: new Set(),
receivedClose: false
}

Expand Down Expand Up @@ -242,7 +242,7 @@ class WebSocketStream {
// 6.1. Wait until there is sufficient buffer space in stream to send the message.

// 6.2. If the closing handshake has not yet started , Send a WebSocket Message to stream comprised of data using opcode .
if (this.#handler.closeState === sentCloseFrameState.NOT_SENT) {
if (!this.#handler.closeState.has(sentCloseFrameState.SENT) && !this.#handler.closeState.has(sentCloseFrameState.RECEIVED)) {
const frame = new WebsocketFrameSend(data)

this.#handler.socket.write(frame.createFrame(opcode), () => {
Expand Down Expand Up @@ -353,7 +353,9 @@ class WebSocketStream {

/** @type {import('../websocket').Handler['onSocketClose']} */
#onSocketClose () {
const wasClean = this.#handler.closeState === sentCloseFrameState.SENT && this.#handler.receivedClose
const wasClean =
this.#handler.closeState.has(sentCloseFrameState.SENT) &&
this.#handler.closeState.has(sentCloseFrameState.RECEIVED)

// 1. Change the ready state to CLOSED (3).
this.#handler.readyState = states.CLOSED
Expand All @@ -380,7 +382,7 @@ class WebSocketStream {
// 1006.
let code = result?.code ?? 1005

if (this.#handler.closeState === sentCloseFrameState.NOT_SENT) {
if (!this.#handler.closeState.has(sentCloseFrameState.SENT) && !this.#handler.closeState.has(sentCloseFrameState.RECEIVED)) {
code = 1006
}

Expand Down

0 comments on commit 1d8507a

Please sign in to comment.