Skip to content

Commit

Permalink
Split out some algorithms and define how to obtain 1024 bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
annevk committed Jun 1, 2022
1 parent d4ab1db commit 8feb3d6
Showing 1 changed file with 82 additions and 5 deletions.
87 changes: 82 additions & 5 deletions fetch.bs
Original file line number Diff line number Diff line change
Expand Up @@ -2950,9 +2950,10 @@ and a <a for=/>response</a> <var>response</var>, is to run these steps:
false.
<!-- TODO Integrate https://wicg.github.io/background-fetch/#validate-a-partial-response into Fetch -->

<li><p>Wait for 1024 bytes of <var>response</var>'s <a for=response>body</a> or end-of-file,
whichever comes first and let <var>bytes</var> be those bytes.
<!-- TODO Obtaining these bytes needs to be defined in terms of a transform stream. -->
<li><p>Let <var>bytes</var> be the result of running
<a>obtain a copy of the first 1024 bytes of response</a> given <var>response</var>.

<li><p>If <var>bytes</var> is failure, then return false.

<li>
<p>If the <a>audio or video type pattern matching algorithm</a> given <var>bytes</var> does not
Expand Down Expand Up @@ -2990,6 +2991,79 @@ and a <a for=/>response</a> <var>response</var>, is to run these steps:
<li><p>If <var>mimeType</var>'s <a for="MIME type">essence</a> <a for=string>starts with</a>
"<code>audio/</code>", "<code>image/</code>", or "<code>video/</code>", then return false.

<li><p>Return <a>determine if response is JavaScript and not JSON</a> given <var>response</var>.
</ol>

<hr>

<p>To <dfn>obtain a copy of the first 1024 bytes of response</dfn>, given a <a for=/>response</a>
<var>response</var>, run these steps:

<ol>
<li><p>Let <var>first1024Bytes</var> be null.

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

<ol>
<li><p>Let <var>bytes</var> be the empty <a for=/>byte sequence</a>.

<li><p>Let <var>transformStream</var> be a new {{TransformStream}}.

<li>
<p>Let <var>transformAlgorithm</var> given a <var>chunk</var> be these steps:

<ol>
<li><p><a for=ReadableStream>Enqueue</a> <var>chunk</var> in <var>transformStream</var>.

<li>
<p>If <var>first1024Bytes</var> is null, then:

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

<li><p>Append <var>chunkBytes</var> to <var>bytes</var>.

<li>
<p>If <var>bytes</var>'s <a for="byte sequencue">length</a> is greater than 1024, then:

<ol>
<li><p>Truncate <var>bytes</var> from the end so that it only contains 1024 bytes.

<li><p>Set <var>first1024Bytes</var> to <var>bytes</var>.
</ol>
</ol>
</ol>

<li><p>Let <var>flushAlgorithm</var> be this step: if <var>first1024Bytes</var> is null, then set
<var>first1024Bytes</var> to <var>bytes</var>.

<li><p><a for=TransformStream>Set up</a> <var>transformStream</var> with
<a for="TransformStream/set up"><i>transformAlgorithm</i></a> set to
<var>transformAlgorithm</var> and <a for="TransformStream/set up"><i>flushAlgorithm</i></a> set
to <var>flushAlgorithm</var>.

<li><p>Set <var>response</var>'s <a for=response>body</a>'s <a for=body>stream</a> to the result
of <var>response</var>'s <a for=response>body</a>'s <a for=body>stream</a>
<a for=TransformStream>piped through</a> <var>transformStream</var>.
</ol>

<li><p>Wait until <var>first1024Bytes</var> is non-null or <var>response</var>'s
<a for=response>body</a>'s <a for=body>stream</a> is <a for=ReadableStream>errored</a>.

<li><p>If <var>first1024Bytes</var> is null, then return failure.

<li>Return <var>first1024Bytes</var>.
</ol>

<hr>

<p>To <dfn>determine if response is JavaScript and not JSON</dfn> given a <a for=/>response</a>
<var>response</var>, run these steps:</p>

<ol>
<li><p>Let <var>responseBodyBytes</var> be null.

<li>
Expand All @@ -3013,8 +3087,11 @@ and a <a for=/>response</a> <var>response</var>, is to run these steps:

<li><p><a for=/>Assert</a>: <var>responseBodyBytes</var> is a <a for=/>byte sequence</a>.

<li><p>If <a>parse JSON bytes to a JavaScript value</a> given <var>responseBodyBytes</var> does not
throw, then return false. If it throws, catch the exception and ignore it.
<li>
<p>If <a>parse JSON bytes to a JavaScript value</a> given <var>responseBodyBytes</var> does not
throw, then return false. If it throws, catch the exception and ignore it.

<p class=note>If there is an exception, <var>response</var> is not JSON. If there is not, it is.

<li><p>Let <var>sourceText</var> be the result of <a for=/>decoding</a>
<var>responseBodyBytes</var> given <var>request</var>'s
Expand Down

0 comments on commit 8feb3d6

Please sign in to comment.