@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
@@ -1,10 +1,10 @@
1
1
  import { n as GetItemType, r as GetItemTypeMap } from "./types-Bj3EQoRg.js";
2
- import { Result } from "@vite-hub/runtime";
2
+ import { Duration, Result } from "@vite-hub/runtime";
3
3
  //#region src/types.d.ts
4
4
  type KVDriver = "cloudflare" | "database" | "deno-kv" | "fs-lite" | "redis" | "upstash";
5
5
  /**
6
6
  * The atomic operations of each driver. A store returns `KV_UNSUPPORTED_OPERATION` for an atomic operation that its
7
- * driver does not list. `get`, `set`, `delete`, `has`, `keys`, `list`, and `clear` work on every driver.
7
+ * driver does not list. `get`, `set`, `delete`, `has`, `list`, and `clear` work on every driver.
8
8
  */
9
9
  interface KVDriverAtomicOperations {
10
10
  "cloudflare": never;
@@ -38,7 +38,7 @@ interface UpstashKVStoreConfig {
38
38
  token?: string;
39
39
  url?: string;
40
40
  /**
41
- * Prefix of every key in Upstash. Default `vitehub:kv:<store>`, as for a Redis store, so `kv.clear()` and `kv.keys()`
41
+ * Prefix of every key in Upstash. Default `vitehub:kv:<store>`, as for a Redis store, so `kv.clear()` and `kv.list()`
42
42
  * stay inside the store. An empty value uses the default.
43
43
  */
44
44
  base?: string;
@@ -137,14 +137,23 @@ interface ResolvedKVModuleOptions {
137
137
  store: ResolvedKVStoreConfig;
138
138
  stores?: Record<string, ResolvedKVStoreConfig>;
139
139
  }
140
- type KVOperation = "clear" | "delete" | "deleteIfEquals" | "get" | "getAndDelete" | "getMany" | "has" | "increment" | "keys" | "list" | "lock" | "set" | "setIfAbsent" | "setMany";
140
+ type KVOperation = "clear" | "delete" | "deleteIfEquals" | "get" | "getAndDelete" | "getMany" | "has" | "increment" | "list" | "lock" | "set" | "setIfAbsent" | "setMany";
141
141
  /** Codes that KV operations return in the `[error, data]` tuple. Config errors throw instead. */
142
- type KVErrorCode = "KV_INVALID_CURSOR" | "KV_INVALID_LIMIT" | "KV_INVALID_OPTION" | "KV_INVALID_TTL" | "KV_INVALID_VALUE" | "KV_INVALID_WAIT" | "KV_KEY_TOO_LONG" | "KV_OPERATION_FAILED" | "KV_UNSUPPORTED_OPERATION" | "KV_VALUE_TOO_LARGE";
142
+ type KVErrorCode = "KV_INVALID_CURSOR" | "KV_INVALID_CLEAR_OPTIONS" | "KV_INVALID_LIMIT" | "KV_INVALID_OPTION" | "KV_INVALID_TTL" | "KV_INVALID_VALUE" | "KV_INVALID_WAIT" | "KV_KEY_TOO_LONG" | "KV_OPERATION_FAILED" | "KV_UNSUPPORTED_OPERATION" | "KV_VALUE_TOO_LARGE";
143
143
  interface KVListOptions {
144
144
  /** Opaque continuation cursor returned by a previous page. */
145
145
  cursor?: string;
146
- /** Maximum number of keys to read. Must be a positive integer. */
146
+ /** Maximum number of keys to read. Must be a positive integer. Without it, the page holds every key and no cursor. */
147
+ limit?: number;
148
+ /** Only keys that start with this text, so `"user"` matches `users:1`. `"users:"` matches the keys below `users`. */
149
+ prefix?: string;
150
+ }
151
+ /** One page that a driver reads: `limit` is set. */
152
+ interface KVPageOptions extends KVListOptions {
147
153
  limit: number;
154
+ }
155
+ interface KVClearOptions {
156
+ /** Only keys that start with this text, as in `list()`. Without it, `clear()` deletes every key of the store. */
148
157
  prefix?: string;
149
158
  }
150
159
  /** What `kv.get()` converts the stored value to: `text`, `json`, `bytes`, `stream`, or `blob`. */
@@ -159,16 +168,20 @@ type KVGetOptions<K extends KVGetType | undefined = KVGetType | undefined> = Rec
159
168
  type KVValue<T, K> = K extends KVGetType ? KVGetTypeMap[K] : T;
160
169
  /** Options of `kv.set()`, `kv.setMany()`, and `kv.setIfAbsent()`. Other fields return `KV_INVALID_OPTION`. */
161
170
  interface KVSetOptions {
162
- /** Seconds until the key expires, a positive number. Every driver expires the key. Cloudflare KV keeps it at least 60 seconds. */
163
- ttl?: number;
171
+ /**
172
+ * How long until the key expires: a number of seconds, or a duration such as `"1h"` or `"7 days"`. Every driver
173
+ * expires the key. Cloudflare KV keeps it at least 60 seconds.
174
+ */
175
+ ttl?: number | Duration;
164
176
  }
165
177
  /** Options of `kv.lock()`. */
166
178
  interface KVLockOptions {
167
179
  /**
168
- * Seconds until the lock expires, so a crashed holder never keeps it. Pick a value longer than the work.
169
- * KV does not renew a lock: when the work runs longer than `ttl`, another runner can take the lock.
180
+ * How long until the lock expires, so a crashed holder never keeps it: a number of seconds, or a duration such as
181
+ * `"30s"`. Pick a value longer than the work. KV does not renew a lock: when the work runs longer than `ttl`, another
182
+ * runner can take the lock.
170
183
  */
171
- ttl: number;
184
+ ttl: number | Duration;
172
185
  /** Seconds to wait for a held lock, polling with backoff. Default `0`: return `{ acquired: false }` at once. */
173
186
  wait?: number;
174
187
  }
@@ -219,7 +232,7 @@ interface KVListPage {
219
232
  * Config errors, for example an unknown store name, throw.
220
233
  */
221
234
  interface KVStorage {
222
- clear(base?: string, options?: unknown): Promise<Result<void, "KV_OPERATION_FAILED">>;
235
+ clear(options?: KVClearOptions): Promise<Result<void, "KV_INVALID_CLEAR_OPTIONS" | "KV_OPERATION_FAILED">>;
223
236
  delete(key: string, options?: unknown): Promise<Result<void, "KV_KEY_TOO_LONG" | "KV_OPERATION_FAILED">>;
224
237
  /**
225
238
  * Deletes the key only when its value equals `expected`, in one atomic step. Returns `true` when this call deleted it.
@@ -249,12 +262,12 @@ interface KVStorage {
249
262
  }): Promise<Result<KVValue<T, K> | null, "KV_OPERATION_FAILED" | "KV_UNSUPPORTED_OPERATION">>;
250
263
  has(key: string, options?: unknown): Promise<Result<boolean, "KV_KEY_TOO_LONG" | "KV_OPERATION_FAILED">>;
251
264
  /**
252
- * Atomically increments a counter. The TTL is applied only when the counter is created.
265
+ * Atomically increments a counter. The TTL, a number of seconds or a duration such as `"1m"`, is applied only when
266
+ * the counter is created.
253
267
  * The drivers in {@link KVDriversWith}`<"increment">` support it. The others return `KV_UNSUPPORTED_OPERATION`.
254
268
  */
255
- increment(key: string, ttl: number): Promise<Result<number, "KV_INVALID_TTL" | "KV_OPERATION_FAILED" | "KV_UNSUPPORTED_OPERATION">>;
256
- keys(base?: string, options?: unknown): Promise<Result<string[], "KV_OPERATION_FAILED">>;
257
- list(options: KVListOptions): Promise<Result<KVListPage, "KV_INVALID_CURSOR" | "KV_INVALID_LIMIT" | "KV_OPERATION_FAILED">>;
269
+ increment(key: string, ttl: number | Duration): Promise<Result<number, "KV_INVALID_TTL" | "KV_OPERATION_FAILED" | "KV_UNSUPPORTED_OPERATION">>;
270
+ list(options?: KVListOptions): Promise<Result<KVListPage, "KV_INVALID_CURSOR" | "KV_INVALID_LIMIT" | "KV_OPERATION_FAILED">>;
258
271
  /**
259
272
  * A lock on `key` that one runner holds at a time, across server processes that share the store. It writes a random
260
273
  * owner token with `setIfAbsent()` and a TTL, and frees it with `deleteIfEquals()`, so a runner never deletes a lock
@@ -263,7 +276,8 @@ interface KVStorage {
263
276
  */
264
277
  lock(key: string, options: KVLockOptions): KVLock;
265
278
  /**
266
- * Writes a value. `{ ttl }` expires it after that many seconds; without `ttl`, the value stays until you delete or replace it.
279
+ * Writes a value. `{ ttl }` expires it after a number of seconds or a duration such as `"1h"`; without `ttl`, the
280
+ * value stays until you delete or replace it.
267
281
  * Cloudflare KV stores return `KV_KEY_TOO_LONG` and `KV_VALUE_TOO_LARGE` before a write that Cloudflare rejects.
268
282
  */
269
283
  set<T = unknown>(key: string, value: T, options?: KVSetOptions): Promise<Result<void, "KV_INVALID_OPTION" | "KV_INVALID_TTL" | "KV_KEY_TOO_LONG" | "KV_OPERATION_FAILED" | "KV_VALUE_TOO_LARGE">>;
@@ -297,7 +311,7 @@ interface KVStoreOptions {
297
311
  mode?: "read" | "write";
298
312
  }
299
313
  /**
300
- * KV handle from `kv.store(name, { mode: "read" })`, with only `get()`, `getMany()`, `has()`, `keys()`, and `list()`.
314
+ * KV handle from `kv.store(name, { mode: "read" })`, with only `get()`, `getMany()`, `has()`, and `list()`.
301
315
  * It limits this API, not the host: the KV binding or provider token keeps full access, so it is not a security
302
316
  * boundary against code that reads the binding directly.
303
317
  */
@@ -305,8 +319,7 @@ interface ReadonlyKVStorage {
305
319
  get: KVStorage["get"];
306
320
  getMany: KVStorage["getMany"];
307
321
  has: KVStorage["has"];
308
- keys: KVStorage["keys"];
309
322
  list: KVStorage["list"];
310
323
  }
311
324
  //#endregion
312
- export { RedisKVStoreConfig as A, KVOptions as C, KVStoreName as D, KVStoreConfig as E, ResolvedKVModuleOptions as F, ResolvedKVStoreConfig as I, ResolvedRedisKVStoreConfig as L, ResolvedDatabaseKVStoreConfig as M, ResolvedDenoKVStoreConfig as N, KVStoreOptions as O, ResolvedFsLiteKVStoreConfig as P, ResolvedUpstashKVStoreConfig as R, KVOperation as S, KVStorage as T, KVLock as _, InferredKVStoreConfig as a, KVLockOptions as b, KVDriverAtomicOperations as c, KVErrorCode as d, KVGetOptions as f, KVListPage as g, KVListOptions as h, FsLiteKVStoreConfig as i, ResolvedCloudflareKVStoreConfig as j, ReadonlyKVStorage as k, KVDriversWith as l, KVGetTypeMap as m, DatabaseKVStoreConfig as n, KVAtomicOperation as o, KVGetType as p, DenoKVStoreConfig as r, KVDriver as s, CloudflareKVStoreConfig as t, KVEntry as u, KVLockAcquisition as v, KVSetOptions as w, KVLockRunResult as x, KVLockErrorCode as y, UpstashKVStoreConfig as z };
325
+ export { KVStoreOptions as A, ResolvedUpstashKVStoreConfig as B, KVOperation as C, KVStorage as D, KVSetOptions as E, ResolvedDenoKVStoreConfig as F, ResolvedFsLiteKVStoreConfig as I, ResolvedKVModuleOptions as L, RedisKVStoreConfig as M, ResolvedCloudflareKVStoreConfig as N, KVStoreConfig as O, ResolvedDatabaseKVStoreConfig as P, ResolvedKVStoreConfig as R, KVLockRunResult as S, KVPageOptions as T, UpstashKVStoreConfig as V, KVListPage as _, InferredKVStoreConfig as a, KVLockErrorCode as b, KVDriver as c, KVEntry as d, KVErrorCode as f, KVListOptions as g, KVGetTypeMap as h, FsLiteKVStoreConfig as i, ReadonlyKVStorage as j, KVStoreName as k, KVDriverAtomicOperations as l, KVGetType as m, DatabaseKVStoreConfig as n, KVAtomicOperation as o, KVGetOptions as p, DenoKVStoreConfig as r, KVClearOptions as s, CloudflareKVStoreConfig as t, KVDriversWith as u, KVLock as v, KVOptions as w, KVLockOptions as x, KVLockAcquisition as y, ResolvedRedisKVStoreConfig as z };
@@ -1,5 +1,5 @@
1
- import { n as kvErrorDiagnostics } from "./error-diagnostics-BxqvtEIb.js";
2
- import { a as upstashUrlEnvNames, i as upstashTokenEnvNames, s as readEnv } from "./config-BXEVoZE3.js";
1
+ import { n as kvErrorDiagnostics } from "./error-diagnostics-C54QQEhN.js";
2
+ import { a as upstashUrlEnvNames, i as upstashTokenEnvNames, s as readEnv } from "./config-LfpUj9hs.js";
3
3
  //#region src/runtime/upstash.ts
4
4
  function isMaskedValue(value) {
5
5
  return !value || /^\*+$/.test(value);
@@ -1,6 +1,7 @@
1
1
  declare module "#vitehub/kv/config" {
2
2
  export const hosting: string | undefined
3
3
  export const kv: false | import("./index.js").ResolvedKVModuleOptions
4
+ export const durabilityReportsStores: boolean
4
5
  /** Loads the databases of the app. Set when a store uses the `database` driver. */
5
6
  export const loadDatabases: (() => Promise<Record<string, { db: object } | undefined>>) | undefined
6
7
  const config: { hosting?: string, kv: typeof kv }
package/dist/vite.d.ts CHANGED
@@ -1,6 +1,7 @@
1
- import { C as KVOptions, F as ResolvedKVModuleOptions } from "./types-DEiKew_3.js";
1
+ import { L as ResolvedKVModuleOptions, w as KVOptions } from "./types-Dv-OSXZC.js";
2
2
  import { r as ViteHubCliPluginMetadata } from "./cli-DpPhz_Ek.js";
3
- import { n as OperationContributionEntry } from "./operation-definition-8gOe-ubD.js";
3
+ import { n as OperationContributionEntry } from "./operation-definition-nmRu_O3V.js";
4
+ import "nostics";
4
5
  import { Plugin } from "vite";
5
6
  //#region src/config.d.ts
6
7
  interface KVResolutionInput {
@@ -9,6 +10,7 @@ interface KVResolutionInput {
9
10
  * store falls back to `fs-lite` when the runtime has no binding. Builds leave it unset.
10
11
  */
11
12
  dev?: boolean;
13
+ prerender?: boolean;
12
14
  env?: Record<string, string | undefined>;
13
15
  hosting?: string;
14
16
  /**
@@ -157,20 +159,30 @@ export interface KVVitePluginAPI {
157
159
  /** The variables that the KV stores read on the deployed host. See `kvDeploymentEnv()`. */
158
160
  deploymentEnv: DeploymentEnvContribution;
159
161
  getConfig: () => KVViteRuntimeConfig;
162
+ getBuildConfig: () => KVViteRuntimeConfig;
160
163
  }
161
164
  export type KVVitePlugin = Plugin & {
162
165
  api: KVVitePluginAPI;
163
166
  nitro: {
164
167
  name: string;
165
- setup: (nitro: {
166
- options: NitroCloudflareKVTarget;
167
- }) => void;
168
+ setup: (nitro: KVNitroBuild) => void;
168
169
  };
169
170
  vitehub: ViteHubCliPluginMetadata & ViteHubInspectionPluginMetadata & DurabilityPluginMetadata & {
170
171
  operations: readonly OperationContributionEntry[];
171
172
  };
172
173
  };
173
174
  export declare function hubKVOptionalPeerResolver(): Plugin;
175
+ /** The part of Nitro's prerender config that KV changes. */
176
+ interface KVPrerenderConfig {
177
+ virtual?: Record<string, string | (() => string)>;
178
+ }
179
+ /** The parts of a Nitro build that KV changes. */
180
+ interface KVNitroBuild {
181
+ hooks?: {
182
+ hook: (name: "prerender:config", callback: (config: KVPrerenderConfig) => void) => unknown;
183
+ };
184
+ options: NitroCloudflareKVTarget;
185
+ }
174
186
  interface NitroCloudflareKVTarget {
175
187
  handlers?: unknown[];
176
188
  kv?: Record<string, unknown>;