Bun

Bun v1.0.3


Colin McDonnell · 2023 年 9 月 22 日

Bun v1.0.3 修復了許多錯誤,新增了對透過 TypeScript 的 emitDecoratorMetadata 進行元數據反射的支援、改進了 fetch 相容性、解鎖了如 Azure Artifacts 和 Artifactory 等私人註冊表、bunx 錯誤修復、console.log() 深度從 8 降至 2、Node.js 相容性改進,等等。

感謝您回報問題。我們正努力盡快修復它們。

Bun 是一個極速 JavaScript 執行時、打包器、轉譯器和套件管理器 — 一應俱全。我們最近發布了很多 Bun 的變更。以下是最近幾個版本的摘要。如果您錯過了

  • v1.0.0 - Bun 的第一個穩定版本!
  • v1.0.1 - .json 和 .toml 檔案的具名導入,bun install、node:path、Buffer 的錯誤修復
  • v1.0.2 - 使 --watch 更快,加上錯誤修復

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

emitDecoratorMetadata (Nest.js、TypeORM 等使用)

Bun 現在支援透過 TypeScript 的 emitDecoratorMetadata 進行元數據反射。這表示您可以在 Bun 中使用 Nest.js,並且可以開箱即用。

tsconfig.json
{
  "compilerOptions": {
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true
  }
}

emitDecoratorMetadata 是一個 TypeScript 編譯器選項,可透過 reflect-metadata 啟用裝飾器元數據的生成。

改進的 Nest.js 支援

emitDecoratorMetadata 的新增大幅改進了 Nest.js 支援,因為它依賴自訂的 "Reflectors" 將元數據和中介軟體附加到路由處理程序。

@Post()
@Roles(['admin']) // custom authentication decorator
async create(@Body() createCatDto: CreateCatDto) {
  this.catsService.create(createCatDto);
}

module.parent 支援

在 Node 中,CommonJS module.parent getter 是對要求目前模組的父模組的參考。這對於判斷模組是直接運行還是被另一個模組要求非常有用。

if (module.parent) {
  // this module was required by another module
} else {
  // this module was run directly
}

Bun 現在以與 Node 相同的方式支援 module.parent

對於 ES 模組,您可以使用 import.meta.main 來找出目前模組是否啟動了 Bun 進程。

私人 npm 註冊表的錯誤修復

fetch 的改進解鎖了 bun install 與如 Azure Artifacts 和 JFrog Artifactory 等常見私人套件註冊表的使用。

您可以使用 bunfig.toml 而不是 .npmrc 來設定私人註冊表。在您的 bunfig.toml 檔案的 [install.registry] 區段下提供 urlusernamepassword 的值。

bunfig.toml
[install.registry]
url = "https://pkgs.dev.azure.com/my-azure-artifacts-user/_packaging/my-azure-artifacts-user/npm/registry"
username = "my-azure-artifacts-user"
# Bun v1.0.3+ supports using an environment variable here
password = "$NPM_PASSWORD"

或者,指定一個 token 而不是使用者名稱和密碼。這對於 Artifactory 非常有用。

bunfig.toml
[install.registry]
url = "https://my-artifactory-server.com/artifactory/api/npm/npm-virtual"
token = "$NPM_TOKEN"

如需更完整的資訊,請參考我們的新指南

[0.5ms] env 訊息現在預設為靜音

很多人要求這樣做,所以 @colinhacks 實現了它。[0.5ms] env loaded 訊息現在預設為靜音。您仍然可以透過在 bunfig.toml 中將 logLevel 設定為 "debug" 來看到它

Node.js 相容性改進

Node.js 相容性的工作仍在繼續。以下是此版本中的一些改進

實作 console.Console 建構函式

Node.js console.Console 建構函式已實作。

import { Console } from "console";
import { createWriteStream } from "fs";

const writer = new Console({ stdout: createWriteStream("log.txt") });

writer.log("hello");
writer.log("world", { x: 2 });

這修復了使用 Bun 執行時執行的 vitest 和 webpack 的 ts-loader——儘管請考慮改用 bun testbun build!當然,有很多工具和程式碼庫依賴這些技術,它們現在將與 Bun 搭配運作。

空的環境變數現在顯示為空字串 ("")

process.envBun.env 中,空的環境變數顯示為 undefined 而不是空字串。這已修復,感謝 @liz3

dns.lookupService 現在已實作

node:dns 函式 dns.lookupService 現在已實作。感謝 @Hanaasagi

console.log() 深度現在為 2 而不是 8

8 很好,但會導致大型物件填滿您的終端螢幕的問題。感謝 @JibranKalia。這使行為與 Node.js 一致。

node_api_create_external_string_latin1 和 node_api_create_external_string_utf16

node_api 函式 node_api_create_external_string_latin1node_api_create_external_string_utf16 現在已實作

node:http 回呼中的 listen() 錯誤修復

