Bun v1.0.4 修復了 62 個錯誤,新增了 server.requestIP
,支援執行階段外掛程式中的虛擬模組,並減少了 Bun.serve()
中的記憶體消耗。感謝您回報問題。我們正努力盡快修復這些問題。
Bun 是一個速度極快的 JavaScript 執行階段、打包器、轉譯器和套件管理器 — 功能All-in-One。我們最近發布了許多 Bun 的變更。以下是最近幾個版本的重點回顧。以防您錯過了
v1.0.0
- Bun 的第一個穩定版本!v1.0.1
- 針對 .json & .toml 檔案的具名匯入、bun install、node:path、Buffer 的錯誤修復v1.0.2
- 加快--watch
速度,以及錯誤修復v1.0.3
-emitDecoratorMetadata
、Nest.js 支援、私有registry修復,以及許多錯誤修復
安裝 Bun
curl -fsSL https://bun.dev.org.tw/install | bash
npm install -g bun
brew tap oven-sh/bun
brew install bun
docker pull oven/bun
docker run --rm --init --ulimit memlock=-1:-1 oven/bun
升級 Bun
bun upgrade
功能
減少 Bun.serve()
中的記憶體用量
Bun.serve()
現在會將手動管理的每個請求記憶體用量報告給 JavaScriptCore 的垃圾收集器。這在某些情況下將 Bun.serve()
的記憶體用量減少了 50%。
在下一個 Bun 版本中
— Jarred Sumner (@jarredsumner) 2023年9月28日
Bun.serve() 使用更少的記憶體
在 80 萬次請求後
Elysia
v1.0.4:47 MB
v1.0.3:71 MB
Fastify
v1.0.4:114 MB
v1.0.3:267 MB
Express
v1.0.4:116 MB
v1.0.3:167 MB pic.twitter.com/XSjyo3aR9G
實作 server.requestIP()
透過 #6165
,現在可以使用 server.requestIP()
檢索給定 Request
的 IP 位址。
Bun.serve({
port: 3000,
handler: (req, res) => {
console.log(server.requestIP(req));
},
});
這不會讀取諸如 X-Forwarded-For
或 X-Real-IP
之類的標頭。它僅返回套接字的 IP 位址,該位址可能對應於代理伺服器的 IP 位址。
我們還為 node:http
新增了獲取套接字位址的支援。net.Socket().address
現在返回一個具有 address
、port
和 family
屬性的物件。
Bun.plugin
中的虛擬模組
透過 #6167
,Bun 的外掛程式系統變得更加靈活且與 esbuild 相容。除了自訂載入器 (import stuff from "./stuff.foo"
) 之外,它現在還支援完全虛擬模組 (import stuff from "foo"
)。
您可以透過註冊外掛程式來註冊虛擬模組。
import { plugin } from "bun";
plugin({
name: "my plugin",
setup(builder) {
builder.module("my-virtual-module", () => {
return {
exports: {
hello: "world",
},
loader: "object",
};
});
},
});
然後,可以像使用普通模組一樣使用此虛擬模組
import { hello } from "my-virtual-module";
console.log(hello); // "world"
// require("my-virtual-module") also works
// await import("my-virtual-module") also works
// require.resolve("my-virtual-module") also works
此功能目前僅支援執行階段外掛程式,不適用於 bun build
。
支援 console.dir
中的參數
感謝 @liz3 在 #6059
中新增對 console.dir()
第二個參數物件的支援
console.dir({ 1: { 2: { 3: 3 } } }, { depth: 0, colors: false });
可能造成中斷的變更
fetch()
& bun install
現在的網路逾時時間為 5 分鐘,而不是 30 秒
在收到許多關於 fetch()
& bun install
逾時時間過短的報告後,我們已將 fetch()
的預設逾時時間從 30 秒增加到 5 分鐘。這與 Google Chrome 的預設值一致,應有助於高延遲連線。這也適用於 bun install
。請注意,此逾時不是針對請求的累計時間,而是針對接收到的每個資料區塊之間的時間。如果您想完全停用逾時,可以將 timeout: false
傳遞給 fetch()
。
bun install
現在將工作區成員套件的 package.json "version"
欄位儲存在鎖定檔中
先前,bun install
會在下次檢索時從其他現有資料推斷 package.json 版本,但事實證明這效果不佳,因為您並非總是需要工作區成員套件的 package.json 版本(它們可能沒有版本)。鎖定檔現在儲存工作區成員套件的 package.json 版本,這有助於解決一個問題,即當依賴項在 package.json 中使用 npm 版本,但引用本機工作區版本時,會導致 bun install
失敗。
這是對現有鎖定檔的就地增量更新,除了下次鎖定檔具有設定 version
的工作區成員套件時,git 狀態會顯示 dirty 之外,不應有其他影響。從技術上講,這不是一個重大變更,因為這之前就無法正常運作,但感覺值得注意。
bun install
錯誤修復
已對 bun install
進行了幾項穩定性改進,包括工作區。
- 修復在工作區成員套件內執行
bun add <package>
的問題#6092
- 修復
bun install
在初始安裝後「忘記」工作區存在的問題 - 修復了分支名稱包含斜線時影響 Git/GitHub 依賴項的錯誤
#5941
。 - 修復
bun install
中偶爾發生的掛起#6192
- 支援本機
.tgz
依賴項#5812
- 修復了儲存鎖定檔時可能發生的決定性錯誤,導致
git status
在實際沒有任何變更時報告 dirty 工作樹。
重大錯誤修復
修復在某些情況下,狀態碼為 3xx 時導致 fetch
超時的錯誤
存在一個錯誤,在某些情況下,當包含 3xx 狀態碼和空body,且未包含某些標頭時,fetch
會超時。請求將完成,但最終會超時而不是報告成功。這也會在某些情況下影響 bun install
。
修復沒有 super()
的 Error
子類的 captureStackTrace
#6063 修復了在擴展類別中,在沒有 super()
的建構函式內使用 captureStackTrace
時發生的崩潰問題。
class ExtendedError extends Error {
constructor() {
super();
Error.captureStackTrace(this, ExtendedError);
}
}
class AnotherError extends ExtendedError {}
throw new AnotherError();
修復導致「連線被拒絕」錯誤的 DNS 解析錯誤
當第一個傳回的結果連線失敗時,我們對 getaddrinfo
的使用不正確。getaddrinfo
傳回結果的連結列表,但我們只查看了第一個結果。現在我們會查看所有結果。
在 ws
的 'connection'
回呼中實作 isBinary
根據 #5944
import WebSocket, { WebSocketServer } from "ws";
const wss = new WebSocketServer({
port: 3000,
});
wss.on("connection", (ws) => {
ws.on("message", (data, isBinary) => {
// isBinary is now implemented
console.log("received", data, isBinary);
});
});
Node.js 相容性改進
- 感謝 @paperclover,已修復了幾個影響 Next.js pages router 的錯誤
- 感謝 @jhmaster,
util.inspect
已被重寫,以更相容於 Node.js - 已修復 macOS 上
fs.rm
無法移除寫入保護檔案的錯誤 - 感謝 @Hanaasagi,已修復了
fs.exists
的回呼版本錯誤地包含error
參數的錯誤
更新日誌
#5903 | fix(runtime): exclude unevaluated module in require.cache by @Hanaasagi |
#5941 | [install] fix GitHub dependency bugs by @dylan-conway |
#5944 | isBinary by @dylan-conway |
#5986 | Fixes #5985 by @Jarred-Sumner |
#5950 | Use c-ares function for checking if a string is an IP address by @Jarred-Sumner |
#6000 | Correctly fix #5888 by @Jarred-Sumner |
#6001 | Do not use removefileat() by @Jarred-Sumner |
#6026 | fix latest dev build panic by @Hanaasagi |
#6013 | Fix create command with template prefixed with @ char #6007 by @axlEscalada |
#6030 | Add fs.statfs{Sync} to missing fs apis by @techvlad |
#6032 | Make error message for new URL(invalid) better by @Jarred-Sumner |
#5998 | Add Module._extensions by @Jarred-Sumner |
#6036 | Drain microtasks at end of abort() if called into JS by @Jarred-Sumner |
#6063 | fix captureStackTrace inside constructor without super in extended by @dylan-conway |
#5771 | Improve Docker images by @Electroid |
#6090 | fix: Docker - Include bunx symlink in distroless variant by @polarathene |
#6086 | fix(fetch/server) fix server end of stream, fix fetch not streaming without content-length or chunked encoding, fix case when stream do not return a promise on pull by @cirospaciari |
#6059 | fix: support console.dir options object correctly by @liz3 |
#6092 | fix workspace dependency install by @dylan-conway |
#6097 | fix(node:fs): fix fs.exists callback parameters by @Hanaasagi |
#6100 | fix: Docker - Apply workaround with RUN to symlink bunx by @polarathene |
#5825 | fix: implement correct behaviour for urls with blob: scheme by @liz3 |
#6122 | fix(bun install): Handle vercel and github tarball path dependencies by @booniepepper |
#6123 | revert fix for passing empty env vars to bun run by @dylan-conway |
#5932 | deadCodeElimination toggle for Bun.Transpiler by @jhmaster2000 |
#6130 | fix typescript metadata for import identifiers by @dylan-conway |
#4493 | Complete rework of the majority of node:util , primarily util.inspect by @jhmaster2000 |
#6095 | Get Next.js Pages Router to work by @paperclover |
#6135 | Reduce memory usage of HTTP server by @Jarred-Sumner |
#6118 | Add local tarball install #5812 by @axlEscalada |
#6158 | Upgrade to latest Node.js version by @Jarred-Sumner |
#6162 | Fixes #6053 by @Jarred-Sumner |
#6165 | feat(runtime): implement server.requestIp + node:http socket.address() by @paperclover |
#5766 | fix(resolver): support encoded file urls by @paperclover |
#5945 | fix(runtime): Socket.prototype is undefined by @paperclover |
#6154 | fix: don't set default request method when creating a Request from another by @liz3 |
#6185 | fix(runtime): followup for server.requestIP by @paperclover |
#6167 | Implement virtual module support in Bun.plugin by @Jarred-Sumner |
#6192 | Fix hang in bun install by @Jarred-Sumner |
#6195 | tweak github actions by @Jarred-Sumner |
#6206 | Fix bug causing "Connection Refused" errors by @Jarred-Sumner |
#6207 | fix(node:process): fix return value of process.kill by @Hanaasagi |
#6219 | Slightly reduce number of open file descriptors in bun install by @Jarred-Sumner |
#6231 | Added the fileExtensions field to file-system-router.md by @cornedor |
#6242 | Warn at start when using AVX build of Bun without AVX support by @Jarred-Sumner |
#6247 | Fix bun install reading Github API from wrong environment variable by @Electroid |
#6217 | Set fetch timeout to 5 minutes by @Jarred-Sumner |
感謝 Bun 的最新貢獻者!
@meck93 @aszenz @cyfung1031 @axlEscalada @techvlad @Dawntraoz @polarathene @DarthDanAmesh @DevinJohw @cornedor @ciceropablo
完整更新日誌:https://github.com/oven-sh/bun/compare/bun-v1.0.3...bun-v1.0.4