Bun v0.5.7 发布,Zig 编写的 JavaScript 运行时

Bun 是采用 Zig 语言编写的高性能 “全家桶” JavaScript 运行时,官方称其为 "all-in-one JavaScript runtime"。所谓 "all in one",是因为 Bun 提供了打包、转译、安装和运行 JavaScript & TypeScript 项目的功能,内置原生打包器 (native bundler)、转译器、task runner、npm 客户端,以及fetchWebSocket等 Web API。

Bun v0.5.7 发布,Zig 编写的 JavaScript 运行时

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 倍

Bun v0.5.7 发布,Zig 编写的 JavaScript 运行时

  • 引入git依赖

Bun 在package.json中现已支持git依赖,可接受多种 git 依赖格式,包括githubgitgit+ssh,git+https等。

  {    "dependencies": {      "zod": "github:colinhacks/zod",      "lodash": "git@github.com: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();    },  };
  • 改进setTimeout()与 Node.js 的兼容性
  • bun wiptest变更为bun test,可更美观地打印差异信息
  • 改进对 AWS Lambda 和 GitHub Actions 的支持

详情查看发布公告

展开阅读全文
发表评论

相关文章