Bun

指南

使用 Bun 将 ReadableStream 转换为 Uint8Array

Bun 提供了许多便捷的函数,用于将 ReadableStream 的内容读取到不同的格式。此代码片段将 ReadableStream 的内容读取到 ArrayBuffer,然后创建一个指向该缓冲区的 Uint8Array

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

此外,还有一个便捷的方法可以直接转换为 Uint8Array

const stream = new ReadableStream();
const uint8 = await Bun.readableStreamToBytes(stream);

有关 Bun 其他 ReadableStream 转换函数的文档,请参阅 文档 > API > Utils