Bun

Bun v1.0.4


Jarred Sumner · 2023年10月3日

Bun v1.0.4 修復了 62 個錯誤,新增了 server.requestIP,支援執行階段外掛程式中的虛擬模組,並減少了 Bun.serve() 中的記憶體消耗。感謝您回報問題。我們正努力盡快修復這些問題。

Bun 是一個速度極快的 JavaScript 執行階段、打包器、轉譯器和套件管理器 — 功能All-in-One。我們最近發布了許多 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 支援、私有registry修復,以及許多錯誤修復

安裝 Bun

curl
npm
brew
docker
curl
curl -fsSL https://bun.dev.org.tw/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() 第二個參數物件的支援

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" 欄位儲存在鎖定檔中

先前,bun install 會在下次檢索時從其他現有資料推斷 package.json 版本,但事實證明這效果不佳,因為您並非總是需要工作區成員套件的 package.json 版本(它們可能沒有版本)。鎖定檔現在儲存工作區成員套件的 package.json 版本,這有助於解決一個問題,即當依賴項在 package.json 中使用 npm 版本,但引用本機工作區版本時,會導致 bun install 失敗。

這是對現有鎖定檔的就地增量更新,除了下次鎖定檔具有設定 version 的工作區成員套件時,git 狀態會顯示 dirty 之外,不應有其他影響。從技術上講,這不是一個重大變更,因為這之前就無法正常運作,但感覺值得注意。

bun install 錯誤修復

已對 bun install 進行了幾項穩定性改進,包括工作區。

  • 修復在工作區成員套件內執行 bun add <package> 的問題 #6092
  • 修復 bun install 在初始安裝後「忘記」工作區存在的問題
  • 修復了分支名稱包含斜線時影響 Git/GitHub 依賴項的錯誤 #5941
  • 修復 bun install 中偶爾發生的掛起 #6192
  • 支援本機 .tgz 依賴項 #5812
  • 修復了儲存鎖定檔時可能發生的決定性錯誤,導致 git status 在實際沒有任何變更時報告 dirty 工作樹。

重大錯誤修復

修復在某些情況下,狀態碼為 3xx 時導致 fetch 超時的錯誤

存在一個錯誤,在某些情況下,當包含 3xx 狀態碼和空body,且未包含某些標頭時,fetch 會超時。請求將完成,但最終會超時而不是報告成功。這也會在某些情況下影響 bun install

修復沒有 super()Error 子類的 captureStackTrace

#6063 修復了在擴展類別中,在沒有 super() 的建構函式內使用 captureStackTrace 時發生的崩潰問題。

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