Convert a ReadableStream to a Uint8Array

To convert a ReadableStream to a Uint8Array, read its contents into an ArrayBuffer with Bun.readableStreamToArrayBuffer, then create a Uint8Array that points to the buffer.

const stream = new ReadableStream();
const buf = await Bun.readableStreamToArrayBuffer(stream);
const uint8 = new Uint8Array(buf);

The stream's bytes() method converts to a Uint8Array directly.

const stream = new ReadableStream();
const uint8 = await stream.bytes();

Bun.readableStreamToBytes(stream) does the same thing, but is deprecated in favor of stream.bytes().


See Bun's other ReadableStream conversion functions.