Skip to content

Commit

Permalink
Rewrite transmitting request's body
Browse files Browse the repository at this point in the history
  • Loading branch information
annevk committed Feb 18, 2021
1 parent 324cabf commit ac1e951
Showing 1 changed file with 88 additions and 130 deletions.
218 changes: 88 additions & 130 deletions fetch.bs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ lt="authentication entry">authentication entries</a> (for HTTP authentication).
steps:

<ol>
<li><p>If <var>taskDestination</var> is a <a>parallel queue</a>, then
<li><p>If <var>taskDestination</var> is a <a for=/>parallel queue</a>, then
<a lt="enqueue steps" for="parallel queue">enqueue</a> <var>algorithm</var> to
<var>taskDestination</var>.

Expand Down Expand Up @@ -1042,7 +1042,7 @@ worked on in <a href=https://github.com/whatwg/fetch/issues/1156>issue #1156</a>
<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>
null), and an optional null, <a for=/>parallel queue</a>, or <a for=/>global object</a>
<dfn export for="body/incrementally read">taskDestination</dfn> (default null), run these steps:

<ol>
Expand All @@ -1063,9 +1063,9 @@ null), and an optional null or <a for=/>global object</a>
</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>:
<var>reader</var>, <a for=/>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>
Expand Down Expand Up @@ -1124,9 +1124,9 @@ algorithm <var>processBodyChunk</var>, algorithm <var>processError</var>, and nu

<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:
<dfn export for="body/fully read"><var>processError</var></dfn>, and an optional null,
<a for=/>parallel queue</a>, 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
Expand Down Expand Up @@ -1779,126 +1779,6 @@ is to return the result of <a>serializing a request origin</a> with <var>request

<hr>

<p>To <dfn id=concept-request-transmit-body>transmit request body</dfn>, given a
<a for=/>fetch params</a> <var>fetchParams</var>, run these steps:

<ol>
<li>Let <var>body</var> be <var>fetchParams</var>'s <a for="fetch params">request</a>'s
<a for=request>body</a>.

<li>
<p>If <var>body</var> is null and <var>fetchParams</var>'s
<a for="fetch params">process request end-of-body</a> is non-null, then:

<ol>
<li><p>Let <var>processRequestEndOfBody</var> be this step: run <var>fetchParams</var>'s
<a for="fetch params">process request end-of-body</a> given <var>fetchParams</var>'s
<a for="fetch params">request</a>.

<li><p><a>Queue a fetch task</a> given <var>processRequestEndOfBody</var> and
<var>fetchParams</var>'s <a for="fetch params">task destination</a>.

<li>
<p>Otherwise, if <var>body</var> is non-null:

<ol>
<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>transmit-request-body loop</a> given <var>fetchParams</var>,
<var>body</var>, and <var>reader</var>.
</ol>
</ol>

<p>To perform the <dfn>transmit-request-body loop</dfn>, given a <a for=/>fetch params</a>
<var>fetchParams</var>, <a for=/>body</a> <var>body</var>, and {{ReadableStreamDefaultReader}}
object <var>reader</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>If the ongoing fetch is <a for=fetch>terminated</a>, then abort these steps.

<li><p>If <var>chunk</var> is not a {{Uint8Array}} object,
<a lt=terminated for=fetch>terminate</a> the ongoing fetch and abort these steps.

<li><p>Let <var>bs</var> be the <a>byte sequence</a> represented by the {{Uint8Array}} object.

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

<li><p>If <var>fetchParams</var>'s <a for="fetch params">process request body</a> is non-null,
then set <var>processRequestBody</var> to this step: run <var>fetchParams</var>'s
<a for="fetch params">process request body</a> given <var>fetchParams</var>'s
<a for="fetch params">request</a>.

<li>
<p><a>In parallel</a>:

<ol>
<li>
<p>Transmit <var>bs</var>. Whenever one or more bytes are transmitted, increase
<var>body</var>'s <a for=body>transmitted bytes</a> by the number of transmitted bytes and
if <var>processRequestBody</var> is non-null then <a>queue a fetch task</a> given
<var>processRequestBody</var> and <var>fetchParams</var>'s
<a for="fetch params">task destination</a>.

<p class=note>This step blocks until <var>bs</var> is fully transmitted.

<li><p>Let <var>continueAlgorithm</var> be this step: perform the
<a>transmit-request-body loop</a> given <var>fetchParams</var>, <var>body</var>, and
<var>reader</var>.

<li><p>If the ongoing fetch is not <a for=fetch>terminated</a>, then
<a>queue a fetch task</a> given <var>continueAlgorithm</var> and <var>fetchParams</var>'s
<a for="fetch params">task destination</a>.
</ol>
</ol>

<dt><a for="read request">close steps</a>
<dd>
<ol>
<li><p>If the ongoing fetch is <a for=fetch>terminated</a>, then abort these steps.

<li>
<p>If <var>fetchParams</var>'s <a for="fetch params">process request end-of-body</a> is
non-null, then:

