Skip to content

Commit 464326e

Browse files
noamrdomenicannevk
authored
Editorial: stop using promises in internal algorithms
They are not designed for that and don't work well (e.g., resolve expects a JS value). Follows whatwg/streams#1250 and addresses the Fetch part of whatwg/streams#1178. Closes #1568. Co-authored-by: Domenic Denicola <[email protected]> Co-authored-by: Anne van Kesteren <[email protected]>
1 parent 2d78995 commit 464326e

File tree

1 file changed

+78
-79
lines changed

1 file changed

+78
-79
lines changed

fetch.bs

+78-79
Original file line numberDiff line numberDiff line change
@@ -1465,32 +1465,19 @@ null), run these steps. <var>processBody</var> must be an algorithm accepting a
14651465
<li><p>If <var>taskDestination</var> is null, then set <var>taskDestination</var> to the result of
14661466
<a>starting a new parallel queue</a>.
14671467

1468-
<li><p>Let <var>promise</var> be the result of <a>fully reading body as promise</a> given
1469-
<var>body</var>.
1470-
1471-
<li><p>Let <var>fulfilledSteps</var> given a <a>byte sequence</a> <var>bytes</var> be to
1468+
<li><p>Let <var>successSteps</var> given a <a>byte sequence</a> <var>bytes</var> be to
14721469
<a>queue a fetch task</a> to run <var>processBody</var> given <var>bytes</var>, with
14731470
<var>taskDestination</var>.
14741471

1475-
<li><p>Let <var>rejectedSteps</var> be to <a>queue a fetch task</a> to run
1472+
<li><p>Let <var>errorSteps</var> be to <a>queue a fetch task</a> to run
14761473
<var>processBodyError</var>, with <var>taskDestination</var>.
14771474

1478-
<li><p><a for=promise>React</a> to <var>promise</var> with <var>fulfilledSteps</var> and
1479-
<var>rejectedSteps</var>.
1480-
</ol>
1481-
</div>
1482-
1483-
<div algorithm>
1484-
<p>To <dfn export lt="fully reading body as promise">fully read body as promise</dfn>, given a
1485-
<a for=/>body</a> <var>body</var>, run these steps:
1486-
1487-
<ol>
14881475
<li><p>Let <var>reader</var> be the result of <a for=ReadableStream>getting a reader</a> for
1489-
<var>body</var>'s <a for=body>stream</a>. If that threw an exception, then return
1490-
<a>a promise rejected with</a> that exception.
1476+
<var>body</var>'s <a for=body>stream</a>. If that threw an exception, then run
1477+
<var>errorSteps</var> with that exception and return.
14911478

1492-
<li><p>Return the result of <a for=ReadableStreamDefaultReader>reading all bytes</a> from
1493-
<var>reader</var>.
1479+
<li><p><a for=ReadableStreamDefaultReader>Read all bytes</a> from
1480+
<var>reader</var>, given <var>successSteps</var> and <var>errorSteps</var>.
14941481
</ol>
14951482
</div>
14961483

@@ -6886,38 +6873,78 @@ returns failure or a <a for=/>MIME type</a>.
68866873

68876874
<hr>
68886875

6876+
<div algorithm>
68896877
<p>The <dfn attribute for=Body><code>body</code></dfn> getter steps are to return null if
68906878
<a>this</a>'s <a for=Body>body</a> is null; otherwise <a>this</a>'s <a for=Body>body</a>'s
68916879
<a for=body>stream</a>.
6880+
</div>
68926881

6882+
<div algorithm>
68936883
<p>The <dfn attribute for=Body><code>bodyUsed</code></dfn> getter steps are to return true if
68946884
<a>this</a>'s <a for=Body>body</a> is non-null and <a>this</a>'s <a for=Body>body</a>'s
68956885
<a for=body>stream</a> is <a for=ReadableStream>disturbed</a>; otherwise false.
6886+
</div>
68966887

6897-
<p>The <dfn id=concept-body-package-data for=Body>package data</dfn> algorithm, given
6898-
<var>bytes</var>, <var>type</var>, and a <var>mimeType</var>, switches on <var>type</var>, and runs
6899-
the associated steps:
6888+
<div algorithm>
6889+
<p id=concept-body-package-data>The <dfn id=concept-body-consume-body for=Body>consume body</dfn>
6890+
algorithm, given an object that includes {{Body}} <var>object</var> and an algorithm that takes a
6891+
<a for=/>byte sequence</a> and returns a JavaScript value or throws an exception
6892+
<var>convertBytesToJSValue</var>, runs these steps:
69006893

