运行时包管理器打包器测试运行器代码检查格式化指南参考博客反馈

将 ReadableStream 转换为 Uint8Array

要将 ReadableStream 转换为 Uint8Array,请使用 Bun.readableStreamToArrayBuffer 将其内容读取到 ArrayBuffer 中,然后创建一个指向该缓冲区的 Uint8Array。

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

此流的 bytes() 方法可直接转换为 Uint8Array。

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

Bun.readableStreamToBytes(stream) 执行相同的操作,但已弃用,建议使用 stream.bytes()。


查看 Bun 的其他 ReadableStream 转换函数。