<ol>
<li><p>Let <var>processRequestEndOfBody</var> be this step: run <var>fetchParams</var>'s
<a for="fetch params">process request end-of-body</a> given <var>fetchParams</var>'s
<a for="fetch params">request</a>.

<li><p><a>Queue a fetch task</a> given <var>processRequestEndOfBody</var> and
<var>fetchParams</var>'s <a for="fetch params">task destination</a>.
</ol>
</ol>

<dt><a for="read request">error steps</a>, given <var>e</var>
<dd>
<ol>
<li><p>If the ongoing fetch is <a for=fetch>terminated</a>, then abort these steps.

<li><p>If <var>e</var> is an "<code><a exception>AbortError</a></code>" {{DOMException}}, then
<a lt=terminated for=fetch>terminate</a> the ongoing fetch with the aborted flag set.

<li><p>Otherwise, <a lt=terminated for=fetch>terminate</a> the ongoing fetch.
</ol>
</dl>

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

<hr>

<p>To <dfn export for=request id=concept-request-add-range-header>add a range header</dfn> to a
<a for=/>request</a> <var>request</var>, with an integer <var>first</var>, and an optional integer
<var>last</var>, run these steps:
Expand Down Expand Up @@ -4742,7 +4622,6 @@ these steps:
<var>includeCredentials</var>.
</dl>


<li>
<p>Run these steps, but <a>abort when</a> the ongoing fetch is <a for=fetch>terminated</a>:

Expand Down Expand Up @@ -4792,7 +4671,86 @@ these steps:
<li><p>Otherwise, return a <a>network error</a>.
</ol>

<p><a>Transmit request body</a> given <var>fetchParams</var>.
<p>To transmit <var>request</var>'s <a for=request>body</a> <var>body</var>, run these steps:

<ol>
<li>
<p>If <var>body</var> is null and <var>fetchParams</var>'s
<a for="fetch params">process request end-of-body</a> is non-null, then:

<ol>
<li><p>Let <var>processRequestEndOfBody</var> be this step: run <var>fetchParams</var>'s
<a for="fetch params">process request end-of-body</a> given <var>request</var>.

<li><p><a>Queue a fetch task</a> given <var>processRequestEndOfBody</var> and
<var>fetchParams</var>'s <a for="fetch params">task destination</a>.
</ol>

<li>
<p>Otherwise, if <var>body</var> is non-null:

<ol>
<li>
<p>Let <var>processBodyChunk</var> given <var>bytes</var>, be these steps:

<ol>
<li><p>If the ongoing fetch is <a for=fetch>terminated</a>, then abort these steps.

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

<li><p>If <var>fetchParams</var>'s <a for="fetch params">process request body</a> is
non-null, then set <var>processRequestBody</var> to this step: run <var>fetchParams</var>'s
<a for="fetch params">process request body</a> given <var>request</var>.

<li>
<p>Run these steps <a>in parallel</a>:

<ol>
<li>
<p>Transmit <var>bytes</var> and increase <var>body</var>'s
<a for=body>transmitted bytes</a> by <var>bytes</var>'s
<a for="byte sequence">length</a>.

<p class=note>This step blocks until <var>bytes</var> is fully transmitted.

<li><p>If <var>processRequestBody</var> is non-null, then <a>queue a fetch task</a> given
<var>processRequestBody</var> and <var>fetchParams</var>'s
<a for="fetch params">task destination</a>.
</ol>
</ol>

<li>
<p>Let <var>processError</var> given <var>e</var>, be these steps:

<ol>
<li><p>If the ongoing fetch is <a for=fetch>terminated</a>, then abort these steps.

<li><p>If <var>e</var> is an "<code><a exception>AbortError</a></code>" {{DOMException}},
then <a lt=terminated for=fetch>terminate</a> the ongoing fetch with the aborted flag set.

<li><p>Otherwise, <a lt=terminated for=fetch>terminate</a> the ongoing fetch.
</ol>

<li>
<p>Let <var>processEndOfBody</var> be these steps:

<ol>
<li><p>If the ongoing fetch is <a for=fetch>terminated</a>, then abort these steps.

<li><p>If <var>fetchParams</var>'s <a for="fetch params">process request end-of-body</a> is
non-null, then run <var>fetchParams</var>'s
<a for="fetch params">process request end-of-body</a> given <var>request</var>.
</ol>

<li><p><a for=body>Incrementally read</a> <var>request</var>'s <a for=request>body</a> given
<var>processBodyChunk</var>, <var>processError</var>, with
<a for="body/incrementally read"><i>processEndOfBody</i></a> set to
<var>processEndOfBody</var>, and <a for="body/incrementally read"><i>taskDestination</i></a>
set to <var>fetchParams</var>'s <a for="fetch params">task destination</a>.
<!-- This obtains a reader in parallel, but it's mostly fine since we already proxied the
input stream as part of the Request constructor. -->
</ol>
</ol>
<!-- In implementations to date this always happens before a response is processed. -->
</ol>

Expand Down

0 comments on commit ac1e951

Please sign in to comment.