錯誤事件未正確發送到事件監聽器。感謝 @SuperAuguste 修復此問題。

錯誤修復

request.json() 中的崩潰已修復

呼叫 request.json() 時可能發生的崩潰已修復。它是由原生程式碼中不正確的例外處理引起的。

bun pm rm cache 現在可以運作了

bun pm rm cache 命令現在會刪除快取目錄。感謝 @WingLim

最大重新導向 URL 長度現在為 8192 而不是 4096

這修復了某些私人 npm 註冊表的問題。

bunx 從 $PATH 而不是 @version 標籤選擇版本的錯誤修復

導致 bunx$PATH 而不是 @version 標籤選擇可執行檔版本的錯誤已修復

完整變更日誌

#4652VSCode 擴充功能 Bunlock 語法突顯和文件改進,由 @JeremyFunk 貢獻
#5451docs(runtime): 修復一些錯字。由 @zongzi531 貢獻
#5531更新 development.md,由 @sonyarianto 貢獻
#5525fix(corking) 在需要時解除 corking,由 @cirospaciari 貢獻
#5503fix(request) 處理請求中未定義/null/空的 signal,由 @cirospaciari 貢獻
#5521fix(bundler): 在縮小的 require 前面新增空格,由 @davidmhewitt 貢獻
#5505fix(node/fs.watch): 在修剪事件檔名前檢查第一個字元,由 @davidmhewitt 貢獻
#5535webkit 升級,由 @dylan-conway 貢獻
#5555工作區文件 #5379 和 #5229 的後續,由 @bdenham 貢獻
#5579fix: ArrayBufferConstructor 類型簽名,由 @52 貢獻
#5580fix: array-buffer.test-d.ts 測試,由 @52 貢獻
#4693fix: node 相容性,支援空路徑字串 (stat, exist, ...),由 @coratgerl 貢獻
#5603使在瀏覽器套件中導入 node: 模組時的錯誤訊息更清晰,由 @Jarred-Sumner 貢獻
#5576docs: 修復 lockflie nav 中的錯字,由 @Vilsol 貢獻
#5593Docs: 路徑別名修復,由 @e253 貢獻
#5496fix(fetch) 處理 100 continue,由 @cirospaciari 貢獻
#5387feat(encoding): TextDecoder 支援 undefined,由 @WingLim 貢獻
#5481fix(child_process) unref next tick,以便在應用程式退出之前觸發 exit/close 事件,由 @cirospaciari 貢獻
#5529為 bun 實作 VSCode tasks,由 @JeremyFunk 貢獻
#5610fix(install): 當 errno == XDEV 時返回 NotSupported,由 @pan93412 貢獻
#5510修復文件中的 ZLS commit hash,由 @shinichy 貢獻
#5628將 .DS_Store 新增到 gitignore-for-init,由 @Cilooth 貢獻
#5615Workaround #5604,由 @Jarred-Sumner 貢獻
#5626修復文件中的 TypeError,由 @LapsTimeOFF 貢獻
#5656新增一種停用 GC timer 的方法,由 @Jarred-Sumner 貢獻
#5660移除 Makefile 中對 zig 的硬編碼引用,由 @xbjfk 貢獻
#5595feat(console.log): 當類別名稱未知時列印 anonymous,由 @JibranKalia 貢獻
#5572feat(test): 實作 arrayContaining,由 @WingLim 貢獻
#5655bun:sqlite 中,確保在建立 JSValue 時正確設定數字標籤,由 @Jarred-Sumner 貢獻
#5662fix(config): 支援沒有尾部斜線的註冊表 url,由 @Hanaasagi 貢獻
#5685fix(docs): 更新格式,由 @rauny-brandao 貢獻
#5638docs: 從 bun init 新增遺失的選項,由 @jumoog 貢獻
#5594為色盲人士更改圓圈,由 @Hamcker 貢獻
#5689修復 HTTP 監聽行為不符合 node 的問題,由 @paperclover 貢獻
#5448feat(runtime): 實作 console.Console,由 @paperclover 貢獻
#5675實作 node_api_create_external_string_latin1,由 @Jarred-Sumner 貢獻
#5699fix(runtime/node): 允許 new Buffer.alloc() + 升級 WebKit,由 @paperclover 貢獻
#5684fix: 移除 toJSONWithBytes 中不需要的分支,由 @liz3 貢獻
#5696在 makefile 中將 llvm 版本從 15 更新到 16,由 @nithinkjoy-tech 貢獻
#5679fix: 為長度為 0 的進程環境變數提供空字串,由 @liz3 貢獻
#5025bun run 修復了空檔案時遺失腳本錯誤的問題,由 @Parzival-3141 貢獻
#5444新增 navigator 類型定義,由 @ruihe774 貢獻
#5716在註冊表 manifest 請求中編碼套件名稱中的斜線,由 @Jarred-Sumner 貢獻
#5726使 bun install --verbose 更詳細,由 @Jarred-Sumner 貢獻
#5730修復 #3712,由 @Jarred-Sumner 貢獻
#5729使 fetch() 重新導向行為與規範一致,由 @Jarred-Sumner 貢獻
#5744使 artifactory 能夠運作,由 @Jarred-Sumner 貢獻
#5702docs: 更新 Remix 指南,由 @brookslybrand 貢獻
#5620將 react 安裝新增到 react.md,由 @jt3k 貢獻
#5597提醒使用者最新版本,由 @jumoog 貢獻
#5562docs: 更新 net node 文件,由 @weyert 貢獻
#5513docs(development): 錯字可能導致錯誤的 llvm 安裝,由 @sum117 貢獻
#5759文件更新,由 @colinhacks 貢獻
#4571fix(cli): bun pm cache rm 命令無法運作,由 @WingLim 貢獻
#5762fix(Makefile),由 @cirospaciari 貢獻
#5775修復 #5769,由 @Jarred-Sumner 貢獻
#5447新增警告以確保 '--watch' 標誌的正確放置,由 @a4addel 貢獻
#5456更新 modules.md 以解決問題 #5420,由 @h2210316651 貢獻
#4810docs: 新增 Qwik 指南,由 @sanyamkamat 貢獻

