Bun

Bun v1.1.6


Jarred Sumner · 2024年4月28日

Bun 是一個速度極快的 JavaScript 執行環境、打包器、轉譯器和套件管理器 — 功能All-in-one。

Bun v1.1.6 登場!此版本修正了 10 個錯誤 (解決了 512 個 👍 反應)。我們實作了 UDP socket 支援和 node:dgram。DataDog 和 ClickHouseDB 現在可以運作了。我們修正了 v1.1.5 中 node:http 的回歸錯誤。此外,還有 Node.js 相容性改進和錯誤修正。

我們正在舊金山招募系統工程師,一同打造 JavaScript 的未來!

先前的版本

  • v1.1.5 修正了 64 個錯誤 (解決了 101 個 👍)。bun build --compile 跨平台編譯獨立 JavaScript 和 TypeScript 可執行檔至其他平台。透過 type: "text" import 屬性將任何檔案作為文字匯入。引入了新的 crash 報告器。package.json 不會因註解或尾隨逗號而發生錯誤。修正了 bun run --filter 以 0 退出的錯誤。修正了 bun install 中 file: 相依性的錯誤。修正了 node:fsnode:tlsnode:cryptonode:readlinenode:httpnode:worker_threads 中的錯誤
  • v1.1.4 修正了 40 個錯誤 (解決了 84 個 👍 反應)。bun run --filter <workspace> <script> 可讓您平行執行多個 workspace 腳本。在 bun install 中,重新安裝速度提升高達 50%。bun install 的重要可靠性修正。bun:sqlite 支援 using 進行資源清理,並有一些錯誤修正。影響 Next.js Standalone 和 Web Streams 的記憶體洩漏已修正。Node.js 相容性改進,包括 fschild_process。"fetch()" 中「連線已關閉」的修正。Bundows 的一些錯誤修正。
  • v1.1.0 Bundows。Windows 支援來了!

安裝 Bun

curl
npm
powershell
scoop
brew
docker
curl
curl -fsSL https://bun.dev.org.tw/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

UDP Socket

Bun 現在支援 UDP socket。UDP socket 是一種底層、不可靠的訊息傳輸協定,常用於生產監控工具、影音串流工具和遊戲。

import { udpSocket } from "bun";
const server = await udpSocket({
  socket: {
    data(socket, buf, port, addr) {
      console.log(`message from ${addr}:${port}:`);
      console.log(buf.toString());
    },
  },
});

const client = await udpSocket({});
client.send("Hello!", server.port, "127.0.0.1");

感謝 @gvilums 實作 Bun 中的 UDP socket 支援!

node:dgram 已實作

node:dgram 是 Node.js 中用於使用 UDP socket 的模組。基於 Bun 新的 UDP socket 支援,現在已實作 node:dgram。這是 Bun GitHub 儲存庫中最受歡迎的問題之一。

send-10-messages.js
const dgram = require("dgram");
let count = 0;
const { promise, resolve } = Promise.withResolvers();

const server = dgram.createSocket("udp4");
server.on("message", (msg, {port, address}) => {
  console.log(`server got: ${msg} from ${address}:${port}`);
  if (count++ === 9) process.exit(0);
});
server.bind(41234);

const client = dgram.createSocket("udp4");
for (let i = 0; i < 10; i++) {
  client.send("hello", 41234, "127.0.0.1");
}
await promise;

感謝 @gvilums 在 Bun 中實作 node:dgram

Bun 中的 DataDog

DataDog 的 dd-trace 模組現在可以在 Bun 中運作了。DataDog 是一個流行的基礎架構監控工具。

感謝 @gvilums 和 @paperclover 讓 DataDog 可以在 Bun 中運作!

Bun 中的 ClickHouse

官方的 Node.js ClickHouse 用戶端現在可以在 Bun 中運作了。

感謝 @nektro 讓 ClickHouse 可以在 Bun 中運作!

Bun 中的 SvelteKit

已修正搭配 SvelteKit 使用 fetch 時會發生的錯誤。

當使用 Request 子類別呼叫 fetch 時,Bun 會略過呼叫 methodurl 的 getter,而是使用內部的 methodurl 屬性。由於 SvelteKitRequest 子類別覆寫了這些屬性,這表示我們忽略了來自 Request 子類別的 method 屬性。

class FooRequest extends Request {
  get method() {
    return "POST";
  }
}

await fetch(new FooRequest("https://example.com"));
// This would be a GET request instead of a POST request!

此錯誤現已修正,並且我們的測試套件已更新以防止回歸。

Array#sort 效能提升 15% - 135%

此版本升級到最新版本的 JavaScriptCore,Array.prototype.sort 在 Bun 和 Safari 中現在效能提升了 15% - 135%。感謝 @Constellation。

❯ bun array-sort.mjs # New
cpu: Apple M3 Max
runtime: bun 1.1.6 (arm64-darwin)

