|
1 | 1 | 'use strict'
|
2 | 2 |
|
3 | 3 | 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') |
5 | 5 | const { channels } = require('../../core/diagnostics')
|
6 | 6 | const { makeRequest } = require('../fetch/request')
|
7 | 7 | const { fetching } = require('../fetch/index')
|
@@ -294,7 +294,32 @@ function closeWebSocketConnection (object, code, reason, validate = false) {
|
294 | 294 | }
|
295 | 295 | }
|
296 | 296 |
|
| 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 | + |
297 | 321 | module.exports = {
|
298 | 322 | establishWebSocketConnection,
|
| 323 | + failWebsocketConnection, |
299 | 324 | closeWebSocketConnection
|
300 | 325 | }
|
0 commit comments