|
1 | 1 | 'use strict' |
2 | 2 |
|
3 | 3 | const { uid, states, sentCloseFrameState, emptyBuffer, opcodes } = require('./constants') |
4 | | -const { parseExtensions, isClosed, isClosing, isEstablished, validateCloseCodeAndReason } = require('./util') |
| 4 | +const { parseExtensions, isClosed, isClosing, isEstablished, isConnecting, validateCloseCodeAndReason } = require('./util') |
5 | 5 | const { makeRequest } = require('../fetch/request') |
6 | 6 | const { fetching } = require('../fetch/index') |
7 | 7 | const { Headers, getHeadersList } = require('../fetch/headers') |
@@ -91,12 +91,6 @@ function establishWebSocketConnection (url, protocols, client, handler, options) |
91 | 91 | useParallelQueue: true, |
92 | 92 | dispatcher: options.dispatcher, |
93 | 93 | processResponse (response) { |
94 | | - if (response.type === 'error') { |
95 | | - // If the WebSocket connection could not be established, it is also said |
96 | | - // that _The WebSocket Connection is Closed_, but not _cleanly_. |
97 | | - handler.readyState = states.CLOSED |
98 | | - } |
99 | | - |
100 | 94 | // 1. If response is a network error or its status is not 101, |
101 | 95 | // fail the WebSocket connection. |
102 | 96 | if (response.type === 'error' || response.status !== 101) { |
@@ -299,10 +293,10 @@ function failWebsocketConnection (handler, code, reason, cause) { |
299 | 293 |
|
300 | 294 | handler.controller.abort() |
301 | 295 |
|
302 | | - if (!handler.socket) { |
| 296 | + if (isConnecting(handler.readyState)) { |
303 | 297 | // If the connection was not established, we must still emit an 'error' and 'close' events |
304 | 298 | handler.onSocketClose() |
305 | | - } else if (handler.socket.destroyed === false) { |
| 299 | + } else if (handler.socket?.destroyed === false) { |
306 | 300 | handler.socket.destroy() |
307 | 301 | } |
308 | 302 | } |
|
0 commit comments