Skip to content

Commit

Permalink
Refactor EventSource response processing to be normative
Browse files Browse the repository at this point in the history
  • Loading branch information
noamr committed Feb 13, 2022
1 parent f7f3e82 commit f63a190
Showing 1 changed file with 32 additions and 58 deletions.
90 changes: 32 additions & 58 deletions source
Original file line number Diff line number Diff line change
Expand Up @@ -2477,6 +2477,7 @@ a.setAttribute('href', 'https://example.com/'); // change the content attribute
<li><dfn data-x-href="https://fetch.spec.whatwg.org/#ok-status">ok status</dfn></li>
<li><dfn data-x="navigation-request" data-x-href="https://fetch.spec.whatwg.org/#navigation-request">navigation request</dfn></li>
<li><dfn data-x-href="https://fetch.spec.whatwg.org/#concept-network-error">network error</dfn></li>
<li><dfn data-x-href="https://fetch.spec.whatwg.org/#concept-abort-network-error">aborted network error</dfn></li>
<li>`<dfn data-x="http-origin" data-x-href="https://fetch.spec.whatwg.org/#http-origin"><code>Origin</code></dfn>` header</li>
<li>`<dfn data-x-href="https://fetch.spec.whatwg.org/#http-cross-origin-resource-policy"><code>Cross-Origin-Resource-Policy</code></dfn>` header</li>
<li><dfn data-x-href="https://fetch.spec.whatwg.org/#process-response">process response</dfn></li>
Expand Down Expand Up @@ -100061,22 +100062,44 @@ dictionary <dfn dictionary>EventSourceInit</dfn> {
<li><p>Set <var>ev</var>'s <span data-x="concept-event-stream-request">request</span> to
<var>request</var>.</p></li>

<li><p>Let <var>eventSourceResponse</var> be null.</p></li>

<li><p>Let <var>finalizeEventSourceFetch</var> be to <span>finalize and report timing</span>
with <var>eventSourceResponse</var>, <var>settings</var>'s
<li><p>Let <var>finalizeEventSourceFetch</var> given
<span data-x="concept-response">response</span> <var>res</var>be to
<span>finalize and report timing</span> with <var>res</var>, <var>settings</var>'s
<span data-x="concept-settings-object-global">global object</span>, and
"<code data-x="">other</code>".</p></li>

<li>
<p>Run this step <span>in parallel</span>:</p>
<p><span data-x="concept-fetch">Fetch</span> <var>request</var> with
<i data-x="processResponseEndOfBody">processResponseEndOfBody</i> set to
<var>finalizeEventSourceFetch</var> and <i data-x="process response">processResponse</i> set
to the following steps given <span data-x="concept-response">response</span> <var>res</var>:</p>

<ol>
<!--FETCH--><li><p><span data-x="concept-fetch">Fetch</span> <var>request</var> with
<i data-x="process response">processResponse</i> given
<span data-x="concept-response">response</span> <var>res</var> be to set
<var>eventSourceResponse</var> to <var>res</var>.</p></li>
<li><p>If <var>res</var> is an <span>aborted network error</span>, then
<span>fail the connection</span>.</p></li>

<li><p>Otherwise, if <var>res</var> is a <span>network error</span>, then
<span>reestablish the connection</span>, unless the user agent knows that to be futile, in
which case the user agent may <span>fail the connection</span>.</p></li>

<li><p>Otherwise, if <var>res</var>'s <span data-x="concept-response-status">status</span> is
not 200, or if <var>res</var>'s `<code>Content-Type</code>` is not
`<code>text/event-stream</code>`, then <span>fail the connection</span>.</p>
<!-- including: HTTP 201 Created, 202 Accepted, 203 Non-Authoritative Information, 204 No
Content, 205 Reset Content, 206 Partial Content, 300 Multiple Choices, 304 Not Modified,
400 Bad Request, 403 Forbidden, 404 Not Found, 405 Method Not Allowed, 406 Not Acceptable, 408
Request Timeout, 409 Conflict, 410 Gone, 411 Length Required, 412 Precondition Failed, 413
Request Entity Too Large, 414 Request-URI Too Long, 415 Unsupported Media Type, 416 Requested
Range Not Satisfiable, 417 Expectation Failed, HTTP 500 Internal Server Error, 501 Not
Implemented, 502 Bad Gateway, 503 Service Unavailable, and 504 Gateway Timeout responses, and
505 HTTP Version Not Supported responses --> <!-- mailto: is defined as equivalent to HTTP
204 --><!-- about:blank is defined as having no MIME type -->

<li><p>Otherwise, <span>announce the connection</span> and
<a href="#event-stream-interpretation">interpret</a> <var>res</var>'s
<span data-x="concept-response-body">body</span> line by line.</p></li>
</ol>
</li>

<li><p>Return <var>ev</var>.</p></li>
</ol>
Expand Down Expand Up @@ -100149,55 +100172,6 @@ dictionary <dfn dictionary>EventSourceInit</dfn> {

<h4 id="sse-processing-model"><span id="processing-model-9"></span>Processing model</h4>

<p>The resource indicated in the argument to the <code data-x="dom-EventSource">EventSource</code>
constructor is fetched when the constructor is run.</p>

<p>As data is received, the <span data-x="concept-task">tasks</span> queued by the <span>networking
task source</span> to handle the data must act as follows.</p>

<p>HTTP 200 OK responses with a `<code>Content-Type</code>` header specifying the type
`<code>text/event-stream</code>`, ignoring any <span>MIME type</span> parameters, must be
processed line by line <a href="#event-stream-interpretation">as described below</a>.</p>

<p>When a successful response with a supported <span>MIME type</span> is received, such that the
user agent begins parsing the contents of the stream, the user agent must <span>announce the
connection</span>.</p>

<p>The <span data-x="concept-task">task</span> that the <span>networking task source</span> places
on the <span>task queue</span> once fetching for such a resource (with the correct <span>MIME
type</span>) has completed must cause the user agent to run <var>finalizeEventSourceFetch</var>,
and <span>reestablish the connection</span> <span>in parallel</span>. This applies whether the connection is closed gracefully or unexpectedly
(but does not apply when fetching is canceled by the user agent, e.g., in response to <code
data-x="dom-window-stop">window.stop()</code>, since in those cases the final <span
data-x="concept-task">task</span> is actually discarded). It doesn't apply for the error
conditions listed below except where explicitly specified.</p>

<p>HTTP 200 OK responses that have a <span>Content-Type</span> specifying an unsupported type, or
that have no <span>Content-Type</span> at all, must cause the user agent to <span>fail the
connection</span>.</p> <!-- about:blank is defined as having no MIME type -->

<p id="event-source-network-errors-reconnect">Network errors that prevents the connection from
being established in the first place (e.g. DNS errors), should cause the user agent to
run <var>finalizeEventSourceFetch</var>, and <span>reestablish the connection</span>
<span>in parallel</span>, unless the user agent knows that to be futile, in which case the user
agent may <span>fail the connection</span>.</p>

<p id="event-source-fail-reasons">Any other HTTP response code not listed here, as well as the
cancelation of the fetch algorithm by the user agent (e.g. in response to <code
data-x="dom-window-stop">window.stop()</code> or the user canceling the network connection
manually) must cause the user agent to run <var>finalizeEventSourceFetch</var> and
<span>fail the connection</span>.</p> <!-- including: HTTP
201 Created, 202 Accepted, 203 Non-Authoritative Information, 204 No Content, 205 Reset Content,
206 Partial Content, 300 Multiple Choices, 304 Not Modified, 400 Bad Request, 403 Forbidden, 404
Not Found, 405 Method Not Allowed, 406 Not Acceptable, 408 Request Timeout, 409 Conflict, 410
Gone, 411 Length Required, 412 Precondition Failed, 413 Request Entity Too Large, 414 Request-URI
Too Long, 415 Unsupported Media Type, 416 Requested Range Not Satisfiable, 417 Expectation Failed,
HTTP 500 Internal Server Error, 501 Not Implemented, 502 Bad Gateway, 503 Service Unavailable, and
504 Gateway Timeout responses, and 505 HTTP Version Not Supported responses --> <!-- mailto: is
defined as equivalent to HTTP 204 -->

<hr>

<p>When a user agent is to <dfn>announce the connection</dfn>, the user agent must <span>queue a
task</span> which, if the <code data-x="dom-EventSource-readyState">readyState</code> attribute is
set to a value other than <code data-x="dom-EventSource-CLOSED">CLOSED</code>, sets the <code
Expand Down

0 comments on commit f63a190

Please sign in to comment.