新貢獻者

感謝 Bun 的最新貢獻者!

  • @52 在 https://github.com/oven-sh/bun/pull/5579 中做出了他們的首次貢獻
  • @a4addel 在 https://github.com/oven-sh/bun/pull/5447 中做出了他們的首次貢獻
  • @AaronDewes 在 https://github.com/oven-sh/bun/pull/4779 中做出了他們的首次貢獻
  • @bdenham 在 https://github.com/oven-sh/bun/pull/5555 中做出了他們的首次貢獻
  • @brookslybrand 在 https://github.com/oven-sh/bun/pull/5702 中做出了他們的首次貢獻
  • @Brooooooklyn 在 https://github.com/oven-sh/bun/pull/5788 中做出了他們的首次貢獻
  • @Cilooth 在 https://github.com/oven-sh/bun/pull/5628 中做出了他們的首次貢獻
  • @coratgerl 在 https://github.com/oven-sh/bun/pull/4693 中做出了他們的首次貢獻
  • @ggobbe 在 https://github.com/oven-sh/bun/pull/5786 中做出了他們的首次貢獻
  • @h2210316651 在 https://github.com/oven-sh/bun/pull/5456 中做出了他們的首次貢獻
  • @Hamcker 在 https://github.com/oven-sh/bun/pull/5594 中做出了他們的首次貢獻
  • @igorshapiro 在 https://github.com/oven-sh/bun/pull/5873 中做出了他們的首次貢獻
  • @ImBIOS 在 https://github.com/oven-sh/bun/pull/5885 中做出了他們的首次貢獻
  • @JeremyFunk 在 https://github.com/oven-sh/bun/pull/4652 中做出了他們的首次貢獻
  • @JibranKalia 在 https://github.com/oven-sh/bun/pull/5595 中做出了他們的首次貢獻
  • @jonahsnider 在 https://github.com/oven-sh/bun/pull/5104 中做出了他們的首次貢獻
  • @jt3k 在 https://github.com/oven-sh/bun/pull/5620 中做出了他們的首次貢獻
  • @jumoog 在 https://github.com/oven-sh/bun/pull/5638 中做出了他們的首次貢獻
  • @liz3 在 https://github.com/oven-sh/bun/pull/5684 中做出了他們的首次貢獻
  • @nithinkjoy-tech 在 https://github.com/oven-sh/bun/pull/5696 中做出了他們的首次貢獻
  • @pan93412 在 https://github.com/oven-sh/bun/pull/5610 中做出了他們的首次貢獻
  • @rauny-brandao 在 https://github.com/oven-sh/bun/pull/5685 中做出了他們的首次貢獻
  • @ruihe774 在 https://github.com/oven-sh/bun/pull/5444 中做出了他們的首次貢獻
  • @sanyamkamat 在 https://github.com/oven-sh/bun/pull/4810 中做出了他們的首次貢獻
  • @shinichy 在 https://github.com/oven-sh/bun/pull/5510 中做出了他們的首次貢獻
  • @sum117 在 https://github.com/oven-sh/bun/pull/5513 中做出了他們的首次貢獻
  • @Vilsol 在 https://github.com/oven-sh/bun/pull/5576 中做出了他們的首次貢獻
  • @weyert 在 https://github.com/oven-sh/bun/pull/5562 中做出了他們的首次貢獻
  • @xbjfk 在 https://github.com/oven-sh/bun/pull/5660 中做出了他們的首次貢獻
  • @yadav-saurabh 在 https://github.com/oven-sh/bun/pull/5270 中做出了他們的首次貢獻