Skip to content

Commit 2942e89

Browse files
saschanazdomenic
andauthored
Add an other-specs abstraction to pull from bytes
Helps with whatwg/fetch#1610. Co-authored-by: Domenic Denicola <d@domenic.me>
1 parent b45c0c5 commit 2942e89

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

index.bs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6754,7 +6754,7 @@ mark=] is greater than zero.
67546754

67556755
<hr>
67566756

6757-
The following algorithm must only be used on {{ReadableStream}} instances initialized via the above
6757+
The following algorithms must only be used on {{ReadableStream}} instances initialized via the above
67586758
[=ReadableStream/set up with byte reading support=] algorithm:
67596759

67606760
<div algorithm>
@@ -6786,8 +6786,40 @@ they have more bytes on hand than the [=ReadableStream/current BYOB request view
67866786
[=BufferSource/byte length=]. This avoids unnecessary copies and better respects the wishes of the
67876787
stream's [=consumer=].
67886788

6789+
The following [=ReadableStream/pull from bytes=] algorithm implements these requirements, for the
6790+
common case where bytes are derived from a [=byte sequence=] that serves as the specification-level
6791+
representation of an [=underlying byte source=]. Note that it is conservative and leaves bytes in
6792+
the [=byte sequence=], instead of aggressively [=ReadableStream/enqueueing=] them, so callers of
6793+
this algorithm might want to use the number of remaining bytes as a [=backpressure=] signal.
6794+
6795+
<div algorithm>
6796+
To <dfn export for="ReadableStream">pull from bytes</dfn> with a [=byte sequence=] |bytes| into a
6797+
{{ReadableStream}} |stream|:
6798+
6799+
1. Assert: |stream|.[=ReadableStream/[[controller]]=] [=implements=]
6800+
{{ReadableByteStreamController}}.
6801+
1. Let |available| be |bytes|'s [=byte sequence/length=].
6802+
1. Let |desiredSize| be |available|.
6803+
1. If |stream|'s [=ReadableStream/current BYOB request view=] is non-null, then set |desiredSize|
6804+
to |stream|'s [=ReadableStream/current BYOB request view=]'s [=BufferSource/byte length=].
6805+
1. Let |pullSize| be the smaller value of |available| and |desiredSize|.
6806+
1. Let |pulled| be the first |pullSize| bytes of |bytes|.
6807+
1. Remove the first |pullSize| bytes from |bytes|.
6808+
1. If |stream|'s [=ReadableStream/current BYOB request view=] is non-null, then:
6809+
1. [=ArrayBufferView/Write=] |pulled| into |stream|'s [=ReadableStream/current BYOB request
6810+
view=].
6811+
1. Perform ? [$ReadableByteStreamControllerRespond$](|stream|.[=ReadableStream/[[controller]]=],
6812+
|pullSize|).
6813+
1. Otherwise,
6814+
1. Set |view| to the result of [=ArrayBufferView/create|creating=] a {{Uint8Array}} from |pulled|
6815+
in |stream|'s [=relevant Realm=].
6816+
1. Perform ? [$ReadableByteStreamControllerEnqueue$](|stream|.[=ReadableStream/[[controller]]=],
6817+
|view|).
6818+
</div>
6819+
67896820
Specifications must not [=ArrayBuffer/write=] into the [=ReadableStream/current BYOB request view=]
6790-
after [=ReadableStream/closing=] the corresponding {{ReadableStream}}.
6821+
or [=ReadableStream/pull from bytes=] after [=ReadableStream/closing=] the corresponding
6822+
{{ReadableStream}}.
67916823

67926824
<h4 id="other-specs-rs-reading">Reading</h4>
67936825

0 commit comments

Comments
 (0)