6901-
<dl class=switch>
6902-
<dt><i>ArrayBuffer</i>
6903-
<dd>
6904-
<p>Return a new {{ArrayBuffer}} whose contents are <var>bytes</var>.
6894+
<ol>
6895+
<li><p>If <var>object</var> is <a for=Body>unusable</a>, then return <a>a promise rejected with</a>
6896+
a {{TypeError}}.
69056897

6906-
<p class=note>Allocating an {{ArrayBuffer}} can throw a {{RangeError}}.
6898+
<li><p>Let <var>promise</var> be <a>a new promise</a>.
69076899

6908-
<dt><i>Blob</i>
6909-
<dd><p>Return a {{Blob}} whose contents are <var>bytes</var> and {{Blob/type}} attribute is
6910-
<var>mimeType</var>.
6900+
<li>Let <var>errorSteps</var> given <var>error</var> be to <a>reject</a> <var>promise</var> with
6901+
<var>error</var>.
69116902

6912-
<dt><i>FormData</i>
6913-
<dd>
6914-
<p>If <var>mimeType</var>'s <a for="MIME type">essence</a> is "<code>multipart/form-data</code>",
6915-
then:
6903+
<li>Let <var>successSteps</var> given a <a for=/>byte sequence</a> <var>data</var> be to
6904+
<a for=/>resolve</a> <var>promise</var> with the result of running <var>convertBytesToJSValue</var>
6905+
with <var>data</var>. If that threw an exception, then run <var>errorSteps</var> with that
6906+
exception.
6907+
6908+
<li><p>If <var>object</var>'s <a for=Body>body</a> is null, then run <var>successSteps</var>
6909+
with an empty <a for=/>byte sequence</a>.
6910+
6911+
<li><p>Otherwise, <a for=body>fully read</a> <var>object</var>'s <a for=Body>body</a> given
6912+
<var>successSteps</var>, <var>errorSteps</var>, and <var>object</var>'s
6913+
<a>relevant global object</a>.
6914+
6915+
<li><p>Return <var>promise</var>.
6916+
</ol>
6917+
</div>
6918+
6919+
<div algorithm>
6920+
<p>The <dfn method for=Body><code>arrayBuffer()</code></dfn> method steps are to return the result
6921+
of running <a for=Body>consume body</a> with <a>this</a> and the following step given a
6922+
<a for=/>byte sequence</a> <var>bytes</var>: return a new {{ArrayBuffer}} whose contents are
6923+
<var>bytes</var>.
6924+
6925+
<p class="note">The above method can reject with a {{RangeError}}.
6926+
</div>
69166927

6928+
<div algorithm>
6929+
<p>The <dfn method for=Body><code>blob()</code></dfn> method steps are to return the result
6930+
of running <a for=Body>consume body</a> with <a>this</a> and the following step given a
6931+
<a for=/>byte sequence</a> <var>bytes</var>: return a {{Blob}} whose contents are <var>bytes</var>
6932+
and whose {{Blob/type}} attribute is <a>this</a>'s <a for=Body>MIME type</a>.
6933+
</div>
6934+
6935+
<div algorithm>
6936+
<p>The <dfn method for=Body><code>formData()</code></dfn> method steps are to return the result of
6937+
running <a for=Body>consume body</a> with <a>this</a> and the following step given a
6938+
<a for=/>byte sequence</a> <var>bytes</var>: switch on <a>this</a>'s <a for=Body>MIME type</a>'s
6939+
<a for="MIME type">essence</a> and run the corresponding steps:
6940+
6941+
<dl class=switch>
6942+
<dt>"<code>multipart/form-data</code>",
6943+
<dd>
69176944
<ol>
69186945
<li>
69196946
<p>Parse <var>bytes</var>, using the value of the `<code>boundary</code>` parameter from
6920-
<var>mimeType</var>, per the rules set forth in
6947+
<a>this</a>'s <a for=Body>MIME type</a>, per the rules set forth in
69216948
<cite>Returning Values from Forms: multipart/form-data</cite>. [[!RFC7578]]</p>
69226949

69236950
<p>Each part whose `<code>Content-Disposition</code>` header contains a `<code>filename</code>`
@@ -6950,62 +6977,34 @@ the associated steps:
69506977
`<code>multipart/form-data</code>`, a more detailed parsing specification is to be
69516978
written. Volunteers welcome.
69526979

