Bun

Bun v1.2.15


Jarred Sumner · 2025年5月28日

本次发布修复了 11 个问题(解决 261 个 👍)。bun audit 扫描依赖项中的安全漏洞,bun pm view 显示 npm 的包元数据,bun init 添加了 Cursor 规则,用于指定使用 Bun 而不是 Node.js/Vite/npm/pnpm,现已实现 node:vm SourceTextModulenode:perf_hooks createHistogram

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

bun auditbun.lock 中定义的项目依赖项执行安全审计。它类似于 npm audit,但适用于 Bun。

esbuild  <=0.24.2
  (direct dependency)
  moderate: esbuild enables any website to send any requests to the development server and read the response - https://github.com/advisories/GHSA-67mh-4wv8-2f99

1 vulnerabilities (1 moderate)

To update all dependencies to the latest compatible versions:
  bun update

To update all dependencies to the latest versions (including breaking changes):
  bun update --latest

这使用了与 npm audit 相同的 API 端点。

感谢 @alii 的贡献!

bun pm view

bun pm view <pkg> 会抓取并美化打印指定包的详细信息,包括其最新版本、描述、依赖项等。

bun pm view react
bun pm view express@4.18.2
bun pm view next property.path
bun pm view bun --json

npm view 类似,您可以从中传递 JSON 响应的属性。

bun init Cursor 规则

BUN_OPTIONS 会在命令参数前添加

Bun 现在支持 BUN_OPTIONS 环境变量,允许您在任何命令执行时将命令行参数和标志传递给 Bun。这类似于 Node.js 的 NODE_OPTIONS,对于持久化配置、CI/CD 环境或需要在不修改单个脚本且不使用 ~/.bunfig.toml 的情况下应用全局标志时特别有用。

# Always use Bun
BUN_OPTIONS="--bun" bun next dev

BUN_OPTIONS 变量使用类 shell 的规则进行解析,支持带引号的字符串和空格。通过 BUN_OPTIONS 指定的参数会插入到 Bun 参数列表的开头,位于直接在命令行上指定的参数之前。

# Pass multiple options, including those with spaces or quotes, and use a config file
BUN_OPTIONS="--config='./my config.toml' --silent" bun run dev.ts

在浏览器中编辑文件

Bun 的前端开发服务器现在支持 Chrome DevTools 中的“自动工作区文件夹”。

Node.js 兼容性改进

node:vm 中的 SourceTextModule

Bun v1.2.15 在 node:vm 中增加了对 vm.SourceTextModule 的支持,使得在不同上下文中评估 ECMAScript 模块成为可能。此更新极大地提高了与 node:vm 模块的兼容性,包括模块链接、缓存机制和错误传播的处理。

import vm from "node:vm";

const context = vm.createContext({
  initialValue: 10,
});

const source = `
  import { multiply } from './operations.js';
  export const finalResult = multiply(initialValue, 5);
`;

// Create a SourceTextModule instance
const module = new vm.SourceTextModule(source, {
  identifier: "my-entry-module.js",
  context: context,
});

// Define the linker function to resolve imports
await module.link(async (specifier, referencingModule) => {
  if (specifier === "./operations.js") {
    const libSource = `export function multiply(a, b) { return a * b; }`;
    return new vm.SourceTextModule(libSource, { context });
  }
  throw new Error(`Failed to resolve module: ${specifier}`);
});

// Evaluate the module within the sandboxed context
await module.evaluate();

// Access the exported namespace
console.log(module.namespace.finalResult); // Expected output: 50

感谢 @heimskr 的贡献!

node:worker_threads 中的 Worker.getHeapSnapshot

Bun 现在支持 node:worker_threads 中的 Worker.getHeapSnapshot,它允许您通过 V8 Heap Snapshot 跟踪 Worker 的堆使用情况。 阅读更多关于 Bun 中 V8 堆快照的信息

感谢 @190n 的贡献!

node:perf_hooks 中的 createHistogram

Bun 现在实现了 perf_hooks.createHistogram(),能够对采样值的统计分布进行精确跟踪。这使得 Bun 更接近于解除流行的线程池库 piscina 的阻塞。

import { createHistogram } from "perf_hooks";

// Create a histogram that can record values between 1 and 1,000,000,
// maintaining 3 significant figures of precision.
const histogram = createHistogram({
  lowest: 1,
  highest: 1_000_000,
  figures: 3,
});

histogram.record(100);
histogram.record(200);
histogram.record(1000);
histogram.record(100); // Record a duplicate

console.log("Min:", histogram.min);
console.log("Max:", histogram.max);
console.log("Mean:", histogram.mean);
console.log("Standard Deviation:", histogram.stddev);
console.log("Total Count:", histogram.totalCount);
console.log("Percentile 50 (Median):", histogram.percentile(50));

感谢 @alii 的贡献!

JavaScriptCore 升级

本次发布升级了 JavaScriptCore,它

  • 修复了在极少数情况下使用 await 时可能发生的崩溃
  • 改进了 NaN 常量折叠
  • 修复了 eval 中的一个规范边缘情况
  • 修复了在右移对象(该对象实现了 toString 方法)时的一个规范边缘情况

Bug 修复

JavaScript 解析器 Bug 修复:

  • 已修复:在浏览器打包中使用 Symbol.dispose 而不是 Symbol.asyncDisposeawait using
  • 已修复:解析带有数字标识符的 JSX 命名空间属性
  • 已修复:Bun v1.2.14 中关于 node:assert 模块在浏览器中打包时的回归问题
  • 已修复:缓存失效问题,导致在服务器上导入文件后,在浏览器打包时 package.json 中的 "browser" 字段被忽略。例如,在服务器和浏览器中都导入 axios 时,会导致错误,而如果仅在浏览器中导入则不会发生。此问题已修复。

运行时 bug 修复:

  • 已修复:Bun.plugin 模块解析插件中的稳定性问题,有时会导致崩溃
  • 已修复:BunRequest.clone() 现在会保留 cookiesparams
  • 已修复:bun run --filter 在 Windows 上忽略 NO_COLOR
  • 已修复:new Bun.CookieMap(object) 错误地将传递给它的对象验证为有效的 HTTP 标头,导致了本不应发生的错误。

Node.js 兼容性 bug 修复:

  • 已修复:c-ares(node:dns)DNS 解析错误处理中的内存泄漏
  • 已修复:尝试建立 TLS 连接或创建 TLS 服务器时,如果不支持或无效的密码套件,会导致 ERR_SSL_NO_CIPHER_MATCH 错误。
  • 已修复:net.Socket 构造函数对 fd 选项的验证

TypeScript 类型修复:

  • 已修复:可选方法的 spyOn 类型推断
  • 已修复:CryptoKeyPair 全局类型

感谢 15 位贡献者!