UNPKG

5.18 kBMarkdownView Raw
1# Cloudflare Storage Area
2
3An implementation of the StorageArea ([1],[2],[3]) interface using [Cloudflare Worker's KV](https://developers.cloudflare.com/workers/runtime-apis/kv)
4storage as a backing store.
5
6The goal of this class is ease of use and compatibility with other Storage Area implementations,
7such as [`kv-storage-polyfill`](https://github.com/GoogleChromeLabs/kv-storage-polyfill).
8
9While work on [the specification](https://wicg.github.io/kv-storage/) itself has stopped,
10it's still a good interface for asynchronous data access that feels native to JavaScript.
11
12## Usage
13
14``` js
15import { StorageArea } from '@worker-tools/cloudflare-kv-storage';
16const storage = new StorageArea('foobar');
17```
18
19You can now write cross-platform, cross-worker-env code:
20
21```js
22async 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
28Note 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.
29If the underlying implementation isn't a ` CloudflareStorageArea`, the setting simply won't have any effect.
30
31## Prerequisites
32In your `wrangler.toml`, make sure to provide a kv namespace binding and a default namespace for the this implementation.
33
34```toml
35kv_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
49Beyond 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
58Note that efficiency is not a goal. Specifically, if you have sizable `ArrayBuffer`s,
59it'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
72If 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
83Worker 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
105Fore more visit [workers.tools](https://workers.tools).
\No newline at end of file