@vite-hub/kv 0.3.1 → 0.4.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.
Files changed (41) hide show
  1. package/README.md +14 -12
  2. package/dist/cli.js +4 -3
  3. package/dist/{cloudflare-kv-BIEUFzRT.js → cloudflare-kv-C3NO4C21.js} +18 -4
  4. package/dist/{cloudflare-kv-local-BBw3ZT1K.js → cloudflare-kv-local-C2sbIrFy.js} +2 -2
  5. package/dist/{config-BXEVoZE3.js → config-LfpUj9hs.js} +25 -23
  6. package/dist/{create-storage-BGsrQKw3.js → create-storage-BORQm2a8.js} +5 -5
  7. package/dist/{cursor-history-BqIQePNP.js → cursor-history-DRHh5bO5.js} +1 -1
  8. package/dist/{database-72Jk3VVk.js → database-oJdy0ZVX.js} +1 -1
  9. package/dist/{deno-kv-BM9mYdUp.js → deno-kv-D_VtffFv.js} +2 -2
  10. package/dist/{driver-BwxgaQ-f.d.ts → driver-R4Z5Z5fl.d.ts} +2 -2
  11. package/dist/{error-diagnostics-BxqvtEIb.js → error-diagnostics-C54QQEhN.js} +10 -2
  12. package/dist/{errors-DbwufFZp.js → errors-DzD6YNEH.js} +31 -3
  13. package/dist/errors.d.ts +6 -1
  14. package/dist/errors.js +2 -2
  15. package/dist/fs-lite-BAgpmNAF.js +2 -0
  16. package/dist/{fs-lite-BLZId9_P.js → fs-lite-BaklCLuV.js} +1 -1
  17. package/dist/index.d.ts +2 -2
  18. package/dist/index.js +42 -27
  19. package/dist/{operation-definition-8gOe-ubD.d.ts → operation-definition-nmRu_O3V.d.ts} +5 -0
  20. package/dist/{operations-Ch-gKaey.js → operations-CtrWLmPK.js} +3 -9
  21. package/dist/{operations-B-WZ7P1h.js → operations-D2iI9oN4.js} +238 -226
  22. package/dist/operations.d.ts +2 -2
  23. package/dist/operations.js +3 -2
  24. package/dist/{redis-shared-E5mahECF.js → redis-shared-5ogRd3-Y.js} +1 -1
  25. package/dist/runtime/cloudflare-kv.d.ts +1 -1
  26. package/dist/runtime/cloudflare-kv.js +1 -1
  27. package/dist/runtime/nitro-plugin.js +7 -4
  28. package/dist/runtime/redis-driver.d.ts +2 -2
  29. package/dist/runtime/redis-driver.js +2 -2
  30. package/dist/runtime/unstorage-driver.js +6 -2
  31. package/dist/runtime/upstash-driver.d.ts +2 -2
  32. package/dist/runtime/upstash-driver.js +2 -2
  33. package/dist/{types-DEiKew_3.d.ts → types-Dv-OSXZC.d.ts} +33 -20
  34. package/dist/{upstash-DDlqTttY.js → upstash-B-vaUYqO.js} +2 -2
  35. package/dist/virtual-module.d.ts +1 -0
  36. package/dist/vite.d.ts +17 -5
  37. package/dist/vite.js +601 -334
  38. package/package.json +5 -5
  39. package/dist/fs-lite-RAoxu8L0.js +0 -2
  40. package/dist/main-F3Y22_uV.js +0 -2353
  41. package/dist/rolldown-runtime-CMFfr-1z.js +0 -26
