Skip to content

Commit 78348e4

Browse files
authored
feat: Remaining KV Functions, File-System KV Store, Cache API Stubs, Workers Sites Support (#57)
* feat(KVNamespace): bring kv implementation more in line with docs This commit adds value types to get and put, the getWithMetadata and list functions, and returns null if a key could not be found. This brings the KV implementation more in line with the documentation here: https://developers.cloudflare.com/workers/runtime-apis/kv. The only things missing now are ReadableStream & FormData support, and potentially errors when data is too large. * test(KVNamespace): add tests for new kv implementation * fix(start): empty string kv namespace added to context * feat(FileKVStore): add file system backed kv store This commit adds an implementation of a KV store that uses the file system to store data. This is required for Workers Sites, but is also a simpler way (compared to Minio) of persisting local KV data between restarts. * test(FileKVStore): add tests for file system backed kv store * feat(caches): add caches api stubs to worker context Workers Sites required the caches API to be in scope, but they don't actually need to cache any data to function. So this commit stubs out the API and puts it in the worker context. * feat(Workers Sites): add Workers Sites support If the passed wrangler.toml file has a [site] section with a bucket field, the __STATIC_CONTENT KV namespace and __STATIC_CONTENT_MANIFEST object will be injected into the worker context, allowing the Worker Site to be served. * feat(FileKVStore): enable file system backed kv store by env This allows the file system backed KV store to be used for all KV namespaces, not just for Workers Sites. Setting the "KV_FILE_ROOT" environment variable enables this. For namespace "NAMESPACE", files will be stored in "KV_FILE_ROOT/NAMESPACE". Values will be stored in files named the same as their key. Metadata and expiration information (if provided) is stored in a .meta.json file prefixed with the key. * docs(README): add file system kv store and workers sites to README * docs(README): add note add importing kv asset handler
1 parent ce60044 commit 78348e4

File tree

12 files changed

+1302
-34
lines changed

12 files changed

+1302
-34
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ $ nodemon --watch /path/to/worker.js --signal SIGHUP --exec 'cloudflare-worker-l
3434
* Cloudflare key value store if you pass in the KV_NAMESPACE environment variable
3535
* Cloudflare [event.passThroughOnException()](https://workers.cloudflare.com/docs/reference/workers-concepts/fetch-event-lifecycle/#passthroughonexception) for runtime exception handling
3636
* Cloudflare Environment Variables and Secrets loaded from a wrangler.toml
37+
* Workers Sites
3738
* ... this list should probably have more things
3839

3940
## Contributors
@@ -42,6 +43,7 @@ $ nodemon --watch /path/to/worker.js --signal SIGHUP --exec 'cloudflare-worker-l
4243
* Jeremy Danyow (@jdanyow)
4344
* Rita Liu (@rita-liu)
4445
* Nolan Woods (@innovate-invent)
46+
* Brendan Coll (@mrbbot)
4547

4648
## Future enhancements
4749

@@ -68,6 +70,11 @@ MINIO_PORT, MINIO_USE_SSL, MINIO_REGION, MINIO_TRANSPORT, MINIO_SESSIONTOKEN, an
6870

6971
See [the Minio documentation](https://docs.min.io/docs/javascript-client-api-reference.html) for details on the various parameters.
7072

73+
## CloudFlare KV Store emulation using the File System
74+
75+
To use the File System as the KV store simply provide the KV_FILE_ROOT option as an environment
76+
variable. A directory will be created in here for each KV namespace.
77+
7178
## CloudFlare Environment Variables and Secrets
7279

7380
Support for CloudFlare Environment Variables and Secrets is provided via a wrangler.toml file.
@@ -96,3 +103,11 @@ Two features are provided while loading the wrangler.toml:
96103

97104
Additionally, any 'kv-namespaces' in the wrangler.toml will be appended to the list of namespaces
98105
provided by KV_NAMESPACES.
106+
107+
## Workers Sites
108+
109+
If a wrangler.toml file containing a `[site]` section with a `bucket` directory is loaded, the Workers Sites
110+
default KV namespace and manifest will be added to the worker's scope. Calls to `getAssetFromKV` will always
111+
return the latest version of an asset in the `bucket` directory. Note that you'll need to bundle your worker
112+
code (with Webpack for instance) before running it to use `@cloudflare/kv-asset-handler`, as `import`/
113+
`require` are not in workers' scopes.

0 commit comments

Comments
 (0)