Skip to content

Bun 提供了許多便捷函數,用於將 ReadableStream 的內容讀取為不同格式。此代碼片段將 ReadableStream 的內容讀取為 ArrayBuffer,然後創建一個指向該緩沖區的 Uint8Array

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

此外,還有一個便捷方法可以直接轉換為 Uint8Array

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

請參閱 文檔 > API > 工具函數 獲取 Bun 其他 ReadableStream 轉換函數的文檔。

Bun學習網由www.bunjs.com.cn整理維護