Bun

Bun v0.5.2


Jarred Sumner · 2023 年 1 月 31 日

我們正在招聘 C/C++ 和 Zig 工程師,以打造 JavaScript 的未來!

Bun v0.5.2 在 bun install 中新增了對 github: 依賴項的支援,並修復了 bun install 中的幾個錯誤。Buffer 更快,並且通過了更多 Node.js 測試。bun run 現在支援執行 WebAssembly 二進制檔案,包括 WASI 支援。由於 JavaScriptCore 和 @Constellation 的工作,WebAssembly 效能得到了提升。剩餘的 node:dns resolve 函數已實作。Express 的 body-parser 套件在 Bun 中可以運作。MongoDB 在 Bun 中可以運作。

# Install Bun
curl https://bun.dev.org.tw/install | bash

# Upgrade to latest release of Bun
bun upgrade

您可以使用 npm 安裝 bun(感謝 @Electroid

npm install -g bun

GitHub 依賴項

bun install 現在支援使用 github: 前綴將 GitHub 儲存庫新增為依賴項。

package.json:

{
  "name": "zoddy",
  "dependencies": {
    "zod": "github:colinhacks/zod"
  }
}

index.ts:

import { z } from "zod";
console.log(z.string().parse("hello"));

輸出

❯ bun index.ts
{ success: true, error: undefined, value: 'hello' }

npmyarn 和其他套件管理器也支援此功能。

bun install 的更高穩定性

此版本包含一些重要的錯誤修復,使 bun install 更加穩定。感謝 @alexlamsl 修復了這些問題!

  • 由於 semver 比較程式碼中的錯誤,當安裝具有不尋常的 build/pre 標籤組合的套件時,Bun 有時會顯示「failed to resolve」錯誤。https://github.com/oven-sh/bun/pull/1854
  • bun link 在使用作用域套件時失敗,例如 @scope/name。https://github.com/oven-sh/bun/pull/1892
  • bun install <package> 未正確處理 file://link:// 前綴。https://github.com/oven-sh/bun/pull/1895
  • v0.5.1 中引入的 npm: 別名,存在一些未正確處理的邊緣情況。https://github.com/oven-sh/bun/pull/1927
  • 某些邊緣情況會在安裝套件時導致分段錯誤,現已修復。

MongoDB 支援

現在 MongoDB 可以在 Bun 中運作了,感謝 @cirospaciari。您現在可以使用 Bun 搭配 MongoDB 構建 Web 應用程式。

import { MongoClient } from "mongodb";

const client = new MongoClient("mongodb://127.0.0.1:27017");
await client.connect();
const db = client.db("test");
const collection = db.collection("test");
await collection.insertOne({ hello: "world" });
const result = await collection.findOne({ hello: "world" });

console.log(result);
// { _id: 60a7b5b0b9c3b8b5b8b5b9c3, hello: 'world' }

body-parser 支援

Express 的 body-parser 套件現在可以在 Bun 中運作。先前,由於 Bun 的 StringDecoder 實作中的錯誤,此套件會拋出錯誤。

import express from "express";
import bodyParser from "body-parser";

const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.get("/", (req, res) => {
  res.send("Hello World!");
});

app.post("/echo", (req, res) => {
  res.send(req.body);
});

app.listen(3000, () => {
  console.log("Example app listening at https://127.0.0.1:3000");
});

輸出

curl -X POST -d '{"hello":"world"}' -H "Content-Type: application/json" https://127.0.0.1:3000/echo
{"hello":"world"}

使用 bun run 執行 WebAssembly 檔案

bun run 現在支援執行 WebAssembly 二進制檔案,並且還包括 WASI 支援。

❯ bun run hello.wasm
"Hello, World!"

Bun 也支援 node:wasi 模組。Bun 的實作基於 wasi-js 的出色工作,作者是 @williamstein,它是 @wasmerio 團隊的 wasmer-js 的分支,而 wasmer-js 又是 node-wasi 的分支,作者是 @devsnek

更快且更相容的 Buffer

Buffer.from() 在使用類型化陣列時速度提升高達 2 倍。

之前

cpu: Apple M1 Max
runtime: bun 0.5.1 (arm64-darwin)

Buffer.from(ArrayBuffer(100))                   76.48 ns/iter  (68.83 ns … 375.76 ns)  73.46 ns 247.37 ns 262.24 ns
Buffer.from(Uint8Array(100))                    54.28 ns/iter  (49.57 ns … 192.52 ns)  52.21 ns 107.67 ns 118.02 ns
Buffer.from(Uint8Array(0))                      51.29 ns/iter  (48.58 ns … 127.98 ns)  50.54 ns  95.97 ns 109.26 ns

之後

cpu: Apple M1 Max
runtime: bun 0.5.2 (arm64-darwin)

Buffer.from(ArrayBuffer(100))                   33.91 ns/iter  (27.52 ns … 343.25 ns)  31.03 ns 203.33 ns 219.75 ns
Buffer.from(Uint8Array(100))                    31.67 ns/iter  (28.72 ns … 103.17 ns)  30.07 ns  78.78 ns  82.05 ns
Buffer.from(Uint8Array(0))                      27.88 ns/iter  (25.95 ns … 185.95 ns)  27.24 ns  67.63 ns  77.94 ns

現在也通過了 Buffer.write()Buffer.byteLength 的 Node.js 測試。我們新增了已棄用的 parentoffset 屬性,以及幾個錯誤修復。相容性尚未達到 100%,但已非常接近。

我們還修復了一個錯誤,Bun 會錯誤地報告 Bun v0.5.1 中發生的「無效編碼」。

更多 node:dns 支援

現在 node:dns 支援更多方法,感謝 @cirospaciari

import dns from "node:dns";

dns.resolveCname("example.com");
// ...
// dns.resolveMx();
// dns.resolveNs();
// dns.resolveTxt();
// dns.resolveSrv();
// dns.resolvePtr();
// dns.resolveSoa();

Node.js 相容性修復

  • Bun 支援 process.execArgvprocess.argv0,許多套件(包括 Vite)都使用它們。

  • 已新增一個較慢的 node:zlib Polyfill。我們將在未來新增更快的實作。

  • 已修復導致 EventEmitter 回呼中的 this 值為 undefined 的錯誤。

  • Node-API 的 napi_create_symbol 未正確處理符號描述,現已修復。https://github.com/oven-sh/bun/commit/aa456805ddc9fd44152d73888ecb8733b60f34b9

  • Node-API 的 napi_define_properties 實作不支援符號,現已修復。https://github.com/oven-sh/bun/commit/4570ff77807a334f7bcd23e4b69b758d365b82a0

  • node:httpaddress() 函數遺失,且 onListen 回呼未處理所有情況。https://github.com/oven-sh/bun/commit/befd97a891d7de50ae130cdf262b2bf6d5ac69bc

  • 也修復了 fs.stat() 處理非 ASCII 路徑時的一個錯誤。https://github.com/oven-sh/bun/issues/1887

錯誤修復

  • 先前,bun --hot 的檔案系統監視器在 Linux 上或當文字編輯器使用交換檔案或原子檔案儲存時,無法可靠地運作。此問題已修復。當熱重載模組拋出例外時,它也不會報告例外。此問題也已修復。https://github.com/oven-sh/bun/commit/421588d63119fb15cd4db06838bb7058d72cc727
  • Bun 的 WebSocket 客戶端有時會導致掛起。感謝 @dylan-conway 修復了此問題。https://github.com/oven-sh/bun/pull/1910
  • 當主體大小超過 65536 位元組時,Bun 的 WebSocket 客戶端無法運作。感謝 @cirospaciari 修復了此問題。https://github.com/oven-sh/bun/pull/1909
  • 查詢字串參數現在可以用於匯入說明符中,這將使該資料夾的模組解析快取失效。
  • 先前,使用 nginx 作為 Bun 的反向代理需要於 nginx 設定中指定 proxy_http_version 1.1。現在它也可以使用預設的 proxy_http_version 1.0 運作。感謝 @cirospaciari 調查並修復了此問題。
  • 已修復影響使用 query.get()query.run() 進行複雜查詢的 SQLite 客戶端錯誤。https://github.com/oven-sh/bun/issues/1366
  • 已修復建構函式屬性存取器的 TypeScript 轉譯器錯誤。https://github.com/oven-sh/bun/pull/1883
  • 已修復類別內宣告的 TypeScript 轉譯器錯誤。

變更日誌

#1867建構函式參數屬性由 @dylan-conway 在類別表達式中降低
#1854@alexlamsl 修復 semver ^ & ~ 表達式的解析
#1869@alexlamsl 進行的次要清理
#1870@cirospaciari 實作 resolveSrv
#1862@cirospaciari 合併來自請求參數的參數與 fetch 的第二個參數,將 verbose 和 proxy 選項移動到 cond 參數,為 fetch 新增非 TLS 測試
#1883@dylan-conway 修復建構函式陳述式順序
#1884@dylan-conway 修復子程序節點測試掛起
#1881@cirospaciari 修復 buffer.write 中的引數,修復 buffer.write 針對 utf16 傳回的大小,修復 base64 的大小計算,修復 hex te 大小的計算
#1874@Electroid 進行的 npm install bun
#1892@alexlamsl 支援作用域套件的 bun link
#1875@alexlamsl 支援 GitHub URL 作為依賴項
#1894@scally 新增 FileSystemRouter + React 範例
#1895@alexlamsl 正確解析 CLI 中的套件規範
#1909@cirospaciari 修復 WS 客戶端的大型套件接收
#1910@dylan-conway 修復 websocket 掛起
#1903@cirospaciari 實作 DNS 中的所有待處理 resolve 方法
#1914Express.js 嘗試使用函數作為主機名稱,由 @cirospaciari 實作
#1917@dylan-conway 確保使用 toSliceClone 分配名稱
#1911@alexlamsl 在完全解析後附加 GitHub 套件
#1923@u9g 修復 if 條件始終為 true 的問題
#1927@alexlamsl 修復具有別名依賴項的邊緣情況
#1924@alexlamsl 正規化 bun add 套件規範
#1926@alexlamsl 解析為 GitHub URL
#1929@Jarred-Sumner 支援使用 bun run 執行 WASI (WebAssembly) 檔案
#1930@alexlamsl 修復 bun add 中的更多邊緣情況
#1934@Ygnys 更新 README.md
#1937@alexlamsl 修復 bunx 中的版本解析
#1938@alexlamsl 報告無效的輸入檔案作為測試失敗
#1941@alexlamsl 修復 assert() 崩潰
#1943@cirospaciari 修復 utf16le 填充和 utf8 utf16 的部分寫入