From d3dadfd3a3b62277a09095fad054c401fe1c3562 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Wed, 15 Jan 2025 17:34:10 +0100 Subject: [PATCH] Resolve circular dependency Signed-off-by: Matteo Collina --- lib/web/websocket/connection.js | 27 ++++++++++++++++++++- lib/web/websocket/receiver.js | 2 +- lib/web/websocket/stream/websocketstream.js | 4 +-- lib/web/websocket/util.js | 27 --------------------- lib/web/websocket/websocket.js | 3 +-- 5 files changed, 30 insertions(+), 33 deletions(-) diff --git a/lib/web/websocket/connection.js b/lib/web/websocket/connection.js index 6ecc53af179..0d0c5801a3e 100644 --- a/lib/web/websocket/connection.js +++ b/lib/web/websocket/connection.js @@ -1,7 +1,7 @@ 'use strict' const { uid, states, sentCloseFrameState, emptyBuffer, opcodes } = require('./constants') -const { failWebsocketConnection, parseExtensions, isClosed, isClosing, isEstablished, validateCloseCodeAndReason } = require('./util') +const { parseExtensions, isClosed, isClosing, isEstablished, validateCloseCodeAndReason } = require('./util') const { channels } = require('../../core/diagnostics') const { makeRequest } = require('../fetch/request') const { fetching } = require('../fetch/index') @@ -294,7 +294,32 @@ function closeWebSocketConnection (object, code, reason, validate = false) { } } +/** + * @param {import('./websocket').Handler} handler + * @param {number} code + * @param {string|undefined} reason + * @returns {void} + */ +function failWebsocketConnection (handler, code, reason) { + // If _The WebSocket Connection is Established_ prior to the point where + // the endpoint is required to _Fail the WebSocket Connection_, the + // endpoint SHOULD send a Close frame with an appropriate status code + // (Section 7.4) before proceeding to _Close the WebSocket Connection_. + if (isEstablished(handler.readyState)) { + closeWebSocketConnection(handler, code, reason, false) + } + + handler.controller.abort() + + if (handler.socket?.destroyed === false) { + handler.socket.destroy() + } + + handler.onFail(code, reason) +} + module.exports = { establishWebSocketConnection, + failWebsocketConnection, closeWebSocketConnection } diff --git a/lib/web/websocket/receiver.js b/lib/web/websocket/receiver.js index 3f5bc544b7f..3ea603e4858 100644 --- a/lib/web/websocket/receiver.js +++ b/lib/web/websocket/receiver.js @@ -7,13 +7,13 @@ const { channels } = require('../../core/diagnostics') const { isValidStatusCode, isValidOpcode, - failWebsocketConnection, websocketMessageReceived, utf8Decode, isControlFrame, isTextBinaryFrame, isContinuationFrame } = require('./util') +const { failWebsocketConnection } = require('./connection') const { WebsocketFrameSend } = require('./frame') const { PerMessageDeflate } = require('./permessage-deflate') diff --git a/lib/web/websocket/stream/websocketstream.js b/lib/web/websocket/stream/websocketstream.js index 71d63a5767a..c44183dd8b5 100644 --- a/lib/web/websocket/stream/websocketstream.js +++ b/lib/web/websocket/stream/websocketstream.js @@ -3,8 +3,8 @@ const { createDeferredPromise, environmentSettingsObject } = require('../../fetch/util') const { states, opcodes, sentCloseFrameState } = require('../constants') const { webidl } = require('../../fetch/webidl') -const { getURLRecord, isValidSubprotocol, isEstablished, failWebsocketConnection, utf8Decode } = require('../util') -const { establishWebSocketConnection, closeWebSocketConnection } = require('../connection') +const { getURLRecord, isValidSubprotocol, isEstablished, utf8Decode } = require('../util') +const { establishWebSocketConnection, failWebsocketConnection, closeWebSocketConnection } = require('../connection') const { types } = require('node:util') const { channels } = require('../../../core/diagnostics') const { WebsocketFrameSend } = require('../frame') diff --git a/lib/web/websocket/util.js b/lib/web/websocket/util.js index 45e74498568..3e9c5479043 100644 --- a/lib/web/websocket/util.js +++ b/lib/web/websocket/util.js @@ -156,32 +156,6 @@ function isValidStatusCode (code) { return code >= 3000 && code <= 4999 } -/** - * @param {import('./websocket').Handler} handler - * @param {number} code - * @param {string|undefined} reason - * @returns {void} - */ -function failWebsocketConnection (handler, code, reason) { - // If _The WebSocket Connection is Established_ prior to the point where - // the endpoint is required to _Fail the WebSocket Connection_, the - // endpoint SHOULD send a Close frame with an appropriate status code - // (Section 7.4) before proceeding to _Close the WebSocket Connection_. - if (isEstablished(handler.readyState)) { - // avoid circular require - performance is not important here - const { closeWebSocketConnection } = require('./connection') - closeWebSocketConnection(handler, code, reason, false) - } - - handler.controller.abort() - - if (handler.socket?.destroyed === false) { - handler.socket.destroy() - } - - handler.onFail(code, reason) -} - /** * @see https://datatracker.ietf.org/doc/html/rfc6455#section-5.5 * @param {number} opcode @@ -350,7 +324,6 @@ module.exports = { fireEvent, isValidSubprotocol, isValidStatusCode, - failWebsocketConnection, websocketMessageReceived, utf8Decode, isControlFrame, diff --git a/lib/web/websocket/websocket.js b/lib/web/websocket/websocket.js index 63bf67c6f8e..651eadd32f9 100644 --- a/lib/web/websocket/websocket.js +++ b/lib/web/websocket/websocket.js @@ -10,12 +10,11 @@ const { isClosing, isValidSubprotocol, fireEvent, - failWebsocketConnection, utf8Decode, toArrayBuffer, getURLRecord } = require('./util') -const { establishWebSocketConnection, closeWebSocketConnection } = require('./connection') +const { establishWebSocketConnection, closeWebSocketConnection, failWebsocketConnection } = require('./connection') const { ByteParser } = require('./receiver') const { kEnumerableProperty } = require('../../core/util') const { getGlobalDispatcher } = require('../../global')