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