Skip to content

Bun 通过 Bun.CookieBun.CookieMap 提供用于处理 HTTP cookies 的原生 API。这些 API 提供快速、易于使用的方法来解析、生成和操作 HTTP 请求和响应中的 cookies。

CookieMap 类

Bun.CookieMap 提供类似 Map 的接口用于处理 cookies 集合。它实现了 Iterable 接口,允许你将其与 for...of 循环和其他迭代方法一起使用。

ts
// 空 cookie map
const cookies = new Bun.CookieMap();

// 从 cookie 字符串创建
const cookies1 = new Bun.CookieMap("name=value; foo=bar");

// 从对象创建
const cookies2 = new Bun.CookieMap({
  session: "abc123",
  theme: "dark",
});

// 从名称/值对数组创建
const cookies3 = new Bun.CookieMap([
  ["session", "abc123"],
  ["theme", "dark"],
]);

在 HTTP 服务器中

在 Bun 的 HTTP 服务器中,请求对象上的 cookies 属性(在 routes 中)是 CookieMap 的实例:

ts
const server = Bun.serve({
  routes: {
    "/": req => {
      // 访问请求 cookies
      const cookies = req.cookies;

      // 获取特定 cookie
      const sessionCookie = cookies.get("session");
      if (sessionCookie != null) {
        console.log(sessionCookie);
      }

      // 检查 cookie 是否存在
      if (cookies.has("theme")) {
        // ...
      }

      // 设置 cookie,它将自动应用于响应
      cookies.set("visited", "true");

      return new Response("Hello");
    },
  },
});

console.log("服务器监听于:" + server.url);

方法

get(name: string): string | null

按名称检索 cookie。如果 cookie 不存在则返回 null

ts
// 按名称获取
const cookie = cookies.get("session");

if (cookie != null) {
  console.log(cookie);
}

has(name: string): boolean

检查是否存在具有给定名称的 cookie。

ts
// 检查 cookie 是否存在
if (cookies.has("session")) {
  // cookie 存在
}

set(name: string, value: string): void

set(options: CookieInit): void

添加或更新 map 中的 cookie。cookies 默认为 { path: "/", sameSite: "lax" }

ts
// 按名称和值设置
cookies.set("session", "abc123");

// 使用选项对象设置
cookies.set({
  name: "theme",
  value: "dark",
  maxAge: 3600,
  secure: true,
});

// 使用 Cookie 实例设置
const cookie = new Bun.Cookie("visited", "true");
cookies.set(cookie);

delete(name: string): void

delete(options: CookieStoreDeleteOptions): void

从 map 中移除 cookie。当应用于 Response 时,这会添加一个值为空字符串且过期日期为过去的 cookie。只有当 domain 和 path 与创建 cookie 时相同,cookie 才会在浏览器上成功删除。

ts
// 使用默认 domain 和 path 按名称删除
cookies.delete("session");

// 使用 domain/path 选项删除
cookies.delete({
  name: "session",
  domain: "example.com",
  path: "/admin",
});

toJSON(): Record<string, string>

将 cookie map 转换为可序列化格式。

ts
const json = cookies.toJSON();

toSetCookieHeaders(): string[]

返回 Set-Cookie 头值的数组,可用于应用所有 cookie 更改。

使用 Bun.serve() 时,你不需要显式调用此方法。对 req.cookies map 所做的任何更改都会自动应用于响应头。此方法主要在使用其他 HTTP 服务器实现时很有用。

js
import { createServer } from "node:http";
import { CookieMap } from "bun";

const server = createServer((req, res) => {
  const cookieHeader = req.headers.cookie || "";
  const cookies = new CookieMap(cookieHeader);

  cookies.set("view-count", Number(cookies.get("view-count") || "0") + 1);
  cookies.delete("session");

  res.writeHead(200, {
    "Content-Type": "text/plain",
    "Set-Cookie": cookies.toSetCookieHeaders(),
  });
  res.end(`找到 ${cookies.size} 个 cookies`);
});

server.listen(3000, () => {
  console.log("服务器运行于 http://localhost:3000/");
});

迭代

CookieMap 提供多种迭代方法:

ts
// 迭代 [name, cookie] 条目
for (const [name, value] of cookies) {
  console.log(`${name}: ${value}`);
}

// 使用 entries()
for (const [name, value] of cookies.entries()) {
  console.log(`${name}: ${value}`);
}

// 使用 keys()
for (const name of cookies.keys()) {
  console.log(name);
}

// 使用 values()
for (const value of cookies.values()) {
  console.log(value);
}

// 使用 forEach
cookies.forEach((value, name) => {
  console.log(`${name}: ${value}`);
});

属性

size: number

返回 map 中 cookies 的数量。

ts
console.log(cookies.size); // cookies 数量

Bun.Cookie 表示具有名称、值和属性的 HTTP cookie。

ts
import { Cookie } from "bun";

// 创建基本 cookie
const cookie = new Bun.Cookie("name", "value");

// 创建带选项的 cookie
const secureSessionCookie = new Bun.Cookie("session", "abc123", {
  domain: "example.com",
  path: "/admin",
  expires: new Date(Date.now() + 86400000), // 1 天
  httpOnly: true,
  secure: true,
  sameSite: "strict",
});

// 从 cookie 字符串解析
const parsedCookie = new Bun.Cookie("name=value; Path=/; HttpOnly");

// 从选项对象创建
const objCookie = new Bun.Cookie({
  name: "theme",
  value: "dark",
  maxAge: 3600,
  secure: true,
});

构造函数

ts
// 使用名称/值的基本构造函数
new Bun.Cookie(name: string, value: string);

