Skip to content

Commit e910423

Browse files
committed
Split out some algorithms and define how to obtain 1024 bytes
1 parent 6194d0f commit e910423

File tree

1 file changed

+82
-5
lines changed

1 file changed

+82
-5
lines changed

fetch.bs

Lines changed: 82 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2950,9 +2950,10 @@ and a <a for=/>response</a> <var>response</var>, is to run these steps:
29502950
false.
29512951
<!-- TODO Integrate https://wicg.github.io/background-fetch/#validate-a-partial-response into Fetch -->
29522952

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

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

2994+
<li><p>Return <a>determine if response is JavaScript and not JSON</a> given <var>response</var>.
2995+
</ol>
2996+
2997+
<hr>
2998+
2999+
<p>To <dfn>obtain a copy of the first 1024 bytes of response</dfn>, given a <a for=/>response</a>
3000+
<var>response</var>, run these steps:
3001+
3002+
<ol>
3003+
<li><p>Let <var>first1024Bytes</var> be null.
3004+
3005+
<li>
3006+
<p><a for=/>In parallel</a>:
3007+
3008+
<ol>
3009+
<li><p>Let <var>bytes</var> be the empty <a for=/>byte sequence</a>.
3010+
3011+
<li><p>Let <var>transformStream</var> be a new {{TransformStream}}.
3012+
3013+
<li>
3014+
<p>Let <var>transformAlgorithm</var> given a <var>chunk</var> be these steps:
3015+
3016+
<ol>
3017+
<li><p><a for=ReadableStream>Enqueue</a> <var>chunk</var> in <var>transformStream</var>.
3018+
3019+
<li>
3020+
<p>If <var>first1024Bytes</var> is null, then:
3021+
3022+
<ol>
3023+
<li><p>Let <var>chunkBytes</var> be
3024+
<a lt="get a copy of the bytes held by the buffer source">a copy of the bytes held by</a>
3025+
<var>chunk</var>.
3026+
3027+
<li><p>Append <var>chunkBytes</var> to <var>bytes</var>.
3028+
3029+
<li>
3030+
<p>If <var>bytes</var>'s <a for="byte sequencue">length</a> is greater than 1024, then:
3031+
3032+
<ol>
3033+
<li><p>Truncate <var>bytes</var> from the end so that it only contains 1024 bytes.
3034+
3035+
<li><p>Set <var>first1024Bytes</var> to <var>bytes</var>.
3036+
</ol>
3037+
</ol>
3038+
</ol>
3039+
3040+
<li><p>Let <var>flushAlgorithm</var> be this step: if <var>first1024Bytes</var> is null, then set
3041+
<var>first1024Bytes</var> to <var>bytes</var>.
3042+
3043+
<li><p><a for=TransformStream>Set up</a> <var>transformStream</var> with
3044+
<a for="TransformStream/set up"><i>transformAlgorithm</i></a> set to
3045+
<var>transformAlgorithm</var> and <a for="TransformStream/set up"><i>flushAlgorithm</i></a> set
3046+
to <var>flushAlgorithm</var>.
3047+
3048+
<li><p>Set <var>response</var>'s <a for=response>body</a>'s <a for=body>stream</a> to the result
3049+
of <var>response</var>'s <a for=response>body</a>'s <a for=body>stream</a>
3050+
<a for=TransformStream>piped through</a> <var>transformStream</var>.
3051+
</ol>
3052+
3053+
<li><p>Wait until <var>first1024Bytes</var> is non-null or <var>response</var>'s
3054+
<a for=response>body</a>'s <a for=body>stream</a> is <a for=ReadableStream>errored</a>.
3055+
3056+
<li><p>If <var>first1024Bytes</var> is null, then return failure.
3057+
3058+
<li>Return <var>first1024Bytes</var>.
3059+
</ol>
3060+
3061+
<hr>
3062+
3063+
<p>To <dfn>determine if response is JavaScript and not JSON</dfn> given a <a for=/>response</a>
3064+
<var>response</var>, run these steps:</p>
3065+
3066+
<ol>
29933067
<li><p>Let <var>responseBodyBytes</var> be null.
29943068

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

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

3016-
<li><p>If <a>parse JSON bytes to a JavaScript value</a> given <var>responseBodyBytes</var> does not
3017-
throw, then return false. If it throws, catch the exception and ignore it.
3090+
<li>
3091+
<p>If <a>parse JSON bytes to a JavaScript value</a> given <var>responseBodyBytes</var> does not
3092+
throw, then return false. If it throws, catch the exception and ignore it.
3093+
3094+
<p class=note>If there is an exception, <var>response</var> is not JSON. If there is not, it is.
30183095

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

0 commit comments

Comments
 (0)