Skip to content

Commit

Permalink
Formalize reading bodies
Browse files Browse the repository at this point in the history
Closes #661.
  • Loading branch information
annevk committed Feb 17, 2021
1 parent 72be227 commit 672ed2e
Showing 1 changed file with 127 additions and 14 deletions.
141 changes: 127 additions & 14 deletions fetch.bs
Original file line number Diff line number Diff line change
Expand Up @@ -1037,6 +1037,131 @@ worked on in <a href=https://github.com/whatwg/fetch/issues/1156>issue #1156</a>
<var>body</var>.
</ol>

<hr>

<p>To <dfn export for=body>incrementally read</dfn> a <a for=/>body</a> <var>body</var>, given an
algorithm <var>processBodyChunk</var>, an algorithm <var>processError</var>, an optional null or
algorithm <dfn export for="body/incrementally read"><var>processEndOfBody</var></dfn> (default
null), and an optional null or <a for=/>global object</a>
<dfn export for="body/incrementally read">taskDestination</dfn> (default null), run these steps:

<ol>
<li><p>Assert: <var>body</var>'s <a for=body>stream</a> is non-null.

<li><p>If <var>taskDestination</var> is null, then set <var>taskDestination</var> to the result of
<a>starting a new parallel queue</a>.

<li>
<p>Let <var>reader</var> be the result of <a for=ReadableStream>getting a reader</a> for
<var>body</var>'s <a for=body>stream</a>.

<p class="note no-backref">This operation cannot throw an exception.

<li><p>Perform the <a>incrementally-read loop</a> given <var>reader</var>,
<var>taskDestination</var>, <var>processBodyChunk</var>, <var>processError</var>, and
<var>processEndOfBody</var>.
</ol>

<p>To perform the <dfn>incrementally-read loop</dfn>, given a {{ReadableStreamDefaultReader}} object
<var>reader</var>, <a>parallel queue</a> or <a for=/>global object</a> <var>taskDestination</var>,
algorithm <var>processBodyChunk</var>, algorithm <var>processError</var>, and null or algorithm
<var>processEndOfBody</var>:

<ol>
<li>
<p>Let <var>readRequest</var> be the following <a>read request</a>:

<dl>
<dt><a for="read request">chunk steps</a>, given <var>chunk</var>
<dd>
<ol>
<li><p>Let <var>continueAlgorithm</var> be null.

<li><p>If <var>chunk</var> is not a {{Uint8Array}} object, then set
<var>continueAlgorithm</var> to this step: run <var>processError</var> given a {{TypeError}}.

<li>
<p>Otherwise:

<ol>
<li><p>Let <var>bytes</var> be a <a lt="get a copy of the buffer source">copy of</a>
<var>chunk</var>.

<li>
<p>Set <var>continueAlgorithm</var> to these steps:

<ol>
<li><p>Run <var>processBodyChunk</var> given <var>bytes</var>.

<li><p>Perform the <a>incrementally-read loop</a> given <var>reader</var>,
<var>taskDestination</var>, <var>processBodyChunk</var>, <var>processError</var>, and
<var>processEndOfBody</var>.
</ol>
</ol>

<li><p><a>Queue a fetch task</a> given <var>continueAlgorithm</var> and
<var>taskDestination</var>.
</ol>

<dt><a for="read request">close steps</a>
<dd><ol><li><p>If <var>processEndOfBody</var> is non-null, then <a>queue a fetch task</a> given
<var>processEndOfBody</var> and <var>taskDestination</var>.</ol>

<dt><a for="read request">error steps</a>, given <var>e</var>
<dd>
<ol>
<li><p>Let <var>errorAlgorithm</var> be this step: run <var>processError</var> given
<var>e</var>.

<li><p><a>Queue a fetch task</a> given <var>errorAlgorithm</var> and
<var>taskDestination</var>.
</ol>
</dl>

<li><p><a for=ReadableStreamDefaultReader>Read a chunk</a> from <var>reader</var> given
<var>readRequest</var>.
</ol>

<p>To <dfn export for=body>fully read</dfn> a <a for=/>body</a> <var>body</var>, given an algorithm
<dfn export for="body/fully read"><var>processBody</var></dfn>, an optional algorithm
<dfn export for="body/fully read"><var>processError</var></dfn>, and an optional null or
<a for=/>global object</a> <dfn export for="body/fully read">taskDestination</dfn> (default null),
run these steps:

<ol>
<li><p>If <var>taskDestination</var> is null, then set <var>taskDestination</var> to the result of
<a>starting a new parallel queue</a>.

<li><p>Let <var>promise</var> be the result of <a>fully reading body as promise</a> given
<var>body</var>.

<li><p>Let <var>fulfilledSteps</var> given a <a>byte sequence</a> <var>bytes</var> be to
<a>queue a fetch task</a> to run <var>processBody</var> given <var>bytes</var>, with
<var>taskDestination</var>.

<li><p>Let <var>rejectedSteps</var> be to <a>queue a fetch task</a> to run <var>processError</var>,
with <var>taskDestination</var>.

<li><p><a for=promise>React</a> to <var>promise</var> with <var>fulfilledSteps</var> and
<var>rejectedSteps</var>.
</ol>

<p>To <dfn export lt="fully reading body as promise">fully read body as promise</dfn>, given a
<a for=/>body</a> <var>body</var>, run these steps:

<ol>
<li><p>Assert: <var>body</var>'s <a for=body>stream</a> is non-null.

<li><p>Let <var>reader</var> be the result of <a for=ReadableStream>getting a reader</a> for
<var>body</var>'s <a for=body>stream</a>. If that threw an exception, then return
<a>a promise rejected with</a> that exception.

<li><p>Return the result of <a for=ReadableStreamDefaultReader>reading all bytes</a> from
<var>reader</var>.
</ol>

<hr>

<p>To <dfn export>handle content codings</dfn> given <var>codings</var> and <var>bytes</var>, run
these steps:

Expand Down Expand Up @@ -5727,20 +5852,8 @@ the associated steps:
<li><p>Let <var>promise</var> be <a>a promise resolved with</a> an empty
<a for=/>byte sequence</a>.

<li>
<p>If <var>object</var>'s <a for=Body>body</a> is non-null, then:

<ol>
<li><p>Let <var>stream</var> be <var>object</var>'s <a for=Body>body</a>'s
<a for=body>stream</a>.

<li><p>Let <var>reader</var> be the result of <a for=ReadableStream>getting a reader</a> from
<var>stream</var>. If that threw an exception, then return <a>a promise rejected with</a> that
exception.

<li><p>Set <var>promise</var> to the result of
<a for=ReadableStreamDefaultReader>reading all bytes</a> from <var>reader</var>.
</ol>
<li><p>If <var>object</var>'s <a for=Body>body</a> is non-null, then set <var>promise</var> to the
result of <a>fully reading body as promise</a> given <var>object</var>'s <a for=Body>body</a>.

<li><p>Let <var>steps</var> be to return the result of <a>package data</a> with the first argument
given, <var>type</var>, and <var>object</var>'s <a for=Body>MIME type</a>.
Expand Down

0 comments on commit 672ed2e

Please sign in to comment.