set-cookie-parser is a JavaScript/TypeScript library designed to parse `Set-Cookie` HTTP response headers into structured JavaScript objects. It currently stands at stable version 3.1.0 and is actively maintained, with a healthy release cadence and recent updates. The library is highly versatile, accepting various inputs including single header strings, arrays of header strings, Node.js `http.ServerResponse` objects, and `fetch()` `Response` objects. A key differentiator is its ability to return either an array of cookie objects or a map where cookie names are keys, depending on configuration. Each parsed cookie object provides a comprehensive set of attributes, including `name`, `value`, `path`, `domain`, `expires` (as `Date` objects), `maxAge`, `secure`, `httpOnly`, `sameSite`, and `partitioned`. It ships with built-in TypeScript types, ensuring robust type checking and improved developer experience in modern TypeScript projects.
npm install set-cookie-parserVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to parse `Set-Cookie` headers from an HTTP response object, iterating and logging the properties of each parsed cookie.
Ensure `maxAge` is converted: `res.cookie('name', 'value', { maxAge: cookie.maxAge * 1000 });`Implement client-side validation for `cookie.sameSite` values, e.g., `if (!['Strict', 'Lax', 'None'].includes(cookie.sameSite)) { /* handle invalid value */ }`Always ensure the input to `parseSetCookie` is an object from which `Set-Cookie` headers can be legitimately read, typically a response object.
For ES Modules: `import { parseSetCookie } from 'set-cookie-parser';` For CommonJS: `const { parseSetCookie } = require('set-cookie-parser');`Always check if the `cookies` array (or map) is populated before attempting to access its elements or properties, and ensure the input to `parseSetCookie` is valid and contains `Set-Cookie` headers.
If you are working with `fetch` responses, ensure you pass the actual `Response` object or its `response.headers` object directly to `parseSetCookie`.
No dependency data recorded yet.