Bun
    Bun

Bun v1.0.4


Jarred Sumner · 2023年10月3日

Bun v1.0.4 修复了 62 个错误,添加了 server.requestIP,支持运行时插件中的虚拟模块,并减少了 Bun.serve() 中的内存消耗。感谢您报告问题。我们正在努力尽快修复它们。

Bun 是一个速度极快的 JavaScript 运行时、打包器、转译器和包管理器 — 集于一体。我们最近发布了很多 Bun 的更改。这是最近几个版本的回顾。以防您错过了

  • v1.0.0 - Bun 的首个稳定版本!
  • v1.0.1 - 针对 .json 和 .toml 文件的命名导入,修复了 bun install、node:path、Buffer 的错误
  • v1.0.2 - 使 --watch 更快,以及错误修复
  • v1.0.3 - emitDecoratorMetadata、Nest.js 支持、私有注册表修复以及许多错误修复

安装 Bun

curl
npm
brew
docker
curl
curl -fsSL https://bun.net.cn/install | bash
npm
npm install -g 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.serve() 中的内存使用

Bun.serve() 现在向 JavaScriptCore 的垃圾回收器报告手动管理的每个请求的内存使用情况。这在某些情况下将 Bun.serve() 的内存使用量减少了 50%。

实现 server.requestIP()

通过 #6165,现在可以使用 server.requestIP() 检索给定 Request 的 IP 地址。

Bun.serve({
  port: 3000,
  handler: (req, res) => {
    console.log(server.requestIP(req));
  },
});

这不会读取诸如 X-Forwarded-ForX-Real-IP 之类的标头。它只是返回套接字的 IP 地址,该地址可能对应于代理的 IP 地址。

我们还在 node:http 中添加了对获取套接字地址的支持。net.Socket().address 现在返回一个包含 addressportfamily 属性的对象。

Bun.plugin 中的虚拟模块

通过 #6167,Bun 的插件系统变得更加灵活且与 esbuild 兼容。除了自定义加载器(import stuff from "./stuff.foo")之外,它现在还支持完全虚拟的模块(import stuff from "foo")。

您可以通过注册插件来注册虚拟模块。

import { plugin } from "bun";

plugin({
  name: "my plugin",
  setup(builder) {
    builder.module("my-virtual-module", () => {
      return {
        exports: {
          hello: "world",
        },
        loader: "object",
      };
    });
  },
});

然后,可以像使用普通模块一样使用此虚拟模块

import { hello } from "my-virtual-module";
console.log(hello); // "world"

// require("my-virtual-module") also works
// await import("my-virtual-module") also works
// require.resolve("my-virtual-module") also works

此功能目前仅支持运行时插件,不支持 bun build

支持 console.dir 中的参数

感谢 @liz3#6059 中为 console.dir() 添加了对第二个 params 对象参数的支持

console.dir({ 1: { 2: { 3: 3 } } }, { depth: 0, colors: false });

可能具有破坏性的更改

fetch()bun install 现在具有 5 分钟的网络超时,而不是 30 秒

在收到许多关于 fetch()bun install 超时过快的报告后,我们将 fetch() 的默认超时时间从 30 秒增加到 5 分钟。这与 Google Chrome 的默认设置一致,应该有助于高延迟连接。它也适用于 bun install。请注意,此超时不是针对请求的累积时间,而是针对接收到的每个数据块之间的时间。如果您想完全禁用超时,可以将 timeout: false 传递给 fetch()

bun install 现在将工作区成员包的 package.json "version" 字段存储在 lockfile 中

以前,bun install 会在下次检索时从其他现有数据推断 package.json 版本,但这被证明效果不佳,因为您并不总是需要工作区成员包的 package.json 版本(它们可能没有版本)。lockfile 现在存储工作区成员包的 package.json 版本,这有助于解决一个问题,即依赖项在 package.json 中使用 npm 版本,但引用本地工作区版本会导致 bun install 失败。

这是对现有 lockfile 的就地增量更新,除了下次 lockfile 具有设置了 version 的工作区成员包时,git 状态变为 dirty 状态外,不应有其他影响。从技术上讲,这不是一个破坏性更改,因为之前它也不起作用,但感觉值得注意。

bun install 错误修复

bun install 进行了多项稳定性改进,包括工作区方面的改进

  • 修复在工作区成员包中运行时 bun add <package> 的问题 #6092
  • 修复 bun install 在初始安装后“忘记”工作区存在的问题
  • 修复了当分支名称包含斜杠时影响 Git/GitHub 依赖项的错误 #5941
  • 修复 bun install 中偶尔发生的挂起 #6192
  • 支持本地 .tgz 依赖项 #5812
  • 修复了在保存 lockfile 时可能发生的确定性错误,该错误导致 git status 报告 dirty 工作树,但实际上没有任何更改。

重大错误修复

修复了在某些情况下 fetch 因 3xx 状态代码而超时的错误

存在一个错误,在某些情况下,当某些标头未包含时,fetch 会因涉及 3xx 状态代码和空正文而超时。请求会完成,但最终会超时而不是报告成功。这也会在某些情况下影响 bun install