package/dist/index.js CHANGED
@@ -1,11 +1,12 @@
1
- import { n as kvErrorDiagnostics } from "./error-diagnostics-BxqvtEIb.js";
2
- import { n as kvKeyLimitCodes, o as kvWriteLimitCodes, r as kvResult, s as kvStoreWithMode } from "./errors-DbwufFZp.js";
3
- import { r as normalizeKVOptions, s as readEnv } from "./config-BXEVoZE3.js";
1
+ import { n as kvErrorDiagnostics } from "./error-diagnostics-C54QQEhN.js";
2
+ import { a as kvResult, c as kvWriteLimitCodes, i as kvListLimitInvalid, l as listKVKeys, r as kvKeyLimitCodes, t as kvClearOptionsError, u as kvStoreWithMode } from "./errors-DzD6YNEH.js";
3
+ import { r as normalizeKVOptions, s as readEnv } from "./config-LfpUj9hs.js";
4
4
  import { n as getActiveCloudflareEnv } from "./cloudflare-env-lookup-Bs0VDpU5.js";
5
- import { n as normalizeKVListPrefix, t as createKVStorage$1 } from "./create-storage-BGsrQKw3.js";
5
+ import { n as normalizeKVListPrefix, t as createKVStorage$1 } from "./create-storage-BORQm2a8.js";
6
6
  import { o as normalizeKey } from "./utils2-FozcTsmb.js";
7
- import { t as resolveRuntimeKVOptions } from "./upstash-DDlqTttY.js";
7
+ import { t as resolveRuntimeKVOptions } from "./upstash-B-vaUYqO.js";
8
8
  import { i as setItemsOneByOne, n as getItemsOneByOne } from "./batch--v0ri69O.js";
9
+ import { parseDuration } from "@vite-hub/runtime";
9
10
  import { getViteHubTestMode } from "@vite-hub/runtime/internal/test-mode";
10
11
  import { destr } from "destr";
11
12
  //#region src/runtime/driver.ts
