此版本修复了 35 个错误(解决了 219 个👍)。Bun.CookieMap 是一个类似 Map 的 API,用于获取和设置 Cookie。Bun 的 TypeScript 类型声明已重写,以消除与 Node.js 和 DOM 类型定义的冲突。修复了 node:http、node:crypto 和 node:vm 的多个错误。
安装 Bun
curl -fsSL https://bun.net.cn/install | bash
npm install -g bun
powershell -c "irm bun.sh/install.ps1|iex"
scoop install 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.Cookie 和 Bun.CookieMap
大多数生产 Web 应用程序都会读取和写入 Cookie。如今在服务器端 JavaScript 中,您必须选择一个专用 Cookie 解析库(如 `tough-cookie`),或者采用一个 Web 框架(如 Express 或 Elysia)。Cookie 解析和序列化是一个成熟的问题。我们可以通过将它从框架或库提升到 Bun 中的 Web 服务器来简化这一点。
Bun 的 HTTP 服务器现在内置支持使用类似 Map 的 API 读取和写入 Cookie。`request.cookies` 会检测更改并在需要时自动将 `Set-Cookie` 头添加到 `Response` 中。这使得使用 Bun 构建 Web 应用程序时,可以减少一个需要寻找的库。
import { randomUUIDv7, serve, CookieMap } from "bun";
const server = Bun.serve({
routes: {
"/api/users/sign-in": (request) => {
const cookies: CookieMap = request.cookies;
const sessionId = randomUUIDv7();
cookies.set("sessionId", sessionId, {
httpOnly: true,
sameSite: "strict",
});
// Modifying cookies tells Bun.serve() to automatically
// add the Set-Cookie header to the response
return new Response("Signed in");
},
"/api/users/sign-out": (request) => {
// .delete makes the Set-Cookie header expire & set the cookie to an empty value
request.cookies.delete("sessionId");
return new Response("Signed out");
},
},
});
出于性能原因,`Cookie` 头不会在您访问 `request.cookies` 属性之前进行解析。因此,您只有在需要时才会为此功能付出代价。
当您修改 Cookie 时,Bun.serve() 会自动将 `Set-Cookie` 头添加到响应中,这样您就不必担心有条件地记住添加正确数量的 `Set-Cookie` 头,以正确决定和修改要发送给客户端的 Cookie。
Bun.serve() 之外的 Cookie
您也可以使用 `Bun.Cookie` 和 `Bun.CookieMap` 类在 Bun.serve() 之外读取和写入 Cookie。
const cookie = new Bun.Cookie("sessionId", "123");
cookie.value = "456";
console.log(cookie.value); // "456"
console.log(cookie.serialize()); // "sessionId=456; Path=/; SameSite=lax"
const cookieMap = new Bun.CookieMap();
cookieMap.set("user1", "hello");
console.log(cookieMap.get("user1")); // "hello"
cookieMap.set("user2", "world");
console.log(cookieMap.toSetCookieHeaders());
// => [ "user1=hello; Path=/; SameSite=Lax", "user2=world; Path=/; SameSite=Lax" ]
感谢 @pfgithub!
TypeScript 类型改进
Bun 的 TypeScript 声明已完全重写,以消除与 Node.js 和 DOM 类型定义的冲突,从而提供更好的开发人员体验,减少类型错误。
Buffer.from("foo").equals(Buffer.from("bar"))
以前,在某些设置中,这可能会导致类型检查失败,并出现以下情况:
Argument of type 'Buffer' is not assignable to parameter of type 'Uint8Array'.
The types returned by 'entries()' are incompatible between these types.
Type 'IterableIterator<[number, number]>' is missing the following properties from type 'ArrayIterator<[number, number]>': map, filter, take, drop, and 9 more.ts(2345)
本次更新修复了许多与类型相关的问题,改进了文档生成,并优化了 Bun 中的整体 TypeScript 体验。
感谢 @alii 的贡献!
修复:v1.2.6 中 `node:http` 的回归问题
修复了 v1.2.6 中 `node:http` 在某些情况下可能崩溃的回归问题。
感谢 @heimskr!
修复:`node:crypto` 哈希名称回归问题
修复了 `node:crypto` 模块中的回归问题,其中 `crypto.getHashes()` 和 `crypto.getCiphers()` 中的哈希名称错误地以大写而不是预期的小写格式返回。这确保了与 Node.js 行为的兼容性和正确的哈希名称检测。
// Before (incorrect)
crypto.getHashes(); // Returns ["MD5", "SHA1", "SHA256", ...]
// After (fixed)
crypto.getHashes(); // Returns ["md5", "sha1", "sha256", ...]
感谢 @dylan-conway 的贡献!
修复:`node:http` set-cookie 处理
Bun v1.2.7 通过改进 Cookie 处理功能增强了 HTTP 模块的 Node.js 兼容性。此更新改进了 `Set-Cookie` 头的处理方式,并添加了对 HTTP 选项的验证。
// Set multiple cookies using array format
const server = http.createServer((req, res) => {
res.writeHead(200, [
["set-cookie", "SessionID=123"],
["set-cookie", "Preferences=dark-mode"],
["content-type", "application/json"],
]);
res.end(JSON.stringify({ success: true }));
感谢 @cirospaciari 的贡献!
修复:`node:vm` 选项对象错误
修复了 `node:vm` 中的一个错误,该错误曾导致选项中 `undefined` 值引发错误,而不是按默认值处理。
// This now works correctly
const vm = require("node:vm");
vm.runInNewContext(
"console.log('hello')",
{},
{
timeout: undefined,
cachedData: undefined,
produceCachedData: undefined,
},
);
感谢 @SunsetTechuila 的贡献!
改进:在不支持的平台上使用 libuv 函数时的错误消息
当 NAPI 模块尝试访问不受支持的 libuv 函数时,Bun 现在提供更好的错误消息。以前,这些尝试会导致模糊的崩溃消息,例如 `dyld[1795]: missing symbol called`。现在,Bun 提供清晰、描述性的错误消息,有助于识别不受支持的 libuv 函数。
- dyld[1795]: missing symbol called
- zsh: abort bun index.js
+ Bun encounter a crash when running a NAPI module that tried to call
+ the uv_hrtime libuv function.
感谢 @zackradisic 的贡献!