1 | # Request Cookie Store
|
2 | An implementation of the [Cookie Store API](https://wicg.github.io/cookie-store) for request handlers.
|
3 |
|
4 | It uses the `Cookie` header of a request to populate the store and
|
5 | keeps a record of changes that can be exported as a list of `Set-Cookie` headers.
|
6 |
|
7 | It is intended as a cookie middleware for Cloudflare Workers or other [Worker Runtimes][wks], but perhaps there are other uses as well.
|
8 | It 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).
|
9 |
|
10 | ## Recipes
|
11 | The following snippets should convey how this is intended to be used.
|
12 | Aso see [the interface](./src/interface.ts) for more usage options.
|
13 |
|
14 |
|
15 | ### Creating a New Store
|
16 | ```ts
|
17 | import { RequestCookieStore } from '@worker-tools/request-cookie-store';
|
18 |
|
19 | // Creating a request on the fly. Typically it will be provided by CF Workers, etc.
|
20 | const request = new Request('/', { headers: { 'cookie': 'foo=bar; fizz=buzz' } });
|
21 |
|
22 | const cookieStore = new RequestCookieStore(request);
|
23 | ```
|
24 |
|
25 | We can now access cookie values from the store like so:
|
26 |
|
27 | ```ts
|
28 | const value = (await cookieStore.get(name))?.value;
|
29 | ```
|
30 |
|
31 | This is a bit verbose, so we'll make it more ergonomic in the next step.
|
32 |
|
33 | ### Fast Read Access
|
34 | To avoid using `await` for every read, we can parse all cookies into a `Map` once:
|
35 |
|
36 | ```ts
|
37 | type Cookies = ReadonlyMap<string, string>;
|
38 |
|
39 | const all = await cookieStore.getAll();
|
40 |
|
41 | new Map(all.map(({ name, value }) => [name, value])) as Cookies;
|
42 | // => Map { "foo" => "bar", "fizz" => "buzz" }
|
43 | ```
|
44 |
|
45 | ### Exporting Headers
|
46 | Use `set` on the cookie store to add cookies and include them in a response.
|
47 | ```ts
|
48 | await cookieStore.set('foo', 'buzz');
|
49 | await cookieStore.set('fizz', 'bar');
|
50 |
|
51 | event.respondWith(new Response(null, cookieStore));
|
52 | ```
|
53 |
|
54 | Will produce the following HTTP headers in Worker Runtimes that support multiple `Set-Cookie` headers:
|
55 |
|
56 | ```http
|
57 | HTTP/1.1 200 OK
|
58 | content-length: 0
|
59 | set-cookie: foo=buzz
|
60 | set-cookie: fizz=bar
|
61 | ```
|
62 |
|
63 |
|
64 | However, 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. -->
|
65 |
|
66 |
|
67 | ### Combine With Other Headers
|
68 | The above example above uses the fact that the cookie store will correctly destructure the `headers` key.
|
69 | To add additional headers to a response, you can do the following:
|
70 |
|
71 | ```ts
|
72 | const response = new Response('{}', {
|
73 | headers: [
|
74 | ['content-type': 'application/json'],
|
75 | ...cookieStore.headers,
|
76 | ],
|
77 | });
|
78 | ```
|
79 |
|
80 | [1]: https://fetch.spec.whatwg.org/#headers-class
|
81 |
|
82 | ## Disclaimers
|
83 | _This is not a polyfill! It is intended as a cookie middleware for Cloudflare Workers or other [Worker Runtimes][wks]!_
|
84 |
|
85 | [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.
|
86 |
|
87 | [wks]: https://workers.js.org/
|
88 |
|
89 | <br/>
|
90 |
|
91 | --------
|
92 |
|
93 | <br/>
|
94 |
|
95 | <p align="center"><a href="https://workers.tools"><img src="https://workers.tools/assets/img/logo.svg" width="100" height="100" /></a>
|
96 | <p align="center">This module is part of the Worker Tools collection<br/>β
|
97 |
|
98 | [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.
|
99 |
|
100 | If you liked this module, you might also like:
|
101 |
|
102 | - π§ [__Worker Router__][router] --- Complete routing solution that works across CF Workers, Deno and Service Workers
|
103 | - π [__Worker Middleware__][middleware] --- A suite of standalone HTTP server-side middleware with TypeScript support
|
104 | - π [__Worker HTML__][html] --- HTML templating and streaming response library
|
105 | - π¦ [__Storage Area__][kv-storage] --- Key-value store abstraction across [Cloudflare KV][cloudflare-kv-storage], [Deno][deno-kv-storage] and browsers.
|
106 | - π [__Response Creators__][response-creators] --- Factory functions for responses with pre-filled status and status text
|
107 | - π [__Stream Response__][stream-response] --- Use async generators to build streaming responses for SSE, etc...
|
108 | - π₯ [__JSON Fetch__][json-fetch] --- Drop-in replacements for Fetch API classes with first class support for JSON.
|
109 | - π¦ [__JSON Stream__][json-stream] --- Streaming JSON parser/stingifier with first class support for web streams.
|
110 |
|
111 | Worker Tools also includes a number of polyfills that help bridge the gap between Worker Runtimes:
|
112 | - βοΈ [__HTML Rewriter__][html-rewriter] --- Cloudflare's HTML Rewriter for use in Deno, browsers, etc...
|
113 | - π [__Location Polyfill__][location-polyfill] --- A `Location` polyfill for Cloudflare Workers.
|
114 | - π¦ [__Deno Fetch Event Adapter__][deno-fetch-event-adapter] --- Dispatches global `fetch` events using Denoβs native HTTP server.
|
115 |
|
116 | [router]: https://workers.tools/router
|
117 | [middleware]: https://workers.tools/middleware
|
118 | [html]: https://workers.tools/html
|
119 | [kv-storage]: https://workers.tools/kv-storage
|
120 | [cloudflare-kv-storage]: https://workers.tools/cloudflare-kv-storage
|
121 | [deno-kv-storage]: https://workers.tools/deno-kv-storage
|
122 | [kv-storage-polyfill]: https://workers.tools/kv-storage-polyfill
|
123 | [response-creators]: https://workers.tools/response-creators
|
124 | [stream-response]: https://workers.tools/stream-response
|
125 | [json-fetch]: https://workers.tools/json-fetch
|
126 | [json-stream]: https://workers.tools/json-stream
|
127 | [request-cookie-store]: https://workers.tools/request-cookie-store
|
128 | [extendable-promise]: https://workers.tools/extendable-promise
|
129 | [html-rewriter]: https://workers.tools/html-rewriter
|
130 | [location-polyfill]: https://workers.tools/location-polyfill
|
131 | [deno-fetch-event-adapter]: https://workers.tools/deno-fetch-event-adapter
|
132 |
|
133 | Fore more visit [workers.tools](https://workers.tools). |
\ | No newline at end of file |