1 | # Cloudflare Storage Area
|
2 |
|
3 | An implementation of the StorageArea ([1],[2],[3]) interface using [Cloudflare Worker's KV](https://developers.cloudflare.com/workers/runtime-apis/kv)
|
4 | storage as a backing store.
|
5 |
|
6 | The goal of this class is ease of use and compatibility with other Storage Area implementations,
|
7 | such as [`kv-storage-polyfill`](https://github.com/GoogleChromeLabs/kv-storage-polyfill).
|
8 |
|
9 | While work on [the specification](https://wicg.github.io/kv-storage/) itself has stopped,
|
10 | it's still a good interface for asynchronous data access that feels native to JavaScript.
|
11 |
|
12 | ## Usage
|
13 |
|
14 | ``` js
|
15 | import { StorageArea } from '@worker-tools/cloudflare-kv-storage';
|
16 | const storage = new StorageArea('foobar');
|
17 | ```
|
18 |
|
19 | You can now write cross-platform, cross-worker-env code:
|
20 |
|
21 | ```js
|
22 | async function myFunc(storage) {
|
23 | await storage.set(['foo', 1], ['bar', 2], { expirationTtl: 5 * 60 });
|
24 | await storage.get(['foo', 1]); // => ['bar', 2]
|
25 | }
|
26 | ```
|
27 |
|
28 | Note that some of the underlying features of Cloudflare KV, such as [`expirationTtl`](https://developers.cloudflare.com/workers/runtime-apis/kv#expiring-keys), are still exposed via the optional options parameter.
|
29 | If the underlying implementation isn't a ` CloudflareStorageArea`, the setting simply won't have any effect.
|
30 |
|
31 | ## Prerequisites
|
32 | In your `wrangler.toml`, make sure to provide a kv namespace binding and a default namespace for the this implementation.
|
33 |
|
34 | ```toml
|
35 | kv_namespaces = [
|
36 | { binding = "KV_NAMESPACE", id = "13c...", preview_id = "13c..." }
|
37 | ]
|
38 |
|
39 | [vars]
|
40 | CF_STORAGE_AREA__DEFAULT_KV_NAMESPACE = "KV_NAMESPACE"
|
41 | ```
|
42 |
|
43 | [1]: https://developers.google.com/web/updates/2019/03/kv-storage
|
44 | [2]: https://css-tricks.com/kv-storage/
|
45 | [3]: https://github.com/WICG/kv-storage
|
46 |
|
47 | ## Features
|
48 |
|
49 | Beyond the cross-worker-env aspects of using StorageArea, CloudflareStorageArea provides a number of quality of life improvements over using Cloudflare's KV directly:
|
50 |
|
51 | * Support for multiple storage areas within a single KV binding
|
52 | * Wrapping and Unwrapping of many built-in types, such as `Map` and `Set` (structured clone algorithm)
|
53 | * Support for non-string keys and complex keys
|
54 | * Abstraction over KV pagination when listing keys
|
55 |
|
56 | ## Disclaimers
|
57 |
|
58 | Note that efficiency is not a goal. Specifically, if you have sizable `ArrayBuffer`s,
|
59 | it's much better to use Cloudflare's KV directly.
|
60 |
|
61 | <br/>
|
62 |
|
63 | --------
|
64 |
|
65 | <br/>
|
66 |
|
67 | <p align="center"><a href="https://workers.tools"><img src="https://workers.tools/assets/img/logo.svg" width="100" height="100" /></a>
|
68 | <p align="center">This module is part of the Worker Tools collection<br/>β
|
69 |
|
70 | [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.
|
71 |
|
72 | If you liked this module, you might also like:
|
73 |
|
74 | - π§ [__Worker Router__][router] --- Complete routing solution that works across CF Workers, Deno and Service Workers
|
75 | - π [__Worker Middleware__][middleware] --- A suite of standalone HTTP server-side middleware with TypeScript support
|
76 | - π [__Worker HTML__][html] --- HTML templating and streaming response library
|
77 | - π¦ [__Storage Area__][kv-storage] --- Key-value store abstraction across [Cloudflare KV][cloudflare-kv-storage], [Deno][deno-kv-storage] and browsers.
|
78 | - π [__Response Creators__][response-creators] --- Factory functions for responses with pre-filled status and status text
|
79 | - π [__Stream Response__][stream-response] --- Use async generators to build streaming responses for SSE, etc...
|
80 | - π₯ [__JSON Fetch__][json-fetch] --- Drop-in replacements for Fetch API classes with first class support for JSON.
|
81 | - π¦ [__JSON Stream__][json-stream] --- Streaming JSON parser/stingifier with first class support for web streams.
|
82 |
|
83 | Worker Tools also includes a number of polyfills that help bridge the gap between Worker Runtimes:
|
84 | - βοΈ [__HTML Rewriter__][html-rewriter] --- Cloudflare's HTML Rewriter for use in Deno, browsers, etc...
|
85 | - π [__Location Polyfill__][location-polyfill] --- A `Location` polyfill for Cloudflare Workers.
|
86 | - π¦ [__Deno Fetch Event Adapter__][deno-fetch-event-adapter] --- Dispatches global `fetch` events using Denoβs native HTTP server.
|
87 |
|
88 | [router]: https://workers.tools/router
|
89 | [middleware]: https://workers.tools/middleware
|
90 | [html]: https://workers.tools/html
|
91 | [kv-storage]: https://workers.tools/kv-storage
|
92 | [cloudflare-kv-storage]: https://workers.tools/cloudflare-kv-storage
|
93 | [deno-kv-storage]: https://workers.tools/deno-kv-storage
|
94 | [kv-storage-polyfill]: https://workers.tools/kv-storage-polyfill
|
95 | [response-creators]: https://workers.tools/response-creators
|
96 | [stream-response]: https://workers.tools/stream-response
|
97 | [json-fetch]: https://workers.tools/json-fetch
|
98 | [json-stream]: https://workers.tools/json-stream
|
99 | [request-cookie-store]: https://workers.tools/request-cookie-store
|
100 | [extendable-promise]: https://workers.tools/extendable-promise
|
101 | [html-rewriter]: https://workers.tools/html-rewriter
|
102 | [location-polyfill]: https://workers.tools/location-polyfill
|
103 | [deno-fetch-event-adapter]: https://workers.tools/deno-fetch-event-adapter
|
104 |
|
105 | Fore more visit [workers.tools](https://workers.tools). |
\ | No newline at end of file |