W3cubDocs

/DOM

ReadableStreamBYOBReader

This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

The ReadableStreamDefaultReader interface of the Streams API represents a BYOB ("bring your own buffer") reader that can be used to read stream data supplied by the developer (e.g. a custom ReadableStream.ReadableSteam() constructor).

Constructor

ReadableStreamBYOBReader()
Creates and returns a ReadableStreamBYOBReader() object instance.

Properties

ReadableStreamBYOBReader.closed
Allows you to write code that responds to an end to the streaming process. Returns a promise that fulfills if the stream becomes closed or the reader's lock is released, or rejects if the stream errors.

Methods

ReadableStreamBYOBReader.cancel()
Cancels the stream, signaling a loss of interest in the stream by a consumer. The supplied reason argument will be given to the underlying source, which may or may not use it.
ReadableStreamBYOBReader.read()
Returns a providing access to the next chunk in the stream's internal queue.
ReadableStreamBYOBReader.releaseLock()
Releases the reader's lock on the stream.

Examples

In the following example, an artifical Response is created to stream HTML fragments fetched from another resource to the browser.

It demonstrates the usage of a ReadableStream in combination with a Uint8Array.

fetch("https://www.example.org/").then((response) => {
  const reader = response.body.getReader();
  const stream = new ReadableStream({
    start(controller) {
      // The following function handles each data chunk
      function push() {
        // "done" is a Boolean and value a "Uint8Array"
        return reader.read().then(({ done, value }) => {
          // Is there no more data to read?
          if (done) {
            // Tell the browser that we have finished sending data
            controller.close();
            return;
          }

          // Get the data and send it to the browser via the controller
          controller.enqueue(value);
        }).then(push);
      };
      
      push();
    }
  });

  return new Response(stream, { headers: { "Content-Type": "text/html" } });
});

Specifications

Specification Status Comment
Streams
The definition of 'ReadableStreamBYOBReader' in that specification.
Living Standard Initial definition

Browser Compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support

52.0

No support[1] ?

39

?
Feature Android Webview Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support 52 52 No support[1] ? 39 ?

[1] Readable streams are currently enabled in Firefox, but hidden behind the dom.streams.enabled and javascript.options.streams prefs.

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamBYOBReader