# ReadableStreamDefaultReader

The `ReadableStreamDefaultReader` interface is the default reader of the Streams API in Azion Runtime. It reads the data of a stream that comes from the network, such as the body of a response to a `fetch()` request. A [ReadableStreamBYOBReader](/en/documentation/devtools/runtime/api-reference/readable-byob-reader/) works only with a stream that has a byte source, but a `ReadableStreamDefaultReader` reads from any type of [ReadableStream](/en/documentation/devtools/runtime/api-reference/readable-stream/) source. For more information, refer to [ReadableStreamDefaultReader](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamDefaultReader) on MDN Web Docs.

---

## Constructor

The [`ReadableStreamDefaultReader()`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamDefaultReader/ReadableStreamDefaultReader) constructor creates and returns a reader for the stream you pass to it:

```javascript
new ReadableStreamDefaultReader(stream)
```

The `getReader()` method of a `ReadableStream` also returns a `ReadableStreamDefaultReader` and locks the stream to it.

---

## Properties

| Property                                                                                        | Type    | Description                                                                                                                                                   |
| ----------------------------------------------------------------------------------------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [`closed`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamDefaultReader/closed) | Promise | Fulfills when the stream closes. Rejects when the stream throws an error or when the lock of the reader is released. Use it to run code when the stream ends. |

---

## Methods

| Method                                                                                                             | Description                                                                                                                                                                                         |
| ------------------------------------------------------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [`reader.cancel(reason)`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamDefaultReader/cancel)     | 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()`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamDefaultReader/read)               | Returns a promise that gives access to the next chunk in the internal queue of the stream, as an object with `value` and `done`. After the last chunk, `done` is `true` and `value` is `undefined`. |
| [`reader.releaseLock()`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamDefaultReader/releaseLock) | Releases the lock of the reader on the stream. After the call, the `locked` property of the stream is `false`.                                                                                      |

---

## Example

This handler fetches a page, reads its body one chunk at a time with a reader, and adds each chunk to a stream it constructs. It returns that stream as the body of a `Response` with an HTML `Content-Type` header:

```javascript
export default {
  async fetch(request, env, ctx) {
    return fetch("https://www.example.org/").then((response) => {
      const reader = response.body.getReader();
      const stream = new ReadableStream({
        start(controller) {
          // Handle each chunk of data
          function push() {
            // "done" is a Boolean and "value" is a Uint8Array
            return reader.read().then(({ done, value }) => {
              // No more data to read: close the stream
              if (done) {
                controller.close();
                return;
              }

              // Pass the chunk to the client through the controller
              controller.enqueue(value);
              push();
            });
          }

          push();
        }
      });

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

The response that the function returns has these values: its status, its `content-type` header, the length of its body in characters, and the first 15 characters of the body:

```json
{
 "status": 200,
 "contentType": "text/html",
 "bodyChars": 577,
 "startsWith": "<!doctype html>"
}
```

---

## Related resources

- [ReadableStream](/en/documentation/devtools/runtime/api-reference/readable-stream.md): The stream that a reader locks, and the methods that pipe, split, and cancel it.
- [ReadableStreamBYOBReader](/en/documentation/devtools/runtime/api-reference/readable-byob-reader.md): The reader that reads a byte stream into a buffer you supply.
- [Response](/en/documentation/devtools/runtime/api-reference/response.md): The response object whose `body` property is the stream a reader reads.
- [Web APIs](/en/documentation/devtools/runtime/api-reference/javascript.md): The other Web APIs that Azion Runtime supports.
