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 中使用串流的資訊。