From 0477e5320a87f88c133ccae59ee2372239da97a6 Mon Sep 17 00:00:00 2001 From: Yarchik Date: Tue, 23 Jun 2026 23:33:41 +0100 Subject: [PATCH 1/2] fix: apply the encode callback to empty cookie values The encode option is documented to mirror decode and should run for every value, but stringifySetCookie used a truthiness test that skipped it for the empty string, emitting a raw name= instead of the encoded result. This broke encode/decode symmetry for signing and encrypting encoders. Use a null check so empty strings are routed through the encoder while the object-form value: undefined still maps to an empty value. This matches the sibling stringifyCookie helper and cookie@0.7.2, which both always call the encoder. --- src/index.ts | 2 +- src/stringify-set-cookie.spec.ts | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index b30003f..4060207 100644 --- a/src/index.ts +++ b/src/index.ts @@ -304,7 +304,7 @@ export function stringifySetCookie( throw new TypeError(`argument name is invalid: ${name}`); } - const value = rawValue ? enc(rawValue) : ""; + const value = rawValue != null ? enc(rawValue) : ""; if (!cookieValueRegExp.test(value)) { throw new TypeError(`argument val is invalid: ${rawValue}`); diff --git a/src/stringify-set-cookie.spec.ts b/src/stringify-set-cookie.spec.ts index 92a56ea..8e8a719 100644 --- a/src/stringify-set-cookie.spec.ts +++ b/src/stringify-set-cookie.spec.ts @@ -127,6 +127,12 @@ describe("cookie.stringifySetCookie", function () { ).toEqual("foo=YmFy"); }); + it("should apply the encoder to an empty value", function () { + expect( + cookie.stringifySetCookie("foo", "", { encode: (v) => "[" + v + "]" }), + ).toEqual("foo=[]"); + }); + it.each(["foo=bar", 'foo"bar', "foo,bar", "foo\\bar", "foo$bar"])( "should serialize value: %s", (value) => { From 099ed03623338f055fe4598a17c16f0cca15da73 Mon Sep 17 00:00:00 2001 From: Blake Embrey Date: Wed, 24 Jun 2026 15:46:52 -0700 Subject: [PATCH 2/2] Fix test --- src/stringify-set-cookie.spec.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/stringify-set-cookie.spec.ts b/src/stringify-set-cookie.spec.ts index 86c69c9..f4e9cd4 100644 --- a/src/stringify-set-cookie.spec.ts +++ b/src/stringify-set-cookie.spec.ts @@ -128,7 +128,10 @@ describe("cookie.stringifySetCookie", () => { it("should apply the encoder to an empty value", function () { expect( - cookie.stringifySetCookie("foo", "", { encode: (v) => "[" + v + "]" }), + stringifySetCookie( + { name: "foo", value: "" }, + { encode: (v) => "[" + v + "]" }, + ), ).toEqual("foo=[]"); });