UNPKG

8.31 kBJavaScriptView Raw
1"use strict";
2var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3 if (kind === "m") throw new TypeError("Private method is not writable");
4 if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5 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");
6 return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7};
8var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9 if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
10 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");
11 return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12};
13var _CloudflareStorageArea_kv, _CloudflareStorageArea_packer, _CloudflareStorageArea_encodeKey, _CloudflareStorageArea_decodeKey, _CloudflareStorageArea_paginationHelper;
14Object.defineProperty(exports, "__esModule", { value: true });
15exports.StorageArea = exports.CFStorageArea = exports.KVStorageArea = exports.CloudflareStorageArea = void 0;
16const idb_key_to_string_1 = require("idb-key-to-string");
17const packer_js_1 = require("./packer.js");
18const OLD_DEFAULT_KV_NAMESPACE_KEY = 'CF_STORAGE_AREA__DEFAULT_KV_NAMESPACE';
19const DEFAULT_KV_NAMESPACE_KEY = 'DEFAULT_KV_NAMESPACE';
20const DEFAULT_STORAGE_AREA_NAME = 'default';
21const DIV = '/';
22const getProcessEnv = (k) => Reflect.get(Reflect.get(Reflect.get(self, 'process') || {}, 'env') || {}, k);
23/**
24 * An implementation of the `StorageArea` interface wrapping Cloudflare Worker's KV store.
25 *
26 * The goal of this class is ease of use and compatibility with other Storage Area implementations,
27 * such as <https://github.com/GoogleChromeLabs/kv-storage-polyfill>.
28 *
29 * While work on [the specification](https://wicg.github.io/kv-storage/) itself has stopped,
30 * it's still a good interface for asynchronous data access that feels native to JavaScript.
31 *
32 * Note that efficiency is not a goal. Specifically, if you have sizable `ArrayBuffer`s,
33 * it's much better to use Cloudflare's KV directly.
34 */
35class CloudflareStorageArea {
36 // @ts-ignore: deno only
37 constructor(name = DEFAULT_STORAGE_AREA_NAME, options = {}) {
38 // @ts-ignore: deno only
39 _CloudflareStorageArea_kv.set(this, void 0);
40 _CloudflareStorageArea_packer.set(this, void 0);
41 _CloudflareStorageArea_encodeKey.set(this, void 0);
42 _CloudflareStorageArea_decodeKey.set(this, void 0);
43 _CloudflareStorageArea_paginationHelper.set(this, void 0);
44 let { namespace, packer = new packer_js_1.StructuredPacker() } = options;
45 namespace = namespace
46 || CloudflareStorageArea.defaultKVNamespace
47 || Reflect.get(self, Reflect.get(self, DEFAULT_KV_NAMESPACE_KEY))
48 || Reflect.get(self, Reflect.get(self, OLD_DEFAULT_KV_NAMESPACE_KEY))
49 || Reflect.get(self, getProcessEnv(DEFAULT_KV_NAMESPACE_KEY));
50 __classPrivateFieldSet(this, _CloudflareStorageArea_kv, namespace
51 ? namespace
52 : typeof name === 'string'
53 ? Reflect.get(self, name)
54 : name, "f");
55 if (!__classPrivateFieldGet(this, _CloudflareStorageArea_kv, "f")) {
56 throw Error('KV binding missing. Consult Workers documentation for details');
57 }
58 __classPrivateFieldSet(this, _CloudflareStorageArea_encodeKey, !namespace
59 ? idb_key_to_string_1.encodeKey
60 : k => `${name}${DIV}${(0, idb_key_to_string_1.encodeKey)(k)}`, "f");
61 __classPrivateFieldSet(this, _CloudflareStorageArea_decodeKey, !namespace
62 ? idb_key_to_string_1.decodeKey
63 : k => (0, idb_key_to_string_1.decodeKey)(k.substring(name.length + 1)), "f");
64 __classPrivateFieldSet(this, _CloudflareStorageArea_paginationHelper, !namespace
65 ? paginationHelper
66 : (kv, { prefix, ...opts } = {}) => paginationHelper(kv, {
67 prefix: `${name}${DIV}${prefix !== null && prefix !== void 0 ? prefix : ''}`,
68 ...opts,
69 }), "f");
70 __classPrivateFieldSet(this, _CloudflareStorageArea_packer, packer, "f");
71 }
72 get(key, opts) {
73 (0, idb_key_to_string_1.throwForDisallowedKey)(key);
74 return __classPrivateFieldGet(this, _CloudflareStorageArea_packer, "f").get(__classPrivateFieldGet(this, _CloudflareStorageArea_kv, "f"), __classPrivateFieldGet(this, _CloudflareStorageArea_encodeKey, "f").call(this, key), opts);
75 }
76 async set(key, value, opts) {
77 (0, idb_key_to_string_1.throwForDisallowedKey)(key);
78 if (value === undefined)
79 await __classPrivateFieldGet(this, _CloudflareStorageArea_kv, "f").delete(__classPrivateFieldGet(this, _CloudflareStorageArea_encodeKey, "f").call(this, key));
80 else {
81 await __classPrivateFieldGet(this, _CloudflareStorageArea_packer, "f").set(__classPrivateFieldGet(this, _CloudflareStorageArea_kv, "f"), __classPrivateFieldGet(this, _CloudflareStorageArea_encodeKey, "f").call(this, key), value, opts);
82 }
83 }
84 delete(key) {
85 (0, idb_key_to_string_1.throwForDisallowedKey)(key);
86 return __classPrivateFieldGet(this, _CloudflareStorageArea_kv, "f").delete(__classPrivateFieldGet(this, _CloudflareStorageArea_encodeKey, "f").call(this, key));
87 }
88 async clear(opts) {
89 for await (const key of __classPrivateFieldGet(this, _CloudflareStorageArea_paginationHelper, "f").call(this, __classPrivateFieldGet(this, _CloudflareStorageArea_kv, "f"), opts)) {
90 await __classPrivateFieldGet(this, _CloudflareStorageArea_kv, "f").delete(key);
91 }
92 }
93 async *keys(opts) {
94 for await (const key of __classPrivateFieldGet(this, _CloudflareStorageArea_paginationHelper, "f").call(this, __classPrivateFieldGet(this, _CloudflareStorageArea_kv, "f"), opts)) {
95 yield __classPrivateFieldGet(this, _CloudflareStorageArea_decodeKey, "f").call(this, key);
96 }
97 }
98 async *values(opts) {
99 for await (const key of __classPrivateFieldGet(this, _CloudflareStorageArea_paginationHelper, "f").call(this, __classPrivateFieldGet(this, _CloudflareStorageArea_kv, "f"), opts)) {
100 yield __classPrivateFieldGet(this, _CloudflareStorageArea_packer, "f").get(__classPrivateFieldGet(this, _CloudflareStorageArea_kv, "f"), key, opts);
101 }
102 }
103 async *entries(opts) {
104 for await (const key of __classPrivateFieldGet(this, _CloudflareStorageArea_paginationHelper, "f").call(this, __classPrivateFieldGet(this, _CloudflareStorageArea_kv, "f"), opts)) {
105 yield [__classPrivateFieldGet(this, _CloudflareStorageArea_decodeKey, "f").call(this, key), await __classPrivateFieldGet(this, _CloudflareStorageArea_packer, "f").get(__classPrivateFieldGet(this, _CloudflareStorageArea_kv, "f"), key, opts)];
106 }
107 }
108 backingStore() {
109 return __classPrivateFieldGet(this, _CloudflareStorageArea_kv, "f");
110 }
111}
112exports.CloudflareStorageArea = CloudflareStorageArea;
113exports.CFStorageArea = CloudflareStorageArea;
114exports.StorageArea = CloudflareStorageArea;
115_CloudflareStorageArea_kv = new WeakMap(), _CloudflareStorageArea_packer = new WeakMap(), _CloudflareStorageArea_encodeKey = new WeakMap(), _CloudflareStorageArea_decodeKey = new WeakMap(), _CloudflareStorageArea_paginationHelper = new WeakMap();
116/** Abstracts Cloudflare KV's cursor-based pagination with async iteration. */
117// @ts-ignore: deno only
118async function* paginationHelper(kv, opts = {}) {
119 let keys;
120 let done;
121 let cursor;
122 do {
123 ({ keys, list_complete: done, cursor } = await kv.list({ ...cursor ? { ...opts, cursor } : opts }));
124 for (const { name } of keys)
125 yield name;
126 } while (!done);
127}
128/** @deprecated for backwards compat with v0.2.0 */
129class KVStorageArea extends CloudflareStorageArea {
130}
131exports.KVStorageArea = KVStorageArea;
132//# sourceMappingURL=index.js.map
\No newline at end of file