Skip to content

Commit e8a93e5

Browse files
authored
Resolve circular dependency (#4006)
Signed-off-by: Matteo Collina <[email protected]>
1 parent b554769 commit e8a93e5

File tree

5 files changed

+30
-33
lines changed

5 files changed

+30
-33
lines changed

lib/web/websocket/connection.js

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
const { uid, states, sentCloseFrameState, emptyBuffer, opcodes } = require('./constants')
4-
const { failWebsocketConnection, parseExtensions, isClosed, isClosing, isEstablished, validateCloseCodeAndReason } = require('./util')
4+
const { parseExtensions, isClosed, isClosing, isEstablished, validateCloseCodeAndReason } = require('./util')
55
const { channels } = require('../../core/diagnostics')
66
const { makeRequest } = require('../fetch/request')
77
const { fetching } = require('../fetch/index')
@@ -294,7 +294,32 @@ function closeWebSocketConnection (object, code, reason, validate = false) {
294294
}
295295
}
296296

297+
/**
298+
* @param {import('./websocket').Handler} handler
299+
* @param {number} code
300+
* @param {string|undefined} reason
301+
* @returns {void}
302+
*/
303+
function failWebsocketConnection (handler, code, reason) {
304+
// If _The WebSocket Connection is Established_ prior to the point where
305+
// the endpoint is required to _Fail the WebSocket Connection_, the
306+
// endpoint SHOULD send a Close frame with an appropriate status code
307+
// (Section 7.4) before proceeding to _Close the WebSocket Connection_.
308+
if (isEstablished(handler.readyState)) {
309+
closeWebSocketConnection(handler, code, reason, false)
310+
}
311+
312+
handler.controller.abort()
313+
314+
if (handler.socket?.destroyed === false) {
315+
handler.socket.destroy()
316+
}
317+
318+
handler.onFail(code, reason)
319+
}
320+
297321
module.exports = {
298322
establishWebSocketConnection,
323+
failWebsocketConnection,
299324
closeWebSocketConnection
300325
}

lib/web/websocket/receiver.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ const { channels } = require('../../core/diagnostics')
77
const {
88
isValidStatusCode,
99
isValidOpcode,
10-
failWebsocketConnection,
1110
websocketMessageReceived,
1211
utf8Decode,
1312
isControlFrame,
1413
isTextBinaryFrame,
1514
isContinuationFrame
1615
} = require('./util')
16+
const { failWebsocketConnection } = require('./connection')
1717
const { WebsocketFrameSend } = require('./frame')
1818
const { PerMessageDeflate } = require('./permessage-deflate')
1919

lib/web/websocket/stream/websocketstream.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
const { createDeferredPromise, environmentSettingsObject } = require('../../fetch/util')
44
const { states, opcodes, sentCloseFrameState } = require('../constants')
55
const { webidl } = require('../../fetch/webidl')
6-
const { getURLRecord, isValidSubprotocol, isEstablished, failWebsocketConnection, utf8Decode } = require('../util')
7-
const { establishWebSocketConnection, closeWebSocketConnection } = require('../connection')
6+
const { getURLRecord, isValidSubprotocol, isEstablished, utf8Decode } = require('../util')
7+
const { establishWebSocketConnection, failWebsocketConnection, closeWebSocketConnection } = require('../connection')
88
const { types } = require('node:util')
99
const { channels } = require('../../../core/diagnostics')
1010
const { WebsocketFrameSend } = require('../frame')

lib/web/websocket/util.js

-27
Original file line numberDiff line numberDiff line change
@@ -156,32 +156,6 @@ function isValidStatusCode (code) {
156156
return code >= 3000 && code <= 4999
157157
}
158158

159-
/**
160-
* @param {import('./websocket').Handler} handler
161-
* @param {number} code
162-
* @param {string|undefined} reason
163-
* @returns {void}
164-
*/
165-
function failWebsocketConnection (handler, code, reason) {
166-
// If _The WebSocket Connection is Established_ prior to the point where
167-
// the endpoint is required to _Fail the WebSocket Connection_, the
168-
// endpoint SHOULD send a Close frame with an appropriate status code
169-
// (Section 7.4) before proceeding to _Close the WebSocket Connection_.
170-
if (isEstablished(handler.readyState)) {
171-
// avoid circular require - performance is not important here
172-
const { closeWebSocketConnection } = require('./connection')
173-
closeWebSocketConnection(handler, code, reason, false)
174-
}
175-
176-
handler.controller.abort()
177-
178-
if (handler.socket?.destroyed === false) {
179-
handler.socket.destroy()
180-
}
181-
182-
handler.onFail(code, reason)
183-
}
184-
185159
/**
186160
* @see https://datatracker.ietf.org/doc/html/rfc6455#section-5.5
187161
* @param {number} opcode
@@ -350,7 +324,6 @@ module.exports = {
350324
fireEvent,
351325
isValidSubprotocol,
352326
isValidStatusCode,
353-
failWebsocketConnection,
354327
websocketMessageReceived,
355328
utf8Decode,
356329
isControlFrame,

lib/web/websocket/websocket.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ const {
1010
isClosing,
1111
isValidSubprotocol,
1212
fireEvent,
13-
failWebsocketConnection,
1413
utf8Decode,
1514
toArrayBuffer,
1615
getURLRecord
1716
} = require('./util')
18-
const { establishWebSocketConnection, closeWebSocketConnection } = require('./connection')
17+
const { establishWebSocketConnection, closeWebSocketConnection, failWebsocketConnection } = require('./connection')
1918
const { ByteParser } = require('./receiver')
2019
const { kEnumerableProperty } = require('../../core/util')
2120
const { getGlobalDispatcher } = require('../../global')

0 commit comments

Comments
 (0)