Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve circular dependency #4006

Merged
merged 2 commits into from
Jan 15, 2025
Merged
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
27 changes: 26 additions & 1 deletion lib/web/websocket/connection.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion lib/web/websocket/receiver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
4 changes: 2 additions & 2 deletions lib/web/websocket/stream/websocketstream.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
27 changes: 0 additions & 27 deletions lib/web/websocket/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -350,7 +324,6 @@ module.exports = {
fireEvent,
isValidSubprotocol,
isValidStatusCode,
failWebsocketConnection,
websocketMessageReceived,
utf8Decode,
isControlFrame,
Expand Down
3 changes: 1 addition & 2 deletions lib/web/websocket/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
Loading