* @param {import('./websocket').Handler} handler * @param {number} code * @param {string|undefined} reason * @param {unknown} cause * @returns {void}
(handler, code, reason, cause)
| 304 | * @returns {void} |
| 305 | */ |
| 306 | function failWebsocketConnection (handler, code, reason, cause) { |
| 307 | // If _The WebSocket Connection is Established_ prior to the point where |
| 308 | // the endpoint is required to _Fail the WebSocket Connection_, the |
| 309 | // endpoint SHOULD send a Close frame with an appropriate status code |
| 310 | // (Section 7.4) before proceeding to _Close the WebSocket Connection_. |
| 311 | if (isEstablished(handler.readyState)) { |
| 312 | closeWebSocketConnection(handler, code, reason, false) |
| 313 | } |
| 314 | |
| 315 | handler.controller.abort() |
| 316 | |
| 317 | if (isConnecting(handler.readyState)) { |
| 318 | // If the connection was not established, we must still emit an 'error' and 'close' events |
| 319 | handler.onSocketClose() |
| 320 | } else if (handler.socket?.destroyed === false) { |
| 321 | handler.socket.destroy() |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | module.exports = { |
| 326 | establishWebSocketConnection, |
no test coverage detected