@vite-hub/kv 0.0.1 → 0.0.2
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 +383 -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/dist/vite.js
CHANGED
|
@@ -1,6 +1,265 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { n as readEnv, r as trimmed, t as normalizeKVOptions } from "./config-C1ofyzSt.js";
|
|
2
|
+
import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
|
|
3
|
+
import { basename, join, resolve } from "node:path";
|
|
4
|
+
//#region src/vite-config.ts
|
|
5
|
+
const KV_VITE_PLUGIN_NAME = "@vite-hub/kv/vite";
|
|
6
|
+
const KV_VIRTUAL_CONFIG_ID = "#vitehub/kv/config";
|
|
7
|
+
function resolveHosting(input) {
|
|
8
|
+
const env = input.env || process.env;
|
|
9
|
+
return trimmed(input.hosting) ?? readEnv(env, "VITEHUB_HOSTING");
|
|
10
|
+
}
|
|
11
|
+
function resolveKVViteConfig(kv, input = {}) {
|
|
12
|
+
const env = input.env || process.env;
|
|
13
|
+
const hosting = resolveHosting(input);
|
|
14
|
+
return {
|
|
15
|
+
hosting,
|
|
16
|
+
kv: normalizeKVOptions(kv, {
|
|
17
|
+
env,
|
|
18
|
+
hosting
|
|
19
|
+
}) ?? false
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
//#region ../internal/src/build/provider-output-config.ts
|
|
24
|
+
function isProviderJsonArray(value, ancestors) {
|
|
25
|
+
if (Object.getPrototypeOf(value) !== Array.prototype || ancestors.has(value)) return false;
|
|
26
|
+
if (Reflect.ownKeys(value).length !== value.length + 1) return false;
|
|
27
|
+
ancestors.add(value);
|
|
28
|
+
try {
|
|
29
|
+
for (let index = 0; index < value.length; index++) {
|
|
30
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, String(index));
|
|
31
|
+
if (!descriptor?.enumerable || !("value" in descriptor) || !isProviderJsonValue(descriptor.value, ancestors)) return false;
|
|
32
|
+
}
|
|
33
|
+
return true;
|
|
34
|
+
} finally {
|
|
35
|
+
ancestors.delete(value);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
function isProviderJsonRecordValue(value, ancestors) {
|
|
39
|
+
const prototype = Object.getPrototypeOf(value);
|
|
40
|
+
if (prototype !== Object.prototype && prototype !== null || ancestors.has(value)) return false;
|
|
41
|
+
ancestors.add(value);
|
|
42
|
+
try {
|
|
43
|
+
return Reflect.ownKeys(value).every((key) => {
|
|
44
|
+
if (typeof key !== "string") return false;
|
|
45
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, key);
|
|
46
|
+
return descriptor?.enumerable === true && "value" in descriptor && isProviderJsonValue(descriptor.value, ancestors);
|
|
47
|
+
});
|
|
48
|
+
} finally {
|
|
49
|
+
ancestors.delete(value);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
function isProviderJsonValue(value, ancestors) {
|
|
53
|
+
if (value === null || typeof value === "boolean" || typeof value === "string") return true;
|
|
54
|
+
if (typeof value === "number") return Number.isFinite(value);
|
|
55
|
+
if (Array.isArray(value)) return isProviderJsonArray(value, ancestors);
|
|
56
|
+
return typeof value === "object" && isProviderJsonRecordValue(value, ancestors);
|
|
57
|
+
}
|
|
58
|
+
function isProviderJsonRecord(value) {
|
|
59
|
+
try {
|
|
60
|
+
return typeof value === "object" && value !== null && !Array.isArray(value) && isProviderJsonRecordValue(value, /* @__PURE__ */ new Set());
|
|
61
|
+
} catch {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
function assertProviderJsonRecord(value) {
|
|
66
|
+
if (!isProviderJsonRecord(value)) throw new TypeError("[vitehub] Provider output config must be a JSON object.");
|
|
67
|
+
}
|
|
68
|
+
function isFileNotFoundError(error) {
|
|
69
|
+
return error instanceof Error && "code" in error && error.code === "ENOENT";
|
|
70
|
+
}
|
|
71
|
+
async function readProviderJsonRecord(file) {
|
|
72
|
+
try {
|
|
73
|
+
const parsed = JSON.parse(await readFile(file, "utf8"));
|
|
74
|
+
assertProviderJsonRecord(parsed);
|
|
75
|
+
return parsed;
|
|
76
|
+
} catch (error) {
|
|
77
|
+
if (isFileNotFoundError(error)) return;
|
|
78
|
+
throw error;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
function deleteOwnedFields(value, ownership) {
|
|
82
|
+
const next = { ...value };
|
|
83
|
+
for (const key of ownership.keys ?? []) delete next[key];
|
|
84
|
+
for (const field of Object.keys(ownership.arrays ?? {})) delete next[field];
|
|
85
|
+
return next;
|
|
86
|
+
}
|
|
87
|
+
function getKeyedArrayEntryValue(value, key) {
|
|
88
|
+
if (!isProviderJsonRecord(value)) return;
|
|
89
|
+
const entryValue = value[key];
|
|
90
|
+
return entryValue === null || typeof entryValue === "boolean" || typeof entryValue === "number" || typeof entryValue === "string" ? entryValue : void 0;
|
|
91
|
+
}
|
|
92
|
+
function preserveUnownedKeyedArrayEntries(value, ownership, replacements = []) {
|
|
93
|
+
const current = Array.isArray(value) ? value : [];
|
|
94
|
+
const replacementKeys = replacements.flatMap((entry) => {
|
|
95
|
+
const key = getKeyedArrayEntryValue(entry, ownership.key);
|
|
96
|
+
return key === void 0 ? [] : [key];
|
|
97
|
+
});
|
|
98
|
+
const ownedKeys = new Set([...ownership.values ?? [], ...replacementKeys]);
|
|
99
|
+
return current.filter((entry) => {
|
|
100
|
+
const key = getKeyedArrayEntryValue(entry, ownership.key);
|
|
101
|
+
return key === void 0 || !ownedKeys.has(key);
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
function mergeOwnedArrays(existing, value, arrays) {
|
|
105
|
+
const next = { ...value };
|
|
106
|
+
for (const [field, ownership] of Object.entries(arrays ?? {})) {
|
|
107
|
+
const incoming = Array.isArray(value[field]) ? value[field] : [];
|
|
108
|
+
const merged = [...preserveUnownedKeyedArrayEntries(existing[field], ownership, incoming), ...incoming];
|
|
109
|
+
if (merged.length) next[field] = merged;
|
|
110
|
+
else delete next[field];
|
|
111
|
+
}
|
|
112
|
+
return next;
|
|
113
|
+
}
|
|
114
|
+
async function persistProviderJsonRecord(file, value, removeIfEmpty) {
|
|
115
|
+
if (removeIfEmpty && !Object.keys(value).length) {
|
|
116
|
+
await rm(file, { force: true });
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
await writeFile(file, `${JSON.stringify(value, null, 2)}\n`, "utf8");
|
|
120
|
+
}
|
|
121
|
+
async function writeProviderOutputConfig(file, value, ownership = {}, options = {}) {
|
|
122
|
+
await persistProviderJsonRecord(file, mergeProviderOutputConfig(await readProviderJsonRecord(file) ?? {}, value, ownership), options.removeIfEmpty === true);
|
|
123
|
+
}
|
|
124
|
+
function mergeProviderOutputConfig(existing, value, ownership = {}) {
|
|
125
|
+
assertProviderJsonRecord(existing);
|
|
126
|
+
assertProviderJsonRecord(value);
|
|
127
|
+
return {
|
|
128
|
+
...deleteOwnedFields(existing, ownership),
|
|
129
|
+
...mergeOwnedArrays(existing, value, ownership.arrays)
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
async function cleanProviderOutputConfig(file, ownership) {
|
|
133
|
+
if (!ownership.keys?.length && !Object.keys(ownership.arrays ?? {}).length) return;
|
|
134
|
+
const existing = await readProviderJsonRecord(file);
|
|
135
|
+
if (!existing) return;
|
|
136
|
+
const next = deleteOwnedFields(existing, ownership);
|
|
137
|
+
let changed = (ownership.keys ?? []).some((key) => key in existing);
|
|
138
|
+
for (const [field, arrayOwnership] of Object.entries(ownership.arrays ?? {})) {
|
|
139
|
+
if (!(field in existing)) continue;
|
|
140
|
+
const current = existing[field];
|
|
141
|
+
const preserved = preserveUnownedKeyedArrayEntries(current, arrayOwnership);
|
|
142
|
+
changed ||= !Array.isArray(current) || preserved.length !== current.length || preserved.length === 0;
|
|
143
|
+
if (preserved.length) next[field] = preserved;
|
|
144
|
+
}
|
|
145
|
+
if (!changed) return;
|
|
146
|
+
await persistProviderJsonRecord(file, next, true);
|
|
147
|
+
}
|
|
148
|
+
//#endregion
|
|
149
|
+
//#region ../internal/src/build/user-entry.ts
|
|
150
|
+
function toSafeAppName(rootDir) {
|
|
151
|
+
return basename(rootDir).replace(/[^a-z0-9-]+/gi, "-").replace(/-+/g, "-").replace(/^-|-$/g, "").toLowerCase();
|
|
152
|
+
}
|
|
153
|
+
//#endregion
|
|
154
|
+
//#region ../internal/src/build/cloudflare.ts
|
|
155
|
+
function createDefaultCloudflareOutputRoot(rootDir) {
|
|
156
|
+
return resolve(rootDir, "dist", toSafeAppName(rootDir));
|
|
157
|
+
}
|
|
158
|
+
async function writeCloudflareWranglerConfig(options) {
|
|
159
|
+
const outputRoot = options.outputRoot ?? createDefaultCloudflareOutputRoot(options.rootDir);
|
|
160
|
+
const configFile = resolve(outputRoot, "wrangler.json");
|
|
161
|
+
if (!options.wranglerConfig) {
|
|
162
|
+
await cleanProviderOutputConfig(configFile, options.wranglerConfigOwnership ?? {});
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
await mkdir(outputRoot, { recursive: true });
|
|
166
|
+
await writeProviderOutputConfig(configFile, options.wranglerConfig, options.wranglerConfigOwnership, { removeIfEmpty: true });
|
|
167
|
+
}
|
|
168
|
+
const VITEHUB_MODES = {
|
|
169
|
+
e2e: "e2e",
|
|
170
|
+
blob: "blob",
|
|
171
|
+
chat: "chat",
|
|
172
|
+
db: "db",
|
|
173
|
+
env: "env",
|
|
174
|
+
kv: "kv",
|
|
175
|
+
queue: "queue",
|
|
176
|
+
schedule: "schedule",
|
|
177
|
+
sandbox: "sandbox",
|
|
178
|
+
workspace: "workspace",
|
|
179
|
+
workflow: "workflow"
|
|
180
|
+
};
|
|
181
|
+
function isViteCli(argv) {
|
|
182
|
+
return argv.some((arg) => /(?:^|[/\\])vite(?:\.[cm]?js)?$/.test(arg) || arg === "vite");
|
|
183
|
+
}
|
|
184
|
+
function getViteCliMode(argv = process.argv) {
|
|
185
|
+
if (!isViteCli(argv)) return;
|
|
186
|
+
const modes = new Set(Object.values(VITEHUB_MODES));
|
|
187
|
+
for (let index = 0; index < argv.length; index += 1) {
|
|
188
|
+
const arg = argv[index];
|
|
189
|
+
const mode = arg === "--mode" || arg === "-m" ? argv[index + 1] : arg.startsWith("--mode=") ? arg.slice(7) : void 0;
|
|
190
|
+
if (mode && modes.has(mode)) return mode;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
function getViteMode(env = process.env) {
|
|
194
|
+
return env["VITEHUB_VITE_MODE"] ?? getViteCliMode();
|
|
195
|
+
}
|
|
196
|
+
[
|
|
197
|
+
".ts",
|
|
198
|
+
".mts",
|
|
199
|
+
".cts",
|
|
200
|
+
".js",
|
|
201
|
+
".mjs",
|
|
202
|
+
".cjs"
|
|
203
|
+
].flatMap((extension) => [["server", `email${extension}`], [`server.email${extension}`]]);
|
|
204
|
+
function createNoExternalMerger(packageName) {
|
|
205
|
+
return (current) => {
|
|
206
|
+
if (current === true) return true;
|
|
207
|
+
if (!current) return [packageName];
|
|
208
|
+
const values = Array.isArray(current) ? current : [current];
|
|
209
|
+
return values.includes(packageName) ? values : [...values, packageName];
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
function isServerEnvironment(name, config) {
|
|
213
|
+
return name === "ssr" || config.consumer === "server";
|
|
214
|
+
}
|
|
215
|
+
function shouldSkipViteProviderBuild(command, mode) {
|
|
216
|
+
return command === "serve" || mode === "e2e";
|
|
217
|
+
}
|
|
218
|
+
//#endregion
|
|
219
|
+
//#region ../internal/src/arrays.ts
|
|
220
|
+
function pushUnique(arr, item, key = (value) => value) {
|
|
221
|
+
const id = key(item);
|
|
222
|
+
if (arr.some((entry) => key(entry) === id)) return;
|
|
223
|
+
arr.push(item);
|
|
224
|
+
}
|
|
225
|
+
//#endregion
|
|
226
|
+
//#region src/integrations/cloudflare.ts
|
|
227
|
+
function configureCloudflareKV(target, config) {
|
|
228
|
+
for (const store of Object.values(config.stores || { default: config.store })) {
|
|
229
|
+
if (store.driver !== "cloudflare-kv-binding" || !store.namespaceId) continue;
|
|
230
|
+
const { binding, namespaceId } = store;
|
|
231
|
+
target.cloudflare ||= {};
|
|
232
|
+
target.cloudflare.wrangler ||= {};
|
|
233
|
+
target.cloudflare.wrangler.kv_namespaces ||= [];
|
|
234
|
+
pushUnique(target.cloudflare.wrangler.kv_namespaces, {
|
|
235
|
+
binding,
|
|
236
|
+
id: namespaceId
|
|
237
|
+
}, (entry) => entry.binding);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
//#endregion
|
|
2
241
|
//#region src/vite.ts
|
|
3
242
|
const RESOLVED_KV_VIRTUAL_CONFIG_ID = `\0${KV_VIRTUAL_CONFIG_ID}`;
|
|
243
|
+
const RESOLVED_KV_RUNTIME_ID = `\0#vitehub/kv/runtime`;
|
|
244
|
+
const KV_ERRORS_IMPORT_ID = new URL(import.meta.url.endsWith(".ts") ? "./errors.ts" : "./errors.js", import.meta.url).href;
|
|
245
|
+
const UPSTASH_DRIVER_IMPORT_ID = "@vite-hub/kv/runtime/upstash-driver";
|
|
246
|
+
const UNSTORAGE_IMPORT_ID = import.meta.resolve("unstorage");
|
|
247
|
+
const CLOUDFLARE_KV_DRIVER_IMPORT_ID = import.meta.resolve("unstorage/drivers/cloudflare-kv-binding");
|
|
248
|
+
const mergeNoExternal = createNoExternalMerger("@vite-hub/kv");
|
|
249
|
+
const KV_CLOUDFLARE_BINDINGS_FILE = ".vitehub-kv-bindings.json";
|
|
250
|
+
function hubKvOptionalPeerResolver() {
|
|
251
|
+
return {
|
|
252
|
+
name: "@vite-hub/kv/optional-peers",
|
|
253
|
+
enforce: "pre",
|
|
254
|
+
resolveId(id, importer, resolveOptions) {
|
|
255
|
+
if (!resolveOptions?.ssr || !isKVOptionalUpstashImport(id, importer)) return;
|
|
256
|
+
return {
|
|
257
|
+
external: true,
|
|
258
|
+
id
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
}
|
|
4
263
|
function serializeVirtualConfig(config) {
|
|
5
264
|
return [
|
|
6
265
|
`export const hosting = ${JSON.stringify(config.hosting)};`,
|
|
@@ -8,23 +267,143 @@ function serializeVirtualConfig(config) {
|
|
|
8
267
|
"export default { hosting, kv };"
|
|
9
268
|
].join("\n");
|
|
10
269
|
}
|
|
270
|
+
function isCloudflareKVConfig(kv) {
|
|
271
|
+
return Boolean(kv) && Object.values(kv.stores || { default: kv.store }).every((store) => store.driver === "cloudflare-kv-binding");
|
|
272
|
+
}
|
|
273
|
+
function hasUpstashStore(kv) {
|
|
274
|
+
if (!kv) return false;
|
|
275
|
+
return Object.values(kv.stores || { default: kv.store }).some((store) => store.driver === "upstash");
|
|
276
|
+
}
|
|
277
|
+
function isKVOptionalUpstashImport(id, importer) {
|
|
278
|
+
if (id !== UPSTASH_DRIVER_IMPORT_ID || !importer) return false;
|
|
279
|
+
const normalizedImporter = importer.replace(/\\/g, "/").split("?", 1)[0];
|
|
280
|
+
return normalizedImporter.endsWith("/packages/kv/src/runtime/driver.ts") || normalizedImporter.endsWith("/packages/kv/dist/index.js") || normalizedImporter.includes("/@vite-hub/kv/dist/index.js");
|
|
281
|
+
}
|
|
282
|
+
function createCloudflareKVWranglerConfig(kv) {
|
|
283
|
+
if (!kv) return;
|
|
284
|
+
const target = {};
|
|
285
|
+
configureCloudflareKV(target, kv);
|
|
286
|
+
return target.cloudflare?.wrangler?.kv_namespaces?.length ? target.cloudflare.wrangler : void 0;
|
|
287
|
+
}
|
|
288
|
+
function cloudflareBindingsFile(rootDir) {
|
|
289
|
+
return join(createDefaultCloudflareOutputRoot(rootDir), KV_CLOUDFLARE_BINDINGS_FILE);
|
|
290
|
+
}
|
|
291
|
+
async function readOwnedCloudflareKVBindings(rootDir) {
|
|
292
|
+
try {
|
|
293
|
+
const parsed = JSON.parse(await readFile(cloudflareBindingsFile(rootDir), "utf8"));
|
|
294
|
+
return Array.isArray(parsed) ? parsed.filter((value) => typeof value === "string") : [];
|
|
295
|
+
} catch (error) {
|
|
296
|
+
if (error.code === "ENOENT") return [];
|
|
297
|
+
throw error;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
async function writeOwnedCloudflareKVBindings(rootDir, bindings) {
|
|
301
|
+
const file = cloudflareBindingsFile(rootDir);
|
|
302
|
+
if (!bindings.length) {
|
|
303
|
+
await rm(file, { force: true });
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
await mkdir(createDefaultCloudflareOutputRoot(rootDir), { recursive: true });
|
|
307
|
+
await writeFile(file, `${JSON.stringify([...new Set(bindings)], null, 2)}\n`, "utf8");
|
|
308
|
+
}
|
|
309
|
+
function serializeCloudflareRuntime(config) {
|
|
310
|
+
return [
|
|
311
|
+
`import { createStorage } from ${JSON.stringify(UNSTORAGE_IMPORT_ID)};`,
|
|
312
|
+
`import createDriver from ${JSON.stringify(CLOUDFLARE_KV_DRIVER_IMPORT_ID)};`,
|
|
313
|
+
`import { kvResult } from ${JSON.stringify(KV_ERRORS_IMPORT_ID)};`,
|
|
314
|
+
"",
|
|
315
|
+
`const kvConfig = ${JSON.stringify(config, null, 2)}`,
|
|
316
|
+
"const storages = new Map();",
|
|
317
|
+
"",
|
|
318
|
+
"function resolveStoreConfig(name) {",
|
|
319
|
+
" const stores = kvConfig.stores || { default: kvConfig.store };",
|
|
320
|
+
" const store = stores[name];",
|
|
321
|
+
" if (!store) throw new Error(`[vitehub] Unknown KV store \"${name}\".`);",
|
|
322
|
+
" return store;",
|
|
323
|
+
"}",
|
|
324
|
+
"",
|
|
325
|
+
"function resolveStorage(name = \"default\") {",
|
|
326
|
+
" const store = resolveStoreConfig(name);",
|
|
327
|
+
" const existing = storages.get(name);",
|
|
328
|
+
" if (existing) return existing;",
|
|
329
|
+
" const storage = createStorage({ driver: createDriver(store) });",
|
|
330
|
+
" storages.set(name, storage);",
|
|
331
|
+
" return storage;",
|
|
332
|
+
"}",
|
|
333
|
+
"",
|
|
334
|
+
"async function resolveStorageResult(name, operation) {",
|
|
335
|
+
" resolveStoreConfig(name);",
|
|
336
|
+
" return kvResult(operation, name, async () => resolveStorage(name));",
|
|
337
|
+
"}",
|
|
338
|
+
"",
|
|
339
|
+
"function createKVStorage(name = \"default\") {",
|
|
340
|
+
" return {",
|
|
341
|
+
" async clear(base, options) { const [error, storage] = await resolveStorageResult(name, \"clear\"); return error ? [error, undefined] : kvResult(\"clear\", name, async () => { await storage.clear(base, options); }); },",
|
|
342
|
+
" async del(key, options) { const [error, storage] = await resolveStorageResult(name, \"del\"); return error ? [error, undefined] : kvResult(\"del\", name, async () => { await storage.removeItem(key, options); }); },",
|
|
343
|
+
" async get(key, options) { const [error, storage] = await resolveStorageResult(name, \"get\"); return error ? [error, undefined] : kvResult(\"get\", name, () => storage.getItem(key, options)); },",
|
|
344
|
+
" async has(key, options) { const [error, storage] = await resolveStorageResult(name, \"has\"); return error ? [error, undefined] : kvResult(\"has\", name, () => storage.hasItem(key, options)); },",
|
|
345
|
+
" async keys(base, options) { const [error, storage] = await resolveStorageResult(name, \"keys\"); return error ? [error, undefined] : kvResult(\"keys\", name, () => storage.getKeys(base, options)); },",
|
|
346
|
+
" async set(key, value, options) { const [error, storage] = await resolveStorageResult(name, \"set\"); return error ? [error, undefined] : kvResult(\"set\", name, async () => { await storage.setItem(key, value, options); }); },",
|
|
347
|
+
" store(storeName) { return createKVStorage(storeName); },",
|
|
348
|
+
" };",
|
|
349
|
+
"}",
|
|
350
|
+
"",
|
|
351
|
+
"export const kv = createKVStorage();",
|
|
352
|
+
""
|
|
353
|
+
].join("\n");
|
|
354
|
+
}
|
|
11
355
|
function hubKv(options) {
|
|
356
|
+
let resolved;
|
|
12
357
|
let runtimeConfig;
|
|
13
358
|
const getConfig = () => runtimeConfig ??= resolveKVViteConfig(options);
|
|
14
359
|
return {
|
|
15
360
|
name: KV_VITE_PLUGIN_NAME,
|
|
361
|
+
enforce: "pre",
|
|
16
362
|
api: { getConfig },
|
|
17
|
-
nitro: kvNitroModule,
|
|
18
363
|
configResolved(config) {
|
|
364
|
+
resolved = config;
|
|
19
365
|
runtimeConfig = resolveKVViteConfig(config.kv ?? options);
|
|
20
366
|
},
|
|
21
|
-
|
|
367
|
+
configEnvironment(name, config) {
|
|
368
|
+
if (!isServerEnvironment(name, config)) return;
|
|
369
|
+
return { resolve: { noExternal: mergeNoExternal(config.resolve?.noExternal) } };
|
|
370
|
+
},
|
|
371
|
+
resolveId(id, importer, resolveOptions) {
|
|
372
|
+
if (id === UPSTASH_DRIVER_IMPORT_ID && resolveOptions?.ssr && !hasUpstashStore(getConfig().kv) && isKVOptionalUpstashImport(id, importer)) return {
|
|
373
|
+
external: true,
|
|
374
|
+
id
|
|
375
|
+
};
|
|
376
|
+
if (id === "@vite-hub/kv" && isCloudflareKVConfig(getConfig().kv)) return RESOLVED_KV_RUNTIME_ID;
|
|
22
377
|
if (id === "#vitehub/kv/config") return RESOLVED_KV_VIRTUAL_CONFIG_ID;
|
|
23
378
|
},
|
|
24
379
|
load(id) {
|
|
380
|
+
if (id === RESOLVED_KV_RUNTIME_ID) {
|
|
381
|
+
const kv = getConfig().kv;
|
|
382
|
+
if (isCloudflareKVConfig(kv)) return serializeCloudflareRuntime(kv);
|
|
383
|
+
}
|
|
25
384
|
if (id === RESOLVED_KV_VIRTUAL_CONFIG_ID) return serializeVirtualConfig(getConfig());
|
|
385
|
+
},
|
|
386
|
+
closeBundle: {
|
|
387
|
+
order: "post",
|
|
388
|
+
sequential: true,
|
|
389
|
+
async handler() {
|
|
390
|
+
if (!resolved || shouldSkipViteProviderBuild(resolved.command, getViteMode())) return;
|
|
391
|
+
const wranglerConfig = createCloudflareKVWranglerConfig(getConfig().kv);
|
|
392
|
+
const nextBindings = wranglerConfig?.kv_namespaces?.map((binding) => binding.binding) ?? [];
|
|
393
|
+
const previousBindings = await readOwnedCloudflareKVBindings(resolved.root);
|
|
394
|
+
if (!wranglerConfig && !previousBindings.length) return;
|
|
395
|
+
await writeCloudflareWranglerConfig({
|
|
396
|
+
rootDir: resolved.root,
|
|
397
|
+
wranglerConfigOwnership: { arrays: { kv_namespaces: {
|
|
398
|
+
key: "binding",
|
|
399
|
+
values: [...previousBindings, ...nextBindings]
|
|
400
|
+
} } },
|
|
401
|
+
...wranglerConfig ? { wranglerConfig } : {}
|
|
402
|
+
});
|
|
403
|
+
await writeOwnedCloudflareKVBindings(resolved.root, nextBindings);
|
|
404
|
+
}
|
|
26
405
|
}
|
|
27
406
|
};
|
|
28
407
|
}
|
|
29
408
|
//#endregion
|
|
30
|
-
export { KV_VIRTUAL_CONFIG_ID, KV_VITE_PLUGIN_NAME, hubKv, resolveKVViteConfig };
|
|
409
|
+
export { KV_VIRTUAL_CONFIG_ID, KV_VITE_PLUGIN_NAME, hubKv, hubKvOptionalPeerResolver, resolveKVViteConfig };
|
package/package.json
CHANGED
|
@@ -1,80 +1,71 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vite-hub/kv",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "Vite-first key-value storage primitives and
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "Vite-first key-value storage primitives and integration for ViteHub.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"cloudflare",
|
|
7
|
+
"kv",
|
|
8
|
+
"vercel",
|
|
9
|
+
"vite",
|
|
10
|
+
"vitehub"
|
|
11
|
+
],
|
|
7
12
|
"homepage": "https://github.com/vite-hub/vitehub#readme",
|
|
13
|
+
"bugs": "https://github.com/vite-hub/vitehub/issues",
|
|
14
|
+
"license": "Apache-2.0",
|
|
15
|
+
"author": "onmax <maximogarciamtnez@gmail.com>",
|
|
8
16
|
"repository": {
|
|
9
17
|
"type": "git",
|
|
10
18
|
"url": "git+https://github.com/vite-hub/vitehub.git",
|
|
11
19
|
"directory": "packages/kv"
|
|
12
20
|
},
|
|
13
|
-
"
|
|
14
|
-
|
|
15
|
-
"
|
|
16
|
-
"vite",
|
|
17
|
-
"nitro",
|
|
18
|
-
"nuxt",
|
|
19
|
-
"kv",
|
|
20
|
-
"cloudflare",
|
|
21
|
-
"vercel"
|
|
21
|
+
"files": [
|
|
22
|
+
"dist",
|
|
23
|
+
"package.json"
|
|
22
24
|
],
|
|
23
|
-
"sideEffects": false,
|
|
24
25
|
"type": "module",
|
|
26
|
+
"sideEffects": false,
|
|
25
27
|
"types": "./dist/index.d.ts",
|
|
26
28
|
"exports": {
|
|
27
29
|
".": "./dist/index.js",
|
|
28
|
-
"./
|
|
29
|
-
"./
|
|
30
|
-
"./runtime/nitro-plugin": "./dist/runtime/nitro-plugin.js",
|
|
30
|
+
"./errors": "./dist/errors.js",
|
|
31
|
+
"./runtime/upstash-driver": "./dist/runtime/upstash-driver.js",
|
|
31
32
|
"./virtual": "./dist/virtual.js",
|
|
32
33
|
"./vite": "./dist/vite.js",
|
|
33
34
|
"./package.json": "./package.json"
|
|
34
35
|
},
|
|
35
|
-
"files": [
|
|
36
|
-
"dist",
|
|
37
|
-
"package.json"
|
|
38
|
-
],
|
|
39
36
|
"dependencies": {
|
|
40
|
-
"
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
|
|
37
|
+
"defu": "^6.1.7",
|
|
38
|
+
"unstorage": "^2.0.0-alpha.7",
|
|
39
|
+
"@vite-hub/runtime": "0.0.2"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"ofetch": "^1.5.1",
|
|
43
|
+
"publint": "^0.3.21",
|
|
44
|
+
"typescript": "^6.0.3",
|
|
45
|
+
"vite": "^8.0.8",
|
|
46
|
+
"vite-plus": "^0.1.24",
|
|
47
|
+
"vitest": "^4.1.4",
|
|
48
|
+
"@vite-hub/internal": "0.0.1"
|
|
44
49
|
},
|
|
45
50
|
"peerDependencies": {
|
|
46
|
-
"@upstash/redis": "^1.
|
|
47
|
-
"
|
|
48
|
-
"vite": ">=6.0.0"
|
|
51
|
+
"@upstash/redis": "^1.38.0",
|
|
52
|
+
"vite": ">=8.0.0"
|
|
49
53
|
},
|
|
50
54
|
"peerDependenciesMeta": {
|
|
51
55
|
"@upstash/redis": {
|
|
52
56
|
"optional": true
|
|
53
57
|
},
|
|
54
|
-
"nitro": {
|
|
55
|
-
"optional": true
|
|
56
|
-
},
|
|
57
58
|
"vite": {
|
|
58
59
|
"optional": true
|
|
59
60
|
}
|
|
60
61
|
},
|
|
61
|
-
"
|
|
62
|
-
"
|
|
63
|
-
"nitro": "3.0.260311-beta",
|
|
64
|
-
"ofetch": "^1.5.1",
|
|
65
|
-
"publint": "^0.3.15",
|
|
66
|
-
"tsdown": "^0.21.8",
|
|
67
|
-
"tsx": "^4.21.0",
|
|
68
|
-
"typescript": "^6.0.2",
|
|
69
|
-
"vite": "^8.0.8",
|
|
70
|
-
"vitest": "^4.1.4",
|
|
71
|
-
"@vite-hub/internal": "0.0.1"
|
|
62
|
+
"engines": {
|
|
63
|
+
"node": ">=24.0.0"
|
|
72
64
|
},
|
|
73
65
|
"scripts": {
|
|
74
|
-
"build": "
|
|
75
|
-
"dev": "
|
|
66
|
+
"build": "vp pack",
|
|
67
|
+
"dev": "vp pack --watch",
|
|
76
68
|
"typecheck": "tsc --noEmit -p ./tsconfig.json",
|
|
77
|
-
"test": "
|
|
78
|
-
"test:e2e": "tsx ./test/e2e.ts"
|
|
69
|
+
"test": "vp run -t @vite-hub/kv#build && vp test"
|
|
79
70
|
}
|
|
80
71
|
}
|
package/dist/driver-CPgLtkh4.js
DELETED
|
@@ -1,81 +0,0 @@
|
|
|
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 };
|
package/dist/env-Cs7n_E1y.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
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 };
|