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);
請參閱 文件 > API > 工具程式,以取得關於 Bun 其他 ReadableStream
轉換功能的說明文件。