Bun 是采用 Zig 语言编写的高性能 “全家桶” JavaScript 运行时,官方称其为 "all-in-one JavaScript runtime"。所谓 "all in one",是因为 Bun 提供了打包、转译、安装和运行 JavaScript & TypeScript 项目的功能,内置原生打包器 (native bundler)、转译器、task runner、npm 客户端,以及fetch、WebSocket等 Web API。
Bun 最近发布了 0.5.7 版本,主要变化:
- 支持
FormData
FormData是标准的 Web API,用于处理分段上传中的表单字段和文件。
const formData = new FormData(); formData.set("attachment-id", crypto.randomUUID()); formData.set("attachment", Bun.file("./package.json")); const response = await fetch("https://example.com/upload", { method: "POST", body: formData, }); 测试显示,在 Bun 中,response.formData()运行:
- 比 Node v19.6.0 快 25 倍
- 比 Deno v1.30.3 快 4 倍
- 引入
git依赖
Bun 在package.json中现已支持git依赖,可接受多种 git 依赖格式,包括github, git, git+ssh,git+https等。
{ "dependencies": { "zod": "github:colinhacks/zod", "lodash": "[email protected]:lodash/lodash.git#4.17.21" } } 此外还可以使用bun install添加git依赖项。
- 为
AbortSignal添加fetch()
支持使用AbortSignal取消fetch()request。
await fetch("https://example.com", { // Abort if the response is not received after 1 second signal: AbortSignal.timeout(1000), }); 当从 HTTP Server 收到Request时,也可以使用AbortSignal。
export default { async fetch(request: Request): Promise<Response> { request.signal.addEventListener("abort", () => { console.log("Client aborted the request"); }); // ... return new Response(); }, }; 详情查看发布公告。






还没有评论,来说两句吧...