{"name":"set-cookie-parser","version":"2.7.0","description":"Parses set-cookie headers into objects","homepage":"https://github.com/nfriedly/set-cookie-parser","repository":{"type":"git","url":"git+https://github.com/nfriedly/set-cookie-parser.git"},"author":{"name":"Nathan Friedly","url":"http://nfriedly.com/"},"main":"./lib/set-cookie.js","sideEffects":false,"keywords":["set-cookie","set","cookie","cookies","header","parse","parser"],"devDependencies":{"eslint":"^8.57.0","eslint-config-prettier":"^9.1.0","eslint-plugin-prettier":"^5.1.3","husky":"^9.0.11","mocha":"^10.3.0","prettier":"^3.2.5","pretty-quick":"^4.0.0","sinon":"^17.0.1"},"scripts":{"lint":"eslint . --ignore-pattern '!.eslintrc.js'","test":"npm run lint && mocha","autofix":"npm run lint -- --fix","precommit":"npm test"},"license":"MIT","prettier":{"trailingComma":"es5"},"_id":"set-cookie-parser@2.7.0","gitHead":"20397fef57c2895388ef99b0f479662a37deb539","bugs":{"url":"https://github.com/nfriedly/set-cookie-parser/issues"},"_nodeVersion":"20.16.0","_npmVersion":"10.8.1","dist":{"integrity":"sha512-lXLOiqpkUumhRdFF3k1osNXCy9akgx/dyPZ5p8qAg9seJzXr5ZrlqZuWIMuY6ejOsVLE6flJ5/h3lsn57fQ/PQ==","shasum":"ef5552b56dc01baae102acb5fc9fb8cd060c30f9","tarball":"http://registry-npm.ywwl.com/set-cookie-parser/download/set-cookie-parser-2.7.0.tgz","fileCount":4,"unpackedSize":15966,"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/set-cookie-parser@2.7.0","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIByXSZxdVcrrO8b5qHbfwhGp43n+bZ3rsCoFPS12PhwjAiEApYS3/szklWl+GpLtzIH7z4b/1ajx0YPm0MKvCmvxlOo="}],"size":5983,"noattachment":false,"key":"/set-cookie-parser/-/set-cookie-parser-2.7.0.tgz"},"_npmUser":{"name":"nfriedly","email":"nathan@nfriedly.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/set-cookie-parser_2.7.0_1722546094339_0.9322553227426882"},"_hasShrinkwrap":false,"readme":"# set-cookie-parser \n\n[![Node.js CI](https://github.com/nfriedly/set-cookie-parser/actions/workflows/node.js.yml/badge.svg)](https://github.com/nfriedly/set-cookie-parser/actions/workflows/node.js.yml)\n[![NPM version][npm-image]][npm-url] \n[![npm downloads](https://img.shields.io/npm/dm/set-cookie-parser)][npm-url]\n\nParses set-cookie headers into objects\n\nAccepts a single `set-cookie` header value, an array of `set-cookie` header values, a Node.js response object, or a `fetch()` `Response` object that may have 0 or more `set-cookie` headers.\n\nAlso accepts an optional options object. Defaults:\n\n```js\n{\n    decodeValues: true,  // Calls decodeURIComponent on each value - default: true\n    map: false,          // Return an object instead of an array - default: false\n    silent: false,       // Suppress the warning that is logged when called on a request instead of a response - default: false\n}\n```\n\nReturns either an array of cookie objects or a map of name => cookie object with `{map: true}`. Each cookie object will have, at a minimum `name` and `value` properties, and may have additional properties depending on the set-cookie header:\n\n* `name` - cookie name (string)\n* `value` - cookie value (string)\n* `path` - URL path to limit the scope to (string or undefined)\n* `domain` - domain to expand the scope to (string or undefined, may begin with \".\" to indicate the named domain or any subdomain of it)\n* `expires` - absolute expiration date for the cookie (Date object or undefined)\n* `maxAge` - relative expiration time of the cookie in seconds from when the client receives it (integer or undefined)\n  * Note: when using with [express's res.cookie() method](http://expressjs.com/en/4x/api.html#res.cookie), multiply `maxAge` by 1000 to convert to milliseconds.\n* `secure` - indicates cookie should only be sent over HTTPs (true or undefined)\n* `httpOnly` - indicates cookie should *not* be accessible to client-side JavaScript (true or undefined)\n* `sameSite` - indicates if cookie should be included in cross-site requests ([more info](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#samesitesamesite-value)) (string or undefined)\n  * Note: valid values are `\"Strict\"`, `\"Lax\"`, and `\"None\"`, but set-cookie-parser coppies the value verbatim and does *not* perform any validation.\n* `partitioned` - indicates cookie should be scoped to the combination of 3rd party domain + top page domain ([more info](https://developer.mozilla.org/en-US/docs/Web/Privacy/Privacy_sandbox/Partitioned_cookies)) (true or undefined)\n\n(The output format is loosely based on the input format of https://www.npmjs.com/package/cookie)\n\n## Install\n\n```sh\n$ npm install --save set-cookie-parser\n```\n\n\n## Usage\n\n### Get array of cookie objects\n\n```js\nvar http = require('http');\nvar setCookie = require('set-cookie-parser');\n\nhttp.get('http://example.com', function(res) {\n  var cookies = setCookie.parse(res, {\n    decodeValues: true  // default: true\n  });\n\n  cookies.forEach(console.log);\n}\n```\n\nExample output:\n\n```js\n[\n    {\n        name: 'bam',\n        value: 'baz'\n    },\n    {\n        name: 'foo',\n        value: 'bar',\n        path: '/',\n        expires: new Date('Tue Jul 01 2025 06:01:11 GMT-0400 (EDT)'),\n        maxAge: 1000,\n        domain: '.example.com',\n        secure: true,\n        httpOnly: true,\n        sameSite: 'lax'\n    }\n]\n```\n\n### Get map of cookie objects\n\n```js\nvar http = require('http');\nvar setCookie = require('set-cookie-parser');\n\nhttp.get('http://example.com', function(res) {\n  var cookies = setCookie.parse(res, {\n    decodeValues: true,  // default: true\n    map: true            // default: false\n  });\n\n  var desiredCookie = cookies['session'];\n  console.log(desiredCookie);\n});\n```\nExample output:\n```js\n{\n    bam: {\n        name: 'bam',\n        value: 'baz'\n    },\n    foo: {\n        name: 'foo',\n        value: 'bar',\n        path: '/',\n        expires: new Date('Tue Jul 01 2025 06:01:11 GMT-0400 (EDT)'),\n        maxAge: 1000,\n        domain: '.example.com',\n        secure: true,\n        httpOnly: true,\n        sameSite: 'lax'\n    }\n}\n```\n\n### Creating a new, modified set-cookie header\n\nThis library can be used in conjunction with the [cookie](https://www.npmjs.com/package/cookie) library to modify and replace set-cookie headers:\n\n```js\nconst libCookie = require('cookie');\nconst setCookie = require('set-cookie-parser');\n\nfunction modifySetCookie(res){\n  // parse the set-cookie headers with this library\n  let cookies = setCookie.parse(res);\n  \n  // modify the cookies here\n  // ...\n  \n  // create new set-cookie headers using the cookie library\n  res.headers['set-cookie'] = cookies.map(function(cookie) {\n      return libCookie.serialize(cookie.name, cookie.value, cookie);\n  });\n}\n```\n\nSee a real-world example of this in [unblocker](https://github.com/nfriedly/node-unblocker/blob/08a89ec27274b46dcd80d0a324a59406f2bdad3d/lib/cookies.js#L67-L85)\n\n## Usage in React Native (and with some other fetch implementations)\n\nReact Native follows the Fetch spec more closely and combines all of the Set-Cookie header values into a single string.\nThe `splitCookiesString` method reverses this.\n\n```js\nvar setCookie = require('set-cookie-parser');\n\nvar response = fetch(/*...*/);\n\n// This is mainly for React Native; Node.js does not combine set-cookie headers.\nvar combinedCookieHeader = response.headers.get('Set-Cookie');\nvar splitCookieHeaders = setCookie.splitCookiesString(combinedCookieHeader)\nvar cookies = setCookie.parse(splitCookieHeaders);\n\nconsole.log(cookies); // should be an array of cookies\n```\n\nThis behavior may become a default part of parse in the next major release, but requires the extra step for now.\n\nNote that the `fetch()` spec now includes a `getSetCookie()` method that provides un-combined `Set-Cookie` headers. This library will automatically use that method if it is present.\n\n## API\n\n### parse(input, [options])\n\nParses cookies from a string, array of strings, or a http response object. \nAlways returns an array, regardless of input format. (Unless the `map` option is set, in which case it always returns an object.)\n\n### parseString(individualSetCookieHeader, [options])\n\nParses a single set-cookie header value string. Options default is `{decodeValues: true}`. Used under-the-hood by `parse()`. \nReturns an object.\n\n### splitCookiesString(combinedSetCookieHeader)\n\nIt's uncommon, but the HTTP spec does allow for multiple of the same header to have their values combined (comma-separated) into a single header. \nThis method splits apart a combined header without choking on commas that appear within a cookie's value (or expiration date).\nReturns an array of strings that may be passed to `parse()`.\n\n## References\n\n* [RFC 6265: HTTP State Management Mechanism](https://tools.ietf.org/html/rfc6265)\n* [draft-ietf-httpbis-rfc6265bis-10](https://httpwg.org/http-extensions/draft-ietf-httpbis-rfc6265bis.html)\n\n## License\n\nMIT © [Nathan Friedly](http://www.nfriedly.com/)\n\n\n[npm-image]: https://badge.fury.io/js/set-cookie-parser.svg\n[npm-url]: https://npmjs.org/package/set-cookie-parser\n","publish_time":1722546094476,"maintainers":[{"name":"nfriedly","email":"nathan@nfriedly.com"}],"_cnpm_publish_time":1722546094476,"dist-tags":{"backports":"2.7.3","latest":"4.0.0"}}