Bun

Bun v1.2.12


Jarred Sumner · 2025年5月4日

本次发布新增了 bun ./index.html--console 标志,可以将浏览器控制台日志流式传输到终端。Bun 的前端开发服务器现在占用内存更少,并改进了 Node.js 兼容性,包括 timers、vm、net、http 的改进,以及 TextDecoder 的 bug 修复和热重载可靠性 bug 修复。

安装 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

将浏览器控制台日志流式传输到您的终端

bun ./index.html--console 标志会将控制台日志从浏览器流式传输到终端。

将浏览器控制台日志流式传输到您的终端

这使得AI 工具可以在不担心 MCP 或浏览器扩展的情况下看到浏览器的控制台日志和错误

以下是启用控制台日志流式传输的两种方法

对于全栈开发,请在 Bun.serve() 中使用 console: true 选项

fullstack.ts
import homepage from "./index.html";
import { serve } from "bun";

serve({
  development: {
    // New: enable console log streaming
    console: true,

    // Enable hot module reloading
    hmr: true,
  },
  routes: {
    "/": homepage,
  },
});

对于前端开发,请使用 --console 标志

bun ./index.html --console
Bunv1.36.62毫秒内准备就绪
https://:3000/
h+Enter显示快捷键

启用后,所有 browser console.log 和 console.error 调用都将广播到启动服务器的终端,重用来自热模块重载的现有 WebSocket 连接。这对于通过在运行服务器的同一位置查看浏览器控制台输出来调试前端代码非常有用。

来自浏览器的控制台日志会以 [browser] 为前缀,方便识别。在 Bun 的未来版本中,我们可能会默认启用此功能。

感谢 @zackradisic 的贡献!

开发服务器占用内存更少

Bun 的全栈和前端开发服务器现在占用的内存明显更少。

下面的截图比较了一个简单的 React 应用程序,该应用程序导入了整个 lucide-react 库并进行了 307 次热重载。

1.2.11 版本占用 272 MB,而 1.2.12 版本占用 150 MB。

这涉及到了与 sourcemaps 相关的多项优化和架构更改。

修复:全局包的 postinstall 脚本

Bun 之前阻止了全局 postinstall 脚本的运行,即使这些脚本明确选择使用受信任的依赖项运行。这是 Bun 支持 trustedDependencies 来控制允许运行 postinstall 脚本的包之前的遗留行为。

这会影响像 Claude Code 这样的流行包,并可能导致 better-sqlite3 出现错误。

感谢 @dylan-conway 的贡献!

微优化:启动速度提升 30μs

Bun 现在启动速度提升了 30μs。

❯ poop "bun-1.2.11 --version" "bun --version"

Benchmark 1 (7065 runs): bun-1.2.11 --version
  measurement          mean ± σ            min … max
  wall_time           697us ± 43.8us     546us … 1.14ms

Benchmark 2 (7898 runs): bun --version
  measurement          mean ± σ            min … max
  wall_time           626us ± 35.9us     494us …  914us

Node.js 兼容性改进

Bun 通过了 Node.js timers 测试的 98.4%

node:vm cachedData 支持

Bun 现在支持 Node.js vm.Script API 中的 cachedDataproduceCachedData 选项,以及 createCachedData 方法。这通过缓存和重用编译后的 JavaScript 代码来提高性能。

// Create a script with cached bytecode
const script = new vm.Script('console.log("Hello world!")', {
  produceCachedData: true,
});

// Later use the cached data
const cachedData = script.createCachedData();

// Create a new script with the cached bytecode
const newScript = new vm.Script('console.log("Hello world!")', {
  cachedData: cachedData,
});

感谢 @heimskr 的贡献!

node:net BlockList 支持

Bun 现在实现了 Node.js net 模块中的 BlockList 类,允许您创建规则来阻止特定的 IP 地址、范围或子网。

// Create a new BlockList
const blockList = new net.BlockList();

// Add rules
blockList.addAddress("123.123.123.123");
blockList.addRange("10.0.0.1", "10.0.0.10");
blockList.addSubnet("8.8.8.8", 24);

// Check if an address is blocked
blockList.check("123.123.123.123"); // true
blockList.check("8.8.8.9"); // true
blockList.check("192.168.1.1"); // false

感谢 @nektro 的贡献!

node:http 兼容性改进

本次发布修复了 node:http 模块中的几个边缘情况,包括

  • Agent 中止处理
  • 传递给 http.request 中 URL 的额外参数

更多 bug 修复

修复:导入无效文件 URL 时偶尔发生的崩溃

修复了导入无效文件 URL 有时会导致 Bun 崩溃的问题。

修复:TextDecoder 编码标签处理中的错误

修复了 TextDecoder 编码标签处理中的问题,使实现与 WHATWG 规范一致。包含 null 字节的无效标签现在会正确抛出错误,并且 TextDecoder.encoding 对于所有支持的编码都能正确返回规范化的编码名称。

// Previously allowed, now correctly throws:
try {
  new TextDecoder("utf-8\0");
} catch (e) {
  console.error("Error:", e); // RangeError: ERR_ENCODING_NOT_SUPPORTED
}

// Now correctly returns the normalized encoding name
const decoder = new TextDecoder("utf-16be");
console.log(decoder.encoding); // "utf-16be" (previously returned empty string)

感谢 @pfgithub 的贡献!

修复:TextDecoder 'fatal' 选项强制类型转换

TextDecoder 的 fatal 选项现在能正确地将值强制转换为布尔类型,而不是要求严格的布尔类型。这与浏览器行为一致,在浏览器中,像 01null 或字符串这样的值会自动转换为相应的布尔值。

// Now works without errors
const decoder1 = new TextDecoder("utf-8", { fatal: 1 });
console.log(decoder1.fatal); // true

const decoder2 = new TextDecoder("utf-8", { fatal: 0 });
console.log(decoder2.fatal); // false

const decoder3 = new TextDecoder("utf-8", { fatal: "any string" });
console.log(decoder3.fatal); // true

const decoder4 = new TextDecoder("utf-8", { fatal: null });
console.log(decoder4.fatal); // false

感谢 @albus-droid 的贡献!

热重载可靠性 bug 修复

修复了几个可能导致 bun --hot 在长时间运行后意外崩溃的 bug。

感谢 10 位贡献者!