> ## Documentation Index
> Fetch the complete documentation index at: https://bun.ll1025.cn/llms.txt
> Use this file to discover all available pages before exploring further.

# 将 Buffer 转换为 ReadableStream

从 [`Buffer`](https://nodejs.org/api/buffer.html) 创建 [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream) 的简单方法是使用 `ReadableStream` 构造函数并将整个数组作为一个块入队。对于大缓冲区，这种方法可能不太理想，因为它不会以小块方式流式传输数据。

```ts theme={null}
const buf = Buffer.from("hello world");
const stream = new ReadableStream({
  start(controller) {
    controller.enqueue(buf);
    controller.close();
  },
});
```

***

要以更小的块流式传输数据，先从 `Buffer` 创建一个 `Blob` 实例，然后使用 [`Blob.stream()`](https://developer.mozilla.org/en-US/docs/Web/API/Blob/stream) 创建一个 `ReadableStream`。

```ts theme={null}
const buf = Buffer.from("hello world");
const blob = new Blob([buf]);
const stream = blob.stream();
```

***

向 `.stream()` 传递一个数字来设置块大小。

```ts theme={null}
const buf = Buffer.from("hello world");
const blob = new Blob([buf]);

// 设置块大小为 1024 字节
const stream = blob.stream(1024);
```

***

参见 [二进制数据](/runtime/binary-data#conversion)。
