{"id":13688529,"url":"https://github.com/worker-tools/request-cookie-store","last_synced_at":"2025-04-14T15:05:26.989Z","repository":{"id":62422704,"uuid":"325918028","full_name":"worker-tools/request-cookie-store","owner":"worker-tools","description":"An implementation of the Cookie Store API for request handlers.","archived":false,"fork":false,"pushed_at":"2024-02-20T21:17:51.000Z","size":150,"stargazers_count":8,"open_issues_count":1,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-05-09T17:04:05.481Z","etag":null,"topics":["cloudflare","cloudflare-workers","cookie-middleware","cookie-store","cookies","deno","middleware","standards-adherence"],"latest_commit_sha":null,"homepage":"https://workers.tools/request-cookie-store","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/worker-tools.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","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,"publiccode":null,"codemeta":null}},"created_at":"2021-01-01T04:44:44.000Z","updated_at":"2024-06-17T09:08:17.164Z","dependencies_parsed_at":"2024-06-17T09:08:16.500Z","dependency_job_id":"d120eedb-be82-4343-a044-0a7215c3342e","html_url":"https://github.com/worker-tools/request-cookie-store","commit_stats":null,"previous_names":[],"tags_count":26,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/worker-tools%2Frequest-cookie-store","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/worker-tools%2Frequest-cookie-store/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/worker-tools%2Frequest-cookie-store/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/worker-tools%2Frequest-cookie-store/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/worker-tools","download_url":"https://codeload.github.com/worker-tools/request-cookie-store/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224874808,"owners_count":17384337,"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":["cloudflare","cloudflare-workers","cookie-middleware","cookie-store","cookies","deno","middleware","standards-adherence"],"created_at":"2024-08-02T15:01:16.013Z","updated_at":"2024-11-16T04:22:09.904Z","avatar_url":"https://github.com/worker-tools.png","language":"TypeScript","funding_links":[],"categories":["deno"],"sub_categories":[],"readme":"# Request Cookie Store\nAn implementation of the [Cookie Store API](https://wicg.github.io/cookie-store) for request handlers. \n\nIt uses the `Cookie` header of a request to populate the store and\nkeeps a record of changes that can be exported as a list of `Set-Cookie` headers.\n\nIt is intended as a cookie middleware for Cloudflare Workers or other [Worker Runtimes][wks], but perhaps there are other uses as well.\nIt is best combined with [**Signed Cookie Store**](https://github.com/worker-tools/signed-cookie-store) or [**Encrypted Cookie Store**](https://github.com/worker-tools/encrypted-cookie-store).\n\n## Recipes \nThe following snippets should convey how this is intended to be used.\nAso see [the interface](./src/interface.ts) for more usage options.\n\n\n### Creating a New Store\n```ts\nimport { RequestCookieStore } from '@worker-tools/request-cookie-store';\n\n// Creating a request on the fly. Typically it will be provided by CF Workers, etc.\nconst request = new Request('/', { headers: { 'cookie': 'foo=bar; fizz=buzz' } });\n\nconst cookieStore = new RequestCookieStore(request);\n```\n\nWe can now access cookie values from the store like so:\n\n```ts\nconst value = (await cookieStore.get(name))?.value;\n```\n\nThis is a bit verbose, so we'll make it more ergonomic in the next step.\n\n### Fast Read Access\nTo avoid using `await` for every read, we can parse all cookies into a `Map` once:\n\n```ts\ntype Cookies = ReadonlyMap\u003cstring, string\u003e;\n\nconst all = await cookieStore.getAll();\n\nnew Map(all.map(({ name, value }) =\u003e [name, value])) as Cookies;\n// =\u003e Map { \"foo\" =\u003e \"bar\", \"fizz\" =\u003e \"buzz\" }\n```\n\n### Exporting Headers \nUse `set` on the cookie store to add cookies and include them in a response.\n```ts\nawait cookieStore.set('foo', 'buzz');\nawait cookieStore.set('fizz', 'bar');\n\nevent.respondWith(new Response(null, cookieStore));\n```\n\nWill produce the following HTTP headers in Worker Runtimes that support multiple `Set-Cookie` headers:\n\n```http\nHTTP/1.1 200 OK\ncontent-length: 0\nset-cookie: foo=buzz\nset-cookie: fizz=bar\n```\n\n\u003c!-- Note that [due to the weirdness][1] of the `Headers` class, inspecting the response in JS will not produce the intended result (`set-cookie` headers will appear concatenated). \nHowever, Worker Runtimes such as Cloudflare Workers will put multiple headers on the network when provided a \"[header list](https://fetch.spec.whatwg.org/#concept-header-list)\", i.e. an array of tuples. --\u003e\n\n\n### Combine With Other Headers\nThe above example above uses the fact that the cookie store will correctly destructure the `headers` key. \nTo add additional headers to a response, you can do the following:\n\n```ts\nconst response = new Response('{}', {\n  headers: [\n    ['content-type': 'application/json'],\n    ...cookieStore.headers,\n  ],\n});\n```\n\n[1]: https://fetch.spec.whatwg.org/#headers-class\n\n## Disclaimers\n_This is not a polyfill! It is intended as a cookie middleware for Cloudflare Workers or other [Worker Runtimes][wks]!_\n\n[Due to the weirdness][1] of the Fetch API `Headers` class w.r.t `Set-Cookie` (or rather, the lack of special treatment), it is not likely to work in a Service Worker.\n\n[wks]: https://workers.js.org/\n\n\u003cbr/\u003e\n\n--------\n\n\u003cbr/\u003e\n\n\u003cp align=\"center\"\u003e\u003ca href=\"https://workers.tools\"\u003e\u003cimg src=\"https://workers.tools/assets/img/logo.svg\" width=\"100\" height=\"100\" /\u003e\u003c/a\u003e\n\u003cp align=\"center\"\u003eThis module is part of the Worker Tools collection\u003cbr/\u003e⁕\n\n[Worker Tools](https://workers.tools) are a collection of TypeScript libraries for writing web servers in [Worker Runtimes](https://workers.js.org) such as Cloudflare Workers, Deno Deploy and Service Workers in the browser. \n\nIf you liked this module, you might also like:\n\n- 🧭 [__Worker Router__][router] --- Complete routing solution that works across CF Workers, Deno and Service Workers\n- 🔋 [__Worker Middleware__][middleware] --- A suite of standalone HTTP server-side middleware with TypeScript support\n- 📄 [__Worker HTML__][html] --- HTML templating and streaming response library\n- 📦 [__Storage Area__][kv-storage] --- Key-value store abstraction across [Cloudflare KV][cloudflare-kv-storage], [Deno][deno-kv-storage] and browsers.\n- 🆗 [__Response Creators__][response-creators] --- Factory functions for responses with pre-filled status and status text\n- 🎏 [__Stream Response__][stream-response] --- Use async generators to build streaming responses for SSE, etc...\n- 🥏 [__JSON Fetch__][json-fetch] --- Drop-in replacements for Fetch API classes with first class support for JSON.\n- 🦑 [__JSON Stream__][json-stream] --- Streaming JSON parser/stingifier with first class support for web streams.\n\nWorker Tools also includes a number of polyfills that help bridge the gap between Worker Runtimes:\n- ✏️ [__HTML Rewriter__][html-rewriter] --- Cloudflare's HTML Rewriter for use in Deno, browsers, etc...\n- 📍 [__Location Polyfill__][location-polyfill] --- A `Location` polyfill for Cloudflare Workers.\n- 🦕 [__Deno Fetch Event Adapter__][deno-fetch-event-adapter] --- Dispatches global `fetch` events using Deno’s native HTTP server.\n\n[router]: https://workers.tools/router\n[middleware]: https://workers.tools/middleware\n[html]: https://workers.tools/html\n[kv-storage]: https://workers.tools/kv-storage\n[cloudflare-kv-storage]: https://workers.tools/cloudflare-kv-storage\n[deno-kv-storage]: https://workers.tools/deno-kv-storage\n[kv-storage-polyfill]: https://workers.tools/kv-storage-polyfill\n[response-creators]: https://workers.tools/response-creators\n[stream-response]: https://workers.tools/stream-response\n[json-fetch]: https://workers.tools/json-fetch\n[json-stream]: https://workers.tools/json-stream\n[request-cookie-store]: https://workers.tools/request-cookie-store\n[extendable-promise]: https://workers.tools/extendable-promise\n[html-rewriter]: https://workers.tools/html-rewriter\n[location-polyfill]: https://workers.tools/location-polyfill\n[deno-fetch-event-adapter]: https://workers.tools/deno-fetch-event-adapter\n\nFore more visit [workers.tools](https://workers.tools).","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fworker-tools%2Frequest-cookie-store","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fworker-tools%2Frequest-cookie-store","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fworker-tools%2Frequest-cookie-store/lists"}