将 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()。