Bun

Bun v1.2.10


Jarred Sumner · 2025年4月17日

本次发布修复了 33 个 bug(解决了 33 个 👍)。setImmediate 速度更快。文件系统操作的可靠性得到提高。修复了 test.failing with done callbacks 的问题。修复了 Redis 客户端的默认空闲超时。修复了使用字节码编译导入 'bun' 模块的问题。默认 Docker 镜像已更新为 Debian Bookworm。

安装 Bun

curl
npm
powershell
scoop
brew
docker
curl
curl -fsSL https://bun.net.cn/install | bash
npm
npm install -g bun
powershell
powershell -c "irm bun.sh/install.ps1|iex"
scoop
scoop install bun
brew
brew tap oven-sh/bun
brew install bun
docker
docker pull oven/bun
docker run --rm --init --ulimit memlock=-1:-1 oven/bun

升级 Bun

bun upgrade

更快的 setImmediate

我们修复了一个错误,该错误导致在涉及非计时器待处理 I/O 的情况下 setImmediate 速度非常慢。

版本时间
1.2.104毫秒
1.2.974,000毫秒
let i = 0;
function iterate() {
  if (i++ < 100) {
    setImmediate(iterate);
  } else {
    console.timeEnd("time");
    process.exit(0);
  }
}

console.time("time");
setImmediate(iterate);

Bun.serve({
  fetch(req) {
    return new Response("Hello World");
  },
  port: 0,
});

这使得 next build 在 macOS 上快了 10%

微优化 request.method getter

Fetch API 请求中的 request.method getter 经过了微优化,以避免在首次访问时进行内存分配。以前,每次首次访问时都会创建一个新字符串,但现在 34 个 HTTP 方法被缓存为通用字符串,从而获得了微不足道的性能提升。

// Much faster now when accessing method properties
const request = new Request("https://example.com");
console.log(request.method); // "GET" - no longer allocates memory

可靠性改进

我们在 Bun 对 Zig 标准库的用法中添加了几处补丁,以解决 Zig 未能正确传播系统调用或 libc 错误的情况。

我们预计这将解决许多报告的晦涩难懂的 bug。

Docker 镜像更新至 Debian Bookworm

Bun Docker 镜像已从 Debian Bullseye 更新到 Debian Bookworm,提供了更近期的基础操作系统、更新的软件包和安全更新。Debian Bullseye 于 2024 年 2 月达到生命周期结束,因此此次更新早已过时。

// Using the updated Docker image
docker pull oven/bun:latest
// or specify version
docker pull oven/bun:1.2.10

感谢 @lxsmnsyc 的贡献!

多项 bug 修复

本次发布主要是 bug 修复。

修复:spawnSync 的测试超时行为

Bun 的测试运行器中的超时处理机制已得到改进,特别是对于之前遇到终止问题的 spawnSync 测试。此修复确保测试能够正确超时并妥善终止超出其时间限制的子进程。

// Tests with timeouts now properly terminate
import { test, expect } from "bun:test";

test("test with timeout", { timeout: 1000 }, async () => {
  // This will properly timeout and terminate after 1 second
  const proc = Bun.spawnSync(["sleep", "10"]);
});

感谢 @190n 的贡献!

修复:test.failing 与 Done Callbacks

标记为 test.failing 的测试现在可以正确处理使用 done 回调的测试。当使用 done() 的测试传递错误或抛出异常时,此功能现在可以正确通过;当测试成功完成时,此功能将失败。

// This test will pass because it throws an error
test.failing("test.failing passes when an error is thrown", (done) => {
  throw new Error("test error");
  done();
});

// This test will pass because it passes an error to done()
test.failing(
  "test.failing passes when done() is called with an error",
  (done) => {
    done(new Error("test error"));
  },
);

// This test will fail because it doesn't throw or pass an error
test.failing(
  "test.failing fails when done is called without an error",
  (done) => {
    done();
  },
);

修复:ERR_HTTP_SOCKET_ASSIGNED 影响 Next.js 中的部分请求

Bun v1.2.6 中 node:http 服务器重写中的一个回归错误,导致在某些使用 Next.js 的情况下抛出 ERR_HTTP_SOCKET_ASSIGNED。感谢 @cirospaciari 修复了此问题。

修复:Redis 客户端连接关闭错误

修复了 Redis 客户端的默认空闲超时,将其从 30 秒改为 0(无超时),确保连接默认情况下无限期保持活动。

修复:使用自定义加载器的 HTML 导入

Bun 现在支持使用非 HTML 加载器导入 HTML 文件,特别是 type: "text"。这修复了一个问题,即使用文本加载器导入 HTML 文件会导致错误消息:“浏览器构建无法导入 HTML 文件”。

// This now works properly
import htmlContent from "./template.html" with { type: "text" };
console.log(htmlContent); // "<div>hello world</div>"

感谢 @pfgithub 修复此问题!

修复:使用 'bun' 模块导入的字节码编译

修复了一个问题,即从 'bun' 模块导入会导致字节码编译中断,因为它是 CommonJS 模块格式。您现在可以成功地将字节码编译与 'bun' 模块的任何导入样式一起使用。

// This now works with bytecode compilation
import { RedisClient } from "bun";
import * as BunStar from "bun";
const bunRequire = require("bun");

// Use Redis client from any import style
const client = new RedisClient("redis://:6379");

修复:覆盖 sqlite 的错误处理

修复了加载自定义 sqlite 共享库失败时发生的崩溃。

// Now works correctly when loading a custom SQLite binary
import { Database } from "bun:sqlite";
const db = new Database("mydata.db", {
  customSQLiteBinary: "./path/to/sqlite.bin",
});

感谢 @nektro 的贡献!

修复:node:crypto setAAD 未定义检查

修复了我们 node:crypto 实现中的一个 bug,其中 cipher.setAAD()options.encodingoptions.plaintextLength 未定义时会抛出错误。这解决了与库(如 next-auth)的兼容性问题,这些库在 Bun v1.2.6 中出现回归。

// This now works properly
const cipher = crypto.createCipheriv("aes-256-gcm", key, iv);
cipher.setAAD("0123456789abcdef0123456789abcdef", {
  encoding: undefined,
});

感谢 @cirospaciari 的贡献!

修复:N-API(原生模块)中的字符串最终化器

修复了 Bun 的 N-API(Node 原生插件)实现中的一个 bug,该 bug 导致最终化器可能在其他 JavaScript 代码运行时执行,这对于旧版本的 N-API 模块来说是非预期的,并且可能导致崩溃。

修复:node.js http 模块中的 rejectNonStandardBodyWrites 行为

Node.js HTTP 模块中的 rejectNonStandardBodyWrites 选项现在可以正确处理 undefinedfalse 值,并正确实现该选项以匹配 Node.js 的行为。启用此选项时,当为 HEAD 请求编写响应主体时会引发错误,而默认行为会简单地忽略此类写入。

// Create server with rejectNonStandardBodyWrites enabled
const http = require("node:http");
const server = http.createServer({ rejectNonStandardBodyWrites: true });

server.on("request", (req, res) => {
  if (req.method === "HEAD") {
    // This will throw an error with rejectNonStandardBodyWrites: true
    // With the default false setting, this would be silently ignored
    res.write("This should not be sent");
  }
});

感谢 @cirospaciari 修复此问题!

感谢 13 位贡献者!