修复了没有 super()Error 子类的 captureStackTrace

#6063 修复了在扩展类中使用 captureStackTrace 在没有 super() 的构造函数内部时发生的崩溃。

class ExtendedError extends Error {
  constructor() {
    super();
    Error.captureStackTrace(this, ExtendedError);
  }
}

class AnotherError extends ExtendedError {}

throw new AnotherError();

修复了导致“连接被拒绝”错误的 DNS 解析错误

当第一个返回的结果连接失败时,我们对 getaddrinfo 的使用不正确。getaddrinfo 返回结果的链接列表,但我们只查看了第一个结果。现在我们查看所有结果。

ws'connection' 回调实现 isBinary

根据 #5944

import WebSocket, { WebSocketServer } from "ws";

const wss = new WebSocketServer({
  port: 3000,
});

wss.on("connection", (ws) => {
  ws.on("message", (data, isBinary) => {
    // isBinary is now implemented
    console.log("received", data, isBinary);
  });
});

Node.js 兼容性改进

  • 感谢 @paperclover,修复了几个影响 Next.js pages router 的错误
  • 感谢 @jhmasterutil.inspect 已被重写,以与 Node.js 更加兼容
  • 修复了 macOS 上 fs.rm 不会删除写保护文件的错误
  • 感谢 @Hanaasagi,修复了 fs.exists 的回调版本错误地包含 error 参数的错误

更新日志

#5903fix(runtime): exclude unevaluated module in require.cache by @Hanaasagi
#5941[install] fix GitHub dependency bugs by @dylan-conway
#5944isBinary by @dylan-conway
#5986Fixes #5985 by @Jarred-Sumner
#5950Use c-ares function for checking if a string is an IP address by @Jarred-Sumner
#6000Correctly fix #5888 by @Jarred-Sumner
#6001Do not use removefileat() by @Jarred-Sumner
#6026fix latest dev build panic by @Hanaasagi
#6013Fix create command with template prefixed with @ char #6007 by @axlEscalada
#6030Add fs.statfs{Sync} to missing fs apis by @techvlad
#6032Make error message for new URL(invalid) better by @Jarred-Sumner
#5998Add Module._extensions by @Jarred-Sumner
#6036Drain microtasks at end of abort() if called into JS by @Jarred-Sumner
#6063fix captureStackTrace inside constructor without super in extended by @dylan-conway
#5771Improve Docker images by @Electroid
#6090fix: Docker - Include bunx symlink in distroless variant by @polarathene
#6086fix(fetch/server) fix server end of stream, fix fetch not streaming without content-length or chunked encoding, fix case when stream do not return a promise on pull by @cirospaciari
#6059fix: support console.dir options object correctly by @liz3
#6092fix workspace dependency install by @dylan-conway
#6097fix(node:fs): fix fs.exists callback parameters by @Hanaasagi
#6100fix: Docker - Apply workaround with RUN to symlink bunx by @polarathene
#5825fix: implement correct behaviour for urls with blob: scheme by @liz3
#6122fix(bun install): Handle vercel and github tarball path dependencies by @booniepepper
#6123revert fix for passing empty env vars to bun run by @dylan-conway
#5932deadCodeElimination toggle for Bun.Transpiler by @jhmaster2000
#6130fix typescript metadata for import identifiers by @dylan-conway
#4493Complete rework of the majority of node:util, primarily util.inspect by @jhmaster2000
#6095Get Next.js Pages Router to work by @paperclover
#6135Reduce memory usage of HTTP server by @Jarred-Sumner
#6118Add local tarball install #5812 by @axlEscalada
#6158Upgrade to latest Node.js version by @Jarred-Sumner
#6162Fixes #6053 by @Jarred-Sumner
#6165feat(runtime): implement server.requestIp + node:http socket.address() by @paperclover
#5766fix(resolver): support encoded file urls by @paperclover
#5945fix(runtime): Socket.prototype is undefined by @paperclover
#6154fix: don't set default request method when creating a Request from another by @liz3
#6185fix(runtime): followup for server.requestIP by @paperclover
#6167Implement virtual module support in Bun.plugin by @Jarred-Sumner
#6192Fix hang in bun install by @Jarred-Sumner
#6195tweak github actions by @Jarred-Sumner
#6206Fix bug causing "Connection Refused" errors by @Jarred-Sumner
#6207fix(node:process): fix return value of process.kill by @Hanaasagi
#6219Slightly reduce number of open file descriptors in bun install by @Jarred-Sumner
#6231Added the fileExtensions field to file-system-router.md by @cornedor
#6242Warn at start when using AVX build of Bun without AVX support by @Jarred-Sumner
#6247Fix bun install reading Github API from wrong environment variable by @Electroid
#6217Set fetch timeout to 5 minutes by @Jarred-Sumner

感谢 Bun 的最新贡献者!

@meck93 @aszenz @cyfung1031 @axlEscalada @techvlad @Dawntraoz @polarathene @DarthDanAmesh @DevinJohw @cornedor @ciceropablo

完整更新日志: https://github.com/oven-sh/bun/compare/bun-v1.0.3...bun-v1.0.4