unstorage
Version:
Universal Storage Layer
51 lines (50 loc) • 1.35 kB
JavaScript
import { defineDriver } from "./utils/index.mjs";
export default defineDriver((opts = {}) => {
const binding = getBinding(opts.binding);
async function getKeys(base) {
const kvList = await binding.list(base ? { prefix: base } : void 0);
return kvList.keys.map((key) => key.name);
}
return {
name: "cloudflare-kv-binding",
options: opts,
async hasItem(key) {
return await binding.get(key) !== null;
},
getItem(key) {
return binding.get(key);
},
setItem(key, value) {
return binding.put(key, value);
},
removeItem(key) {
return binding.delete(key);
},
// TODO: use this.getKeys once core is fixed
getKeys,
async clear() {
const keys = await getKeys();
await Promise.all(keys.map((key) => binding.delete(key)));
}
};
});
function getBinding(binding = "STORAGE") {
let bindingName = "[binding]";
if (typeof binding === "string") {
bindingName = binding;
binding = globalThis[bindingName];
}
if (!binding) {
throw new Error(
`Invalid Cloudflare KV binding '${bindingName}': ${binding}`
);
}
for (const key of ["get", "put", "delete"]) {
if (!(key in binding)) {
throw new Error(
`Invalid Cloudflare KV binding '${bindingName}': '${key}' key is missing`
);
}
}
return binding;
}