// 使用名称、值和选项的构造函数
new Bun.Cookie(name: string, value: string, options: CookieInit);

// 从 cookie 字符串构造函数
new Bun.Cookie(cookieString: string);

// 从 cookie 对象构造函数
new Bun.Cookie(options: CookieInit);

属性

ts
cookie.name; // string - Cookie 名称
cookie.value; // string - Cookie 值
cookie.domain; // string | null - 域名范围(未指定时为 null)
cookie.path; // string - URL 路径范围(默认为 "/")
cookie.expires; // number | undefined - 过期时间戳(自纪元起的毫秒数)
cookie.secure; // boolean - 需要 HTTPS
cookie.sameSite; // "strict" | "lax" | "none" - SameSite 设置
cookie.partitioned; // boolean - cookie 是否分区(CHIPS)
cookie.maxAge; // number | undefined - 最大年龄(秒)
cookie.httpOnly; // boolean - 仅通过 HTTP 访问(不可通过 JavaScript)

方法

isExpired(): boolean

检查 cookie 是否已过期。

ts
// 已过期的 cookie(日期在过去)
const expiredCookie = new Bun.Cookie("name", "value", {
  expires: new Date(Date.now() - 1000),
});
console.log(expiredCookie.isExpired()); // true

// 有效 cookie(使用 maxAge 而不是 expires)
const validCookie = new Bun.Cookie("name", "value", {
  maxAge: 3600, // 1 小时(秒)
});
console.log(validCookie.isExpired()); // false

// 会话 cookie(无过期时间)
const sessionCookie = new Bun.Cookie("name", "value");
console.log(sessionCookie.isExpired()); // false

serialize(): string

toString(): string

返回适合 Set-Cookie 头的 cookie 字符串表示形式。

ts
const cookie = new Bun.Cookie("session", "abc123", {
  domain: "example.com",
  path: "/admin",
  expires: new Date(Date.now() + 86400000),
  secure: true,
  httpOnly: true,
  sameSite: "strict",
});

console.log(cookie.serialize());
// => "session=abc123; Domain=example.com; Path=/admin; Expires=Sun, 19 Mar 2025 15:03:26 GMT; Secure; HttpOnly; SameSite=strict"
console.log(cookie.toString());
// => "session=abc123; Domain=example.com; Path=/admin; Expires=Sun, 19 Mar 2025 15:03:26 GMT; Secure; HttpOnly; SameSite=strict"

toJSON(): CookieInit

将 cookie 转换为适合 JSON 序列化的普通对象。

ts
const cookie = new Bun.Cookie("session", "abc123", {
  secure: true,
  httpOnly: true,
});

const json = cookie.toJSON();
// => {
//   name: "session",
//   value: "abc123",
//   path: "/",
//   secure: true,
//   httpOnly: true,
//   sameSite: "lax",
//   partitioned: false
// }

// 可与 JSON.stringify 一起使用
const jsonString = JSON.stringify(cookie);

静态方法

将 cookie 字符串解析为 Cookie 实例。

ts
const cookie = Bun.Cookie.parse("name=value; Path=/; Secure; SameSite=Lax");

console.log(cookie.name); // "name"
console.log(cookie.value); // "value"
console.log(cookie.path); // "/"
console.log(cookie.secure); // true
console.log(cookie.sameSite); // "lax"

用于创建 cookie 的工厂方法。

ts
const cookie = Bun.Cookie.from("session", "abc123", {
  httpOnly: true,
  secure: true,
  maxAge: 3600,
});

类型

ts
interface CookieInit {
  name?: string;
  value?: string;
  domain?: string;
  /** 默认为 '/'。要允许浏览器设置路径,使用空字符串。 */
  path?: string;
  expires?: number | Date | string;
  secure?: boolean;
  /** 默认为 `lax`。 */
  sameSite?: CookieSameSite;
  httpOnly?: boolean;
  partitioned?: boolean;
  maxAge?: number;
}

interface CookieStoreDeleteOptions {
  name: string;
  domain?: string | null;
  path?: string;
}

interface CookieStoreGetOptions {
  name?: string;
  url?: string;
}

type CookieSameSite = "strict" | "lax" | "none";

class Cookie {
  constructor(name: string, value: string, options?: CookieInit);
  constructor(cookieString: string);
  constructor(cookieObject?: CookieInit);

  readonly name: string;
  value: string;
  domain?: string;
  path: string;
  expires?: Date;
  secure: boolean;
  sameSite: CookieSameSite;
  partitioned: boolean;
  maxAge?: number;
  httpOnly: boolean;

  isExpired(): boolean;

  serialize(): string;
  toString(): string;
  toJSON(): CookieInit;

  static parse(cookieString: string): Cookie;
  static from(name: string, value: string, options?: CookieInit): Cookie;
}

class CookieMap implements Iterable<[string, string]> {
  constructor(init?: string[][] | Record<string, string> | string);

  get(name: string): string | null;

  toSetCookieHeaders(): string[];

  has(name: string): boolean;
  set(name: string, value: string, options?: CookieInit): void;
  set(options: CookieInit): void;
  delete(name: string): void;
  delete(options: CookieStoreDeleteOptions): void;
  delete(name: string, options: Omit<CookieStoreDeleteOptions, "name">): void;
  toJSON(): Record<string, string>;

  readonly size: number;

  entries(): IterableIterator<[string, string]>;
  keys(): IterableIterator<string>;
  values(): IterableIterator<string>;
  forEach(callback: (value: string, key: string, map: CookieMap) => void): void;
  [Symbol.iterator](): IterableIterator<[string, string]>;
}

Bun学习网由www.bunjs.com.cn整理维护