ReadableStreamBYOBReader
The ReadableStreamBYOBReader interface of Azion Runtime: its constructor, the closed property, and the methods that read a byte stream into a buffer you supply.
The ReadableStreamBYOBReader interface is part of the Streams API of Azion Runtime. It reads a byte stream into a buffer that you supply, without copying the data. Use it to move data from sources that deliver it as a series of anonymous bytes, such as files. BYOB stands for “Bring Your Own Buffer”. For more information, refer to ReadableStreamBYOBReader on MDN Web Docs.
Constructor
The ReadableStreamBYOBReader() constructor creates and returns a reader for the stream you pass to it:
You can also create the reader from the stream: call getReader() on a ReadableStream and set mode: 'byob' in its options. The examples on this page create the stream as a byte stream, with type: 'bytes' in its handlers.
Properties
| Property | Type | Description |
|---|---|---|
closed | Promise | Fulfills when the stream closes. Rejects when the stream throws an error or when the reader releases its lock. Use it to run code when the stream ends. |
Methods
| Method | Description |
|---|---|
reader.cancel(reason) | Returns a promise that resolves when the stream is canceled. A call signals that the consumer has lost interest in the stream. The underlying source receives reason and can use it or ignore it. |
reader.read(view) | Takes a view that the stream writes data into, such as a Uint8Array. Returns a promise that resolves with the next chunk of the stream, or rejects when the stream is closed or has an error. |
reader.releaseLock() | Releases the lock of the reader on the stream. |
Examples
This handler constructs a byte stream that holds 5 bytes, creates a BYOB reader with getReader(), and reads the stream into an 8-byte buffer. It returns the class name of the reader and the result of read():
The function returns these values. The returned view holds the 5 bytes the stream wrote, not the full 8 bytes of the buffer:
To read a stream chunk by chunk into one buffer, call read() again from the result of each read, with a view of the part of the buffer that is still empty. In this sample, stream is a byte stream of 10 bytes and buffer holds 4,000 bytes: