Skip to content

Commit eab774e

Browse files
Use new Streams algorithms (#1533)
* Use new Streams algorithms Follows whatwg/streams#1073. * Namespacing tweaks * No subsubsteps Co-authored-by: Jake Archibald <[email protected]> * Missing :: Co-authored-by: Jake Archibald <[email protected]>
1 parent e4c2953 commit eab774e

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

docs/index.bs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ spec: fetch;
3434
type: dfn
3535
for: /; text: fetch
3636
text: empty
37-
type: interface
38-
text: ReadableStream
3937
spec: infra;
4038
type: dfn;
4139
text: list
@@ -1590,25 +1588,34 @@ spec: webappsec-referrer-policy; urlPrefix: https://w3c.github.io/webappsec-refe
15901588
1. Let |done| be false.
15911589
1. Let |potentialResponse| be a copy of |response|'s associated [=Response/response=], except for its [=response/body=].
15921590
1. If |response|'s [=response/body=] is non-null, run these substeps:
1593-
1. Let |reader| be the result of [=get a reader|getting a reader=] from |response|'s [=response/body=]'s [=stream=].
1591+
1. Let |reader| be the result of [=ReadableStream/getting a reader=] from |response|'s [=response/body=]'s [=stream=].
1592+
1. Let |pullAlgorithm| be an action that runs these steps:
1593+
1. Let |readRequest| be a new [=read request=] with the following [=struct/items=]:
1594+
: [=read request/chunk steps=], given |chunk|
1595+
::
1596+
1. Assert: |chunk| is a {{Uint8Array}}.
1597+
1. Append the bytes represented by |chunk| to |bytes|.
1598+
1. Perform ! [=DetachArrayBuffer=](|chunk|.\[[ViewedArrayBuffer]]).
1599+
: [=read request/close steps=]
1600+
::
1601+
1. Set |end-of-body| to true.
1602+
: [=read request/error steps=]
1603+
::
1604+
1. [=ReadableStream/error=] |newStream| with a {{TypeError}}.
1605+
1. [=ReadableStreamDefaultReader/Read a chunk=] from |reader| given |readRequest|.
1606+
1. Let |cancelAlgorithm| be an action that [=ReadableStreamDefaultReader/cancels=] |reader|.
15941607
1. Let |highWaterMark| be a non-negative, non-NaN number, chosen by the user agent.
15951608
1. Let |sizeAlgorithm| be an algorithm that accepts a [=chunk=] object and returns a non-negative, non-NaN, non-infinite number, chosen by the user agent.
1596-
1. Let |pull| be an action that runs these subsubsteps:
1597-
1. Let |promise| be the result of [=read a chunk|reading a chunk=] from |response|'s [=response/body=]'s [=stream=] with |reader|.
1598-
1. When |promise| is fulfilled with an object whose `done` property is false and whose `value` property is a `Uint8Array` object, append the bytes represented by the `value` property to |bytes| and perform ! [=DetachArrayBuffer=] with the `ArrayBuffer` object wrapped by the `value` property.
1599-
1. When |promise| is fulfilled with an object whose `done` property is true, set |end-of-body| to true.
1600-
1. When |promise| is fulfilled with a value that matches with neither of the above patterns, or |promise| is rejected, [=ReadableStream/error=] |newStream| with a `TypeError`.
1601-
1. Let |cancel| be an action that [=ReadableStream/cancels=] |response|'s [=response/body=]'s [=stream=] with |reader|.
1602-
1. Let |newStream| be the result of [=ReadableStream/construct a ReadableStream object=] with |highWaterMark|, |sizeAlgorithm|, |pull|, and |cancel| in |targetRealm|.
1609+
1. Let |newStream| be the result of [=ReadableStream/creating=] a {{ReadableStream}} with <a for=ReadableStream/create><var ignore>pullAlgorithm</var></a> set to |pullAlgorithm|, <a for=ReadableStream/create><var ignore>cancelAlgorithm</var></a> set to |cancelAlgorithm|, <a for=ReadableStream/create><var ignore>highWaterMark</var></a> set to |highWaterMark|, and <a for=ReadableStream/create><var ignore>sizeAlgorithm</var></a> set to |sizeAlgorithm|, in |targetRealm|.
16031610
1. Set |potentialResponse|'s [=response/body=] to a new [=/body=] whose [=stream=] is |newStream|.
16041611
1. Run these subsubsteps repeatedly [=in parallel=] while |done| is false:
1605-
1. If |newStream| is [=errored=], then set |done| to true.
1612+
1. If |newStream| is [=ReadableStream/errored=], then set |done| to true.
16061613
1. Otherwise, if |bytes| is empty and |end-of-body| is true, then [=ReadableStream/close=] |newStream| and set |done| to true.
16071614
1. Otherwise, if |bytes| is not empty, run these subsubsubsteps:
16081615
1. Let |chunk| be a subsequence of |bytes| starting from the beginning of |bytes|.
16091616
1. Remove |chunk| from |bytes|.
1610-
1. Let |buffer| be an `ArrayBuffer` object created in |targetRealm| and containing |chunk|.
1611-
1. [=ReadableStream/Enqueue=] a `Uint8Array` object created in |targetRealm| and wrapping |buffer| to |newStream|.
1617+
1. Let |buffer| be an {{ArrayBuffer}} object created in |targetRealm| and containing |chunk|.
1618+
1. [=ReadableStream/Enqueue=] a {{Uint8Array}} object created in |targetRealm| and wrapping |buffer| to |newStream|.
16121619

16131620
Note: These substeps are meant to produce the observable equivalent of "piping" |response|'s [=response/body=]'s [=stream=] into |potentialResponse|.
16141621

@@ -1976,8 +1983,8 @@ spec: webappsec-referrer-policy; urlPrefix: https://w3c.github.io/webappsec-refe
19761983
1. Let |bodyReadPromise| be [=a promise resolved with=] undefined.
19771984
1. If |innerResponse|'s [=response/body=] is non-null, run these substeps:
19781985
1. Let |stream| be |innerResponse|'s [=response/body=]'s [=body/stream=].
1979-
1. Let |reader| be the result of [=ReadableStream/get a reader|getting a reader=] for |stream|.
1980-
1. Set |bodyReadPromise| to the result of [=Read all bytes|reading all bytes=] from |stream| with |reader|.
1986+
1. Let |reader| be the result of [=ReadableStream/getting a reader=] for |stream|.
1987+
1. Set |bodyReadPromise| to the result of [=ReadableStreamDefaultReader/reading all bytes=] from |reader|.
19811988

19821989
Note: This ensures that |innerResponse|'s [=response/body=] is [=Body/locked=], and we have a full buffered copy of the body in |clonedResponse|. An implementation could optimize by streaming directly to disk rather than memory.
19831990

0 commit comments

Comments
 (0)