Edge Developer Platform
  • Edge Functions
    • Overview
    • Getting Started
    • Operation Guide
      • Function Management
      • Function Trigger
    • Runtime APIs
      • addEventListener
      • Cache
      • Cookies
      • Encoding
      • Fetch
      • FetchEvent
      • Headers
      • Request
      • Response
      • Streams
        • ReadableStream
        • ReadableStreamBYOBReader
        • ReadableStreamDefaultReader
        • TransformStream
        • WritableStream
        • WritableStreamDefaultWriter
      • Web Crypto
      • Web standards
      • Images
        • ImageProperties
    • Sample Functions
      • Returning an HTML Page
      • Returning a JSON Object
      • Fetch Remote Resources
      • Authenticating a Request Header
      • Modifying a Response Header
      • Performing an A/B Test
      • Setting Cookies
      • Performing Redirect Based on the Request Location
      • Using the Cache API
      • Caching POST Requests
      • Responding in Streaming Mode
      • Merging Resources and Responding in Streaming Mode
      • Protecting Data from Tampering
      • Rewriting a m3u8 File and Configuring Authentication
      • Adaptive Image Resize
      • Image Adaptive WebP
      • Customize Referer restriction rules
      • Remote Authentication
      • HMAC Digital Signature
      • Naming a Downloaded File
      • Obtaining Client IP Address
    • Best Practices
      • Adaptive Image Format Conversion via Edge Functions

ReadableStreamBYOBReader

The ReadableStreamBYOBReader API defines a reader for a readable stream. It is designed based on the standard Web API ReadableStreamBYOBReader. BYOB is an abbreviation of bring your own buffer. A ReadableStreamBYOBReader object allows to read data from streams and write the read data to the buffer, thereby reducing replicas.
Note:
A ReadableStreamBYOBReader object cannot be constructed directly. You can use the ReadableStream.getReader method to construct a ReadableStreamBYOBReader object.

Overview

// Use TransformStream to construct a ReadableStream object.
const { readable } = new TransformStream();

// Use the ReadableStream object to obtain the reader.
const reader = readable.getReader({
mode: 'byob',
});

Attributes

// readable.locked
readonly locked: boolean;
The locked attribute returns a Promise object. If the stream is closed, the status of the Promise object is fulfilled. If an exception occurs on the stream or the lock on the reader is released, the status of the Promise object is rejected.

Methods

read

reader.read(bufferView: ArrayBufferView): Promise<{value: ArrayBufferView, done: boolean}>;
The read() method reads data from the stream and writes the read data to the bufferView on the buffer.
Note:
You cannot initiate the next stream reading operation until the current stream reading operation ends.

Returned values

The reader.read method returns a Promise object that contains the read data and the reading status.
If a chunk is available, the Promise object is in the fulfilled status and contains an object in the { value: theChunk, done: false } format.
If the stream is closed, the status of the Promise object is switched to fulfilled, and an object in the { value: theChunk, done: true } format is contained.
If an exception occurs on the stream, the Promise object is in the rejected status, and the relevant error information is included.

cancel

reader.cancel(reason?: string): Promise<string>;
The cancel() method closes the stream and ends the reading operation.

releaseLock

reader.releaseLock(): void;
The releaseLock() method cancels the association with the stream and releases the lock on the stream.

References