Skip to content
Cloudflare Docs

ReadableStream BYOBReader

Background

BYOB is an abbreviation of bring your own buffer. A ReadableStreamBYOBReader allows reading into a developer-supplied buffer, thus minimizing copies.

An instance of ReadableStreamBYOBReader is functionally identical to ReadableStreamDefaultReader with the exception of the read method.

A ReadableStreamBYOBReader is not instantiated via its constructor. Rather, it is retrieved from a ReadableStream:

JavaScript
const { readable, writable } = new TransformStream();
const reader = readable.getReader({ mode: 'byob' });

Methods

  • read(bufferArrayBufferView) : Promise<ReadableStreamBYOBReadResult>

    • Returns a promise with the next available chunk of data read into a passed-in buffer.
  • readAtLeast(minBytes, bufferArrayBufferView) : Promise<ReadableStreamBYOBReadResult>

    • Returns a promise with the next available chunk of data read into a passed-in buffer. The promise will not resolve until at least minBytes bytes have been read. However, fewer than minBytes bytes may be returned if the end of the stream is reached or the underlying stream is closed. Specifically:

      • If minBytes or more bytes are available, the promise resolves with { value: <buffer view sized to bytes read>, done: false }.
      • If the stream ends after some bytes have been read but fewer than minBytes, the promise resolves with the partial data: { value: <buffer view sized to bytes actually read>, done: false }. The next call to read or readAtLeast will then return { value: undefined, done: true }.
      • If the stream ends with zero bytes available (that is, the stream is already at EOF), the promise resolves with { value: <zero-length view>, done: true }.
      • If the stream errors, the promise rejects.
      • minBytes must be at least 1, and must not exceed the byte length of bufferArrayBufferView, or the promise rejects with a TypeError.

Common issues