@vite-hub/kv 0.0.3 → 0.1.0
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/LICENSE +201 -0
- package/README.md +40 -7
- package/dist/cli-Cniq99iX.d.ts +85 -0
- package/dist/cli.d.ts +15 -0
- package/dist/cli.js +448 -0
- package/dist/cloudflare-env-C5rM7kUY.js +74 -0
- package/dist/{config-C1ofyzSt.js → config-DR6_5ZwI.js} +37 -17
- package/dist/continuations-DPyH_vr1.js +63 -0
- package/dist/create-storage-29gMzf41.js +442 -0
- package/dist/cursor-history-dqYDlsaD.js +148 -0
- package/dist/deno-kv-CSYGvRx4.js +120 -0
- package/dist/driver-D9fI27jl.d.ts +94 -0
- package/dist/error-diagnostics-D_cQeZTi.js +82 -0
- package/dist/errors-Dc2oiW-Y.js +38 -0
- package/dist/errors.d.ts +12 -7
- package/dist/errors.js +2 -20
- package/dist/fs-lite-CbLKxLut.js +90 -0
- package/dist/index.d.ts +5 -4
- package/dist/index.js +119 -49
- package/dist/object-B1NW521B.js +7 -0
- package/dist/operations-ByuDE12r.js +1442 -0
- package/dist/operations-pn8_eGob.js +432 -0
- package/dist/operations-tNUdj3G4.d.ts +212 -0
- package/dist/operations.d.ts +68 -0
- package/dist/operations.js +455 -0
- package/dist/results-p7nPCnaJ.js +6 -0
- package/dist/runtime/cloudflare-kv.d.ts +6 -0
- package/dist/runtime/cloudflare-kv.js +100 -0
- package/dist/runtime/upstash-driver.d.ts +5 -2
- package/dist/runtime/upstash-driver.js +111 -2
- package/dist/types-CMDvN0Wr.d.ts +81 -0
- package/dist/utils2-Dsb9P4_2.js +28 -0
- package/dist/virtual-module.d.ts +6 -0
- package/dist/virtual.d.ts +2 -6
- package/dist/vite.d.ts +30 -10
- package/dist/vite.js +4910 -213
- package/package.json +57 -15
- package/dist/deno-kv-JnitV_jG.js +0 -54
- package/dist/types-Dg4l318i.d.ts +0 -60
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { n as kvErrorDiagnostics } from "./error-diagnostics-D_cQeZTi.js";
|
|
2
|
+
import { n as recordCursor, t as createCursorHistory } from "./cursor-history-dqYDlsaD.js";
|
|
3
|
+
//#region src/runtime/deno-kv.ts
|
|
4
|
+
function getDenoRuntime() {
|
|
5
|
+
return globalThis.Deno;
|
|
6
|
+
}
|
|
7
|
+
function toDenoKey(key) {
|
|
8
|
+
return [key];
|
|
9
|
+
}
|
|
10
|
+
function fromDenoKey(key) {
|
|
11
|
+
return key.length === 1 && typeof key[0] === "string" ? key[0] : void 0;
|
|
12
|
+
}
|
|
13
|
+
function toDenoExpireIn(ttl) {
|
|
14
|
+
if (typeof ttl !== "number" || !Number.isFinite(ttl) || ttl <= 0) return void 0;
|
|
15
|
+
const expireIn = Math.max(1, Math.ceil(ttl * 1e3));
|
|
16
|
+
return Number.isSafeInteger(expireIn) ? { expireIn } : void 0;
|
|
17
|
+
}
|
|
18
|
+
function createDenoKVDriver(options = { driver: "deno-kv" }) {
|
|
19
|
+
let kvPromise;
|
|
20
|
+
let disposed = false;
|
|
21
|
+
let disposal;
|
|
22
|
+
const cursorNamespace = `deno-kv:${options.path ?? "default"}`;
|
|
23
|
+
const cursorHistories = createCursorHistory(cursorNamespace);
|
|
24
|
+
function open() {
|
|
25
|
+
if (disposed) return Promise.reject(/* @__PURE__ */ new Error("KV storage has been disposed."));
|
|
26
|
+
if (kvPromise) return kvPromise;
|
|
27
|
+
const pending = (async () => {
|
|
28
|
+
const openKv = getDenoRuntime()?.openKv;
|
|
29
|
+
if (!openKv) throw kvErrorDiagnostics.KV_DENO_KV_UNAVAILABLE();
|
|
30
|
+
return openKv(options.path);
|
|
31
|
+
})();
|
|
32
|
+
kvPromise = pending;
|
|
33
|
+
pending.catch(() => {
|
|
34
|
+
if (kvPromise === pending) kvPromise = void 0;
|
|
35
|
+
});
|
|
36
|
+
return pending;
|
|
37
|
+
}
|
|
38
|
+
async function matchingKeys(base = "") {
|
|
39
|
+
const kv = await open();
|
|
40
|
+
const keys = [];
|
|
41
|
+
for await (const entry of kv.list({ prefix: [] })) if (fromDenoKey(entry.key)?.startsWith(base)) keys.push(entry.key);
|
|
42
|
+
return keys;
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
name: "deno-kv",
|
|
46
|
+
options,
|
|
47
|
+
async clear(base = "") {
|
|
48
|
+
const kv = await open();
|
|
49
|
+
for (const key of await matchingKeys(base)) await kv.delete(key);
|
|
50
|
+
},
|
|
51
|
+
dispose() {
|
|
52
|
+
return disposal ??= (async () => {
|
|
53
|
+
disposed = true;
|
|
54
|
+
cursorHistories.clear();
|
|
55
|
+
try {
|
|
56
|
+
if (kvPromise) {
|
|
57
|
+
(await kvPromise).close?.();
|
|
58
|
+
kvPromise = void 0;
|
|
59
|
+
}
|
|
60
|
+
} finally {
|
|
61
|
+
cursorHistories.clear();
|
|
62
|
+
}
|
|
63
|
+
})();
|
|
64
|
+
},
|
|
65
|
+
async getItem(key) {
|
|
66
|
+
const entry = await (await open()).get(toDenoKey(key));
|
|
67
|
+
return entry.versionstamp === null ? null : entry.value;
|
|
68
|
+
},
|
|
69
|
+
async getKeys(base = "") {
|
|
70
|
+
return (await matchingKeys(base)).flatMap((key) => fromDenoKey(key) ?? []).sort();
|
|
71
|
+
},
|
|
72
|
+
async listKeys({ cursor, limit, prefix = "" }) {
|
|
73
|
+
if (disposed) throw new Error("KV storage has been disposed.");
|
|
74
|
+
const continuation = cursor ? cursorHistories.take(cursor) : {
|
|
75
|
+
providerCursor: void 0,
|
|
76
|
+
history: /* @__PURE__ */ new Set(),
|
|
77
|
+
namespace: cursorNamespace
|
|
78
|
+
};
|
|
79
|
+
try {
|
|
80
|
+
const cursors = continuation.history;
|
|
81
|
+
const iterator = (await open()).list({ prefix: [] }, {
|
|
82
|
+
cursor: continuation.providerCursor,
|
|
83
|
+
limit
|
|
84
|
+
});
|
|
85
|
+
const keys = [];
|
|
86
|
+
for await (const entry of iterator) {
|
|
87
|
+
const key = fromDenoKey(entry.key);
|
|
88
|
+
if (key?.startsWith(prefix)) keys.push(key);
|
|
89
|
+
}
|
|
90
|
+
if (!iterator.cursor) {
|
|
91
|
+
cursorHistories.complete(cursor);
|
|
92
|
+
return { keys };
|
|
93
|
+
}
|
|
94
|
+
if (disposed) throw new Error("KV storage has been disposed.");
|
|
95
|
+
if (cursors.has(iterator.cursor)) throw kvErrorDiagnostics.KV_PROVIDER_INVALID_RESPONSE({ problem: "Deno KV list returned a repeated pagination cursor." });
|
|
96
|
+
if (!recordCursor(cursors, iterator.cursor)) throw kvErrorDiagnostics.KV_PROVIDER_INVALID_RESPONSE({ problem: "Deno KV rejected pagination after the cursor history limit was reached." });
|
|
97
|
+
const nextCursor = cursorHistories.store(iterator.cursor, cursors);
|
|
98
|
+
cursorHistories.complete(cursor);
|
|
99
|
+
return {
|
|
100
|
+
keys,
|
|
101
|
+
cursor: nextCursor
|
|
102
|
+
};
|
|
103
|
+
} catch (error) {
|
|
104
|
+
if (cursor && !disposed) cursorHistories.restore(cursor, continuation);
|
|
105
|
+
throw error;
|
|
106
|
+
}
|
|
107
|
+
},
|
|
108
|
+
async hasItem(key) {
|
|
109
|
+
return (await (await open()).get(toDenoKey(key))).versionstamp !== null;
|
|
110
|
+
},
|
|
111
|
+
async removeItem(key) {
|
|
112
|
+
await (await open()).delete(toDenoKey(key));
|
|
113
|
+
},
|
|
114
|
+
async setItem(key, value, writeOptions) {
|
|
115
|
+
await (await open()).set(toDenoKey(key), value, toDenoExpireIn(writeOptions?.ttl));
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
//#endregion
|
|
120
|
+
export { createDenoKVDriver as default };
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { o as KVListOptions, s as KVListPage } from "./types-CMDvN0Wr.js";
|
|
2
|
+
import "unstorage/drivers/cloudflare-kv-binding";
|
|
3
|
+
import "unstorage/drivers/upstash";
|
|
4
|
+
import "unstorage/drivers/fs-lite";
|
|
5
|
+
import "unstorage/drivers/azure-app-configuration";
|
|
6
|
+
import "unstorage/drivers/azure-cosmos";
|
|
7
|
+
import "unstorage/drivers/azure-key-vault";
|
|
8
|
+
import "unstorage/drivers/azure-storage-blob";
|
|
9
|
+
import "unstorage/drivers/azure-storage-table";
|
|
10
|
+
import "unstorage/drivers/capacitor-preferences";
|
|
11
|
+
import "unstorage/drivers/cloudflare-cache-binding";
|
|
12
|
+
import "unstorage/drivers/cloudflare-kv-http";
|
|
13
|
+
import "unstorage/drivers/cloudflare-r2-binding";
|
|
14
|
+
import "unstorage/drivers/db0";
|
|
15
|
+
import "unstorage/drivers/deno-kv-node";
|
|
16
|
+
import "unstorage/drivers/deno-kv";
|
|
17
|
+
import "unstorage/drivers/fs";
|
|
18
|
+
import "unstorage/drivers/github";
|
|
19
|
+
import "unstorage/drivers/http";
|
|
20
|
+
import "unstorage/drivers/indexedb";
|
|
21
|
+
import "unstorage/drivers/localstorage";
|
|
22
|
+
import "unstorage/drivers/lru-cache";
|
|
23
|
+
import "unstorage/drivers/mongodb";
|
|
24
|
+
import "unstorage/drivers/netlify-blobs";
|
|
25
|
+
import "unstorage/drivers/overlay";
|
|
26
|
+
import "unstorage/drivers/planetscale";
|
|
27
|
+
import "unstorage/drivers/redis";
|
|
28
|
+
import "unstorage/drivers/s3";
|
|
29
|
+
import "unstorage/drivers/session-storage";
|
|
30
|
+
import "unstorage/drivers/uploadthing";
|
|
31
|
+
import "unstorage/drivers/vercel-blob";
|
|
32
|
+
import "unstorage/drivers/vercel-runtime-cache";
|
|
33
|
+
//#region ../../node_modules/.pnpm/unstorage@2.0.0-alpha.10/node_modules/unstorage/dist/_chunks/types.d.mts
|
|
34
|
+
type StorageValue = null | string | number | boolean | object;
|
|
35
|
+
type WatchEvent = "update" | "remove";
|
|
36
|
+
type WatchCallback = (event: WatchEvent, key: string) => any;
|
|
37
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
38
|
+
type Unwatch = () => MaybePromise<void>;
|
|
39
|
+
interface StorageMeta {
|
|
40
|
+
atime?: Date;
|
|
41
|
+
mtime?: Date;
|
|
42
|
+
ttl?: number;
|
|
43
|
+
[key: string]: StorageValue | Date | undefined;
|
|
44
|
+
}
|
|
45
|
+
type TransactionOptions = Record<string, any>;
|
|
46
|
+
type GetKeysOptions = TransactionOptions & {
|
|
47
|
+
maxDepth?: number;
|
|
48
|
+
};
|
|
49
|
+
interface DriverFlags {
|
|
50
|
+
maxDepth?: boolean;
|
|
51
|
+
ttl?: boolean;
|
|
52
|
+
}
|
|
53
|
+
interface Driver<OptionsT = any, InstanceT = any> {
|
|
54
|
+
name?: string;
|
|
55
|
+
flags?: DriverFlags;
|
|
56
|
+
options?: OptionsT;
|
|
57
|
+
getInstance?: () => InstanceT;
|
|
58
|
+
hasItem: (key: string, opts: TransactionOptions) => MaybePromise<boolean>;
|
|
59
|
+
getItem: (key: string, opts?: TransactionOptions) => MaybePromise<StorageValue>;
|
|
60
|
+
/** @experimental */
|
|
61
|
+
getItems?: (items: {
|
|
62
|
+
key: string;
|
|
63
|
+
options?: TransactionOptions;
|
|
64
|
+
}[], commonOptions?: TransactionOptions) => MaybePromise<{
|
|
65
|
+
key: string;
|
|
66
|
+
value: StorageValue;
|
|
67
|
+
}[]>;
|
|
68
|
+
/** @experimental */
|
|
69
|
+
getItemRaw?: (key: string, opts: TransactionOptions) => MaybePromise<unknown>;
|
|
70
|
+
setItem?: (key: string, value: string, opts: TransactionOptions) => MaybePromise<void>;
|
|
71
|
+
/** @experimental */
|
|
72
|
+
setItems?: (items: {
|
|
73
|
+
key: string;
|
|
74
|
+
value: string;
|
|
75
|
+
options?: TransactionOptions;
|
|
76
|
+
}[], commonOptions?: TransactionOptions) => MaybePromise<void>;
|
|
77
|
+
/** @experimental */
|
|
78
|
+
setItemRaw?: (key: string, value: any, opts: TransactionOptions) => MaybePromise<void>;
|
|
79
|
+
removeItem?: (key: string, opts: TransactionOptions) => MaybePromise<void>;
|
|
80
|
+
getMeta?: (key: string, opts: TransactionOptions) => MaybePromise<StorageMeta | null>;
|
|
81
|
+
getKeys: (base: string, opts: GetKeysOptions) => MaybePromise<string[]>;
|
|
82
|
+
clear?: (base: string, opts: TransactionOptions) => MaybePromise<void>;
|
|
83
|
+
dispose?: () => MaybePromise<void>;
|
|
84
|
+
watch?: (callback: WatchCallback) => MaybePromise<Unwatch>;
|
|
85
|
+
}
|
|
86
|
+
//#endregion
|
|
87
|
+
//#region src/runtime/driver.d.ts
|
|
88
|
+
type KVRuntimeDriver = Driver & Record<string, unknown> & {
|
|
89
|
+
getAndDeleteItem?: <T = unknown>(key: string) => Promise<T | null>;
|
|
90
|
+
incrementItem?: (key: string, ttl: number) => Promise<number>;
|
|
91
|
+
listKeys: (options: KVListOptions) => Promise<KVListPage>;
|
|
92
|
+
};
|
|
93
|
+
//#endregion
|
|
94
|
+
export { KVRuntimeDriver as t };
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { defineErrors } from "@vite-hub/runtime";
|
|
2
|
+
//#region src/error-diagnostics.ts
|
|
3
|
+
const cursorProblems = {
|
|
4
|
+
"expired": "The list cursor has expired.",
|
|
5
|
+
"foreign": "The list cursor belongs to a different storage backend.",
|
|
6
|
+
"in-use": "The list cursor is already in use.",
|
|
7
|
+
"invalid": "The list cursor is invalid or exceeds the maximum size."
|
|
8
|
+
};
|
|
9
|
+
const kvErrorDiagnostics = /*#__PURE__*/ defineErrors({
|
|
10
|
+
docs: "https://vitehub.dev/docs/kv/limits-and-errors",
|
|
11
|
+
codes: {
|
|
12
|
+
KV_INVALID_CURSOR: {
|
|
13
|
+
why: ({ problem }) => cursorProblems[problem],
|
|
14
|
+
fix: "Pass the `cursor` of the previous page once, to the same store. If it fails, start again without `cursor`."
|
|
15
|
+
},
|
|
16
|
+
KV_INVALID_LIMIT: {
|
|
17
|
+
why: "`limit` must be a positive integer.",
|
|
18
|
+
fix: "Pass a positive integer, for example `kv.list({ limit: 100 })`."
|
|
19
|
+
},
|
|
20
|
+
KV_INVALID_TTL: {
|
|
21
|
+
why: "`ttl` must be a positive number of seconds within the safe integer range.",
|
|
22
|
+
fix: "Pass the TTL in seconds, for example `kv.increment('attempts', 60)`."
|
|
23
|
+
},
|
|
24
|
+
KV_OPERATION_FAILED: {
|
|
25
|
+
why: ({ operation, store }) => `kv.${operation}() failed on the "${store}" store.`,
|
|
26
|
+
fix: "Read `error.cause` for the provider error. Retry when the provider is available again."
|
|
27
|
+
},
|
|
28
|
+
KV_UNSUPPORTED_OPERATION: {
|
|
29
|
+
why: ({ operation, store }) => `kv.${operation}() needs atomic operations, and the "${store}" store does not support them.`,
|
|
30
|
+
fix: "Use the `upstash` driver for this store. For request budgets, use the Rate Limit primitive."
|
|
31
|
+
},
|
|
32
|
+
KV_DENO_KV_UNAVAILABLE: {
|
|
33
|
+
why: "The `deno-kv` driver needs `Deno.openKv()`, and this runtime does not have it.",
|
|
34
|
+
fix: "Run on Deno with KV enabled, or select another KV driver."
|
|
35
|
+
},
|
|
36
|
+
KV_DISABLED: {
|
|
37
|
+
why: "KV is not configured in this runtime.",
|
|
38
|
+
fix: "Add `hubKv()` to the Vite config and do not set `kv: false`."
|
|
39
|
+
},
|
|
40
|
+
KV_INVALID_CONFIG: {
|
|
41
|
+
why: ({ path }) => `\`${path}\` must be a plain object.`,
|
|
42
|
+
fix: "Pass an object, for example `kv: { driver: 'fs-lite' }`."
|
|
43
|
+
},
|
|
44
|
+
KV_INVALID_DRIVER: {
|
|
45
|
+
why: ({ driver }) => `\`kv.driver\` ${driver} is not a KV driver.`,
|
|
46
|
+
fix: "Set `kv.driver` to \"cloudflare-kv-binding\", \"deno-kv\", \"upstash\", or \"fs-lite\"."
|
|
47
|
+
},
|
|
48
|
+
KV_MISSING_DEFAULT_STORE: {
|
|
49
|
+
why: "`kv.stores.default` is missing.",
|
|
50
|
+
fix: "Add a `default` store when you configure named KV stores."
|
|
51
|
+
},
|
|
52
|
+
KV_MISSING_ENV: {
|
|
53
|
+
why: ({ names }) => `Upstash KV needs the runtime environment variable ${names}.`,
|
|
54
|
+
fix: "Set the variable in the host environment, or set `url` and `token` in the KV config."
|
|
55
|
+
},
|
|
56
|
+
KV_STORE_NOT_FOUND: {
|
|
57
|
+
why: ({ store }) => `The KV store "${store}" is not configured.`,
|
|
58
|
+
fix: "Add the store under `kv.stores`, or use the name of a configured store."
|
|
59
|
+
},
|
|
60
|
+
KV_CLI_USAGE: {
|
|
61
|
+
why: ({ problem }) => problem,
|
|
62
|
+
fix: "Run `vitehub kv --help` to see the commands and options."
|
|
63
|
+
},
|
|
64
|
+
KV_PROVIDER_INVALID_RESPONSE: {
|
|
65
|
+
why: ({ problem }) => `The KV provider returned a response that ViteHub cannot use. ${problem}`,
|
|
66
|
+
fix: "Retry the operation. If it fails again, open a ViteHub issue with the driver name."
|
|
67
|
+
},
|
|
68
|
+
KV_PROVISION_FAILED: {
|
|
69
|
+
why: ({ problem }) => `Cloudflare KV provisioning failed. ${problem}`,
|
|
70
|
+
fix: "Run `vitehub provision run --provider cloudflare --dry-run` and check the Cloudflare token permissions."
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
/** Codes that a KV operation throws because the code or the deployment must change. */
|
|
75
|
+
const kvConfigErrorCodes = /* @__PURE__ */ new Set([
|
|
76
|
+
"KV_DENO_KV_UNAVAILABLE",
|
|
77
|
+
"KV_DISABLED",
|
|
78
|
+
"KV_MISSING_ENV",
|
|
79
|
+
"KV_STORE_NOT_FOUND"
|
|
80
|
+
]);
|
|
81
|
+
//#endregion
|
|
82
|
+
export { kvErrorDiagnostics as n, kvConfigErrorCodes as t };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { n as kvErrorDiagnostics, t as kvConfigErrorCodes } from "./error-diagnostics-D_cQeZTi.js";
|
|
2
|
+
import { isRuntimeDiagnostic, toResultError } from "@vite-hub/runtime";
|
|
3
|
+
//#region src/errors.ts
|
|
4
|
+
function hasCode(error, codes) {
|
|
5
|
+
return codes.some((code) => code === error.code);
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Runs one KV operation and returns `[error, data]`.
|
|
9
|
+
* A KV error with one of `codes` is returned with Runtime's public serializer. A config error is thrown.
|
|
10
|
+
* Any other failure becomes `KV_OPERATION_FAILED` with the failure in `cause`.
|
|
11
|
+
*/
|
|
12
|
+
async function kvResult(operation, store, run, codes = []) {
|
|
13
|
+
try {
|
|
14
|
+
return [void 0, await run()];
|
|
15
|
+
} catch (cause) {
|
|
16
|
+
if (isRuntimeDiagnostic(cause) && kvConfigErrorCodes.has(cause.code)) throw cause;
|
|
17
|
+
if (isRuntimeDiagnostic(cause) && hasCode(cause, codes)) return [toResultError(cause), void 0];
|
|
18
|
+
return [kvErrorDiagnostics.KV_OPERATION_FAILED({
|
|
19
|
+
cause,
|
|
20
|
+
operation,
|
|
21
|
+
store
|
|
22
|
+
}), void 0];
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
function kvStoreNotFoundError(store) {
|
|
26
|
+
return kvErrorDiagnostics.KV_STORE_NOT_FOUND({ store });
|
|
27
|
+
}
|
|
28
|
+
function kvUnsupportedOperationError(operation, store) {
|
|
29
|
+
return kvErrorDiagnostics.KV_UNSUPPORTED_OPERATION({
|
|
30
|
+
operation,
|
|
31
|
+
store
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
function kvInvalidLimitError() {
|
|
35
|
+
return kvErrorDiagnostics.KV_INVALID_LIMIT();
|
|
36
|
+
}
|
|
37
|
+
//#endregion
|
|
38
|
+
export { kvUnsupportedOperationError as i, kvResult as n, kvStoreNotFoundError as r, kvInvalidLimitError as t };
|
package/dist/errors.d.ts
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
|
-
import { a as
|
|
2
|
-
import {
|
|
3
|
-
|
|
1
|
+
import { a as KVErrorCode, l as KVOperation } from "./types-CMDvN0Wr.js";
|
|
2
|
+
import { Result, ResultError } from "@vite-hub/runtime";
|
|
4
3
|
//#region src/errors.d.ts
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
/**
|
|
5
|
+
* Runs one KV operation and returns `[error, data]`.
|
|
6
|
+
* A KV error with one of `codes` is returned with Runtime's public serializer. A config error is thrown.
|
|
7
|
+
* Any other failure becomes `KV_OPERATION_FAILED` with the failure in `cause`.
|
|
8
|
+
*/
|
|
9
|
+
export declare function kvResult<TData, TCode extends KVErrorCode = never>(operation: KVOperation, store: string, run: () => Promise<TData>, codes?: readonly TCode[]): Promise<Result<TData, TCode | "KV_OPERATION_FAILED">>;
|
|
10
|
+
export declare function kvStoreNotFoundError(store: string): ResultError<"KV_STORE_NOT_FOUND">;
|
|
11
|
+
export declare function kvUnsupportedOperationError(operation: KVOperation, store: string): ResultError<"KV_UNSUPPORTED_OPERATION">;
|
|
12
|
+
export declare function kvInvalidLimitError(): ResultError<"KV_INVALID_LIMIT">;
|
|
13
|
+
//#endregion
|
package/dist/errors.js
CHANGED
|
@@ -1,20 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
function kvError(operation, store, cause) {
|
|
4
|
-
return new ViteHubError("KV_OPERATION_FAILED", "KV storage operation failed.", {
|
|
5
|
-
cause,
|
|
6
|
-
details: {
|
|
7
|
-
operation,
|
|
8
|
-
store
|
|
9
|
-
}
|
|
10
|
-
});
|
|
11
|
-
}
|
|
12
|
-
async function kvResult(operation, store, run) {
|
|
13
|
-
try {
|
|
14
|
-
return [null, await run()];
|
|
15
|
-
} catch (cause) {
|
|
16
|
-
return [kvError(operation, store, cause), void 0];
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
//#endregion
|
|
20
|
-
export { kvError, kvResult };
|
|
1
|
+
import { i as kvUnsupportedOperationError, n as kvResult, r as kvStoreNotFoundError, t as kvInvalidLimitError } from "./errors-Dc2oiW-Y.js";
|
|
2
|
+
export { kvInvalidLimitError, kvResult, kvStoreNotFoundError, kvUnsupportedOperationError };
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { n as kvErrorDiagnostics } from "./error-diagnostics-D_cQeZTi.js";
|
|
2
|
+
import { t as createKVContinuations } from "./continuations-DPyH_vr1.js";
|
|
3
|
+
import { opendir, stat } from "node:fs/promises";
|
|
4
|
+
import { join, relative, resolve, sep } from "node:path";
|
|
5
|
+
import createDriver from "unstorage/drivers/fs-lite";
|
|
6
|
+
//#region src/runtime/fs-lite.ts
|
|
7
|
+
function createFsLiteKVDriver(options) {
|
|
8
|
+
const root = resolve(options.base);
|
|
9
|
+
const driver = createDriver(options);
|
|
10
|
+
const getItem = driver.getItem;
|
|
11
|
+
driver.getItem = async (key, operationOptions) => {
|
|
12
|
+
const value = await getItem(key, operationOptions);
|
|
13
|
+
if (value instanceof Error) {
|
|
14
|
+
if ("code" in value && value.code === "ENOTDIR") return null;
|
|
15
|
+
throw value;
|
|
16
|
+
}
|
|
17
|
+
return value;
|
|
18
|
+
};
|
|
19
|
+
const hasItem = driver.hasItem;
|
|
20
|
+
driver.hasItem = async (key, operationOptions) => {
|
|
21
|
+
if (!await hasItem(key, operationOptions)) return false;
|
|
22
|
+
try {
|
|
23
|
+
return (await stat(join(root, key.replace(/:/g, "/")))).isFile();
|
|
24
|
+
} catch (error) {
|
|
25
|
+
if (error instanceof Error && "code" in error && (error.code === "ENOENT" || error.code === "ENOTDIR")) return false;
|
|
26
|
+
throw error;
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
const expired = () => kvErrorDiagnostics.KV_INVALID_CURSOR({ problem: "expired" });
|
|
30
|
+
const continuations = createKVContinuations({
|
|
31
|
+
expired,
|
|
32
|
+
release: (iterator) => iterator.return(void 0)
|
|
33
|
+
});
|
|
34
|
+
const dispose = driver.dispose;
|
|
35
|
+
driver.dispose = async () => {
|
|
36
|
+
try {
|
|
37
|
+
await continuations.dispose();
|
|
38
|
+
} finally {
|
|
39
|
+
await dispose?.call(driver);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
driver.listKeys = async ({ cursor, limit, prefix = "" }) => {
|
|
43
|
+
const keys = [];
|
|
44
|
+
async function* walk() {
|
|
45
|
+
const pendingDirectories = [root];
|
|
46
|
+
while (pendingDirectories.length > 0) {
|
|
47
|
+
const directory = pendingDirectories.pop();
|
|
48
|
+
let entries;
|
|
49
|
+
try {
|
|
50
|
+
entries = await opendir(directory);
|
|
51
|
+
} catch (error) {
|
|
52
|
+
if (directory === root && error instanceof Error && "code" in error && error.code === "ENOENT") return;
|
|
53
|
+
if (directory !== root && error instanceof Error && "code" in error && (error.code === "ENOENT" || error.code === "ENOTDIR")) {
|
|
54
|
+
yield void 0;
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
throw error;
|
|
58
|
+
}
|
|
59
|
+
for await (const entry of entries) {
|
|
60
|
+
const path = join(directory, entry.name);
|
|
61
|
+
if (entry.isDirectory()) {
|
|
62
|
+
pendingDirectories.push(path);
|
|
63
|
+
yield void 0;
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
yield entry.isFile() ? relative(root, path) : void 0;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
const iterator = cursor ? continuations.take(cursor) : walk();
|
|
71
|
+
if (!iterator) throw expired();
|
|
72
|
+
let scanned = 0;
|
|
73
|
+
while (scanned < limit) {
|
|
74
|
+
const entry = await iterator.next();
|
|
75
|
+
if (entry.done) return { keys };
|
|
76
|
+
scanned++;
|
|
77
|
+
const path = entry.value;
|
|
78
|
+
if (path === void 0) continue;
|
|
79
|
+
const key = path.split(sep).join(":");
|
|
80
|
+
if (key.startsWith(prefix)) keys.push(key);
|
|
81
|
+
}
|
|
82
|
+
return {
|
|
83
|
+
keys,
|
|
84
|
+
cursor: await continuations.retain(iterator)
|
|
85
|
+
};
|
|
86
|
+
};
|
|
87
|
+
return driver;
|
|
88
|
+
}
|
|
89
|
+
//#endregion
|
|
90
|
+
export { createFsLiteKVDriver as default };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { _ as
|
|
2
|
-
|
|
1
|
+
import { _ as ResolvedUpstashKVStoreConfig, a as KVErrorCode, c as KVModuleOptions, d as KVStoreConfig, f as ResolvedCloudflareKVStoreConfig, g as ResolvedKVStoreConfig, h as ResolvedKVModuleOptions, i as KVDriver, l as KVOperation, m as ResolvedFsLiteKVStoreConfig, n as DenoKVStoreConfig, o as KVListOptions, p as ResolvedDenoKVStoreConfig, r as FsLiteKVStoreConfig, s as KVListPage, t as CloudflareKVStoreConfig, u as KVStorage, v as UpstashKVStoreConfig } from "./types-CMDvN0Wr.js";
|
|
3
2
|
//#region src/runtime/storage.d.ts
|
|
4
|
-
|
|
3
|
+
/** Releases every cached store. Call from the application's shutdown hook after stopping new work. */
|
|
4
|
+
export declare function disposeKVStores(): Promise<void>;
|
|
5
|
+
export declare const kv: KVStorage;
|
|
5
6
|
//#endregion
|
|
6
|
-
export {
|
|
7
|
+
export type { CloudflareKVStoreConfig, DenoKVStoreConfig, FsLiteKVStoreConfig, KVDriver, KVErrorCode, KVListOptions, KVListPage, KVModuleOptions, KVOperation, KVStorage, KVStoreConfig, ResolvedCloudflareKVStoreConfig, ResolvedDenoKVStoreConfig, ResolvedFsLiteKVStoreConfig, ResolvedKVModuleOptions, ResolvedKVStoreConfig, ResolvedUpstashKVStoreConfig, UpstashKVStoreConfig };
|