NOTE
本页文档介绍的 Bun.file 和 Bun.write API 经过高度优化,代表使用 Bun 执行文件系统任务的推荐方式。对于 Bun.file 尚未支持的操作(如 mkdir 或 readdir),你可以使用 Bun 对 node:fs 模块的 近乎完整 实现。
读取文件(Bun.file())
Bun.file(path): BunFile
使用 Bun.file(path) 函数创建 BunFile 实例。BunFile 表示惰性加载的文件;初始化它实际上不会从磁盘读取文件。
const foo = Bun.file("foo.txt"); // 相对于 cwd
foo.size; // 字节数
foo.type; // MIME 类型该引用符合 Blob 接口,因此可以各种格式读取内容。
const foo = Bun.file("foo.txt");
await foo.text(); // 内容为字符串
await foo.json(); // 内容为 JSON 对象
await foo.stream(); // 内容为 ReadableStream
await foo.arrayBuffer(); // 内容为 ArrayBuffer
await foo.bytes(); // 内容为 Uint8Array文件引用也可以使用数字 文件描述符 或 file:// URL 创建。
Bun.file(1234);
Bun.file(new URL(import.meta.url)); // 对当前文件的引用BunFile 可以指向磁盘上文件不存在的位置。
const notreal = Bun.file("notreal.txt");
notreal.size; // 0
notreal.type; // "text/plain;charset=utf-8"
const exists = await notreal.exists(); // false默认 MIME 类型是 text/plain;charset=utf-8,但可以通过向 Bun.file 传递第二个参数来覆盖。
const notreal = Bun.file("notreal.json", { type: "application/json" });
notreal.type; // => "application/json;charset=utf-8"为方便起见,Bun 将 stdin、stdout 和 stderr 公开为 BunFile 实例。
Bun.stdin; // 只读
Bun.stdout;
Bun.stderr;删除文件(file.delete())
你可以通过调用 .delete() 函数来删除文件。
await Bun.file("logs.json").delete();写入文件(Bun.write())
Bun.write(destination, data): Promise<number>
Bun.write 函数是一个多用途工具,用于将各种有效负载写入磁盘。
第一个参数是 destination,可以是以下任何类型:
string:文件系统上的路径。使用"path"模块来操作路径。URL:file://描述符。BunFile:文件引用。
第二个参数是要写入的数据。可以是以下任何内容:
stringBlob(包括BunFile)ArrayBuffer或SharedArrayBufferTypedArray(Uint8Array等)Response
所有可能的排列都使用当前平台上最快的可用系统调用处理。
系统调用
| 输出 | 输入 | 系统调用 | 平台 |
|---|---|---|---|
| file | file | copy_file_range | Linux |
| file | pipe | sendfile | Linux |
| pipe | pipe | splice | Linux |
| terminal | file | sendfile | Linux |
| terminal | terminal | sendfile | Linux |
| socket | file 或 pipe | sendfile(如果是 http,不是 https) | Linux |
| file(不存在) | file(路径) | clonefile | macOS |
| file(存在) | file | fcopyfile | macOS |
| file | Blob 或 string | write | macOS |
| file | Blob 或 string | write | Linux |
要将字符串写入磁盘:
const data = `这是最好的时代,这是最坏的时代。`;
await Bun.write("output.txt", data);要将文件复制到磁盘上的另一个位置:
const input = Bun.file("input.txt");
const output = Bun.file("output.txt"); // 还不存在!
await Bun.write(output, input);要将字节数组写入磁盘:
const encoder = new TextEncoder();
const data = encoder.encode("datadatadata"); // Uint8Array
await Bun.write("output.txt", data);要将文件写入 stdout:
const input = Bun.file("input.txt");
await Bun.write(Bun.stdout, input);要将 HTTP 响应的主体写入磁盘:
const response = await fetch("https://bun.com");
await Bun.write("index.html", response);使用 FileSink 增量写入
Bun 提供名为 FileSink 的原生增量文件写入 API。要从 BunFile 获取 FileSink 实例:
const file = Bun.file("output.txt");
const writer = file.writer();要增量写入文件,调用 .write()。
const file = Bun.file("output.txt");
const writer = file.writer();
writer.write("这是最好的时代\n");
writer.write("这是最坏的时代\n");这些块将在内部缓冲。要将缓冲区刷新到磁盘,使用 .flush()。这返回刷新的字节数。
writer.flush(); // 将缓冲区写入磁盘当达到 FileSink 的 高水位标记 时,缓冲区也会自动刷新;也就是说,当其内部缓冲区已满时。此值可以配置。
const file = Bun.file("output.txt");
const writer = file.writer({ highWaterMark: 1024 * 1024 }); // 1MB要刷新缓冲区并关闭文件:
writer.end();请注意,默认情况下,bun 进程将保持活动状态,直到此 FileSink 使用 .end() 显式关闭。要退出此行为,你可以 "unref" 实例。
writer.unref();
// 稍后 "re-ref" 它
writer.ref();目录
Bun 的 node:fs 实现很快,我们还没有实现特定于 Bun 的目录读取 API。目前,你应该在 Bun 中使用 node:fs 来处理目录。
读取目录(readdir)
要在 Bun 中读取目录,使用 node:fs 中的 readdir。
import { readdir } from "node:fs/promises";
// 读取当前目录中的所有文件
const files = await readdir(import.meta.dir);递归读取目录
要在 Bun 中递归读取目录,使用带有 recursive: true 的 readdir。
import { readdir } from "node:fs/promises";
// 递归读取当前目录中的所有文件
const files = await readdir("../", { recursive: true });创建目录(mkdir)
要递归创建目录,使用 node:fs 中的 mkdir:
import { mkdir } from "node:fs/promises";
await mkdir("path/to/dir", { recursive: true });基准测试
以下是 Linux cat 命令的 3 行实现。
// 用法
// bun ./cat.ts ./path-to-file
import { resolve } from "path";
const path = resolve(process.argv.at(-1));
await Bun.write(Bun.stdout, Bun.file(path));要运行文件:
bun ./cat.ts ./path-to-file它在 Linux 上运行大文件时比 GNU cat 快 2 倍。
参考
interface Bun {
stdin: BunFile;
stdout: BunFile;
stderr: BunFile;
file(path: string | number | URL, options?: { type?: string }): BunFile;
write(
destination: string | number | BunFile | URL,
input: string | Blob | ArrayBuffer | SharedArrayBuffer | TypedArray | Response,
): Promise<number>;
}
interface BunFile {
readonly size: number;
readonly type: string;
text(): Promise<string>;
stream(): ReadableStream;
arrayBuffer(): Promise<ArrayBuffer>;
json(): Promise<any>;
writer(params: { highWaterMark?: number }): FileSink;
exists(): Promise<boolean>;
}
export interface FileSink {
write(chunk: string | ArrayBufferView | ArrayBuffer | SharedArrayBuffer): number;
flush(): number | Promise<number>;
end(error?: Error): number | Promise<number>;
start(options?: { highWaterMark?: number }): void;
ref(): void;
unref(): void;
}