Bun.file() 函式接受一個路徑並返回一個 BunFile 例項。BunFile 類擴充套件了 Blob,允許您以各種格式惰性地讀取檔案。使用 .stream() 以增量方式將檔案作為 ReadableStream 消耗。
const path = "/path/to/package.json";
const file = Bun.file(path);
const stream = file.stream();
可以使用 for await 將流的塊作為 非同步迭代器 進行消耗。
for await (const chunk of stream) {
chunk; // => Uint8Array
}
有關在 Bun 中使用流的更多資訊,請參閱 Streams 文件。