{"id":24285089,"url":"https://github.com/bluzzi/cookie-muncher","last_synced_at":"2025-08-14T18:34:39.038Z","repository":{"id":154026444,"uuid":"631449993","full_name":"Bluzzi/cookie-muncher","owner":"Bluzzi","description":"Effortless cookie management for server and browser 🍪","archived":false,"fork":false,"pushed_at":"2024-12-25T20:18:57.000Z","size":156,"stargazers_count":17,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-08T21:43:20.080Z","etag":null,"topics":["browser","cookie","http","parser","rfc6265","serializer"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/cookie-muncher","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Bluzzi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2023-04-23T03:55:02.000Z","updated_at":"2024-12-25T20:19:00.000Z","dependencies_parsed_at":null,"dependency_job_id":"e1381e56-3fc8-4db4-915b-c51f384b8b19","html_url":"https://github.com/Bluzzi/cookie-muncher","commit_stats":null,"previous_names":["we-use/cookie","bluzzi/cookie-muncher"],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Bluzzi%2Fcookie-muncher","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Bluzzi%2Fcookie-muncher/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Bluzzi%2Fcookie-muncher/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Bluzzi%2Fcookie-muncher/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Bluzzi","download_url":"https://codeload.github.com/Bluzzi/cookie-muncher/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234158527,"owners_count":18788583,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["browser","cookie","http","parser","rfc6265","serializer"],"created_at":"2025-01-16T06:19:25.157Z","updated_at":"2025-01-16T06:19:25.698Z","avatar_url":"https://github.com/Bluzzi.png","language":"TypeScript","readme":"# Cookie Muncher\nA lightweight and typesafe package for manipulating cookies in NodeJS and the browser.\n\n- 🚀 Lightweight\n- 🌏 Works in all browsers\n- 🧪 Unit tested  \n- 🔷 Typesafe\n- 📦 Support ESM \u0026 CJS  \n- ✅ [RFC 6265](https://datatracker.ietf.org/doc/html/rfc6265) compliant\n- 📖 Well documented\n\n## Installation\nYou can install Cookie Muncher via NPM, Yarn or PNPM.\n\n```sh\nnpm install cookie-muncher\n```\n\n```sh\nyarn add cookie-muncher\n```\n\n```sh\npnpm install cookie-muncher\n```\n\n## Usage\nThis package has two separate modules:\n- `httpCookie`: serialize and parse cookie from HTTP headers (`Cookie`, `Set-Cookie`)\n- `domCookie`: manage cookies from browser DOM (set, remove, get)\n\n### `httpCookie.serialize(cookie: Cookie, options?: HttpCookieOptions): string`\nSerialize a cookie into a HTTP `Set-Cookie` header string.\n\n```ts\nimport type { Cookie, HttpCookieOptions } from \"cookie-muncher\";\nimport { httpCookie, CookieMaxAge } from \"cookie-muncher\";\n\nconst cookie: Cookie = {\n  name: \"foo\",\n  value: \"bar\",\n};\n\nconst options: HttpCookieOptions = {\n  maxAge: CookieMaxAge.TwoWeeks,\n  secure: true,\n  sameSite: \"strict\",\n};\n\nconsole.log(httpCookie.serialize(cookie, options));\n// Output: \"foo=bar; Max-Age=3600; Path=/; Secure; SameSite=Strict\"\n```\n\n### `httpCookie.parse(cookies: string): Cookie[]`\nParse a HTTP `Cookie` header string of cookies into an array of cookie objects.\n\n```ts\nimport { httpCookie } from \"cookie-muncher\";\n\nconst cookies = \"foo=bar; equation=E%3Dmc%5E2\";\n\nconsole.log(httpCookie.parse(cookies));\n// Output: [{ name: \"foo\", value: \"bar\" }, { name: \"equation\", value: \"E=mc^2\" }]\n```\n\n### `domCookie.set(cookie: Cookie, options?: DomCookieOptions): void`\nCreate an edit cookie. \n\nTo be able to edit a cookie, you must define the same `Domain` and `Path` as the cookie.\n\nYou cannot create or edit `HttpOnly` cookies.\n\nYou may not create more than 50 cookies for a single `Domain` and each cookie must not exceed 4096 bytes. If it does, an error will be thrown by this function.\n\n```ts\nimport { domCookie } from \"cookie-muncher\";\n\ndomCookie.set({ name: \"foo\", value: \"bar\" });\ndomCookie.set({ name: \"bar\", value: \"foo\" }, { path: \"/bar\" });\n\n```\n\n### `domCookie.get(name: string): Cookie | null`\nGet the value of a cookie. \n\nYou cannot get the value of an `HttpOnly` cookie. \n\nMake sure the `Path` of the cookie is accessible in the current context.\n\n```ts\nimport { domCookie } from \"cookie-muncher\";\n\nconsole.log(domCookie.get(\"foo\")); \n// Ouput: { name: \"foo\", value: \"bar\" }\n```\n\n### `domCookie.getAll(): Cookie[]`\nGet all existing cookies. With the same limitations as the `domCookie.get(name: string)` function.\n\n```ts\nimport { domCookie } from \"cookie-muncher\";\n\nconsole.log(domCookie.getAll());\n// Output: [{ name: \"foo\", value: \"bar\" }, { name: \"bar\", value: \"foo\" }]\n```\n\n### `domCookie.remove(name: string, options?: DomCookieOptions): void`\nDelete a cookie, make sure to set the same `Domain` and `Path` of the cookie you wish to delete.\n\n```ts\nimport { domCookie } from \"cookie-muncher\";\n\ndomCookie.remove(\"foo\", { path: \"/bar\" });\n```\n\n### `HttpCookieOptions` \u0026 `DomCookieOptions`\n```ts\nimport type { HttpCookieOptions, DomCookieOptions } from \"cookie-muncher\";\n```\n\n#### `domain`\nIndicates the input for the [Domain Set-Cookie attribute](https://datatracker.ietf.org/doc/html/rfc6265#section-5.2.3). The cookie is typically applied to only the current domain when no domain is set by default, and this is recognized by most clients.\n\n#### `expires`\nSpecifies the Date object to be used as the value for the [Expires Set-Cookie attribute](https://datatracker.ietf.org/doc/html/rfc6265#section-5.2.1). By default, the cookie has no expiration, which is recognized by most clients as a \"non-persistent cookie\" that gets deleted upon a certain condition, such as closing the web browser application.\n\n#### `maxAge`\nSpecifies the `number` (in seconds) to be used as the value for the [Max-Age Set-Cookie attribute](https://datatracker.ietf.org/doc/html/rfc6265#section-5.2.2). The given number will be rounded down to an integer. By default, the cookie has no maximum age.\n\nAccording to the [cookie storage model specification](https://datatracker.ietf.org/doc/html/rfc6265#section-5.3), if both `expires` and `maxAge` are set, then `maxAge` takes precedence. However, it is possible that not all clients will obey this rule, so if both are set, they should point to the same date and time to ensure proper functionality.\n\n#### `httpOnly`\nSpecifies the `boolean` value to be used for the [HttpOnly Set-Cookie attribute](https://datatracker.ietf.org/doc/html/rfc6265#section-5.2.6). The `HttpOnly` attribute is set if the value is truthy; otherwise, it is not set. By default, the `HttpOnly` attribute is not set.\n\nIt's important to exercise caution when setting this attribute to `true` because compliant clients will restrict client-side JavaScript from accessing the cookie via `document.cookie`.\n\nIt's worth noting that `HttpOnly` cookies are inaccessible to client-side JavaScript, which also means that you cannot create an `HttpOnly` cookie using client-side JavaScript.\n\n\u003e **Note**\n\u003e This parameter is disabled on the `DomCookieOptions` type.\n\n#### `path`\nIndicates the input for the [Path Set-Cookie attribute](https://datatracker.ietf.org/doc/html/rfc6265#section-5.2.4). By default, the path is set to the [\"default-path\"](https://datatracker.ietf.org/doc/html/rfc6265#section-5.1.4).\n\n#### `sameSite`\nIndicates the input for the [SameSite Set-Cookie attribute](tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-09#section-5.4.7) :\n- lax: sets the `SameSite` attribute to `Lax` for lax same-site enforcement\n- none: sets the `SameSite` attribute to `None` for explicit cross-site cookies\n- strict: sets the `SameSite` attribute to `Strict` for strict same-site enforcement\n\nFor more information about the different enforcement levels, refer to [the specification](https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-09#section-5.4.7).\n\nIt's important to note that the SameSite attribute is not yet fully standardized and may change in the future. As a result, many clients may ignore this attribute until they understand it.\n\n#### `secure`\nSpecifies the `boolean` value for the [Secure Set-Cookie attribute](https://datatracker.ietf.org/doc/html/rfc6265#section-5.2.5). When truthy, the `Secure` attribute is set, otherwise it is not. By default, the `Secure` attribute is not set.\n\nBe careful when setting this to `true`, as compliant clients will not send the cookie back to the server in the future if the browser does not have an HTTPS connection.\n\n\nThis method specifies the `boolean` value for the [Secure Set-Cookie attribute](https://datatracker.ietf.org/doc/html/rfc6265#section-5.2.5). Setting it to `true` will enable the `Secure` attribute, which is not set by default. It's important to note that if you set this to `true`, the cookie will only be sent back to the server in the future if the browser has an HTTPS connection. Therefore, you should be careful when using this attribute to ensure that your application works as intended in all scenarios.\n\n## License\nThis package is MIT licensed.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbluzzi%2Fcookie-muncher","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbluzzi%2Fcookie-muncher","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbluzzi%2Fcookie-muncher/lists"}