Appearance
Cookie 助手
🌐 Cookie Helper
Cookie Helper 提供了一个简单的界面来管理 cookie,使开发者能够无缝地设置、解析和删除 cookie。
🌐 The Cookie Helper provides an easy interface to manage cookies, enabling developers to set, parse, and delete cookies seamlessly.
导入
🌐 Import
ts
import { Hono } from 'hono'
import {
deleteCookie,
getCookie,
getSignedCookie,
setCookie,
setSignedCookie,
generateCookie,
generateSignedCookie,
} from 'hono/cookie'用法
🌐 Usage
常规 Cookie
🌐 Regular cookies
ts
app.get('/cookie', (c) => {
setCookie(c, 'cookie_name', 'cookie_value')
const yummyCookie = getCookie(c, 'cookie_name')
deleteCookie(c, 'cookie_name')
const allCookies = getCookie(c)
// ...
})签名 Cookie
🌐 Signed cookies
注意:由于使用 WebCrypto API 创建 HMAC SHA-256 签名具有异步特性,因此设置和获取签名的 Cookie 会返回一个 Promise。
ts
app.get('/signed-cookie', (c) => {
const secret = 'secret' // make sure it's a large enough string to be secure
await setSignedCookie(c, 'cookie_name0', 'cookie_value', secret)
const fortuneCookie = await getSignedCookie(
c,
secret,
'cookie_name0'
)
deleteCookie(c, 'cookie_name0')
// `getSignedCookie` will return `false` for a specified cookie if the signature was tampered with or is invalid
const allSignedCookies = await getSignedCookie(c, secret)
// ...
})Cookie 生成
🌐 Cookie Generation
generateCookie 和 generateSignedCookie 函数允许你直接创建 cookie 字符串,而无需将它们设置在响应头中。
generateCookie
ts
// Basic cookie generation
const cookie = generateCookie('delicious_cookie', 'macha')
// Returns: 'delicious_cookie=macha; Path=/'
// Cookie with options
const cookie = generateCookie('delicious_cookie', 'macha', {
path: '/',
secure: true,
httpOnly: true,
domain: 'example.com',
})generateSignedCookie
ts
// Basic signed cookie generation
const signedCookie = await generateSignedCookie(
'delicious_cookie',
'macha',
'secret chocolate chips'
)
// Signed cookie with options
const signedCookie = await generateSignedCookie(
'delicious_cookie',
'macha',
'secret chocolate chips',
{
path: '/',
secure: true,
httpOnly: true,
}
)注意:与 setCookie 和 setSignedCookie 不同,这些函数仅生成 cookie 字符串。如果需要,你需要手动将它们设置在请求头中。
选项
🌐 Options
setCookie 和 setSignedCookie
🌐 setCookie & setSignedCookie
- 域名:
string - 过期时间:
Date - httpOnly:
boolean - 最大年龄:
number - 路径:
string - 安全:
boolean - sameSite:
'Strict'|'Lax'|'None' - 优先级:
'Low' | 'Medium' | 'High' - 前缀:
secure|'host' - 已分区:
boolean
示例:
🌐 Example:
ts
// Regular cookies
setCookie(c, 'great_cookie', 'banana', {
path: '/',
secure: true,
domain: 'example.com',
httpOnly: true,
maxAge: 1000,
expires: new Date(Date.UTC(2000, 11, 24, 10, 30, 59, 900)),
sameSite: 'Strict',
})
// Signed cookies
await setSignedCookie(
c,
'fortune_cookie',
'lots-of-money',
'secret ingredient',
{
path: '/',
secure: true,
domain: 'example.com',
httpOnly: true,
maxAge: 1000,
expires: new Date(Date.UTC(2000, 11, 24, 10, 30, 59, 900)),
sameSite: 'Strict',
}
)deleteCookie
- 路径:
string - 安全:
boolean - 域名:
string
示例:
🌐 Example:
ts
deleteCookie(c, 'banana', {
path: '/',
secure: true,
domain: 'example.com',
})deleteCookie 返回被删除的值:
ts
const deletedCookie = deleteCookie(c, 'delicious_cookie')__Secure- 和 __Host- 前缀
🌐 __Secure- and __Host- prefix
Cookie 助手支持用于 cookie 名称的 __Secure- 和 __Host- 前缀。
🌐 The Cookie helper supports __Secure- and __Host- prefixes for cookie names.
如果要验证 cookie 名称是否有前缀,请指定前缀选项。
🌐 If you want to verify if the cookie name has a prefix, specify the prefix option.
ts
const securePrefixCookie = getCookie(c, 'yummy_cookie', 'secure')
const hostPrefixCookie = getCookie(c, 'yummy_cookie', 'host')
const securePrefixSignedCookie = await getSignedCookie(
c,
secret,
'fortune_cookie',
'secure'
)
const hostPrefixSignedCookie = await getSignedCookie(
c,
secret,
'fortune_cookie',
'host'
)此外,如果你希望在设置 cookie 时指定前缀,请为前缀选项指定一个值。
🌐 Also, if you wish to specify a prefix when setting the cookie, specify a value for the prefix option.
ts
setCookie(c, 'delicious_cookie', 'macha', {
prefix: 'secure', // or `host`
})
await setSignedCookie(
c,
'delicious_cookie',
'macha',
'secret choco chips',
{
prefix: 'secure', // or `host`
}
)遵循最佳实践
🌐 Following the best practices
新的 Cookie RFC(又名 cookie-bis)和 CHIPS 包含一些开发者应遵循的 Cookie 设置最佳实践。
🌐 A New Cookie RFC (a.k.a cookie-bis) and CHIPS include some best practices for Cookie settings that developers should follow.
- RFC6265bis-13
Max-Age/Expires限制__Host-/__Secure-前缀限制
- CHIPS-01
Partitioned限制
Hono 遵循最佳实践。 当在以下情况下解析 cookie 时,cookie 辅助程序将抛出 Error:
🌐 Hono is following the best practices. The cookie helper will throw an Error when parsing cookies under the following conditions:
- Cookie 名称以
__Secure-开头,但未设置secure选项。 - Cookie 名称以
__Host-开头,但未设置secure选项。 - 该 cookie 名称以
__Host-开头,但path不是/。 - Cookie 名称以
__Host-开头,但设置的是domain。 maxAge选项的值大于 400 天。expires选项的值比当前时间晚 400 天。