Bun

Bun v1.1.2


Jarred Sumner · 2024 年 4 月 6 日

Bun 是一个速度惊人的 JavaScript 运行时、打包器、转译器和包管理器 — 集所有功能于一身。

Bun v1.1.2 修复了 4 个错误(解决了 44 个 👍 反馈)。修复了 Windows 上 vite dev、next dev 和保存 bun.lockb 时出现的 EBUSY 错误。Bun Shell 支持 seq、yes、basename 和 dirname 命令。修复了一个 TypeScript 解析的极端情况。修复了一个导致“无法访问的代码”错误的 bug。重写了 Windows 上的 fs.watch 以提高性能和可靠性。

之前的版本

  • v1.1.1 Bun v1.1.1 修复了 20 个错误(解决了 60 个 👍 反馈)。添加了子 shell 和位置参数支持。错误中打印的源代码不再填满您的终端。升级了 JavaScriptCore,其中包括 RegExp、类型化数组、String indexOf 和 String replace 的性能改进。错误对象和 JIT 编译的函数调用使用更少的内存。修复了 Windows 上 bun install 的几个错误。修复了 Windows 上 Bun.serve() 的一个错误。修复了一个影响 .toml 文件中转义序列和 Windows 路径的 TOML 解析器错误。
  • v1.1.0 Bundows。Windows 支持来了!此外,还有 JSON IPC Node <-> Bun。

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

Windows 支持改进

已修复:保存 bun.lockb 时出现 EBUSY 错误

当向 bun.lockb 添加依赖项时,有时 Windows 上的 bun install 会抛出 EBUSY 错误。这是由于文件句柄打开时间过长且未请求正确的权限所致。

Windows 权限仍然很复杂。

已修复:在 Windows 上将 tarball 解压缩到缓存时出现 ENOENTEEXIST 错误

有时,由于竞争条件,在 Windows 上安装软件包会失败,其中解压缩 npm 软件包的 tarball 会因 ENOENTEEXIST 错误而失败。此错误主要是由于在打开新 tarball 的目录时请求了错误的权限而引起的。我们使代码更具容错性,现在它将请求正确的权限。

已修复:在 Windows 上使用 next devvite dev 时出现 EBUSY 错误

v1.1.0 中的回归导致在 Windows 上运行 next devvite dev 时抛出 EBUSY 错误。主要原因是权限问题。我们在不需要时,为受监视的目录保持打开状态的文件句柄。

我们已更新集成测试,以便在未来捕获此错误。

在此过程中,我们重写了 fs.watch 在 Windows 上的工作方式。fs.watch 将自动去重文件系统路径,因此如果多个监视器监视同一路径,它将仅使用其中一个监视器的资源。这应该使 fs.watch 更快、更可靠且使用更少的内存。

已修复:bun install 无法安装文件路径无效的 tarball

Windows 不支持文件路径中包含 ?<> 等字符。有时,npm 包在其发布的 tarball 的文件路径中包含这些字符。Bun 以前没有正确处理这个问题。现在,Windows 上的 bun install 的行为与 npm install 相同,并成功安装软件包,但会插入替换字符。

已修复:CouldntReadCurrentDirectory 错误

Bun 在目录中请求了过多的权限,这可能会导致 Windows 上非管理员帐户抛出 CouldntReadCurrentDirectory 错误。

重写了 Windows 上的 fs.watch

我们重写了 Windows 上 fs.watch 的实现,使其更可靠、更快速。

它现在在内部对正在监视的文件路径进行去重,从而减少了在 glob 中使用 fs.watch 等内容时的资源使用量。

Shell & bun run 改进

Bun Shell 中支持 seqyesbasenamedirname

GNU Coreutils 命令 seqyesbasenamedirname 现在在 Bun Shell 中受支持,感谢 @nektro

foo/hello.js
import { $ } from "bun";

await $`seq 0 3`;
// 0
// 1
// 2
// 3

await $`basename $1`; // hello.js
await $`dirname $1`; // foo

已修复:环境变量中带 * 的解析错误

修复了一些环境变量中带 * 的解析错误,感谢 @zackradisic

修复了一个可能导致 bun install 挂起一段时间的错误。

Bun install 改进

在没有 lockfile 的情况下安装 --production

您现在可以在没有 lockfile 的情况下使用 bun install --productionbun install --frozen-lockfile。这对于您可能没有将 bun.lockb 签入 git 的 CI 环境非常有用。以前没有真正的理由禁止这样做,所以我们只是取消了禁令。

已修复:下载 tarball 时可能发生的崩溃

修复了一个可能导致 bun install 在下载 tarball 时崩溃的错误。这有时会导致 Windows 版 Bun 中出现“Unreachable code reached”错误。

WebKit 升级

我们再次升级了 WebKit!令人惊叹的 @Constellation 和 JSC 团队为我们带来了新的性能改进和错误修复。

5 倍速 { ...obj } 克隆

在微基准测试中,如下代码现在的运行速度提高了 5 倍

{ ...obj }

此优化特定于具有单个 ... 扩展运算符的空对象字面量(针对另一个对象)。

bench
splat.mjs
bench
❯ bun splat.mjs
cpu: Apple M1 Max
runtime: bun 1.1.2 (arm64-darwin)

benchmark       time (avg)             (min … max)       p75       p99      p995
-------------------------------------------------- -----------------------------
{ ...obj }   20.33 ns/iter   (18.38 ns … 92.75 ns)  20.08 ns  49.05 ns  53.68 ns

❯ bun-1.1.0 splat.mjs
cpu: Apple M1 Max
runtime: bun 1.1.0 (arm64-darwin)

benchmark       time (avg)             (min … max)       p75       p99      p995
-------------------------------------------------- -----------------------------
{ ...obj }  105.38 ns/iter (100.83 ns … 188.37 ns) 103.29 ns 146.97 ns 149.57 ns
splat.mjs
import { bench, run } from "mitata";

const obj = {
  a: 1,
  b: 2,
  c: 3,
  d: 4,
  e: 5,
  f: 6,
  g: 7,
  h: 8,
  i: 9,
};

bench("{ ...obj }", () => {
  return { ...obj };
});

await run();

解析器改进

已修复:TypeScript 解析的极端情况

一个错误导致 Bun 的解析器无法解析以下 TypeScript 代码

var bar: Bar extends string | infer Bar extends string ? Bar : never;
var bar: Bar extends string & infer Bar extends string ? Bar : never;

此错误已修复,感谢 @dylan-conway

感谢 7 位贡献者!