()
| 208 | } |
| 209 | |
| 210 | #connect () { |
| 211 | if (this.#readyState === CLOSED) return |
| 212 | |
| 213 | this.#readyState = CONNECTING |
| 214 | |
| 215 | const fetchParams = { |
| 216 | request: this.#request, |
| 217 | dispatcher: this.#dispatcher |
| 218 | } |
| 219 | |
| 220 | // 14. Let processEventSourceEndOfBody given response res be the following step: if res is not a network error, then reestablish the connection. |
| 221 | const processEventSourceEndOfBody = (response) => { |
| 222 | if (!isNetworkError(response)) { |
| 223 | return this.#reconnect() |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | // 15. Fetch request, with processResponseEndOfBody set to processEventSourceEndOfBody... |
| 228 | fetchParams.processResponseEndOfBody = processEventSourceEndOfBody |
| 229 | |
| 230 | // and processResponse set to the following steps given response res: |
| 231 | fetchParams.processResponse = (response) => { |
| 232 | // 1. If res is an aborted network error, then fail the connection. |
| 233 | |
| 234 | if (isNetworkError(response)) { |
| 235 | // 1. When a user agent is to fail the connection, the user agent |
| 236 | // must queue a task which, if the readyState attribute is set to a |
| 237 | // value other than CLOSED, sets the readyState attribute to CLOSED |
| 238 | // and fires an event named error at the EventSource object. Once the |
| 239 | // user agent has failed the connection, it does not attempt to |
| 240 | // reconnect. |
| 241 | if (response.aborted) { |
| 242 | this.close() |
| 243 | this.dispatchEvent(new Event('error')) |
| 244 | return |
| 245 | // 2. Otherwise, if res is a network error, then reestablish the |
| 246 | // connection, unless the user agent knows that to be futile, in |
| 247 | // which case the user agent may fail the connection. |
| 248 | } else { |
| 249 | this.#reconnect() |
| 250 | return |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | // 3. Otherwise, if res's status is not 200, or if res's `Content-Type` |
| 255 | // is not `text/event-stream`, then fail the connection. |
| 256 | const contentType = response.headersList.get('content-type', true) |
| 257 | const mimeType = contentType !== null ? parseMIMEType(contentType) : 'failure' |
| 258 | const contentTypeValid = mimeType !== 'failure' && mimeType.essence === 'text/event-stream' |
| 259 | if ( |
| 260 | response.status !== 200 || |
| 261 | contentTypeValid === false |
| 262 | ) { |
| 263 | this.close() |
| 264 | this.dispatchEvent(new Event('error')) |
| 265 | return |
| 266 | } |
| 267 |
no test coverage detected