Bun

Bun v0.7.3


Jarred Sumner · 2023 年 8 月 6 日

Bun v0.7.3 新增了程式碼覆蓋率支援,以及依 RegExp 模式篩選測試。已修正 async Node.js fs 函數、bun:sqliteBuffer.prototype.copy 和巨集中的錯誤。

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

Bun 是一個速度驚人的 JavaScript 執行時、打包器、轉譯器和套件管理器,全部集於一身。在過去幾個月中,我們最近發布了許多 Bun 的變更,以下是回顧,以防您錯過

  • v0.6.13 - 實作了模擬 Date、更快的 base64 編碼,以及 WebSocketnode:tls 的修正。
  • v0.6.14 - process.memoryUsage()process.cpuUsage()process.on('beforeExit', cb)process.on('exit', cb) 和當機修正
  • v0.7.0 - Web Workers、--smol、structuredClone()、WebSocket 可靠性改進、node:tls 修正等等。
  • v0.7.1 - ES Modules 載入速度加快 30% - 250%、fs.watch 修正,以及許多 node:fs 相容性改進。
  • v0.7.2 - node:worker_threadsnode:diagnostics_channelBroadcastChannel、Node.js 相容性改進、數個記憶體洩漏的修正

安裝 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 test 中的程式碼覆蓋率報告

bun test 現在內建支援程式碼覆蓋率報告。若要啟用,請傳遞 --coverage 旗標

bun test --coverage

這將在終端機中產生文字報告。

-------------|---------|---------|-------------------
File         | % Funcs | % Lines | Uncovered Line #s
-------------|---------|---------|-------------------
All files    |   38.89 |   42.11 |
 index-0.ts  |   33.33 |   36.84 | 10-15,19-24
 index-1.ts  |   33.33 |   36.84 | 10-15,19-24
 index-10.ts |   33.33 |   36.84 | 10-15,19-24
 index-2.ts  |   33.33 |   36.84 | 10-15,19-24
 index-3.ts  |   33.33 |   36.84 | 10-15,19-24
 index-4.ts  |   33.33 |   36.84 | 10-15,19-24
 index-5.ts  |   33.33 |   36.84 | 10-15,19-24
 index-6.ts  |   33.33 |   36.84 | 10-15,19-24
 index-7.ts  |   33.33 |   36.84 | 10-15,19-24
 index-8.ts  |   33.33 |   36.84 | 10-15,19-24
 index-9.ts  |   33.33 |   36.84 | 10-15,19-24
 index.ts    |  100.00 |  100.00 |
-------------|---------|---------|-------------------

未來版本中計畫推出更詳細的序列化報告。

依 RegExp 模式篩選測試

bun test 現在支援使用 RegExp 模式依名稱篩選測試。若要篩選測試,請傳遞 -t 旗標

bun test -t /foo/

這將執行所有符合 /foo/ 模式的測試。

bun --hot 現在會在重新載入時清除終端機

感謝 @simylein

Bun.plugin 不再使用轉譯器魔法

現在使用 Bun.plugin 的建議方式是搭配 --preload,以確保外掛程式在任何其他程式碼之前載入。

// my markdown plugin
import { plugin, file } from "bun";

plugin({
  name: "Markdown",
  async setup(builder) {
    builder.onLoad({ filter: /\.(md)$/ }, async ({ path }) => {
      console.log(`[markdown-loader] ${path}`);
      const contents = await file(path).text();
      const slug = path.split("/").slice(-1)[0].slice(0, -3);
      return {
        exports: {
          slug,
          contents,
        },
        loader: "object",
      };
    });
  },
});

先前,Bun 會提前注入外掛程式,以便外掛程式可以潛在地與定義外掛程式的檔案一起使用。這造成了... 問題。

現在外掛程式必須使用 --preload 載入,或在 bunfig.toml 中設定 preload

Node.js 相容性改進

dns.getServers()

node:dns 模組現在匯出 dns.getServers(),它會傳回 IP 位址陣列作為字串。感謝 @Hanaasagi

import dns from "node:dns";
import fs from "node:fs";
import os from "node:os";

test("dns.getServers", (done) => {
  function parseResolvConf() {
    let servers = [];
    try {
      const content = fs.readFileSync("/etc/resolv.conf", "utf-8");
      const lines = content.split(os.EOL);

      for (const line of lines) {
        const parts = line.trim().split(/\s+/);
        if (parts.length >= 2 && parts[0] === "nameserver") {
          servers.push(parts[1]);
        }
      }
    } catch (err) {
      done(err);
    }
    return servers;
  }

  const expectServers = parseResolvConf();
  const actualServers = dns.getServers();
  try {
    for (const server of expectServers) {
      expect(actualServers).toContain(server);
    }
  } catch (err) {
    return done(err);
  }
  done();
});

Buffer.copy 錯誤修正

先前,以下測試在 Bun 中會失敗,但在 Node.js 中會通過

it("should ignore sourceEnd if it's out of range", () => {
  const buf1 = Buffer.allocUnsafe(26);
  const buf2 = Buffer.allocUnsafe(10).fill("!");

  for (let i = 0; i < 26; i++) {
    // 97 is the decimal ASCII value for 'a'.
    buf1[i] = i + 97;
  }

  // Copy `buf1` bytes "xyz" into `buf2` starting at byte 1 of `buf2`.
  expect(buf1.copy(buf2, 1, 23, 100)).toBe(3);
  expect(buf2.toString()).toBe("!xyz!!!!!!");
});

此問題已修正,感謝 @buffaybu

Module.wrap()

node:module 模組現在匯出 Module.wrap(code),這是 Webpack 使用的未記錄函數。

錯誤修正

我們也修正了許多錯誤。

bun:sqlite 中的當機

在 Bun v0.7.2 中升級 JavaScriptCore 後,引入了 bun:sqlite 中的當機(不是 JavaScriptCore 的錯,是我們的錯)。此問題已修正。

當資料行傳回超過 64 個字元的字串時,就會發生此當機。

async Node.js fs 函數中的當機

在此版本中修正了 Node.js fs 函數的 async 版本中的執行緒安全問題。有時字串是執行緒區域的(例如當它們來自屬性名稱時),將它們傳遞到另一個執行緒會導致非預期的行為。Bun 的 fs 函數現在會在將執行緒區域字串傳遞到另一個執行緒之前複製它們。這解決了錯誤,而不會造成效能降低。

bun init 現在接受巢狀目錄路徑

感謝 @Hanaasagibun init 現在接受目錄路徑。這可讓您在目前工作目錄以外的目錄中初始化 Bun 專案。

bun init ./foo/my-project

workspace:* 依賴項錯誤修正

一個錯誤導致在某些情況下無法正確找到 workspace:* 依賴項。感謝 @alexlamsl,此問題已修正。

Event、DOMException、ErrorEvent 等現在可寫入

以下 Web API 全域變數現在可寫入

這修正了影響 Angular 伺服器端渲染支援的錯誤。

感謝 @arturovt 修正此問題。

更新日誌

檢視完整更新日誌