Bun

Bun v0.6.4


Ashcon Partovi · 2023 年 5 月 26 日

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

上週,我們在 Bun v0.6.0 中推出了新的 JavaScript 捆綁器

今天,我們發布了對 bun test 的改進、Node.js 相容性錯誤修復、console.log() 改進、可修改的時區、require.cache 支援、捆綁器錯誤修復以及更多內容。

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 test 速度提升高達 80%

我們已將 bun test 的速度提升高達 80%,尤其是在有大量小型測試檔案時。

Bun 過度調度垃圾收集器以同步方式在每個檔案上運行。它已經在堆大小更改時調度 GC,因此這是沒有必要的。這導致記憶體使用量增長了約 9%,考慮到與其他使用相同程式碼的測試運行器相比,Bun 使用的記憶體少了 2-4 倍,這還是可以接受的。

bun test 讀取 .env.test.env.test.local

  • NODE_ENV=test 現在已正確設定。
  • 環境變數可以從 .env.test.env.test.local 檔案中讀取。

bun test 超時

您現在可以透過傳遞 --timeout 或將第三個可選的 number 參數傳遞給 test() 來設定測試的超時時間。

import { test } from "bun:test";

test("i took too long", async () => {
  await Bun.sleep(100);

  // the last argument is a timeout in milliseconds
}, 50);

expect().toHaveLength() 和 expect().toBeEmpty()

現在支援 expect().toHaveLength() 和 expect().toBeEmpty()。這些測試匹配器可用於 Blob、Buffer、File、Headers、Map、Set、String、TypedArray 和類陣列物件。

import { expect, test } from "bun:test";

test("expect().toHaveLength()", () => {
  expect([1, 2, 3]).toHaveLength(3);
  expect([1, 2, 3]).not.toHaveLength(2);

  // Works on Blob, Buffer, File, Headers, Map, Set, String, TypedArray, and Array-like objects, or anything with a .length property
  expect(new Headers({ Origin: "https://bun.dev.org.tw" })).toHaveLength(1);
});

test("expect().toBeEmpty()", () => {
  expect([]).toBeEmpty();
  expect([1]).not.toBeEmpty();

  // Works on Bun.file() too
  expect(Bun.file(import.meta.path)).not.toBeEmpty();
});

在 Twitter 上查看

變更時區

您現在可以透過設定 TZ 環境變數或在程式碼中指定 process.env.TZ 來變更 Bun 用於 DateIntl時區。Node.js 也使用此方法。

process.env.TZ = "America/New_York";

使用 bun test 時,預設時區設定為 Etc/UTC,而不是使用您機器的時區,以協助防止在 CI 環境中執行時測試不穩定。

在 Twitter 上查看

已實作 require.cache

require.cache 是一個 Node.js API,可讓您從快取中刪除模組。這對於控制熱重載行為很有用,現在 Bun 中也支援了。與 Node.js 不同,delete require.cache[id] 也適用於 ESM。

console.log(headers) 顯示標頭

console.log(searchParams) 顯示參數

捆綁器錯誤修復

感謝 @dylan-conway 修復了

  • Bun 在使用「classic」JSX 運行時自動導入 JSX,這是錯誤的。
  • Bun 在使用「automatic」JSX 運行時未從 jsxImportSource 導入 Fragment
  • 在某些情況下,當使用 jsxImportSourceNODE_ENV=development(或未設定)時,Bun 錯誤地選擇了「production」JSX 運行時。

轉譯器錯誤修復

  • 修復了在 Bun 運行時中運行時,列印包含非 ASCII 字元的標籤範本字串時的回歸問題。

Node.js 相容性錯誤修復

  • 現在,當使用 node:https 且未設定 port 時,node:https 會使用正確的連接埠。感謝 @cirospaciari 的修復。
  • 現在,node:httpgetHeader() 具有正確的返回類型。感謝 @Jarred-Sumner 的修復。
  • 現在,當使用 createServer() 時,node:https 會正確使用 { tls: true }。感謝 @cirospaciari 的修復。

bun install 錯誤修復

  • 修正了一個可能導致 bun 無法將二進位檔符號連結到 node_modules/.bin 中的錯誤,感謝 @alexlamsl

以 TypeScript 重寫 JavaScript 內建函式

Bun 的內部 JavaScript 內建函式現在以 TypeScript 實作,並使用 bun build 進行捆綁 + 最小化,這將 Bun 的二進位檔大小減少了約 900 KB,並使我們更容易捕獲錯誤。這要感謝 @paperclover

它還提高了 Bun 在這些 JavaScript 內建函式上的開發速度,因為先前 WebKit 使用的內建函式產生器腳本需要約 9 秒才能運行,而這個新的內建函式產生器腳本大約需要 0.9 秒。

變更日誌

#3008修正了使用 node:https 時 HTTPS 的連接埠,由 @cirospaciari 貢獻
#3007修正了 node:httpgetHeader() 的返回類型,由 @Jarred-Sumner 貢獻
#3012修正了使用 createServer() 時的 { tls: true },由 @cirospaciari 貢獻
#3018實作了 process.env.TZ 並將 bun test 的預設時區變更為 Etc/UTC,由 @Jarred-Sumner 貢獻
#3015修正了 test.todo() 和非同步函式的錯誤,由 @Jarred-Sumner 貢獻
f71eb39降低了 bun test 期間垃圾收集的積極性,由 @Jarred-Sumner 貢獻
#3032修正了 module.exports 的金鑰中帶有空格的錯誤,由 @dylan-conway 貢獻
#3040實作了 bun test --timeout N,可以變更預設的每個測試超時時間,由 @Electroid 貢獻
#3045實作了 require.cache,由 @Jarred-Sumner 貢獻
#3041修正了標籤範本字串中表情符號的錯誤,由 @Jarred-Sumner 貢獻
#3057修正了 jsxImportSourcejsxFactoryjsxFragmentFactory 的各種錯誤,由 @dylan-conway 貢獻
#3051修正了使用 server.fetch(Request) 時的崩潰問題,由 @cirospaciari 貢獻
#3037實作了載入 .env.test.env.{test,production,development}.local 的支援,由 @Jarred-Sumner 貢獻