22
33const { addAbortListener } = require ( 'node:events' )
44const { environmentSettingsObject } = require ( '../../fetch/util' )
5- const { states, opcodes, sentCloseFrameState } = require ( '../constants' )
5+ const { states, opcodes, closingHandshakeStates } = require ( '../constants' )
66const { webidl } = require ( '../../webidl' )
77const { getURLRecord, isValidSubprotocol, isEstablished, utf8Decode } = require ( '../util' )
88const { establishWebSocketConnection, failWebsocketConnection, closeWebSocketConnection } = require ( '../connection' )
@@ -242,7 +242,7 @@ class WebSocketStream {
242242 // 6.1. Wait until there is sufficient buffer space in stream to send the message.
243243
244244 // 6.2. If the closing handshake has not yet started , Send a WebSocket Message to stream comprised of data using opcode .
245- if ( ! this . #handler. closeState . has ( sentCloseFrameState . SENT ) && ! this . #handler . closeState . has ( sentCloseFrameState . RECEIVED ) ) {
245+ if ( this . #handler. closeState . isDisjointFrom ( closingHandshakeStates ) ) {
246246 const frame = new WebsocketFrameSend ( data )
247247
248248 this . #handler. socket . write ( frame . createFrame ( opcode ) , ( ) => {
@@ -347,9 +347,7 @@ class WebSocketStream {
347347
348348 /** @type {import('../websocket').Handler['onSocketClose'] } */
349349 #onSocketClose ( ) {
350- const wasClean =
351- this . #handler. closeState . has ( sentCloseFrameState . SENT ) &&
352- this . #handler. closeState . has ( sentCloseFrameState . RECEIVED )
350+ const wasClean = this . #handler. closeState . isSupersetOf ( closingHandshakeStates )
353351
354352 // 1. Change the ready state to CLOSED (3).
355353 this . #handler. readyState = states . CLOSED
@@ -376,7 +374,7 @@ class WebSocketStream {
376374 // 1006.
377375 let code = result ?. code ?? 1005
378376
379- if ( ! this . #handler. closeState . has ( sentCloseFrameState . SENT ) && ! this . #handler . closeState . has ( sentCloseFrameState . RECEIVED ) ) {
377+ if ( this . #handler. closeState . isDisjointFrom ( closingHandshakeStates ) ) {
380378 code = 1006
381379 }
382380
0 commit comments