Skip to content

Commit 67d4cde

Browse files
authored
Support byte streams (BYOB)
Tests: web-platform-tests/wpt#37910. Fixes #267.
1 parent bc97a85 commit 67d4cde

File tree

1 file changed

+65
-21
lines changed

1 file changed

+65
-21
lines changed

fetch.bs

Lines changed: 65 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5985,25 +5985,73 @@ optional boolean <var>forceNewConnection</var> (default false), run these steps:
59855985
<li><p>Return the <a for=/>appropriate network error</a> for <var>fetchParams</var>.
59865986
</ol>
59875987

5988-
<li><p>Let <var>pullAlgorithm</var> be an algorithm that <a lt=resumed for=fetch>resumes</a> the
5989-
ongoing fetch if it is <a lt=suspend for=fetch>suspended</a>.
5988+
<li>
5989+
<p>Let |buffer| be an empty buffer that can have bytes appended to it.
5990+
5991+
<p class="note">This represents an internal buffer inside the network layer of the user agent.
5992+
5993+
<li>
5994+
<p>Let |pullAlgorithm| be the followings steps:
5995+
5996+
<ol>
5997+
<li><p>Let |promise| be [=a new promise=].
5998+
5999+
<li>
6000+
<p>Run the following steps [=in parallel=]:
6001+
6002+
<ol>
6003+
<li><p>If the size of |buffer| is smaller than a lower limit chosen by the user agent and the
6004+
ongoing fetch is [=fetch/suspend|suspended=], [=fetch/resumed|resume=] the fetch.
6005+
6006+
<li><p>Wait until |buffer| is not empty.
6007+
6008+
<li>
6009+
<p>[=Queue a fetch task=] to run the following steps, with |fetchParams|'s
6010+
[=task destination=].
6011+
6012+
<ol>
6013+
<li><p>Let |available| be the size of |buffer|.
6014+
6015+
<li><p>Let |desiredSize| be |available|.
6016+
6017+
<li><p>If |stream|'s [=ReadableStream/current BYOB request view=] is non-null, then set
6018+
|desiredSize| to |stream|'s [=ReadableStream/current BYOB request view=]'s
6019+
[=BufferSource/byte length=].
6020+
6021+
<li><p>Let |extractSize| be the smaller value of |available| and |desiredSize|.
6022+
6023+
<li><p>Let <var>bytes</var> be the result of extracting |extractSize| of bytes from
6024+
<var>buffer</var>.
6025+
6026+
<li><p>If |stream|s [=ReadableStream/current BYOB request view=] is non-null, then
6027+
[=ArrayBufferView/write=] |bytes| into |stream|'s
6028+
[=ReadableStream/current BYOB request view=], and set |view| to |stream|'s
6029+
[=ReadableStream/current BYOB request view=].
6030+
6031+
<li>Otherwise, set |view| to the result of [=ArrayBufferView/create|creating=] a
6032+
{{Uint8Array}} from |bytes| in |stream|'s [=relevant Realm=].
6033+
6034+
<li><p>[=ReadableStream/Enqueue=] |view| into |stream|.
6035+
6036+
<li><p>If |stream| is [=ReadableStream/errored=], then [=fetch controller/terminate=]
6037+
|fetchParams|'s [=fetch params/controller=].
6038+
6039+
<li><p>[=/Resolve=] |promise| with undefined.
6040+
</ol>
6041+
</ol>
6042+
6043+
<li><p>Return |promise|.
6044+
</ol>
59906045

59916046
<li><p>Let <var>cancelAlgorithm</var> be an algorithm that <a for="fetch controller">aborts</a>
59926047
<var>fetchParams</var>'s <a for="fetch params">controller</a> with <var>reason</var>, given
59936048
<var>reason</var>.
59946049

5995-
<li><p>Let <var>highWaterMark</var> be a non-negative, non-NaN number, chosen by the user agent.
5996-
5997-
<li><p>Let <var>sizeAlgorithm</var> be an algorithm that accepts a <a>chunk</a> object and returns
5998-
a non-negative, non-NaN, non-infinite number, chosen by the user agent.
5999-
60006050
<li><p>Let <var>stream</var> be a <a>new</a> {{ReadableStream}}.
60016051

6002-
<li><p><a for=ReadableStream>Set up</a> <var>stream</var> with
6003-
<a for="ReadableStream/set up"><var>pullAlgorithm</var></a> set to <var>pullAlgorithm</var>,
6004-
<a for="ReadableStream/set up"><var>cancelAlgorithm</var></a> set to <var>cancelAlgorithm</var>,
6005-
<a for="ReadableStream/set up"><var>highWaterMark</var></a> set to <var>highWaterMark</var>, and
6006-
<a for="ReadableStream/set up"><var>sizeAlgorithm</var></a> set to <var>sizeAlgorithm</var>.
6052+
<li><p>[=ReadableStream/set up with byte reading support|Set up=] |stream| with byte reading
6053+
support with <var>[=ReadableStream/set up/pullAlgorithm=]</var> set to |pullAlgorithm|,
6054+
<var>[=ReadableStream/set up/cancelAlgorithm=]</var> set to |cancelAlgorithm|.
60076055

60086056
<li><p>Set <var>response</var>'s <a for=response>body</a> to a new <a for=/>body</a> whose
60096057
<a for=body>stream</a> is <var>stream</var>.
@@ -6057,15 +6105,10 @@ optional boolean <var>forceNewConnection</var> (default false), run these steps:
60576105
<li><p>If <var>bytes</var> is failure, then <a for="fetch controller">terminate</a>
60586106
<var>fetchParams</var>'s <a for="fetch params">controller</a>.
60596107

6060-
<li><p><a for=ReadableStream>Enqueue</a> a {{Uint8Array}} wrapping an {{ArrayBuffer}}
6061-
containing <var>bytes</var> into <var>stream</var>.
6062-
6063-
<li><p>If <var>stream</var> is <a for=ReadableStream>errored</a>, then
6064-
<a for="fetch controller">terminate</a> <var>fetchParams</var>.'s
6065-
<a for="fetch params">controller</a>.
6108+
<li><p>Append |bytes| to |buffer|.
60666109

6067-
<li><p>If <var>stream</var> doesn't <a for=ReadableStream>need more data</a> ask the user
6068-
agent to <a for=fetch>suspend</a> the ongoing fetch.
6110+
<li><p>If the size of |buffer| is larger than an upper limit chosen by the user agent, ask
6111+
the user agent to [=fetch/suspend=] the ongoing fetch.
60696112
</ol>
60706113

60716114
<li><p>Otherwise, if the bytes transmission for <var>response</var>'s message body is done
@@ -6832,7 +6875,8 @@ steps:
68326875
running <var>object</var>'s <a for=Blob>get stream</a>.
68336876

68346877
<li><p>Otherwise, set <var>stream</var> to a <a>new</a> {{ReadableStream}} object, and
6835-
<a for=ReadableStream>set up</a> <var>stream</var>.
6878+
[=ReadableStream/set up with byte reading support|set up=] <var>stream</var> with byte reading
6879+
support.
68366880

68376881
<li><p><a for=/>Assert</a>: <var>stream</var> is a {{ReadableStream}} object.
68386882

0 commit comments

Comments
 (0)