@worker-tools/cloudflare-kv-storage
Version:
An implementation of the StorageArea (1,2,3) interface using Cloudflare Worker's KV storage as a backing store.
127 lines • 7.9 kB
JavaScript
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _CloudflareStorageArea_kv, _CloudflareStorageArea_packer, _CloudflareStorageArea_encodeKey, _CloudflareStorageArea_decodeKey, _CloudflareStorageArea_paginationHelper;
import { encodeKey, decodeKey, throwForDisallowedKey } from 'idb-key-to-string';
import { StructuredPacker } from './packer.js';
const OLD_DEFAULT_KV_NAMESPACE_KEY = 'CF_STORAGE_AREA__DEFAULT_KV_NAMESPACE';
const DEFAULT_KV_NAMESPACE_KEY = 'DEFAULT_KV_NAMESPACE';
const DEFAULT_STORAGE_AREA_NAME = 'default';
const DIV = '/';
const getProcessEnv = (k) => Reflect.get(Reflect.get(Reflect.get(self, 'process') || {}, 'env') || {}, k);
/**
* An implementation of the `StorageArea` interface wrapping Cloudflare Worker's KV store.
*
* The goal of this class is ease of use and compatibility with other Storage Area implementations,
* such as <https://github.com/GoogleChromeLabs/kv-storage-polyfill>.
*
* While work on [the specification](https://wicg.github.io/kv-storage/) itself has stopped,
* it's still a good interface for asynchronous data access that feels native to JavaScript.
*
* Note that efficiency is not a goal. Specifically, if you have sizable `ArrayBuffer`s,
* it's much better to use Cloudflare's KV directly.
*/
export class CloudflareStorageArea {
// @ts-ignore: deno only
constructor(name = DEFAULT_STORAGE_AREA_NAME, options = {}) {
// @ts-ignore: deno only
_CloudflareStorageArea_kv.set(this, void 0);
_CloudflareStorageArea_packer.set(this, void 0);
_CloudflareStorageArea_encodeKey.set(this, void 0);
_CloudflareStorageArea_decodeKey.set(this, void 0);
_CloudflareStorageArea_paginationHelper.set(this, void 0);
let { namespace, packer = new StructuredPacker() } = options;
namespace = namespace
|| CloudflareStorageArea.defaultKVNamespace
|| Reflect.get(self, Reflect.get(self, DEFAULT_KV_NAMESPACE_KEY))
|| Reflect.get(self, Reflect.get(self, OLD_DEFAULT_KV_NAMESPACE_KEY))
|| Reflect.get(self, getProcessEnv(DEFAULT_KV_NAMESPACE_KEY));
__classPrivateFieldSet(this, _CloudflareStorageArea_kv, namespace
? namespace
: typeof name === 'string'
? Reflect.get(self, name)
: name, "f");
if (!__classPrivateFieldGet(this, _CloudflareStorageArea_kv, "f")) {
throw Error('KV binding missing. Consult Workers documentation for details');
}
__classPrivateFieldSet(this, _CloudflareStorageArea_encodeKey, !namespace
? encodeKey
: k => `${name}${DIV}${encodeKey(k)}`, "f");
__classPrivateFieldSet(this, _CloudflareStorageArea_decodeKey, !namespace
? decodeKey
: k => decodeKey(k.substring(name.length + 1)), "f");
__classPrivateFieldSet(this, _CloudflareStorageArea_paginationHelper, !namespace
? paginationHelper
: (kv, { prefix, ...opts } = {}) => paginationHelper(kv, {
prefix: `${name}${DIV}${prefix !== null && prefix !== void 0 ? prefix : ''}`,
...opts,
}), "f");
__classPrivateFieldSet(this, _CloudflareStorageArea_packer, packer, "f");
}
get(key, opts) {
throwForDisallowedKey(key);
return __classPrivateFieldGet(this, _CloudflareStorageArea_packer, "f").get(__classPrivateFieldGet(this, _CloudflareStorageArea_kv, "f"), __classPrivateFieldGet(this, _CloudflareStorageArea_encodeKey, "f").call(this, key), opts);
}
async set(key, value, opts) {
throwForDisallowedKey(key);
if (value === undefined)
await __classPrivateFieldGet(this, _CloudflareStorageArea_kv, "f").delete(__classPrivateFieldGet(this, _CloudflareStorageArea_encodeKey, "f").call(this, key));
else {
await __classPrivateFieldGet(this, _CloudflareStorageArea_packer, "f").set(__classPrivateFieldGet(this, _CloudflareStorageArea_kv, "f"), __classPrivateFieldGet(this, _CloudflareStorageArea_encodeKey, "f").call(this, key), value, opts);
}
}
delete(key) {
throwForDisallowedKey(key);
return __classPrivateFieldGet(this, _CloudflareStorageArea_kv, "f").delete(__classPrivateFieldGet(this, _CloudflareStorageArea_encodeKey, "f").call(this, key));
}
async clear(opts) {
for await (const key of __classPrivateFieldGet(this, _CloudflareStorageArea_paginationHelper, "f").call(this, __classPrivateFieldGet(this, _CloudflareStorageArea_kv, "f"), opts)) {
await __classPrivateFieldGet(this, _CloudflareStorageArea_kv, "f").delete(key);
}
}
async *keys(opts) {
for await (const key of __classPrivateFieldGet(this, _CloudflareStorageArea_paginationHelper, "f").call(this, __classPrivateFieldGet(this, _CloudflareStorageArea_kv, "f"), opts)) {
yield __classPrivateFieldGet(this, _CloudflareStorageArea_decodeKey, "f").call(this, key);
}
}
async *values(opts) {
for await (const key of __classPrivateFieldGet(this, _CloudflareStorageArea_paginationHelper, "f").call(this, __classPrivateFieldGet(this, _CloudflareStorageArea_kv, "f"), opts)) {
yield __classPrivateFieldGet(this, _CloudflareStorageArea_packer, "f").get(__classPrivateFieldGet(this, _CloudflareStorageArea_kv, "f"), key, opts);
}
}
async *entries(opts) {
for await (const key of __classPrivateFieldGet(this, _CloudflareStorageArea_paginationHelper, "f").call(this, __classPrivateFieldGet(this, _CloudflareStorageArea_kv, "f"), opts)) {
yield [__classPrivateFieldGet(this, _CloudflareStorageArea_decodeKey, "f").call(this, key), await __classPrivateFieldGet(this, _CloudflareStorageArea_packer, "f").get(__classPrivateFieldGet(this, _CloudflareStorageArea_kv, "f"), key, opts)];
}
}
backingStore() {
return __classPrivateFieldGet(this, _CloudflareStorageArea_kv, "f");
}
}
_CloudflareStorageArea_kv = new WeakMap(), _CloudflareStorageArea_packer = new WeakMap(), _CloudflareStorageArea_encodeKey = new WeakMap(), _CloudflareStorageArea_decodeKey = new WeakMap(), _CloudflareStorageArea_paginationHelper = new WeakMap();
/** Abstracts Cloudflare KV's cursor-based pagination with async iteration. */
// @ts-ignore: deno only
async function* paginationHelper(kv, opts = {}) {
let keys;
let done;
let cursor;
do {
({ keys, list_complete: done, cursor } = await kv.list({ ...cursor ? { ...opts, cursor } : opts }));
for (const { name } of keys)
yield name;
} while (!done);
}
/** @deprecated for backwards compat with v0.2.0 */
export class KVStorageArea extends CloudflareStorageArea {
}
export { CloudflareStorageArea as CFStorageArea };
export { CloudflareStorageArea as StorageArea };
//# sourceMappingURL=index.js.map