6953-
<p>Otherwise, if <var>mimeType</var>'s <a for="MIME type">essence</a> is
6954-
"<code>application/x-www-form-urlencoded</code>", then:
6955-
6980+
<dt>"<code>application/x-www-form-urlencoded</code>",
6981+
<dd>
69566982
<ol>
6957-
<li><p>Let <var>entries</var> be the result of
6958-
<a lt="urlencoded parser">parsing</a> <var>bytes</var>.
6983+
<li><p>Let <var>entries</var> be the result of <a lt="urlencoded parser">parsing</a>
6984+
<var>bytes</var>.
69596985

69606986
<li><p>If <var>entries</var> is failure, then <a>throw</a> a {{TypeError}}.
69616987

69626988
<li><p>Return a new {{FormData}} object whose <a for=FormData>entry list</a> is
69636989
<var>entries</var>.
69646990
</ol>
69656991

6966-
<p>Otherwise, <a>throw</a> a {{TypeError}}.
6967-
6968-
<dt><i>JSON</i>
6969-
<dd><p>Return the result of running <a>parse JSON from bytes</a> on <var>bytes</var>.
6970-
6971-
<dt><i>text</i>
6972-
<dd><p>Return the result of running <a>UTF-8 decode</a> on
6973-
<var>bytes</var>.
6992+
<dt>Otherwise
6993+
<dd><p><a>Throw</a> a {{TypeError}}.
69746994
</dl>
6995+
</div>
69756996

6976-
<p>The <dfn id=concept-body-consume-body for=Body>consume body</dfn> algorithm, given an
6977-
<var>object</var> and <var>type</var>, runs these steps:
6978-
6979-
<ol>
6980-
<li><p>If <var>object</var> is <a for=Body>unusable</a>, then return <a>a promise rejected with</a>
6981-
a {{TypeError}}.
6982-
6983-
<li><p>Let <var>promise</var> be <a>a promise resolved with</a> an empty
6984-
<a for=/>byte sequence</a>.
6985-
6986-
<li><p>If <var>object</var>'s <a for=Body>body</a> is non-null, then set <var>promise</var> to the
6987-
result of <a>fully reading body as promise</a> given <var>object</var>'s <a for=Body>body</a>.
6988-
6989-
<li><p>Let <var>steps</var> be to return the result of <a>package data</a> with the first argument
6990-
given, <var>type</var>, and <var>object</var>'s <a for=Body>MIME type</a>.
6991-
6992-
<li><p>Return the result of <a>upon fulfillment</a> of <var>promise</var> given <var>steps</var>.
6993-
</ol>
6994-
6995-
<p>The <dfn method for=Body><code>arrayBuffer()</code></dfn> method steps are to return the result
6996-
of running <a for=Body>consume body</a> with <a>this</a> and <i>ArrayBuffer</i>.
6997-
6998-
<p>The <dfn method for=Body><code>blob()</code></dfn> method steps are to return the result of
6999-
running <a for=Body>consume body</a> with <a>this</a> and <i>Blob</i>.
7000-
7001-
<p>The <dfn method for=Body><code>formData()</code></dfn> method steps are to return the result of
7002-
running <a for=Body>consume body</a> with <a>this</a> and <i>FormData</i>.
6997+
<div algorithm>
6998+
<p>The <dfn method for=Body><code>json()</code></dfn> method steps are to return the result
6999+
of running <a for=Body>consume body</a> with <a>this</a> and <a>parse JSON from bytes</a>.
70037000

7004-
<p>The <dfn method for=Body><code>json()</code></dfn> method steps are to return the result of
7005-
running <a for=Body>consume body</a> with <a>this</a> and <i>JSON</i>.
7001+
<p class="note">The above method can reject with a {{SyntaxError}}.
7002+
</div>
70067003

7007-
<p>The <dfn method for=Body><code>text()</code></dfn> method steps are to return the result of
7008-
running <a for=Body>consume body</a> with <a>this</a> and <i>text</i>.
7004+
<div algorithm>
7005+
<p>The <dfn method for=Body><code>text()</code></dfn> method steps are to return the result
7006+
of running <a for=Body>consume body</a> with <a>this</a> and <a>UTF-8 decode</a>.
7007+
</div>
70097008

70107009

70117010
<h3 id=request-class>Request class</h3>

0 commit comments

Comments
 (0)