@vite-hub/kv 0.0.0 → 0.0.1
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 +51 -1
- package/dist/config-Bu0hVhEv.js +102 -0
- package/dist/driver-CPgLtkh4.js +81 -0
- package/dist/env-Cs7n_E1y.js +12 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +116 -0
- package/dist/module-BrvD5I2y.js +256 -0
- package/dist/nitro-B8aUYcjq.js +40 -0
- package/dist/nitro.d.ts +27 -0
- package/dist/nitro.js +2 -0
- package/dist/nuxt.d.ts +58077 -0
- package/dist/nuxt.js +32 -0
- package/dist/runtime/nitro-plugin.d.ts +6 -0
- package/dist/runtime/nitro-plugin.js +26 -0
- package/dist/types-27OsNCU6.d.ts +45 -0
- package/dist/virtual.d.ts +6 -0
- package/dist/virtual.js +1 -0
- package/dist/vite.d.ts +35 -0
- package/dist/vite.js +30 -0
- package/package.json +76 -5
package/README.md
CHANGED
|
@@ -1,3 +1,53 @@
|
|
|
1
1
|
# @vite-hub/kv
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
<p>
|
|
4
|
+
<a href="https://vitehub.dev"><img alt="ViteHub" src="https://img.shields.io/badge/ViteHub-vitehub.dev-646cff?style=flat-square"></a>
|
|
5
|
+
<img alt="TypeScript" src="https://img.shields.io/badge/TypeScript-ready-3178c6?style=flat-square">
|
|
6
|
+
<img alt="Vite" src="https://img.shields.io/badge/Vite-config-646cff?style=flat-square">
|
|
7
|
+
<img alt="KV" src="https://img.shields.io/badge/KV-stores-0f766e?style=flat-square">
|
|
8
|
+
</p>
|
|
9
|
+
|
|
10
|
+
`@vite-hub/kv` gives server code one small-state API for settings, flags, cache entries, and JSON-like records.
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
pnpm add @vite-hub/kv
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Add `@upstash/redis` when you use the Upstash driver.
|
|
19
|
+
|
|
20
|
+
## Minimal API
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
// server/api/settings.put.ts
|
|
24
|
+
import { kv } from "@vite-hub/kv"
|
|
25
|
+
import { defineEventHandler, readBody } from "h3"
|
|
26
|
+
|
|
27
|
+
export default defineEventHandler(async (event) => {
|
|
28
|
+
await kv.set("settings", await readBody(event))
|
|
29
|
+
|
|
30
|
+
return kv.get("settings")
|
|
31
|
+
})
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
// nitro.config.ts
|
|
36
|
+
import { defineNitroConfig } from "nitro/config"
|
|
37
|
+
|
|
38
|
+
export default defineNitroConfig({
|
|
39
|
+
modules: ["@vite-hub/kv/nitro"],
|
|
40
|
+
kv: {
|
|
41
|
+
driver: "fs-lite",
|
|
42
|
+
base: ".data/kv",
|
|
43
|
+
},
|
|
44
|
+
})
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Vite and Nitro
|
|
48
|
+
|
|
49
|
+
Use `hubKv()` in Vite or `@vite-hub/kv/nitro` in Nitro. Both resolve the same `kv` config and expose the same `kv` runtime helper.
|
|
50
|
+
|
|
51
|
+
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
|
+
|
|
53
|
+
Learn more at [vitehub.dev](https://vitehub.dev).
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { n as trimmed, t as readEnv } from "./env-Cs7n_E1y.js";
|
|
2
|
+
import { defu } from "defu";
|
|
3
|
+
//#region ../internal/src/feature-bridge/hosting.ts
|
|
4
|
+
function normalizeHosting(hosting) {
|
|
5
|
+
const normalized = hosting?.trim().toLowerCase().replaceAll("_", "-") || "";
|
|
6
|
+
if (normalized === "cloudflare") return "cloudflare-module";
|
|
7
|
+
return normalized;
|
|
8
|
+
}
|
|
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
|
+
//#endregion
|
|
22
|
+
//#region ../internal/src/object.ts
|
|
23
|
+
function isPlainObject(value) {
|
|
24
|
+
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
25
|
+
}
|
|
26
|
+
//#endregion
|
|
27
|
+
//#region src/integrations/upstash.ts
|
|
28
|
+
const maskedUpstashRuntimeValue = "********";
|
|
29
|
+
function hasUpstashEnv(env) {
|
|
30
|
+
const url = readEnv(env, "KV_REST_API_URL");
|
|
31
|
+
const token = readEnv(env, "KV_REST_API_TOKEN");
|
|
32
|
+
return Boolean(url && token);
|
|
33
|
+
}
|
|
34
|
+
function resolveUpstashStore(config = {}) {
|
|
35
|
+
return {
|
|
36
|
+
driver: "upstash",
|
|
37
|
+
token: trimmed(config.token) ?? maskedUpstashRuntimeValue,
|
|
38
|
+
url: trimmed(config.url) ?? maskedUpstashRuntimeValue
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
//#endregion
|
|
42
|
+
//#region src/config.ts
|
|
43
|
+
function resolveFsLiteStore(config = {}) {
|
|
44
|
+
return defu({ base: trimmed(config.base) }, {
|
|
45
|
+
driver: "fs-lite",
|
|
46
|
+
base: ".data/kv"
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
function resolveCloudflareStore(config = {}, env = process.env) {
|
|
50
|
+
return defu({
|
|
51
|
+
binding: trimmed(config.binding),
|
|
52
|
+
namespaceId: trimmed(config.namespaceId) ?? readEnv(env, "KV_NAMESPACE_ID")
|
|
53
|
+
}, {
|
|
54
|
+
driver: "cloudflare-kv-binding",
|
|
55
|
+
binding: "KV"
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
function resolveExplicitStore(store, env) {
|
|
59
|
+
switch (store.driver) {
|
|
60
|
+
case "cloudflare-kv-binding": return resolveCloudflareStore(store, env);
|
|
61
|
+
case "upstash": return resolveUpstashStore(store);
|
|
62
|
+
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".`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
function createResolvedConfig(store, stores) {
|
|
67
|
+
return stores ? {
|
|
68
|
+
store,
|
|
69
|
+
stores: {
|
|
70
|
+
default: store,
|
|
71
|
+
...stores
|
|
72
|
+
}
|
|
73
|
+
} : { store };
|
|
74
|
+
}
|
|
75
|
+
function hasStoresConfig(options) {
|
|
76
|
+
return !!options && "stores" in options && isPlainObject(options.stores);
|
|
77
|
+
}
|
|
78
|
+
function normalizeKVOptions(options, input = {}) {
|
|
79
|
+
if (options === false) return;
|
|
80
|
+
if (typeof options !== "undefined" && !isPlainObject(options)) throw new TypeError("`kv` must be a plain object.");
|
|
81
|
+
const env = input.env || process.env;
|
|
82
|
+
const hosting = normalizeHosting(input.hosting);
|
|
83
|
+
const explicit = options;
|
|
84
|
+
if (hasStoresConfig(options)) {
|
|
85
|
+
const resolvedStores = Object.fromEntries(Object.entries(options.stores).map(([name, store]) => [name, resolveExplicitStore(store, env)]));
|
|
86
|
+
const defaultStore = resolvedStores.default;
|
|
87
|
+
if (!defaultStore) throw new TypeError("`kv.stores.default` is required when using named KV stores.");
|
|
88
|
+
return createResolvedConfig(defaultStore, resolvedStores);
|
|
89
|
+
}
|
|
90
|
+
if (explicit?.driver) return createResolvedConfig(resolveExplicitStore(explicit, env));
|
|
91
|
+
if (hasUpstashEnv(env)) return createResolvedConfig(resolveUpstashStore());
|
|
92
|
+
if (hosting.includes("vercel")) return createResolvedConfig(resolveUpstashStore());
|
|
93
|
+
if (hosting.includes("cloudflare")) return createResolvedConfig(resolveCloudflareStore({}, env));
|
|
94
|
+
return createResolvedConfig(resolveFsLiteStore());
|
|
95
|
+
}
|
|
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
|
+
//#endregion
|
|
102
|
+
export { detectHosting as i, warnVercelKVFallback as n, isPlainObject as r, normalizeKVOptions as t };
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { t as readEnv } from "./env-Cs7n_E1y.js";
|
|
2
|
+
//#region src/runtime/upstash.ts
|
|
3
|
+
function isMaskedValue(value) {
|
|
4
|
+
return !value || /^\*+$/.test(value);
|
|
5
|
+
}
|
|
6
|
+
function assertRuntimeValue(value, envName) {
|
|
7
|
+
if (isMaskedValue(value)) throw new Error(`Missing runtime environment variable \`${envName}\` for Upstash KV.`);
|
|
8
|
+
}
|
|
9
|
+
function resolveRuntimeUpstashStore(config, env) {
|
|
10
|
+
const envUrl = readEnv(env, "KV_REST_API_URL");
|
|
11
|
+
const envToken = readEnv(env, "KV_REST_API_TOKEN");
|
|
12
|
+
const resolved = {
|
|
13
|
+
...config,
|
|
14
|
+
token: isMaskedValue(config.token) ? envToken || config.token : config.token,
|
|
15
|
+
url: isMaskedValue(config.url) ? envUrl || config.url : config.url
|
|
16
|
+
};
|
|
17
|
+
assertRuntimeValue(resolved.url, "KV_REST_API_URL");
|
|
18
|
+
assertRuntimeValue(resolved.token, "KV_REST_API_TOKEN");
|
|
19
|
+
return resolved;
|
|
20
|
+
}
|
|
21
|
+
function resolveRuntimeKVOptions(config, env = process.env) {
|
|
22
|
+
if (!config || config.store.driver !== "upstash") return config;
|
|
23
|
+
return {
|
|
24
|
+
...config,
|
|
25
|
+
store: resolveRuntimeUpstashStore(config.store, env)
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
//#endregion
|
|
29
|
+
//#region src/runtime/driver.ts
|
|
30
|
+
const lazyDriverMethods = new Set([
|
|
31
|
+
"clear",
|
|
32
|
+
"getItem",
|
|
33
|
+
"getKeys",
|
|
34
|
+
"hasItem",
|
|
35
|
+
"removeItem",
|
|
36
|
+
"setItem"
|
|
37
|
+
]);
|
|
38
|
+
const lazyOptionalDriverMethods = {
|
|
39
|
+
"cloudflare-kv-binding": /* @__PURE__ */ new Set(),
|
|
40
|
+
"fs-lite": new Set([
|
|
41
|
+
"getItemRaw",
|
|
42
|
+
"getMeta",
|
|
43
|
+
"setItemRaw"
|
|
44
|
+
]),
|
|
45
|
+
"upstash": new Set(["getItems"])
|
|
46
|
+
};
|
|
47
|
+
const driverLoaders = {
|
|
48
|
+
"cloudflare-kv-binding": () => import("unstorage/drivers/cloudflare-kv-binding"),
|
|
49
|
+
"fs-lite": () => import("unstorage/drivers/fs-lite"),
|
|
50
|
+
"upstash": () => import("unstorage/drivers/upstash")
|
|
51
|
+
};
|
|
52
|
+
function createLazyKVRuntimeDriver(config) {
|
|
53
|
+
let driverPromise;
|
|
54
|
+
const resolve = () => driverPromise ||= (async () => {
|
|
55
|
+
const runtime = resolveRuntimeKVOptions(config);
|
|
56
|
+
if (!runtime) throw new Error("KV runtime is disabled.");
|
|
57
|
+
const { default: factory } = await driverLoaders[runtime.store.driver]();
|
|
58
|
+
return factory(runtime.store);
|
|
59
|
+
})();
|
|
60
|
+
const target = {
|
|
61
|
+
name: `lazy:${config.store.driver}`,
|
|
62
|
+
options: config.store
|
|
63
|
+
};
|
|
64
|
+
return new Proxy(target, { get(t, prop) {
|
|
65
|
+
const own = t[prop];
|
|
66
|
+
if (own !== void 0) return own;
|
|
67
|
+
if (prop === "dispose") return async () => {
|
|
68
|
+
if (!driverPromise) return;
|
|
69
|
+
const driver = await driverPromise;
|
|
70
|
+
return driver.dispose?.call(driver);
|
|
71
|
+
};
|
|
72
|
+
const optionalMethods = lazyOptionalDriverMethods[config.store.driver];
|
|
73
|
+
if (!lazyDriverMethods.has(prop) && !optionalMethods.has(prop)) return void 0;
|
|
74
|
+
return async (...args) => {
|
|
75
|
+
const driver = await resolve();
|
|
76
|
+
return driver[prop]?.apply(driver, args);
|
|
77
|
+
};
|
|
78
|
+
} });
|
|
79
|
+
}
|
|
80
|
+
//#endregion
|
|
81
|
+
export { createLazyKVRuntimeDriver as t };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
//#region ../internal/src/env.ts
|
|
2
|
+
function trimmed(value) {
|
|
3
|
+
return typeof value === "string" && value.trim().length > 0 ? value.trim() : void 0;
|
|
4
|
+
}
|
|
5
|
+
function readEnv(env, ...names) {
|
|
6
|
+
for (const name of names) {
|
|
7
|
+
const value = trimmed(env[name]);
|
|
8
|
+
if (value) return value;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
//#endregion
|
|
12
|
+
export { trimmed as n, readEnv as t };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { a as KVStorage, c as ResolvedFsLiteKVStoreConfig, d as ResolvedUpstashKVStoreConfig, f as UpstashKVStoreConfig, i as KVModuleOptions, l as ResolvedKVModuleOptions, n as FsLiteKVStoreConfig, o as KVStoreConfig, r as KVDriver, s as ResolvedCloudflareKVStoreConfig, t as CloudflareKVStoreConfig, u as ResolvedKVStoreConfig } from "./types-27OsNCU6.js";
|
|
2
|
+
|
|
3
|
+
//#region src/runtime/storage.d.ts
|
|
4
|
+
declare const kv: KVStorage;
|
|
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 };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { t as readEnv } from "./env-Cs7n_E1y.js";
|
|
2
|
+
import { t as normalizeKVOptions } from "./config-Bu0hVhEv.js";
|
|
3
|
+
import { t as createLazyKVRuntimeDriver } from "./driver-CPgLtkh4.js";
|
|
4
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
5
|
+
import { createStorage } from "unstorage";
|
|
6
|
+
//#region ../internal/src/runtime/cloudflare-env.ts
|
|
7
|
+
let activeEnv;
|
|
8
|
+
const activeEnvStorage = new AsyncLocalStorage();
|
|
9
|
+
function getActiveCloudflareEnv() {
|
|
10
|
+
return activeEnvStorage.getStore() ?? activeEnv ?? globalThis.__env__;
|
|
11
|
+
}
|
|
12
|
+
//#endregion
|
|
13
|
+
//#region src/runtime/hosted-storage.ts
|
|
14
|
+
function assertHostedConfig(config) {
|
|
15
|
+
if (!config) throw new Error("[vitehub] `@vite-hub/kv` requires `hubKv()` and `kv !== false`.");
|
|
16
|
+
return config;
|
|
17
|
+
}
|
|
18
|
+
function createHostedKVStorage(config) {
|
|
19
|
+
return createNamedHostedKVStorage(config, "default");
|
|
20
|
+
}
|
|
21
|
+
function createNamedHostedKVStorage(config, name) {
|
|
22
|
+
const resolved = assertHostedConfig(config);
|
|
23
|
+
const store = (resolved.stores || { default: resolved.store })[name];
|
|
24
|
+
if (!store) throw new Error(`[vitehub] Unknown KV store "${name}".`);
|
|
25
|
+
return createStorage({ driver: createLazyKVRuntimeDriver({
|
|
26
|
+
store,
|
|
27
|
+
stores: {
|
|
28
|
+
default: store,
|
|
29
|
+
[name]: store
|
|
30
|
+
}
|
|
31
|
+
}) });
|
|
32
|
+
}
|
|
33
|
+
//#endregion
|
|
34
|
+
//#region src/runtime/storage.ts
|
|
35
|
+
const storagePromises = /* @__PURE__ */ new Map();
|
|
36
|
+
function inferHosting(env) {
|
|
37
|
+
if (getActiveCloudflareEnv()) return "cloudflare";
|
|
38
|
+
const explicit = readEnv(env, "VITEHUB_HOSTING", "NITRO_PRESET");
|
|
39
|
+
if (explicit) return explicit;
|
|
40
|
+
return readEnv(env, "KV_REST_API_URL") ? "vercel" : void 0;
|
|
41
|
+
}
|
|
42
|
+
function shouldFallbackHostedConfigImport(error) {
|
|
43
|
+
const code = error?.code;
|
|
44
|
+
return code === "MODULE_NOT_FOUND" || code === "ERR_MODULE_NOT_FOUND" || code === "ERR_PACKAGE_IMPORT_NOT_DEFINED" || code === "ERR_UNSUPPORTED_ESM_URL_SCHEME";
|
|
45
|
+
}
|
|
46
|
+
async function resolveHostedConfig() {
|
|
47
|
+
const virtualConfigId = "#vitehub/kv/config";
|
|
48
|
+
try {
|
|
49
|
+
return (await import(
|
|
50
|
+
/* @vite-ignore */
|
|
51
|
+
virtualConfigId
|
|
52
|
+
)).kv;
|
|
53
|
+
} catch (error) {
|
|
54
|
+
if (!shouldFallbackHostedConfigImport(error)) throw error;
|
|
55
|
+
const env = typeof process !== "undefined" ? process.env : {};
|
|
56
|
+
return normalizeKVOptions(void 0, {
|
|
57
|
+
env,
|
|
58
|
+
hosting: inferHosting(env)
|
|
59
|
+
}) || false;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
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
|
+
async function resolveStorage(name = "default") {
|
|
74
|
+
const existing = storagePromises.get(name);
|
|
75
|
+
if (existing) return existing;
|
|
76
|
+
if (inferHosting(typeof process !== "undefined" ? process.env : {}) === "vercel") {
|
|
77
|
+
const promise = resolveHostedConfig().then((config) => name === "default" ? createHostedKVStorage(config) : createNamedHostedKVStorage(config, name));
|
|
78
|
+
storagePromises.set(name, promise);
|
|
79
|
+
return promise;
|
|
80
|
+
}
|
|
81
|
+
const storageName = name === "default" ? "kv" : `kv:${name}`;
|
|
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
|
+
});
|
|
86
|
+
storagePromises.set(name, promise);
|
|
87
|
+
return promise;
|
|
88
|
+
}
|
|
89
|
+
function createKVStorage(name = "default") {
|
|
90
|
+
return {
|
|
91
|
+
async clear(base, options) {
|
|
92
|
+
await (await resolveStorage(name)).clear(base, options);
|
|
93
|
+
},
|
|
94
|
+
async del(key, options) {
|
|
95
|
+
await (await resolveStorage(name)).removeItem(key, options);
|
|
96
|
+
},
|
|
97
|
+
async get(key, options) {
|
|
98
|
+
return (await resolveStorage(name)).getItem(key, options);
|
|
99
|
+
},
|
|
100
|
+
async has(key, options) {
|
|
101
|
+
return (await resolveStorage(name)).hasItem(key, options);
|
|
102
|
+
},
|
|
103
|
+
async keys(base, options) {
|
|
104
|
+
return (await resolveStorage(name)).getKeys(base, options);
|
|
105
|
+
},
|
|
106
|
+
async set(key, value, options) {
|
|
107
|
+
await (await resolveStorage(name)).setItem(key, value, options);
|
|
108
|
+
},
|
|
109
|
+
store(storeName) {
|
|
110
|
+
return createKVStorage(storeName);
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
const kv = createKVStorage();
|
|
115
|
+
//#endregion
|
|
116
|
+
export { kv };
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
import { n as trimmed, t as readEnv } from "./env-Cs7n_E1y.js";
|
|
2
|
+
import { i as detectHosting, n as warnVercelKVFallback, r as isPlainObject, t as normalizeKVOptions } from "./config-Bu0hVhEv.js";
|
|
3
|
+
import { i as resolveRuntimeEntry$1, r as assertNoVitePluginInNitro } from "./nitro-B8aUYcjq.js";
|
|
4
|
+
import { readFile } from "node:fs/promises";
|
|
5
|
+
//#region ../../node_modules/.pnpm/pathe@2.0.3/node_modules/pathe/dist/shared/pathe.M-eThtNZ.mjs
|
|
6
|
+
const _DRIVE_LETTER_START_RE = /^[A-Za-z]:\//;
|
|
7
|
+
function normalizeWindowsPath(input = "") {
|
|
8
|
+
if (!input) return input;
|
|
9
|
+
return input.replace(/\\/g, "/").replace(_DRIVE_LETTER_START_RE, (r) => r.toUpperCase());
|
|
10
|
+
}
|
|
11
|
+
const _IS_ABSOLUTE_RE = /^[/\\](?![/\\])|^[/\\]{2}(?!\.)|^[A-Za-z]:[/\\]/;
|
|
12
|
+
function cwd() {
|
|
13
|
+
if (typeof process !== "undefined" && typeof process.cwd === "function") return process.cwd().replace(/\\/g, "/");
|
|
14
|
+
return "/";
|
|
15
|
+
}
|
|
16
|
+
const resolve = function(...arguments_) {
|
|
17
|
+
arguments_ = arguments_.map((argument) => normalizeWindowsPath(argument));
|
|
18
|
+
let resolvedPath = "";
|
|
19
|
+
let resolvedAbsolute = false;
|
|
20
|
+
for (let index = arguments_.length - 1; index >= -1 && !resolvedAbsolute; index--) {
|
|
21
|
+
const path = index >= 0 ? arguments_[index] : cwd();
|
|
22
|
+
if (!path || path.length === 0) continue;
|
|
23
|
+
resolvedPath = `${path}/${resolvedPath}`;
|
|
24
|
+
resolvedAbsolute = isAbsolute(path);
|
|
25
|
+
}
|
|
26
|
+
resolvedPath = normalizeString(resolvedPath, !resolvedAbsolute);
|
|
27
|
+
if (resolvedAbsolute && !isAbsolute(resolvedPath)) return `/${resolvedPath}`;
|
|
28
|
+
return resolvedPath.length > 0 ? resolvedPath : ".";
|
|
29
|
+
};
|
|
30
|
+
function normalizeString(path, allowAboveRoot) {
|
|
31
|
+
let res = "";
|
|
32
|
+
let lastSegmentLength = 0;
|
|
33
|
+
let lastSlash = -1;
|
|
34
|
+
let dots = 0;
|
|
35
|
+
let char = null;
|
|
36
|
+
for (let index = 0; index <= path.length; ++index) {
|
|
37
|
+
if (index < path.length) char = path[index];
|
|
38
|
+
else if (char === "/") break;
|
|
39
|
+
else char = "/";
|
|
40
|
+
if (char === "/") {
|
|
41
|
+
if (lastSlash === index - 1 || dots === 1);
|
|
42
|
+
else if (dots === 2) {
|
|
43
|
+
if (res.length < 2 || lastSegmentLength !== 2 || res[res.length - 1] !== "." || res[res.length - 2] !== ".") {
|
|
44
|
+
if (res.length > 2) {
|
|
45
|
+
const lastSlashIndex = res.lastIndexOf("/");
|
|
46
|
+
if (lastSlashIndex === -1) {
|
|
47
|
+
res = "";
|
|
48
|
+
lastSegmentLength = 0;
|
|
49
|
+
} else {
|
|
50
|
+
res = res.slice(0, lastSlashIndex);
|
|
51
|
+
lastSegmentLength = res.length - 1 - res.lastIndexOf("/");
|
|
52
|
+
}
|
|
53
|
+
lastSlash = index;
|
|
54
|
+
dots = 0;
|
|
55
|
+
continue;
|
|
56
|
+
} else if (res.length > 0) {
|
|
57
|
+
res = "";
|
|
58
|
+
lastSegmentLength = 0;
|
|
59
|
+
lastSlash = index;
|
|
60
|
+
dots = 0;
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (allowAboveRoot) {
|
|
65
|
+
res += res.length > 0 ? "/.." : "..";
|
|
66
|
+
lastSegmentLength = 2;
|
|
67
|
+
}
|
|
68
|
+
} else {
|
|
69
|
+
if (res.length > 0) res += `/${path.slice(lastSlash + 1, index)}`;
|
|
70
|
+
else res = path.slice(lastSlash + 1, index);
|
|
71
|
+
lastSegmentLength = index - lastSlash - 1;
|
|
72
|
+
}
|
|
73
|
+
lastSlash = index;
|
|
74
|
+
dots = 0;
|
|
75
|
+
} else if (char === "." && dots !== -1) ++dots;
|
|
76
|
+
else dots = -1;
|
|
77
|
+
}
|
|
78
|
+
return res;
|
|
79
|
+
}
|
|
80
|
+
const isAbsolute = function(p) {
|
|
81
|
+
return _IS_ABSOLUTE_RE.test(p);
|
|
82
|
+
};
|
|
83
|
+
//#endregion
|
|
84
|
+
//#region ../internal/src/feature-bridge/feature-options.ts
|
|
85
|
+
function cloneFeatureOptions(feature, options) {
|
|
86
|
+
if (!isPlainObject(options)) throw new TypeError(`[vitehub] \`${feature}\` must be a plain object.`);
|
|
87
|
+
return { ...options };
|
|
88
|
+
}
|
|
89
|
+
function normalizeFeatureOptions(feature, options) {
|
|
90
|
+
if (options === false || typeof options === "undefined") return void 0;
|
|
91
|
+
return cloneFeatureOptions(feature, options);
|
|
92
|
+
}
|
|
93
|
+
//#endregion
|
|
94
|
+
//#region ../internal/src/feature-bridge/feature-engine.ts
|
|
95
|
+
function createFeatureEngine(options) {
|
|
96
|
+
return options;
|
|
97
|
+
}
|
|
98
|
+
function normalizeFeaturePublicOptions(feature, options) {
|
|
99
|
+
return normalizeFeatureOptions(feature, options);
|
|
100
|
+
}
|
|
101
|
+
function readFeaturePublicOptions(source, key) {
|
|
102
|
+
if (source.kind === "vite") return source.userConfig[key];
|
|
103
|
+
return source.nitro.options[key];
|
|
104
|
+
}
|
|
105
|
+
function resolveDefaultOptions(defaultOptions) {
|
|
106
|
+
return typeof defaultOptions === "function" ? defaultOptions() : defaultOptions;
|
|
107
|
+
}
|
|
108
|
+
function applyRuntimeConfig(runtimeConfig, key, config) {
|
|
109
|
+
runtimeConfig[key] = config;
|
|
110
|
+
}
|
|
111
|
+
async function readWorkspaceDeps(rootDir) {
|
|
112
|
+
const packageJson = JSON.parse(await readFile(resolve(rootDir, "package.json"), "utf8"));
|
|
113
|
+
return {
|
|
114
|
+
...packageJson.dependencies,
|
|
115
|
+
...packageJson.devDependencies
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
function resolveViteRoot(config) {
|
|
119
|
+
return resolve(process.cwd(), typeof config.root === "string" ? config.root : ".");
|
|
120
|
+
}
|
|
121
|
+
function resolveRawOptions(engine, source) {
|
|
122
|
+
const rawOptions = engine.readPublicOptions(source);
|
|
123
|
+
return typeof rawOptions === "undefined" && typeof engine.defaultOptions !== "undefined" ? resolveDefaultOptions(engine.defaultOptions) : rawOptions;
|
|
124
|
+
}
|
|
125
|
+
function resolveNormalizedConfig(engine, source) {
|
|
126
|
+
const rawOptions = resolveRawOptions(engine, source);
|
|
127
|
+
const hosting = source.kind === "vite" ? detectHosting({ options: source.userConfig }) : detectHosting(source.nitro);
|
|
128
|
+
if (rawOptions === false) return {
|
|
129
|
+
config: false,
|
|
130
|
+
hosting: hosting || void 0
|
|
131
|
+
};
|
|
132
|
+
const normalizedOptions = engine.normalizeOptions(rawOptions);
|
|
133
|
+
if (!normalizedOptions) return void 0;
|
|
134
|
+
return {
|
|
135
|
+
config: engine.resolveConfig ? engine.resolveConfig(normalizedOptions, hosting) : normalizedOptions,
|
|
136
|
+
hosting: hosting || void 0
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
async function buildFeatureResolvedState(engine, source) {
|
|
140
|
+
const resolved = resolveNormalizedConfig(engine, source);
|
|
141
|
+
if (!resolved) return void 0;
|
|
142
|
+
const rootDir = source.kind === "vite" ? resolveViteRoot(source.userConfig) : source.nitro.options.rootDir;
|
|
143
|
+
const runtimeConfig = source.kind === "nitro" ? source.nitro.options.runtimeConfig ||= {} : {};
|
|
144
|
+
if (resolved.hosting) runtimeConfig.hosting ||= resolved.hosting;
|
|
145
|
+
if (engine.assignRuntimeConfig) engine.assignRuntimeConfig(runtimeConfig, resolved.config);
|
|
146
|
+
else applyRuntimeConfig(runtimeConfig, engine.configKey, resolved.config);
|
|
147
|
+
const deps = engine.loadDeps ? await readWorkspaceDeps(rootDir) : {};
|
|
148
|
+
return {
|
|
149
|
+
rootDir,
|
|
150
|
+
config: resolved.config,
|
|
151
|
+
deps,
|
|
152
|
+
runtimeConfig,
|
|
153
|
+
hosting: resolved.hosting
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
async function buildFeatureNitroContext(engine, nitro) {
|
|
157
|
+
return await buildFeatureResolvedState(engine, {
|
|
158
|
+
kind: "nitro",
|
|
159
|
+
nitro
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
function createFeatureNitroBridge(engine) {
|
|
163
|
+
return {
|
|
164
|
+
name: engine.name,
|
|
165
|
+
async setup(nitro) {
|
|
166
|
+
await assertNoVitePluginInNitro(nitro, `${engine.name}/vite`, `${engine.name}/nitro`);
|
|
167
|
+
const context = await buildFeatureNitroContext(engine, nitro);
|
|
168
|
+
if (!context) return;
|
|
169
|
+
await engine.setupNitro?.(nitro, context);
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
//#endregion
|
|
174
|
+
//#region ../internal/src/arrays.ts
|
|
175
|
+
function pushUnique(arr, item, key = (value) => value) {
|
|
176
|
+
const id = key(item);
|
|
177
|
+
if (arr.some((entry) => key(entry) === id)) return;
|
|
178
|
+
arr.push(item);
|
|
179
|
+
}
|
|
180
|
+
//#endregion
|
|
181
|
+
//#region src/integrations/cloudflare.ts
|
|
182
|
+
function configureCloudflareKV(target, config) {
|
|
183
|
+
for (const store of Object.values(config.stores || { default: config.store })) {
|
|
184
|
+
if (store.driver !== "cloudflare-kv-binding" || !store.namespaceId) continue;
|
|
185
|
+
const { binding, namespaceId } = store;
|
|
186
|
+
target.cloudflare ||= {};
|
|
187
|
+
target.cloudflare.wrangler ||= {};
|
|
188
|
+
target.cloudflare.wrangler.kv_namespaces ||= [];
|
|
189
|
+
pushUnique(target.cloudflare.wrangler.kv_namespaces, {
|
|
190
|
+
binding,
|
|
191
|
+
id: namespaceId
|
|
192
|
+
}, (entry) => entry.binding);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
//#endregion
|
|
196
|
+
//#region src/vite-config.ts
|
|
197
|
+
const KV_VITE_PLUGIN_NAME = "@vite-hub/kv/vite";
|
|
198
|
+
const KV_VIRTUAL_CONFIG_ID = "#vitehub/kv/config";
|
|
199
|
+
function resolveHosting(input) {
|
|
200
|
+
const env = input.env || process.env;
|
|
201
|
+
return trimmed(input.hosting) ?? readEnv(env, "NITRO_PRESET", "VITEHUB_HOSTING");
|
|
202
|
+
}
|
|
203
|
+
function resolveKVViteConfig(kv, input = {}) {
|
|
204
|
+
const env = input.env || process.env;
|
|
205
|
+
const hosting = resolveHosting(input);
|
|
206
|
+
return {
|
|
207
|
+
hosting,
|
|
208
|
+
kv: normalizeKVOptions(kv, {
|
|
209
|
+
env,
|
|
210
|
+
hosting
|
|
211
|
+
}) ?? false
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
//#endregion
|
|
215
|
+
//#region src/feature.ts
|
|
216
|
+
function resolveRuntimeEntry(srcRelative, packageSubpath) {
|
|
217
|
+
return resolveRuntimeEntry$1(srcRelative, packageSubpath, import.meta.url);
|
|
218
|
+
}
|
|
219
|
+
function normalizeKVPublicOptions(options) {
|
|
220
|
+
return normalizeFeaturePublicOptions("kv", options);
|
|
221
|
+
}
|
|
222
|
+
function readKVPublicOptions(source) {
|
|
223
|
+
const options = readFeaturePublicOptions(source, "kv");
|
|
224
|
+
return typeof options === "undefined" ? {} : options;
|
|
225
|
+
}
|
|
226
|
+
//#endregion
|
|
227
|
+
//#region src/nitro/module.ts
|
|
228
|
+
const kvNitroModule = createFeatureNitroBridge(createFeatureEngine({
|
|
229
|
+
name: "@vite-hub/kv",
|
|
230
|
+
feature: "kv",
|
|
231
|
+
configKey: "kv",
|
|
232
|
+
normalizeOptions: normalizeKVPublicOptions,
|
|
233
|
+
resolveConfig(options, hosting) {
|
|
234
|
+
return resolveKVViteConfig(options, {
|
|
235
|
+
env: process.env,
|
|
236
|
+
hosting
|
|
237
|
+
}).kv;
|
|
238
|
+
},
|
|
239
|
+
readPublicOptions: readKVPublicOptions,
|
|
240
|
+
setupNitro(nitro, context) {
|
|
241
|
+
if (!context.config) return;
|
|
242
|
+
const target = nitro;
|
|
243
|
+
const resolved = context.config;
|
|
244
|
+
target.options.alias ||= {};
|
|
245
|
+
target.options.alias["@vite-hub/kv"] = resolveRuntimeEntry("./index", "@vite-hub/kv");
|
|
246
|
+
target.options.plugins ||= [];
|
|
247
|
+
const plugin = resolveRuntimeEntry("./runtime/nitro-plugin", "@vite-hub/kv/runtime/nitro-plugin");
|
|
248
|
+
if (!target.options.plugins.includes(plugin)) target.options.plugins.push(plugin);
|
|
249
|
+
target.options.storage ||= {};
|
|
250
|
+
target.options.storage.kv = resolved.store;
|
|
251
|
+
configureCloudflareKV(target.options, resolved);
|
|
252
|
+
warnVercelKVFallback(target, resolved, context.hosting);
|
|
253
|
+
}
|
|
254
|
+
}));
|
|
255
|
+
//#endregion
|
|
256
|
+
export { resolveKVViteConfig as i, KV_VIRTUAL_CONFIG_ID as n, KV_VITE_PLUGIN_NAME as r, kvNitroModule as t };
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { resolveModulePath } from "exsolve";
|
|
2
|
+
//#region ../internal/src/nitro.ts
|
|
3
|
+
function resolveRuntimeEntry(srcRelative, packageSubpath, importMetaUrl) {
|
|
4
|
+
return resolveModulePath(srcRelative, {
|
|
5
|
+
extensions: [".ts", ".mts"],
|
|
6
|
+
from: importMetaUrl,
|
|
7
|
+
try: true
|
|
8
|
+
}) ?? resolveModulePath(packageSubpath, {
|
|
9
|
+
extensions: [".js", ".mjs"],
|
|
10
|
+
from: importMetaUrl
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
function isPromiseLike(value) {
|
|
14
|
+
return typeof value === "object" && value !== null && "then" in value && typeof value.then === "function";
|
|
15
|
+
}
|
|
16
|
+
async function hasNamedVitePlugin(plugin, name) {
|
|
17
|
+
if (isPromiseLike(plugin)) return hasNamedVitePlugin(await plugin, name);
|
|
18
|
+
if (Array.isArray(plugin)) {
|
|
19
|
+
for (const entry of plugin) if (await hasNamedVitePlugin(entry, name)) return true;
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
return typeof plugin === "object" && plugin !== null && "name" in plugin && plugin.name === name;
|
|
23
|
+
}
|
|
24
|
+
function hasNitroModule(entry, moduleId, moduleName) {
|
|
25
|
+
if (entry === moduleId) return true;
|
|
26
|
+
if (typeof entry !== "object" || entry === null) return false;
|
|
27
|
+
const module = "nitro" in entry ? entry.nitro : entry;
|
|
28
|
+
return typeof module === "object" && module !== null && "name" in module && module.name === moduleName;
|
|
29
|
+
}
|
|
30
|
+
async function assertNoVitePlugin(vite, vitePluginName, integrationName) {
|
|
31
|
+
if (await hasNamedVitePlugin(vite?.plugins, vitePluginName)) throw new Error(`[vitehub] Do not configure ${vitePluginName} when using ${integrationName}.`);
|
|
32
|
+
}
|
|
33
|
+
async function assertNoVitePluginInNitro(nitro, vitePluginName, integrationName) {
|
|
34
|
+
await assertNoVitePlugin(nitro.options.vite, vitePluginName, integrationName);
|
|
35
|
+
}
|
|
36
|
+
function assertNoNitroModule(nitro, moduleId, moduleName, integrationName) {
|
|
37
|
+
if (nitro.modules?.some((entry) => hasNitroModule(entry, moduleId, moduleName))) throw new Error(`[vitehub] Do not configure ${moduleId} when using ${integrationName}.`);
|
|
38
|
+
}
|
|
39
|
+
//#endregion
|
|
40
|
+
export { resolveRuntimeEntry as i, assertNoVitePlugin as n, assertNoVitePluginInNitro as r, assertNoNitroModule as t };
|