Skip to content

Commit e049536

Browse files
committed
fix(cookie): correctly default decode in parse() when provided decode argument is undefined
1 parent d66bbd3 commit e049536

File tree

2 files changed

+25
-20
lines changed

2 files changed

+25
-20
lines changed

.changeset/curly-laws-heal.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@otterhttp/cookie": patch
3+
---
4+
5+
Fix: default `decode` option correctly when provided `decode` option is `undefined`

packages/cookie/src/index.ts

+20-20
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ function tryDecode(str: string, decode: (str: string) => string) {
2727
*/
2828
export function parse(
2929
str: string,
30-
options?: {
30+
options: {
3131
decode?: ((str: string) => string) | undefined
32-
},
32+
} = {},
3333
): Record<string, string> {
34-
const decode = options?.decode ?? decodeURIComponent
34+
options.decode ??= decodeURIComponent
3535

3636
const obj: Record<string, string> = {}
3737
const pairs = str.split(pairSplitRegExp)
@@ -49,7 +49,7 @@ export function parse(
4949
if ('"' === val[0]) val = val.slice(1, -1)
5050

5151
// only assign once
52-
if (obj[key] == null) obj[key] = tryDecode(val, decode)
52+
if (obj[key] == null) obj[key] = tryDecode(val, options.decode)
5353
}
5454

5555
return obj
@@ -66,45 +66,45 @@ export type SerializeOptions = Partial<{
6666
expires: Date
6767
}>
6868

69-
export function serialize(name: string, val: string, opt: SerializeOptions = {}): string {
70-
if (!opt.encode) opt.encode = encodeURIComponent
69+
export function serialize(name: string, val: string, options: SerializeOptions = {}): string {
70+
options.encode ??= encodeURIComponent
7171

7272
if (!fieldContentRegExp.test(name)) throw new TypeError("argument name is invalid")
7373

74-
const value = opt.encode(val)
74+
const value = options.encode(val)
7575

7676
if (value && !fieldContentRegExp.test(value)) throw new TypeError("argument val is invalid")
7777

7878
let str = `${name}=${value}`
7979

80-
if (null != opt.maxAge) {
81-
const maxAge = opt.maxAge - 0
80+
if (null != options.maxAge) {
81+
const maxAge = options.maxAge - 0
8282

8383
if (Number.isNaN(maxAge) || !Number.isFinite(maxAge)) throw new TypeError("option maxAge is invalid")
8484

8585
str += `; Max-Age=${Math.floor(maxAge)}`
8686
}
8787

88-
if (opt.domain) {
89-
if (!fieldContentRegExp.test(opt.domain)) throw new TypeError("option domain is invalid")
88+
if (options.domain) {
89+
if (!fieldContentRegExp.test(options.domain)) throw new TypeError("option domain is invalid")
9090

91-
str += `; Domain=${opt.domain}`
91+
str += `; Domain=${options.domain}`
9292
}
9393

94-
if (opt.path) {
95-
if (!fieldContentRegExp.test(opt.path)) throw new TypeError("option path is invalid")
94+
if (options.path) {
95+
if (!fieldContentRegExp.test(options.path)) throw new TypeError("option path is invalid")
9696

97-
str += `; Path=${opt.path}`
97+
str += `; Path=${options.path}`
9898
}
9999

100-
if (opt.expires) str += `; Expires=${opt.expires.toUTCString()}`
100+
if (options.expires) str += `; Expires=${options.expires.toUTCString()}`
101101

102-
if (opt.httpOnly) str += "; HttpOnly"
102+
if (options.httpOnly) str += "; HttpOnly"
103103

104-
if (opt.secure) str += "; Secure"
104+
if (options.secure) str += "; Secure"
105105

106-
if (opt.sameSite) {
107-
const sameSite = typeof opt.sameSite === "string" ? opt.sameSite.toLowerCase() : opt.sameSite
106+
if (options.sameSite) {
107+
const sameSite = typeof options.sameSite === "string" ? options.sameSite.toLowerCase() : options.sameSite
108108

109109
switch (sameSite) {
110110
case true:

0 commit comments

Comments
 (0)