egg-cookies
Advanced tools
Comparing version 2.8.1 to 2.8.2
@@ -64,2 +64,6 @@ /** | ||
partitioned?: boolean; | ||
/** | ||
* Remove unpartitioned same name cookie or not. | ||
*/ | ||
removeUnpartitioned?: boolean; | ||
} | ||
@@ -66,0 +70,0 @@ |
@@ -131,7 +131,28 @@ 'use strict'; | ||
// remove unpartitioned same name cookie first | ||
if (opts.partitioned && opts.removeUnpartitioned) { | ||
const overwrite = opts.overwrite; | ||
if (overwrite) { | ||
opts.overwrite = false; | ||
headers = ignoreCookiesByName(headers, name); | ||
} | ||
const removeCookieOpts = Object.assign({}, opts, { | ||
partitioned: false, | ||
}); | ||
const removeUnpartitionedCookie = new Cookie(name, '', removeCookieOpts); | ||
// if user not set secure, reset secure to ctx.secure | ||
if (opts.secure === undefined) removeUnpartitionedCookie.attrs.secure = this.secure; | ||
headers = pushCookie(headers, removeUnpartitionedCookie); | ||
// signed | ||
if (signed) { | ||
removeUnpartitionedCookie.name += '.sig'; | ||
headers = ignoreCookiesByName(headers, removeUnpartitionedCookie.name); | ||
headers = pushCookie(headers, removeUnpartitionedCookie); | ||
} | ||
} | ||
const cookie = new Cookie(name, value, opts); | ||
// if user not set secure, reset secure to ctx.secure | ||
if (opts.secure === undefined) cookie.attrs.secure = this.secure; | ||
headers = pushCookie(headers, cookie); | ||
@@ -203,3 +224,3 @@ | ||
if (cookie.attrs.overwrite) { | ||
cookies = cookies.filter(c => !c.startsWith(cookie.name + '=')); | ||
cookies = ignoreCookiesByName(cookies, cookie.name); | ||
} | ||
@@ -210,3 +231,8 @@ cookies.push(cookie.toHeader()); | ||
function ignoreCookiesByName(cookies, name) { | ||
const prefix = `${name}=`; | ||
return cookies.filter(c => !c.startsWith(prefix)); | ||
} | ||
Cookies.CookieError = CookieError; | ||
module.exports = Cookies; |
{ | ||
"name": "egg-cookies", | ||
"version": "2.8.1", | ||
"version": "2.8.2", | ||
"description": "cookies module for egg", | ||
@@ -5,0 +5,0 @@ "files": [ |
@@ -41,2 +41,3 @@ # egg-cookies | ||
- partitioned - `Boolean` 是否设置独立分区状态([CHIPS](https://developers.google.com/privacy-sandbox/3pcd/chips))的 Cookie。注意,只有 `secure` 为 true 的时候此配置才会生效。 | ||
- removeUnpartitioned - `Boolean` 是否删除非独立分区状态的同名 cookie。注意,只有 `partitioned` 为 true 的时候此配置才会生效。 | ||
- httpOnly - `Boolean` 如果设置为 ture,则浏览器中不允许读取这个 cookie 的值。 | ||
@@ -43,0 +44,0 @@ - overwrite - `Boolean` 如果设置为 true,在一个请求上重复写入同一个 key 将覆盖前一次写入的值,默认为 false。 |
28083
467