benchmark                            time (avg)             (min … max)       p75       p99      p995
----------------------------------------------------------------------- -----------------------------
Array.sort (64 num, unsorted)    736.22 ns/iter  (673.6 ns … 975.75 ns) 747.02 ns 975.75 ns 975.75 ns
Array.sort (64 num, pre-sorted)  377.94 ns/iter  (370.2 ns … 395.56 ns) 380.33 ns  390.1 ns 395.56 ns

❯ bun-1.1.4 array-sort.mjs # Before
cpu: Apple M3 Max
runtime: bun 1.1.4 (arm64-darwin)

benchmark                            time (avg)             (min … max)       p75       p99      p995
----------------------------------------------------------------------- -----------------------------
Array.sort (64 num, unsorted)      1.14 µs/iter     (1.03 µs … 1.65 µs)   1.19 µs   1.65 µs   1.65 µs
Array.sort (64 num, pre-sorted)    1.07 µs/iter     (1.01 µs … 1.18 µs)   1.08 µs   1.18 µs   1.18 µs

❯ node array-sort.mjs # Node.js, for comparison
cpu: Apple M3 Max
runtime: node v22.0.0 (arm64-darwin)

benchmark                            time (avg)             (min … max)       p75       p99      p995
----------------------------------------------------------------------- -----------------------------
Array.sort (64 num, unsorted)      1.95 µs/iter      (1.8 µs … 2.12 µs)   2.04 µs   2.12 µs   2.12 µs
Array.sort (64 num, pre-sorted)  689.28 ns/iter  (677.4 ns … 719.66 ns) 694.22 ns 719.66 ns 719.66 ns

檢視微基準測試

node:module 中的 Module._resolveLookupPaths 已實作

私有的 Node.js API Module._resolveLookupPaths 現在已在 Bun 中實作。require-in-the-middle 使用此 API 攔截 require 呼叫,以檢測模組載入。

警告:當 dd-trace 使用此功能時,可能會降低應用程式的啟動時間。

node:http 回歸錯誤已修正

已修正 v1.1.5 中 node:http 的回歸錯誤。此回歸錯誤導致帶有錯誤的請求無法正確傳播到錯誤事件。此回歸錯誤影響了 stripe npm 套件以及其他套件。感謝 @nektro 修正此回歸錯誤!

已修正:node:http listen callback 的 this 值錯誤

已修正 listen callback 中 this 值不正確的錯誤。現在 this 值如預期般為 server 實例。

感謝 @nektro 修正此問題。

已修正:Windows 上的跨平台編譯解壓縮錯誤

已修正 bun build --compile 在 Windows 上會錯誤解壓縮 tarball 的錯誤,感謝 @dylan-conway。

已修正:在 Windows 上跨裝置升級 bun 時發生 error.Unexpected 錯誤

Windows NT 錯誤 NOT_SAME_DEVICE 未正確對應到 posix 錯誤 EXDEV,導致在 Windows 上跨不同磁碟機代號升級 Bun 時發生 error.Unexpected 錯誤。此問題已修正。

已修正:node:dns lookup 在 macOS 上未保持事件迴圈運作

先前,以下程式碼在 macOS 上不會記錄 hiii

const dns = require("dns");

dns.lookup("google.com", (err, address, family) => {
  console.log("hiii");
});

此問題已修正。

錯誤原因是我們在 macOS 上完成 DNS 查詢後未排空 microtask。這導致事件迴圈為空,並且程序在 callback 被呼叫之前就退出了。

已修正:TOML 和 JSON 匯入的錯誤

已修正當最初需要 jsontoml 匯入時,它們會通過 CommonJS -> ESM 轉換路徑的錯誤。

以下輸入

console.log(require("./hello.toml"));

🔴 Bun v1.1.5 會輸出 (錯誤)

Module {
  __esModule: true,
  default: {
    hello: "world"
  },
  hello: {
    world: "world"
  }
}

🟢 Bun v1.1.6 現在輸出 (正確)

{
  hello: "world",
}

這也應該稍微減少 TOML 匯入的記憶體使用量。先前,我們會為 TOML 以及原始碼產生 sourcemap,這是沒有必要的。現在我們直接建立物件/值,而無需發出 JavaScript 原始碼。

已修正:Bun.write() 中的記憶體洩漏

當對大型輸出使用 Bun.write() 時,輸入字串或位元組的記憶體未正確釋放。這導致隨著時間推移記憶體大量增長。此問題已修正,並且已新增記憶體洩漏測試,以防止未來再次發生回歸。

已修正:napi_get_date_value 中的 crash

已修正當傳遞意外類型時,napi_get_date_value 中的 crash。此 crash 影響了某些 Node Native Addons。

已修正:HTMLRewriter 與 Bun.file() 可能發生的 crash

當使用 HTMLRewriterBun.file() 轉換磁碟上的 HTML 檔案時,在某些情況下 Bun 可能會 crash 或永不 fulfill Promise。此問題已修正。

已修正:--define 的邊緣案例

已修正當 `--define` 用於全域屬性時會產生不正確輸出的錯誤,感謝 @dylan-conway。

感謝 9 位貢獻者!