@@ -61,7 +62,7 @@ const otherOptionalDriverMethods = {
61
62
  const batchDriverMethods = /* @__PURE__ */ new Set(["getItems", "setItems"]);
62
63
  const lazyOptionalDriverMethods = Object.fromEntries(Object.entries(driverAtomicOperations).map(([driver, operations]) => [driver, /* @__PURE__ */ new Set([...Object.keys(operations).map((operation) => atomicDriverMethods[operation]), ...otherOptionalDriverMethods[driver] ?? []])]));
63
64
  async function createDatabaseDriver(store, loadDatabases) {
64
- const { default: factory, kvSqlClientFromDatabase } = await import("./database-72Jk3VVk.js");
65
+ const { default: factory, kvSqlClientFromDatabase } = await import("./database-oJdy0ZVX.js");
65
66
  return factory(store, async () => {
66
67
  const entry = loadDatabases ? (await loadDatabases())[store.database] : void 0;
67
68
  if (!entry) throw kvErrorDiagnostics.KV_DATABASE_UNAVAILABLE({
@@ -76,7 +77,7 @@ async function createRuntimeDriver(store, loadDatabases) {
76
77
  case "cloudflare": {
77
78
  const { localBase } = store;
78
79
  if (localBase) {
79
- const { default: factory } = await import("./cloudflare-kv-local-BBw3ZT1K.js");
80
+ const { default: factory } = await import("./cloudflare-kv-local-C2sbIrFy.js");
80
81
  return factory({
81
82
  ...store,
82
83
  localBase
@@ -87,11 +88,11 @@ async function createRuntimeDriver(store, loadDatabases) {
87
88
  }
88
89
  case "database": return createDatabaseDriver(store, loadDatabases);
89
90
  case "deno-kv": {
90
- const { default: factory } = await import("./deno-kv-BM9mYdUp.js");
91
+ const { default: factory } = await import("./deno-kv-D_VtffFv.js");
91
92
  return factory(store);
92
93
  }
93
94
  case "fs-lite": {
94
- const { default: factory } = await import("./fs-lite-RAoxu8L0.js");
95
+ const { default: factory } = await import("./fs-lite-BAgpmNAF.js");
95
96
  return factory(store);
96
97
  }
97
98
  case "redis": {
@@ -235,22 +236,36 @@ function createHostedKVStorage(config, name = "default", loadDatabases) {
235
236
  return storage;
236
237
  }
237
238
  //#endregion
239
+ //#region src/runtime/ttl.ts
240
+ /**
241
+ * The seconds of a KV `ttl`: a number of seconds, or a duration such as `"1h"`. `undefined` when it is not a positive
242
+ * time, so the caller returns `KV_INVALID_TTL`.
243
+ */
244
+ function ttlSeconds(ttl) {
245
+ let seconds;
246
+ if (typeof ttl === "number") seconds = ttl;
247
+ else if (typeof ttl !== "string") return;
248
+ else try {
249
+ seconds = parseDuration(ttl) / 1e3;
250
+ } catch {
251
+ return;
252
+ }
253
+ return Number.isFinite(seconds) && seconds > 0 && Number.isSafeInteger(Math.ceil(seconds * 1e3)) ? seconds : void 0;
254
+ }
255
+ //#endregion
238
256
  //#region src/runtime/lock.ts
239
257
  const firstRetryMs = 25;
240
258
  const maxRetryMs = 500;
241
259
  const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
242
- function validTtl(ttl) {
243
- return Number.isFinite(ttl) && ttl > 0 && Number.isSafeInteger(Math.ceil(ttl * 1e3));
244
- }
245
260
  /**
246
261
  * A lock on `key` built on `setIfAbsent()` with a TTL and `deleteIfEquals()`. Each acquire writes a new random owner
247
262
  * token, and the release deletes the key only when it still holds that token.
248
263
  */
249
264
  function createKVLock(store, resolveStorage, key, options) {
250
265
  async function acquire() {
251
- const { ttl } = options;
266
+ const ttl = ttlSeconds(options.ttl);
252
267
  const wait = options.wait ?? 0;
253
- if (!validTtl(ttl)) return [kvErrorDiagnostics.KV_INVALID_TTL(), void 0];
268
+ if (ttl === void 0) return [kvErrorDiagnostics.KV_INVALID_TTL(), void 0];
254
269
  if (!Number.isFinite(wait) || wait < 0) return [kvErrorDiagnostics.KV_INVALID_WAIT(), void 0];
255
270
  const storage = await resolveStorage();
256
271
  return kvResult("lock", store, async () => {
@@ -388,15 +403,17 @@ function readSetTTL(operation, options) {
388
403
  operation,
389
404
  option
390
405
  });
391
- const ttl = options.ttl;
392
- if (ttl === void 0) return;
393
- if (typeof ttl !== "number" || !Number.isFinite(ttl) || ttl <= 0 || !Number.isSafeInteger(Math.ceil(ttl))) throw kvErrorDiagnostics.KV_INVALID_TTL();
406
+ if (options.ttl === void 0) return;
407
+ const ttl = ttlSeconds(options.ttl);
408
+ if (ttl === void 0) throw kvErrorDiagnostics.KV_INVALID_TTL();
394
409
  return ttl;
395
410
  }
396
411
  function createKVStorage(name = "default") {
397
412
  return {
398
- async clear(base, options) {
399
- return runKVOperation("clear", name, async (storage) => storage.clear(base, options));
413
+ async clear(options) {
414
+ const error = kvClearOptionsError(options);
415
+ if (error) return [error, void 0];
416
+ return runKVOperation("clear", name, async (storage) => storage.clear(options?.prefix));
400
417
  },
401
418
  async delete(key, options) {
402
419
  return runKVOperation("delete", name, async (storage) => storage.removeItem(key, options), kvKeyLimitCodes);
@@ -434,19 +451,17 @@ function createKVStorage(name = "default") {
434
451
  operation: "increment",
435
452
  store: name
436
453
  });
437
- if (!Number.isFinite(ttl) || ttl <= 0 || !Number.isSafeInteger(Math.ceil(ttl))) throw kvErrorDiagnostics.KV_INVALID_TTL();
438
- return storage.incrementItem(key, ttl);
454
+ const seconds = ttlSeconds(ttl);
455
+ if (seconds === void 0) throw kvErrorDiagnostics.KV_INVALID_TTL();
456
+ return storage.incrementItem(key, seconds);
439
457
  }, ["KV_INVALID_TTL", "KV_UNSUPPORTED_OPERATION"]);
440
458
  },
441
- async keys(base, options) {
442
- return runKVOperation("keys", name, (storage) => storage.getKeys(base, options));
443
- },
444
459
  lock(key, options) {
445
460
  return createKVLock(name, () => resolveStorage(name), key, options);
446
461
  },
447
- async list(options) {
448
- if (!Number.isInteger(options.limit) || options.limit <= 0) return [kvErrorDiagnostics.KV_INVALID_LIMIT(), void 0];
449
- return runKVOperation("list", name, (storage) => storage.listKeys(options), ["KV_INVALID_CURSOR"]);
462
+ async list(options = {}) {
463
+ if (kvListLimitInvalid(options)) return [kvErrorDiagnostics.KV_INVALID_LIMIT(), void 0];
464
+ return runKVOperation("list", name, (storage) => listKVKeys((page) => storage.listKeys(page), options), ["KV_INVALID_CURSOR"]);
450
465
  },
451
466
  async set(key, value, options) {
452
467
  return runKVOperation("set", name, async (storage) => {
@@ -115,6 +115,11 @@ interface OperationContext {
115
115
  surface: "console" | "cli" | "mcp" | "agent";
116
116
  /** Set by the host from generated build identity, never from request input or runtime NODE_ENV. */
117
117
  development?: boolean;
118
+ /**
119
+ * The project root of a development server, for operations that read build files, such as `bundle.report`. Hosts set
120
+ * it only with `development`, so a deployed server never reads project files through it.
121
+ */
122
+ projectRoot?: string;
118
123
  env?: Record<string, unknown>;
119
124
  waitUntil?: (task: Promise<unknown>) => void;
120
125
  request?: Request;
@@ -1,6 +1,6 @@
1
1
  import { t as redactInspectionText } from "./inspect-BIKQsqX4.js";
2
2
  import { n as invariantError } from "./invariant-DolT9EEx.js";
3
- import { n as kvErrorDiagnostics } from "./error-diagnostics-BxqvtEIb.js";
3
+ import { n as kvErrorDiagnostics } from "./error-diagnostics-C54QQEhN.js";
4
4
  import { t as kvDevMaximumListLimit } from "./results-p7nPCnaJ.js";
5
5
  import { o as normalizeKey } from "./utils2-FozcTsmb.js";
6
6
  import { kv } from "@vite-hub/kv";
@@ -87,12 +87,6 @@ function inspectKVValue(value) {
87
87
  //#endregion
88
88
  //#region src/runtime/preview.ts
89
89
  const maximumValueBytes = 262144;
90
- function valueType(value) {
91
- if (value === null) return "null";
92
- if (Array.isArray(value)) return "array";
93
- if (value instanceof Uint8Array) return "bytes";
94
- return typeof value;
95
- }
96
90
  function truncateValue(value) {
97
91
  const encoder = new TextEncoder();
98
92
  let bytes = 0;
@@ -218,7 +212,7 @@ function boundedJSONStringify(value) {
218
212
  }
219
213
  /** A stored value as text of at most 256 KiB, for a page that shows it. Bytes become hex. */
220
214
  function previewKVValue(value) {
221
- const type = valueType(value);
215
+ const type = kvValueType(value);
222
216
  let format = "json";
223
217
  let rendered;
224
218
  let truncated = false;
@@ -227,7 +221,7 @@ function previewKVValue(value) {
227
221
  editable = true;
228
222
  format = "text";
229
223
  rendered = value;
230
- } else if (value instanceof Uint8Array) {
224
+ } else if (isUint8Array(value)) {
231
225
  format = "text";
232
226
  const maximumBytes = maximumValueBytes / 2;
233
227
  truncated = value.byteLength > maximumBytes;