Skip to content

Commit 72195e2

Browse files
author
Jake Champion
committed
chore: add byob stream reference documentation
1 parent a573654 commit 72195e2

File tree

13 files changed

+425
-0
lines changed

13 files changed

+425
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
8+
# byobRequest
9+
10+
The **`byobRequest`** read-only property of the `ReadableByteStreamController` interface returns the current BYOB request, or `null` if there are no pending requests.
11+
12+
An underlying byte source should check this property, and use it to write data to the stream if it exists (rather than using `ReadableByteStreamController.enqueue()`).
13+
This will result in an efficient zero-byte transfer of the data to the consumer.
14+
15+
## Value
16+
17+
A `ReadableStreamBYOBRequest` object instance, or `null`.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
8+
# close()
9+
10+
The **`close()`** method of the `ReadableByteStreamController` interface closes the associated stream.
11+
12+
This might be called by the underlying source when its data source has been exhausted/completed.
13+
14+
> **Note:** Readers will still be able to read any previously-enqueued chunks from the stream, but once those are read, the stream will become closed.
15+
> However if there is an outstanding and partially written `byobRequest` when `close()` is called, the stream will be errored.
16+
17+
## Syntax
18+
19+
```js
20+
close()
21+
```
22+
23+
### Parameters
24+
25+
None.
26+
27+
### Return value
28+
29+
`undefined`.
30+
31+
### Exceptions
32+
33+
- `TypeError`
34+
- : Thrown if the source object is not a `ReadableByteStreamController`, it is already closed, or the stream is not readable for some other reason.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
8+
# desiredSize
9+
10+
The **`desiredSize`** read-only property of the`ReadableByteStreamController` interface returns the number of bytes required to fill the stream's internal queue to its "desired size".
11+
12+
The value is used by the stream to indicate a preferred flow rate to the underlying source.
13+
Sources that support throttling or pausing their inflow of data (not all do!) should control the inflow such that `desiredSize` of the stream buffer is kept positive and as close to zero as possible.
14+
15+
The `desiredSize` is used to apply backpressure from downstream consumers.
16+
17+
## Value
18+
19+
An integer. Note that this can be negative if the queue is over-full.
20+
21+
The value will be `null` if the stream has errored and `0` if it is closed.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
8+
# enqueue()
9+
10+
The **`enqueue()`** method of the `ReadableByteStreamController` interface enqueues a given chunk on the associated readable byte stream (the chunk is copied into the stream's internal queues).
11+
12+
This should only be used to transfer data to the queue when `byobRequest` is `null`.
13+
14+
## Syntax
15+
16+
```js
17+
enqueue(chunk)
18+
```
19+
20+
### Parameters
21+
22+
- `chunk`
23+
- : The chunk to enqueue.
24+
25+
### Return value
26+
27+
`undefined`.
28+
29+
### Exceptions
30+
31+
- `TypeError`
32+
- : Thrown if the source object is not a `ReadableByteStreamController`, or the stream cannot be read for some other reason, or the chunk is not an object, or the chunk's internal array buffer is non-existent, zero-length, or detached.
33+
It is also thrown if the stream has been closed.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
8+
# error()
9+
10+
The **`error()`** method of the `ReadableByteStreamController` interface causes any future interactions with the associated stream to error with the specified reason.
11+
12+
This is commonly called by an underlying source to surface an error from the interface where it gets its data (such as a file-read or socket error).
13+
It can also be called from elsewhere to trigger a stream error, for example if another part of the system that the stream relies on fails.
14+
15+
## Syntax
16+
17+
```js
18+
error(errorObject)
19+
```
20+
21+
### Parameters
22+
23+
- `errorObject`
24+
- : Any object that you want future interactions to fail with.
25+
26+
### Return value
27+
28+
`undefined`
29+
30+
### Exceptions
31+
32+
- `TypeError`
33+
- : Thrown if the source object is not a `ReadableByteStreamController`, or the stream is not readable for some other reason.
34+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
8+
# ReadableStreamBYOBReader()
9+
10+
The **`ReadableStreamBYOBReader()`** constructor creates and returns a `ReadableStreamBYOBReader` object instance.
11+
12+
> **Note:** You generally wouldn't use this constructor manually;
13+
> instead, you'd use the `ReadableStream.getReader()` method with the argument `"byob"`.
14+
15+
## Syntax
16+
17+
```js
18+
new ReadableStreamBYOBReader(stream)
19+
```
20+
21+
### Parameters
22+
23+
- `stream`
24+
- : The `ReadableStream` to be read.
25+
26+
### Return value
27+
28+
An instance of the `ReadableStreamBYOBReader` object.
29+
30+
### Exceptions
31+
32+
- `TypeError`
33+
- : Thrown if the supplied `stream` parameter is not a `ReadableStream`, or it is already locked for reading by another reader, or its stream controller is not a `ReadableByteStreamController`.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
8+
# cancel()
9+
10+
The **`cancel()`** method of the `ReadableStreamBYOBReader` interface returns a `Promise` that resolves when the stream is canceled.
11+
Calling this method signals a loss of interest in the stream by a consumer.
12+
13+
> **Note:** If the reader is active, the `cancel()` method behaves the same as that for the associated stream (`ReadableStream.cancel()`).
14+
15+
## Syntax
16+
17+
```js
18+
cancel()
19+
cancel(reason)
20+
```
21+
22+
### Parameters
23+
24+
- `reason` __optional__
25+
- : A human-readable reason for the cancellation. The underlying source may or may not use it.
26+
27+
### Return value
28+
29+
A `Promise`, which fulfills with the value given in the `reason` parameter.
30+
31+
### Exceptions
32+
33+
- `TypeError`
34+
- : The source object is not a `ReadableStreamBYOBReader`, or the stream has no owner.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
8+
# closed
9+
10+
The **`closed`** read-only property of the `ReadableStreamBYOBReader` interface returns a `Promise` that fulfills when the stream closes, or rejects if the stream throws an error or the reader's lock is released.
11+
12+
This property enables you to write code that responds to an end to the streaming process.
13+
14+
## Value
15+
16+
A `Promise`.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
8+
# read()
9+
10+
The **`read()`** method of the `ReadableStreamBYOBReader` interface is used to read data into a view on a user-supplied buffer from an associated readable byte stream.
11+
A request for data will be satisfied from the stream's internal queues if there is any data present.
12+
If the stream queues are empty, the request may be supplied as a zero-copy transfer from the underlying byte source.
13+
14+
The method takes as an argument a view on a buffer that supplied data is to be read into, and returns a `Promise`.
15+
The promise fulfills with an object that has properties `value` and `done` when data comes available, or if the stream is cancelled.
16+
If the stream is errored, the promise will be rejected with the relevant error object.
17+
18+
If a chunk of data is supplied, the `value` property will contain a new view.
19+
This will be a view over the same buffer/backing memory (and of the same type) as the original `view` passed to the `read()` method, now populated with the new chunk of data.
20+
Note that once the promise fulfills, the original `view` passed to the method will be detached and no longer usable.
21+
The promise will fulfill with a `value: undefined` if the stream has been cancelled.
22+
In this case the backing memory region of `view` is discarded and not returned to the caller (all previously read data in the view's buffer is lost).
23+
24+
The `done` property indicates whether or not more data is expected.
25+
The value is set `true` if the stream is closed or cancelled, and `false` otherwise.
26+
27+
## Syntax
28+
29+
```js
30+
read(view)
31+
```
32+
33+
### Parameters
34+
35+
- `view`
36+
- : The view that data is to be read into.
37+
38+
### Return value
39+
40+
A `Promise`, which fulfills/rejects with a result depending on the state of the stream.
41+
42+
The following are possible:
43+
44+
- If a chunk is available and the stream is still active, the promise fulfills with an object of the form:
45+
46+
```
47+
{ value: theChunk, done: false }
48+
```
49+
50+
`theChunk` is a view containing the new data.
51+
This is a view of the same type and over the same backing memory as the `view` passed to the `read()` method.
52+
The original `view` will be detached and no longer usable.
53+
54+
- If the stream is closed, the promise fulfills with an object of the form (where `theChunk` has the same properties as above):
55+
56+
```
57+
{ value: theChunk, done: true }
58+
```
59+
60+
- If the stream is cancelled, the promise fulfills with an object of the form:
61+
62+
```
63+
{ value: undefined, done: true }
64+
```
65+
66+
In this case the backing memory is discarded.
67+
68+
- If the stream throws an error, the promise rejects with the relevant error.
69+
70+
### Exceptions
71+
72+
- `TypeError`
73+
- : The source object is not a `ReadableStreamBYOBReader`, the stream has no owner, the view is not an object or has become detached, the view's length is 0, or `ReadableStreamBYOBReader.releaseLock()` is called (when there's is a pending read request).
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
hide_title: false
3+
hide_table_of_contents: false
4+
pagination_next: null
5+
pagination_prev: null
6+
---
7+
8+
# releaseLock()
9+
10+
The **`releaseLock()`** method of the `ReadableStreamBYOBReader` interface releases the reader's lock on the stream.
11+
After the lock is released, the reader is no longer active.
12+
13+
The reader will appear errored if the associated stream is errored when the lock is released; otherwise, the reader will appear closed.
14+
15+
If the reader's lock is released while it still has pending read requests then the promises returned by the reader's `ReadableStreamBYOBReader.read()` method are immediately rejected with a `TypeError`.
16+
Unread chunks remain in the stream's internal queue and can be read later by acquiring a new reader.
17+
18+
## Syntax
19+
20+
```js
21+
releaseLock()
22+
```
23+
24+
### Parameters
25+
26+
None.
27+
28+
### Return value
29+
30+
`undefined`.
31+
32+
### Exceptions
33+
34+
- `TypeError`
35+
- : Thrown if the source object is not a `ReadableStreamBYOBReader`.

0 commit comments

Comments
 (0)