@vite-hub/kv 0.0.1 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -8
- package/dist/{config-Bu0hVhEv.js → config-C1ofyzSt.js} +23 -21
- package/dist/deno-kv-JnitV_jG.js +54 -0
- package/dist/errors.d.ts +8 -0
- package/dist/errors.js +20 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +116 -36
- package/dist/runtime/upstash-driver.d.ts +2 -0
- package/dist/runtime/upstash-driver.js +2 -0
- package/dist/types-Dg4l318i.d.ts +60 -0
- package/dist/vite.d.ts +3 -4
- package/dist/vite.js +384 -4
- package/package.json +37 -46
- package/dist/driver-CPgLtkh4.js +0 -81
- package/dist/env-Cs7n_E1y.js +0 -12
- package/dist/module-BrvD5I2y.js +0 -256
- package/dist/nitro-B8aUYcjq.js +0 -40
- package/dist/nitro.d.ts +0 -27
- package/dist/nitro.js +0 -2
- package/dist/nuxt.d.ts +0 -58077
- package/dist/nuxt.js +0 -32
- package/dist/runtime/nitro-plugin.d.ts +0 -6
- package/dist/runtime/nitro-plugin.js +0 -26
- package/dist/types-27OsNCU6.d.ts +0 -45
package/README.md
CHANGED
|
@@ -25,29 +25,35 @@ import { kv } from "@vite-hub/kv"
|
|
|
25
25
|
import { defineEventHandler, readBody } from "h3"
|
|
26
26
|
|
|
27
27
|
export default defineEventHandler(async (event) => {
|
|
28
|
-
await kv.set("settings", await readBody(event))
|
|
28
|
+
const [writeError] = await kv.set("settings", await readBody(event))
|
|
29
|
+
if (writeError) throw writeError
|
|
29
30
|
|
|
30
|
-
|
|
31
|
+
const [readError, settings] = await kv.get("settings")
|
|
32
|
+
if (readError) throw readError
|
|
33
|
+
return settings
|
|
31
34
|
})
|
|
32
35
|
```
|
|
33
36
|
|
|
34
37
|
```ts
|
|
35
|
-
//
|
|
36
|
-
import {
|
|
38
|
+
// vite.config.ts
|
|
39
|
+
import { hubKv } from "@vite-hub/kv/vite"
|
|
40
|
+
import { defineConfig } from "vite"
|
|
37
41
|
|
|
38
|
-
export default
|
|
39
|
-
modules: ["@vite-hub/kv/nitro"],
|
|
42
|
+
export default defineConfig({
|
|
40
43
|
kv: {
|
|
41
44
|
driver: "fs-lite",
|
|
42
45
|
base: ".data/kv",
|
|
43
46
|
},
|
|
47
|
+
plugins: [hubKv()],
|
|
44
48
|
})
|
|
45
49
|
```
|
|
46
50
|
|
|
47
|
-
## Vite
|
|
51
|
+
## Vite Integration
|
|
48
52
|
|
|
49
|
-
Use `hubKv()` in Vite
|
|
53
|
+
Use `hubKv()` in Vite to resolve KV config and expose the `kv` runtime helper to server code.
|
|
50
54
|
|
|
51
55
|
Providers include local `fs-lite`, [Cloudflare Workers KV](https://developers.cloudflare.com/kv/), and [Upstash Redis](https://upstash.com/docs/redis/overall/getstarted). The storage layer is built on [unstorage](https://unstorage.unjs.io/guide).
|
|
52
56
|
|
|
57
|
+
KV operations return `[error, value]`. Provider failures use `ViteHubError` with code `KV_OPERATION_FAILED`, operation/store details, and the provider failure in `cause`. Invalid configuration and unknown named stores still throw before provider execution.
|
|
58
|
+
|
|
53
59
|
Learn more at [vitehub.dev](https://vitehub.dev).
|
|
@@ -1,23 +1,21 @@
|
|
|
1
|
-
import { n as trimmed, t as readEnv } from "./env-Cs7n_E1y.js";
|
|
2
1
|
import { defu } from "defu";
|
|
3
|
-
//#region ../internal/src/
|
|
2
|
+
//#region ../internal/src/env.ts
|
|
3
|
+
function trimmed(value) {
|
|
4
|
+
return typeof value === "string" && value.trim().length > 0 ? value.trim() : void 0;
|
|
5
|
+
}
|
|
6
|
+
function readEnv(env, ...names) {
|
|
7
|
+
for (const name of names) {
|
|
8
|
+
const value = trimmed(env[name]);
|
|
9
|
+
if (value) return value;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
//#endregion
|
|
13
|
+
//#region ../internal/src/hosting.ts
|
|
4
14
|
function normalizeHosting(hosting) {
|
|
5
15
|
const normalized = hosting?.trim().toLowerCase().replaceAll("_", "-") || "";
|
|
6
16
|
if (normalized === "cloudflare") return "cloudflare-module";
|
|
7
17
|
return normalized;
|
|
8
18
|
}
|
|
9
|
-
function detectHosting(target) {
|
|
10
|
-
return normalizeHosting(target.options.nitro?.preset || target.options.preset || detectPresetArg() || process.env.NITRO_PRESET || process.env.VITEHUB_HOSTING || "");
|
|
11
|
-
}
|
|
12
|
-
function detectPresetArg() {
|
|
13
|
-
const argv = process.argv || [];
|
|
14
|
-
for (let index = 0; index < argv.length; index++) {
|
|
15
|
-
const arg = argv[index];
|
|
16
|
-
if (arg === "--preset") return argv[index + 1] || "";
|
|
17
|
-
if (arg?.startsWith("--preset=")) return arg.slice(9);
|
|
18
|
-
}
|
|
19
|
-
return "";
|
|
20
|
-
}
|
|
21
19
|
//#endregion
|
|
22
20
|
//#region ../internal/src/object.ts
|
|
23
21
|
function isPlainObject(value) {
|
|
@@ -55,12 +53,20 @@ function resolveCloudflareStore(config = {}, env = process.env) {
|
|
|
55
53
|
binding: "KV"
|
|
56
54
|
});
|
|
57
55
|
}
|
|
56
|
+
function resolveDenoStore(config = {}) {
|
|
57
|
+
const path = trimmed(config.path);
|
|
58
|
+
return path ? {
|
|
59
|
+
driver: "deno-kv",
|
|
60
|
+
path
|
|
61
|
+
} : { driver: "deno-kv" };
|
|
62
|
+
}
|
|
58
63
|
function resolveExplicitStore(store, env) {
|
|
59
64
|
switch (store.driver) {
|
|
60
65
|
case "cloudflare-kv-binding": return resolveCloudflareStore(store, env);
|
|
66
|
+
case "deno-kv": return resolveDenoStore(store);
|
|
61
67
|
case "upstash": return resolveUpstashStore(store);
|
|
62
68
|
case "fs-lite": return resolveFsLiteStore(store);
|
|
63
|
-
default: throw new TypeError(`Unknown \`kv.driver\`: ${JSON.stringify(store.driver)}. Expected "cloudflare-kv-binding", "upstash", or "fs-lite".`);
|
|
69
|
+
default: throw new TypeError(`Unknown \`kv.driver\`: ${JSON.stringify(store.driver)}. Expected "cloudflare-kv-binding", "deno-kv", "upstash", or "fs-lite".`);
|
|
64
70
|
}
|
|
65
71
|
}
|
|
66
72
|
function createResolvedConfig(store, stores) {
|
|
@@ -88,15 +94,11 @@ function normalizeKVOptions(options, input = {}) {
|
|
|
88
94
|
return createResolvedConfig(defaultStore, resolvedStores);
|
|
89
95
|
}
|
|
90
96
|
if (explicit?.driver) return createResolvedConfig(resolveExplicitStore(explicit, env));
|
|
97
|
+
if (hosting.includes("deno")) return createResolvedConfig(resolveDenoStore());
|
|
91
98
|
if (hasUpstashEnv(env)) return createResolvedConfig(resolveUpstashStore());
|
|
92
99
|
if (hosting.includes("vercel")) return createResolvedConfig(resolveUpstashStore());
|
|
93
100
|
if (hosting.includes("cloudflare")) return createResolvedConfig(resolveCloudflareStore({}, env));
|
|
94
101
|
return createResolvedConfig(resolveFsLiteStore());
|
|
95
102
|
}
|
|
96
|
-
function warnVercelKVFallback(target, config, hosting) {
|
|
97
|
-
if (!config || !normalizeHosting(hosting).includes("vercel")) return;
|
|
98
|
-
if (!Object.values(config.stores || { default: config.store }).some((store) => store.driver === "fs-lite")) return;
|
|
99
|
-
target.logger.error("Vercel hosting requires Upstash-backed KV. Set `KV_REST_API_URL` and `KV_REST_API_TOKEN`.");
|
|
100
|
-
}
|
|
101
103
|
//#endregion
|
|
102
|
-
export {
|
|
104
|
+
export { readEnv as n, trimmed as r, normalizeKVOptions as t };
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
//#region src/runtime/deno-kv.ts
|
|
2
|
+
function getDenoRuntime() {
|
|
3
|
+
return globalThis.Deno;
|
|
4
|
+
}
|
|
5
|
+
function toDenoKey(key) {
|
|
6
|
+
return [key];
|
|
7
|
+
}
|
|
8
|
+
function fromDenoKey(key) {
|
|
9
|
+
return key.length === 1 && typeof key[0] === "string" ? key[0] : void 0;
|
|
10
|
+
}
|
|
11
|
+
function createDenoKVDriver(options = { driver: "deno-kv" }) {
|
|
12
|
+
let kvPromise;
|
|
13
|
+
const open = () => kvPromise ||= (async () => {
|
|
14
|
+
const openKv = getDenoRuntime()?.openKv;
|
|
15
|
+
if (!openKv) throw new Error("[vitehub] Deno KV requires Deno.openKv(). The runtime must be Deno with KV enabled, or the KV Store needs another driver.");
|
|
16
|
+
return openKv(options.path);
|
|
17
|
+
})();
|
|
18
|
+
async function matchingKeys(base = "") {
|
|
19
|
+
const kv = await open();
|
|
20
|
+
const keys = [];
|
|
21
|
+
for await (const entry of kv.list({ prefix: [] })) if (fromDenoKey(entry.key)?.startsWith(base)) keys.push(entry.key);
|
|
22
|
+
return keys;
|
|
23
|
+
}
|
|
24
|
+
return {
|
|
25
|
+
name: "deno-kv",
|
|
26
|
+
options,
|
|
27
|
+
async clear(base = "") {
|
|
28
|
+
const kv = await open();
|
|
29
|
+
for (const key of await matchingKeys(base)) await kv.delete(key);
|
|
30
|
+
},
|
|
31
|
+
async dispose() {
|
|
32
|
+
if (!kvPromise) return;
|
|
33
|
+
(await kvPromise).close?.();
|
|
34
|
+
kvPromise = void 0;
|
|
35
|
+
},
|
|
36
|
+
async getItem(key) {
|
|
37
|
+
return (await (await open()).get(toDenoKey(key))).value ?? null;
|
|
38
|
+
},
|
|
39
|
+
async getKeys(base = "") {
|
|
40
|
+
return (await matchingKeys(base)).flatMap((key) => fromDenoKey(key) ?? []).sort();
|
|
41
|
+
},
|
|
42
|
+
async hasItem(key) {
|
|
43
|
+
return (await (await open()).get(toDenoKey(key))).value !== null;
|
|
44
|
+
},
|
|
45
|
+
async removeItem(key) {
|
|
46
|
+
await (await open()).delete(toDenoKey(key));
|
|
47
|
+
},
|
|
48
|
+
async setItem(key, value) {
|
|
49
|
+
await (await open()).set(toDenoKey(key), value);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
//#endregion
|
|
54
|
+
export { createDenoKVDriver as default };
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { a as KVErrorDetails, c as KVResult, s as KVOperation } from "./types-Dg4l318i.js";
|
|
2
|
+
import { ViteHubError } from "@vite-hub/runtime";
|
|
3
|
+
|
|
4
|
+
//#region src/errors.d.ts
|
|
5
|
+
declare function kvError(operation: KVOperation, store: string, cause: unknown): ViteHubError<"KV_OPERATION_FAILED", KVErrorDetails>;
|
|
6
|
+
declare function kvResult<TResult>(operation: KVOperation, store: string, run: () => Promise<TResult>): Promise<KVResult<TResult>>;
|
|
7
|
+
//#endregion
|
|
8
|
+
export { kvError, kvResult };
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { ViteHubError } from "@vite-hub/runtime";
|
|
2
|
+
//#region src/errors.ts
|
|
3
|
+
function kvError(operation, store, cause) {
|
|
4
|
+
return new ViteHubError("KV_OPERATION_FAILED", "KV storage operation failed.", {
|
|
5
|
+
cause,
|
|
6
|
+
details: {
|
|
7
|
+
operation,
|
|
8
|
+
store
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
async function kvResult(operation, store, run) {
|
|
13
|
+
try {
|
|
14
|
+
return [null, await run()];
|
|
15
|
+
} catch (cause) {
|
|
16
|
+
return [kvError(operation, store, cause), void 0];
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
//#endregion
|
|
20
|
+
export { kvError, kvResult };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { a as
|
|
1
|
+
import { _ as UpstashKVStoreConfig, a as KVErrorDetails, c as KVResult, d as ResolvedCloudflareKVStoreConfig, f as ResolvedDenoKVStoreConfig, g as ResolvedUpstashKVStoreConfig, h as ResolvedKVStoreConfig, i as KVDriver, l as KVStorage, m as ResolvedKVModuleOptions, n as DenoKVStoreConfig, o as KVModuleOptions, p as ResolvedFsLiteKVStoreConfig, r as FsLiteKVStoreConfig, s as KVOperation, t as CloudflareKVStoreConfig, u as KVStoreConfig } from "./types-Dg4l318i.js";
|
|
2
2
|
|
|
3
3
|
//#region src/runtime/storage.d.ts
|
|
4
4
|
declare const kv: KVStorage;
|
|
5
5
|
//#endregion
|
|
6
|
-
export { type CloudflareKVStoreConfig, type FsLiteKVStoreConfig, type KVDriver, type KVModuleOptions, type KVStorage, type KVStoreConfig, type ResolvedCloudflareKVStoreConfig, type ResolvedFsLiteKVStoreConfig, type ResolvedKVModuleOptions, type ResolvedKVStoreConfig, type ResolvedUpstashKVStoreConfig, type UpstashKVStoreConfig, kv };
|
|
6
|
+
export { type CloudflareKVStoreConfig, type DenoKVStoreConfig, type FsLiteKVStoreConfig, type KVDriver, type KVErrorDetails, type KVModuleOptions, type KVOperation, type KVResult, type KVStorage, type KVStoreConfig, type ResolvedCloudflareKVStoreConfig, type ResolvedDenoKVStoreConfig, type ResolvedFsLiteKVStoreConfig, type ResolvedKVModuleOptions, type ResolvedKVStoreConfig, type ResolvedUpstashKVStoreConfig, type UpstashKVStoreConfig, kv };
|
package/dist/index.js
CHANGED
|
@@ -1,18 +1,111 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { t as normalizeKVOptions } from "./config-
|
|
3
|
-
import { t as createLazyKVRuntimeDriver } from "./driver-CPgLtkh4.js";
|
|
1
|
+
import { kvResult } from "./errors.js";
|
|
2
|
+
import { n as readEnv, t as normalizeKVOptions } from "./config-C1ofyzSt.js";
|
|
4
3
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
5
4
|
import { createStorage } from "unstorage";
|
|
6
|
-
//#region ../internal/src/runtime/cloudflare-env.ts
|
|
7
|
-
let activeEnv;
|
|
8
5
|
const activeEnvStorage = new AsyncLocalStorage();
|
|
9
6
|
function getActiveCloudflareEnv() {
|
|
10
|
-
|
|
7
|
+
const scoped = activeEnvStorage.getStore();
|
|
8
|
+
return scoped ? scoped.env : globalThis.__env__;
|
|
9
|
+
}
|
|
10
|
+
//#endregion
|
|
11
|
+
//#region src/runtime/upstash.ts
|
|
12
|
+
function isMaskedValue(value) {
|
|
13
|
+
return !value || /^\*+$/.test(value);
|
|
14
|
+
}
|
|
15
|
+
function assertRuntimeValue(value, envName) {
|
|
16
|
+
if (isMaskedValue(value)) throw new Error(`Missing runtime environment variable \`${envName}\` for Upstash KV.`);
|
|
17
|
+
}
|
|
18
|
+
function resolveRuntimeUpstashStore(config, env) {
|
|
19
|
+
const envUrl = readEnv(env, "KV_REST_API_URL");
|
|
20
|
+
const envToken = readEnv(env, "KV_REST_API_TOKEN");
|
|
21
|
+
const resolved = {
|
|
22
|
+
...config,
|
|
23
|
+
token: isMaskedValue(config.token) ? envToken || config.token : config.token,
|
|
24
|
+
url: isMaskedValue(config.url) ? envUrl || config.url : config.url
|
|
25
|
+
};
|
|
26
|
+
assertRuntimeValue(resolved.url, "KV_REST_API_URL");
|
|
27
|
+
assertRuntimeValue(resolved.token, "KV_REST_API_TOKEN");
|
|
28
|
+
return resolved;
|
|
29
|
+
}
|
|
30
|
+
function resolveRuntimeKVOptions(config, env = process.env) {
|
|
31
|
+
if (!config || config.store.driver !== "upstash") return config;
|
|
32
|
+
return {
|
|
33
|
+
...config,
|
|
34
|
+
store: resolveRuntimeUpstashStore(config.store, env)
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
//#endregion
|
|
38
|
+
//#region src/runtime/driver.ts
|
|
39
|
+
const lazyDriverMethods = new Set([
|
|
40
|
+
"clear",
|
|
41
|
+
"getItem",
|
|
42
|
+
"getKeys",
|
|
43
|
+
"hasItem",
|
|
44
|
+
"removeItem",
|
|
45
|
+
"setItem"
|
|
46
|
+
]);
|
|
47
|
+
const lazyOptionalDriverMethods = {
|
|
48
|
+
"cloudflare-kv-binding": /* @__PURE__ */ new Set(),
|
|
49
|
+
"deno-kv": /* @__PURE__ */ new Set(),
|
|
50
|
+
"fs-lite": new Set([
|
|
51
|
+
"getItemRaw",
|
|
52
|
+
"getMeta",
|
|
53
|
+
"setItemRaw"
|
|
54
|
+
]),
|
|
55
|
+
"upstash": new Set(["getItems"])
|
|
56
|
+
};
|
|
57
|
+
async function createRuntimeDriver(store) {
|
|
58
|
+
switch (store.driver) {
|
|
59
|
+
case "cloudflare-kv-binding": {
|
|
60
|
+
const { default: factory } = await import("unstorage/drivers/cloudflare-kv-binding");
|
|
61
|
+
return factory(store);
|
|
62
|
+
}
|
|
63
|
+
case "deno-kv": {
|
|
64
|
+
const { default: factory } = await import("./deno-kv-JnitV_jG.js");
|
|
65
|
+
return factory(store);
|
|
66
|
+
}
|
|
67
|
+
case "fs-lite": {
|
|
68
|
+
const { default: factory } = await import("unstorage/drivers/fs-lite");
|
|
69
|
+
return factory(store);
|
|
70
|
+
}
|
|
71
|
+
case "upstash": {
|
|
72
|
+
const { default: factory } = await import("@vite-hub/kv/runtime/upstash-driver");
|
|
73
|
+
return factory(store);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
function createLazyKVRuntimeDriver(config) {
|
|
78
|
+
let driverPromise;
|
|
79
|
+
const resolve = () => driverPromise ||= (async () => {
|
|
80
|
+
const runtime = resolveRuntimeKVOptions(config);
|
|
81
|
+
if (!runtime) throw new Error("KV runtime is disabled.");
|
|
82
|
+
return createRuntimeDriver(runtime.store);
|
|
83
|
+
})();
|
|
84
|
+
const target = {
|
|
85
|
+
name: `lazy:${config.store.driver}`,
|
|
86
|
+
options: config.store
|
|
87
|
+
};
|
|
88
|
+
return new Proxy(target, { get(t, prop) {
|
|
89
|
+
const own = t[prop];
|
|
90
|
+
if (own !== void 0) return own;
|
|
91
|
+
if (prop === "dispose") return async () => {
|
|
92
|
+
if (!driverPromise) return;
|
|
93
|
+
const driver = await driverPromise;
|
|
94
|
+
return driver.dispose?.call(driver);
|
|
95
|
+
};
|
|
96
|
+
const optionalMethods = lazyOptionalDriverMethods[config.store.driver];
|
|
97
|
+
if (!lazyDriverMethods.has(prop) && !optionalMethods.has(prop)) return void 0;
|
|
98
|
+
return async (...args) => {
|
|
99
|
+
const driver = await resolve();
|
|
100
|
+
return driver[prop]?.apply(driver, args);
|
|
101
|
+
};
|
|
102
|
+
} });
|
|
11
103
|
}
|
|
12
104
|
//#endregion
|
|
13
105
|
//#region src/runtime/hosted-storage.ts
|
|
106
|
+
var KVStoreConfigurationError = class extends Error {};
|
|
14
107
|
function assertHostedConfig(config) {
|
|
15
|
-
if (!config) throw new
|
|
108
|
+
if (!config) throw new KVStoreConfigurationError("[vitehub] `@vite-hub/kv` requires `hubKv()` and `kv !== false`.");
|
|
16
109
|
return config;
|
|
17
110
|
}
|
|
18
111
|
function createHostedKVStorage(config) {
|
|
@@ -21,7 +114,7 @@ function createHostedKVStorage(config) {
|
|
|
21
114
|
function createNamedHostedKVStorage(config, name) {
|
|
22
115
|
const resolved = assertHostedConfig(config);
|
|
23
116
|
const store = (resolved.stores || { default: resolved.store })[name];
|
|
24
|
-
if (!store) throw new
|
|
117
|
+
if (!store) throw new KVStoreConfigurationError(`[vitehub] Unknown KV store "${name}".`);
|
|
25
118
|
return createStorage({ driver: createLazyKVRuntimeDriver({
|
|
26
119
|
store,
|
|
27
120
|
stores: {
|
|
@@ -35,8 +128,9 @@ function createNamedHostedKVStorage(config, name) {
|
|
|
35
128
|
const storagePromises = /* @__PURE__ */ new Map();
|
|
36
129
|
function inferHosting(env) {
|
|
37
130
|
if (getActiveCloudflareEnv()) return "cloudflare";
|
|
38
|
-
const explicit = readEnv(env, "VITEHUB_HOSTING"
|
|
131
|
+
const explicit = readEnv(env, "VITEHUB_HOSTING");
|
|
39
132
|
if (explicit) return explicit;
|
|
133
|
+
if (typeof globalThis.Deno?.openKv === "function") return "deno";
|
|
40
134
|
return readEnv(env, "KV_REST_API_URL") ? "vercel" : void 0;
|
|
41
135
|
}
|
|
42
136
|
function shouldFallbackHostedConfigImport(error) {
|
|
@@ -44,12 +138,8 @@ function shouldFallbackHostedConfigImport(error) {
|
|
|
44
138
|
return code === "MODULE_NOT_FOUND" || code === "ERR_MODULE_NOT_FOUND" || code === "ERR_PACKAGE_IMPORT_NOT_DEFINED" || code === "ERR_UNSUPPORTED_ESM_URL_SCHEME";
|
|
45
139
|
}
|
|
46
140
|
async function resolveHostedConfig() {
|
|
47
|
-
const virtualConfigId = "#vitehub/kv/config";
|
|
48
141
|
try {
|
|
49
|
-
return (await import(
|
|
50
|
-
/* @vite-ignore */
|
|
51
|
-
virtualConfigId
|
|
52
|
-
)).kv;
|
|
142
|
+
return (await import("#vitehub/kv/config")).kv;
|
|
53
143
|
} catch (error) {
|
|
54
144
|
if (!shouldFallbackHostedConfigImport(error)) throw error;
|
|
55
145
|
const env = typeof process !== "undefined" ? process.env : {};
|
|
@@ -59,17 +149,6 @@ async function resolveHostedConfig() {
|
|
|
59
149
|
}) || false;
|
|
60
150
|
}
|
|
61
151
|
}
|
|
62
|
-
async function assertRuntimeStore(name) {
|
|
63
|
-
if (name === "default") return;
|
|
64
|
-
try {
|
|
65
|
-
const config = (await import("nitro/runtime-config")).useRuntimeConfig().kv;
|
|
66
|
-
if (config) {
|
|
67
|
-
if (!(config.stores || { default: config.store })[name]) throw new Error(`[vitehub] Unknown KV store "${name}".`);
|
|
68
|
-
}
|
|
69
|
-
} catch (error) {
|
|
70
|
-
if (error.message.startsWith("[vitehub] Unknown KV store")) throw error;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
152
|
async function resolveStorage(name = "default") {
|
|
74
153
|
const existing = storagePromises.get(name);
|
|
75
154
|
if (existing) return existing;
|
|
@@ -78,33 +157,34 @@ async function resolveStorage(name = "default") {
|
|
|
78
157
|
storagePromises.set(name, promise);
|
|
79
158
|
return promise;
|
|
80
159
|
}
|
|
81
|
-
const
|
|
82
|
-
const promise = assertRuntimeStore(name).then(() => import("nitro/storage")).then((module) => module.useStorage(storageName)).catch(async () => {
|
|
83
|
-
if (name === "default") return createHostedKVStorage(await resolveHostedConfig());
|
|
84
|
-
return createNamedHostedKVStorage(await resolveHostedConfig(), name);
|
|
85
|
-
});
|
|
160
|
+
const promise = resolveHostedConfig().then((config) => name === "default" ? createHostedKVStorage(config) : createNamedHostedKVStorage(config, name));
|
|
86
161
|
storagePromises.set(name, promise);
|
|
87
162
|
return promise;
|
|
88
163
|
}
|
|
164
|
+
async function runKVOperation(operation, name, run) {
|
|
165
|
+
const result = await kvResult(operation, name, async () => run(await resolveStorage(name)));
|
|
166
|
+
if (result[0]?.cause instanceof KVStoreConfigurationError) throw result[0].cause;
|
|
167
|
+
return result;
|
|
168
|
+
}
|
|
89
169
|
function createKVStorage(name = "default") {
|
|
90
170
|
return {
|
|
91
171
|
async clear(base, options) {
|
|
92
|
-
|
|
172
|
+
return runKVOperation("clear", name, async (storage) => storage.clear(base, options));
|
|
93
173
|
},
|
|
94
174
|
async del(key, options) {
|
|
95
|
-
|
|
175
|
+
return runKVOperation("del", name, async (storage) => storage.removeItem(key, options));
|
|
96
176
|
},
|
|
97
177
|
async get(key, options) {
|
|
98
|
-
return (
|
|
178
|
+
return runKVOperation("get", name, (storage) => storage.getItem(key, options));
|
|
99
179
|
},
|
|
100
180
|
async has(key, options) {
|
|
101
|
-
return (
|
|
181
|
+
return runKVOperation("has", name, (storage) => storage.hasItem(key, options));
|
|
102
182
|
},
|
|
103
183
|
async keys(base, options) {
|
|
104
|
-
return (
|
|
184
|
+
return runKVOperation("keys", name, (storage) => storage.getKeys(base, options));
|
|
105
185
|
},
|
|
106
186
|
async set(key, value, options) {
|
|
107
|
-
|
|
187
|
+
return runKVOperation("set", name, async (storage) => storage.setItem(key, value, options));
|
|
108
188
|
},
|
|
109
189
|
store(storeName) {
|
|
110
190
|
return createKVStorage(storeName);
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { ViteHubError, ViteHubErrorDetails } from "@vite-hub/runtime";
|
|
2
|
+
|
|
3
|
+
//#region src/types.d.ts
|
|
4
|
+
type KVDriver = "cloudflare-kv-binding" | "deno-kv" | "upstash" | "fs-lite";
|
|
5
|
+
interface CloudflareKVStoreConfig {
|
|
6
|
+
driver: "cloudflare-kv-binding";
|
|
7
|
+
binding?: string;
|
|
8
|
+
namespaceId?: string;
|
|
9
|
+
}
|
|
10
|
+
interface UpstashKVStoreConfig {
|
|
11
|
+
driver: "upstash";
|
|
12
|
+
token?: string;
|
|
13
|
+
url?: string;
|
|
14
|
+
}
|
|
15
|
+
interface DenoKVStoreConfig {
|
|
16
|
+
driver: "deno-kv";
|
|
17
|
+
path?: string;
|
|
18
|
+
}
|
|
19
|
+
interface FsLiteKVStoreConfig {
|
|
20
|
+
driver: "fs-lite";
|
|
21
|
+
base?: string;
|
|
22
|
+
}
|
|
23
|
+
type KVStoreConfig = CloudflareKVStoreConfig | DenoKVStoreConfig | UpstashKVStoreConfig | FsLiteKVStoreConfig;
|
|
24
|
+
interface ResolvedCloudflareKVStoreConfig extends CloudflareKVStoreConfig {
|
|
25
|
+
binding: string;
|
|
26
|
+
}
|
|
27
|
+
type ResolvedUpstashKVStoreConfig = Required<UpstashKVStoreConfig>;
|
|
28
|
+
interface ResolvedDenoKVStoreConfig extends DenoKVStoreConfig {
|
|
29
|
+
path?: string;
|
|
30
|
+
}
|
|
31
|
+
interface ResolvedFsLiteKVStoreConfig extends FsLiteKVStoreConfig {
|
|
32
|
+
base: string;
|
|
33
|
+
}
|
|
34
|
+
type ResolvedKVStoreConfig = ResolvedCloudflareKVStoreConfig | ResolvedDenoKVStoreConfig | ResolvedUpstashKVStoreConfig | ResolvedFsLiteKVStoreConfig;
|
|
35
|
+
type KVStoreName = "default" | (string & {});
|
|
36
|
+
interface KVStoresConfig {
|
|
37
|
+
stores: Record<string, KVStoreConfig>;
|
|
38
|
+
}
|
|
39
|
+
type KVModuleOptions = KVStoreConfig | KVStoresConfig | false;
|
|
40
|
+
interface ResolvedKVModuleOptions {
|
|
41
|
+
store: ResolvedKVStoreConfig;
|
|
42
|
+
stores?: Record<string, ResolvedKVStoreConfig>;
|
|
43
|
+
}
|
|
44
|
+
type KVOperation = "clear" | "del" | "get" | "has" | "keys" | "set";
|
|
45
|
+
type KVErrorDetails = ViteHubErrorDetails & {
|
|
46
|
+
operation: KVOperation;
|
|
47
|
+
store: string;
|
|
48
|
+
};
|
|
49
|
+
type KVResult<TResult> = [error: null, value: TResult] | [error: ViteHubError<"KV_OPERATION_FAILED", KVErrorDetails>, value: undefined];
|
|
50
|
+
interface KVStorage {
|
|
51
|
+
clear(base?: string, options?: unknown): Promise<KVResult<void>>;
|
|
52
|
+
del(key: string, options?: unknown): Promise<KVResult<void>>;
|
|
53
|
+
get<T = unknown>(key: string, options?: unknown): Promise<KVResult<T | null>>;
|
|
54
|
+
has(key: string, options?: unknown): Promise<KVResult<boolean>>;
|
|
55
|
+
keys(base?: string, options?: unknown): Promise<KVResult<string[]>>;
|
|
56
|
+
set<T = unknown>(key: string, value: T, options?: unknown): Promise<KVResult<void>>;
|
|
57
|
+
store(name: KVStoreName): KVStorage;
|
|
58
|
+
}
|
|
59
|
+
//#endregion
|
|
60
|
+
export { UpstashKVStoreConfig as _, KVErrorDetails as a, KVResult as c, ResolvedCloudflareKVStoreConfig as d, ResolvedDenoKVStoreConfig as f, ResolvedUpstashKVStoreConfig as g, ResolvedKVStoreConfig as h, KVDriver as i, KVStorage as l, ResolvedKVModuleOptions as m, DenoKVStoreConfig as n, KVModuleOptions as o, ResolvedFsLiteKVStoreConfig as p, FsLiteKVStoreConfig as r, KVOperation as s, CloudflareKVStoreConfig as t, KVStoreConfig as u };
|
package/dist/vite.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { NitroModule } from "nitro/types";
|
|
1
|
+
import { m as ResolvedKVModuleOptions, o as KVModuleOptions } from "./types-Dg4l318i.js";
|
|
3
2
|
import { Plugin } from "vite";
|
|
4
3
|
|
|
5
4
|
//#region src/config.d.ts
|
|
@@ -23,8 +22,8 @@ interface KVVitePluginAPI {
|
|
|
23
22
|
}
|
|
24
23
|
type KVVitePlugin = Plugin & {
|
|
25
24
|
api: KVVitePluginAPI;
|
|
26
|
-
nitro: NitroModule;
|
|
27
25
|
};
|
|
26
|
+
declare function hubKvOptionalPeerResolver(): Plugin;
|
|
28
27
|
declare function hubKv(options?: KVModuleOptions): KVVitePlugin;
|
|
29
28
|
declare module "vite" {
|
|
30
29
|
interface UserConfig {
|
|
@@ -32,4 +31,4 @@ declare module "vite" {
|
|
|
32
31
|
}
|
|
33
32
|
}
|
|
34
33
|
//#endregion
|
|
35
|
-
export { KVVitePlugin, KVVitePluginAPI, type KVViteRuntimeConfig, KV_VIRTUAL_CONFIG_ID, KV_VITE_PLUGIN_NAME, hubKv, resolveKVViteConfig };
|
|
34
|
+
export { KVVitePlugin, KVVitePluginAPI, type KVViteRuntimeConfig, KV_VIRTUAL_CONFIG_ID, KV_VITE_PLUGIN_NAME, hubKv, hubKvOptionalPeerResolver, resolveKVViteConfig };
|