Skip to content
Open
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions lib/web/websocket/connection.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { uid, states, sentCloseFrameState, emptyBuffer, opcodes } = require('./constants')
const { uid, states, sentCloseFrameState, closingHandshakeStates, emptyBuffer, opcodes } = require('./constants')
const { parseExtensions, isClosed, isClosing, isEstablished, isConnecting, validateCloseCodeAndReason } = require('./util')
const { makeRequest } = require('../fetch/request')
const { fetching } = require('../fetch/index')
Expand Down Expand Up @@ -245,7 +245,7 @@ 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.has(sentCloseFrameState.SENT) && !object.closeState.has(sentCloseFrameState.RECEIVED)) {
} else if (object.closeState.isDisjointFrom(closingHandshakeStates)) {
// 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.
Expand Down
14 changes: 14 additions & 0 deletions lib/web/websocket/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ const sentCloseFrameState = {
RECEIVED: 2
}

/**
* Tracks whether the closing handshake has started or completed.
*
* A close state that is disjoint from this set has not started the closing
* handshake yet. A close state that is a superset of this set has completed it.
*
* @type {ReadonlySet<number>}
*/
const closingHandshakeStates = new Set([
sentCloseFrameState.SENT,
sentCloseFrameState.RECEIVED
])

/**
* The WebSocket opcodes.
*
Expand Down Expand Up @@ -116,6 +129,7 @@ const sendHints = {
module.exports = {
uid,
sentCloseFrameState,
closingHandshakeStates,
staticPropertyDescriptors,
states,
opcodes,
Expand Down
4 changes: 2 additions & 2 deletions lib/web/websocket/receiver.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const { Writable } = require('node:stream')
const assert = require('node:assert')
const { parserStates, opcodes, states, emptyBuffer, sentCloseFrameState } = require('./constants')
const { parserStates, opcodes, states, emptyBuffer, sentCloseFrameState, closingHandshakeStates } = require('./constants')
const {
isValidStatusCode,
isValidOpcode,
Expand Down Expand Up @@ -394,7 +394,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) && !this.#handler.closeState.has(sentCloseFrameState.RECEIVED)) {
if (this.#handler.closeState.isDisjointFrom(closingHandshakeStates)) {
// 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: 4 additions & 6 deletions lib/web/websocket/stream/websocketstream.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const { createDeferredPromise } = require('../../../util/promise')
const { environmentSettingsObject } = require('../../fetch/util')
const { states, opcodes, sentCloseFrameState } = require('../constants')
const { states, opcodes, closingHandshakeStates } = require('../constants')
const { webidl } = require('../../webidl')
const { getURLRecord, isValidSubprotocol, isEstablished, utf8Decode } = require('../util')
const { establishWebSocketConnection, failWebsocketConnection, closeWebSocketConnection } = require('../connection')
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.has(sentCloseFrameState.SENT) && !this.#handler.closeState.has(sentCloseFrameState.RECEIVED)) {
if (this.#handler.closeState.isDisjointFrom(closingHandshakeStates)) {
const frame = new WebsocketFrameSend(data)

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

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

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

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

Expand Down
6 changes: 2 additions & 4 deletions lib/web/websocket/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { isArrayBuffer } = require('node:util/types')
const { webidl } = require('../webidl')
const { URLSerializer } = require('../fetch/data-url')
const { environmentSettingsObject } = require('../fetch/util')
const { staticPropertyDescriptors, states, sentCloseFrameState, sendHints, opcodes } = require('./constants')
const { staticPropertyDescriptors, states, sentCloseFrameState, closingHandshakeStates, sendHints, opcodes } = require('./constants')
const {
isConnecting,
isEstablished,
Expand Down Expand Up @@ -569,9 +569,7 @@ class WebSocket extends EventTarget {
// If the TCP connection was closed after the
// WebSocket closing handshake was completed, the WebSocket connection
// is said to have been closed _cleanly_.
const wasClean =
this.#handler.closeState.has(sentCloseFrameState.SENT) &&
this.#handler.closeState.has(sentCloseFrameState.RECEIVED)
const wasClean = this.#handler.closeState.isSupersetOf(closingHandshakeStates)

let code = 1005
let reason = ''
Expand Down
6 changes: 2 additions & 4 deletions test/web-platform-tests/wpt-runner.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const SERVER_READY_CHECKS = [
['wss', (line) => line.includes('wss on port') && line.includes('Listen on:')],
['h2', (line) => line.includes('h2 on port 9000') && line.includes('Starting http2 server')]
]
const SERVER_READY_CHECK_NAMES = new Set(SERVER_READY_CHECKS.map(([name]) => name))

function streamServerLogs (stream, target, onLine) {
let buffer = ''
Expand Down Expand Up @@ -174,10 +175,7 @@ async function runWithTestUtil (testFunction) {
const readinessTimeout = setTimeout(() => {
if (!readySettled) {
readySettled = true
const missing = SERVER_READY_CHECKS
.map(([name]) => name)
.filter((name) => !readyChecks.has(name))
.join(', ')
const missing = [...SERVER_READY_CHECK_NAMES.difference(readyChecks)].join(', ')
rejectReady(new Error(`Timed out waiting for WPT server readiness. Missing: ${missing}`))
}
}, 30_000)
Expand Down
Loading