Node.js 18 fetch and combined set-cookie header

When fetching an endpoint which sets multiple cookies, the response has only a single combined set-cookie header.

Example:

  1. add 2 cookies in endpoint
"set-cookie": [
  "user=alice; Path=/; Expires=Thu, 01 Jun 2023 10:00:00 GMT; HttpOnly",
  "role=guest; Path=/"
]
  1. fetch endpoint. response only has a single set-cookie header with values joined by ,
set-cookie: user=alice; Path=/; Expires=Thu, 01 Jun 2023 10:00:00 GMT; HttpOnly, role=guest; Path=/
  1. a simple split by , is not enought as the cookie could contain additional , characters (eg. in the expires value)
0: user=alice; Path=/; Expires=Thu
1: 01 Jun 2023 10:00:00 GMT; HttpOnly
2: role=guest; Path=/

set-cookie-parser

The library set-cookie-parser has a splitCookiesString(combinedSetCookieHeader) function

references