@vite-hub/kv 0.0.4 → 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/README.md +33 -8
- 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-aM9Ae7tH.js → config-DR6_5ZwI.js} +36 -22
- package/dist/continuations-DPyH_vr1.js +63 -0
- package/dist/{dist-DDW1iC5f.js → create-storage-29gMzf41.js} +70 -75
- package/dist/cursor-history-dqYDlsaD.js +148 -0
- package/dist/deno-kv-CSYGvRx4.js +120 -0
- package/dist/{driver-BT9WefKi.d.ts → driver-D9fI27jl.d.ts} +33 -3
- 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 +92 -64
- 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 +3 -4
- package/dist/runtime/cloudflare-kv.js +84 -8
- package/dist/runtime/upstash-driver.d.ts +4 -6
- package/dist/runtime/upstash-driver.js +34 -44
- package/dist/{types-CO0DRjJa.d.ts → types-CMDvN0Wr.d.ts} +20 -17
- package/dist/utils2-Dsb9P4_2.js +28 -0
- package/dist/virtual.d.ts +1 -1
- package/dist/vite.d.ts +14 -10
- package/dist/vite.js +603 -224
- package/package.json +56 -16
- package/dist/deno-kv-cJx3jU10.js +0 -69
- package/dist/fs-lite-BxzdiJSi.js +0 -79
|
@@ -1,6 +1,36 @@
|
|
|
1
|
-
import { o as KVListOptions, s as KVListPage } from "./types-
|
|
2
|
-
|
|
3
|
-
|
|
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
|
|
4
34
|
type StorageValue = null | string | number | boolean | object;
|
|
5
35
|
type WatchEvent = "update" | "remove";
|
|
6
36
|
type WatchCallback = (event: WatchEvent, key: string) => any;
|
|
@@ -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 };
|
package/dist/index.js
CHANGED
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { n as
|
|
4
|
-
import {
|
|
1
|
+
import { n as kvErrorDiagnostics } from "./error-diagnostics-D_cQeZTi.js";
|
|
2
|
+
import { n as kvResult } from "./errors-Dc2oiW-Y.js";
|
|
3
|
+
import { a as readEnv, n as upstashTokenEnvNames, r as upstashUrlEnvNames, t as normalizeKVOptions } from "./config-DR6_5ZwI.js";
|
|
4
|
+
import { n as getActiveCloudflareEnv } from "./cloudflare-env-C5rM7kUY.js";
|
|
5
|
+
import { n as normalizeKVListPrefix, t as createKVStorage$1 } from "./create-storage-29gMzf41.js";
|
|
6
|
+
import { a as normalizeKey } from "./utils2-Dsb9P4_2.js";
|
|
5
7
|
import { destr } from "destr";
|
|
6
|
-
const activeEnvStorageKey = Symbol.for("vitehub.cloudflare.env-storage");
|
|
7
|
-
const runtimeGlobal = globalThis;
|
|
8
|
-
const activeEnvStorage = runtimeGlobal[activeEnvStorageKey] ||= new AsyncLocalStorage();
|
|
9
|
-
function getActiveCloudflareEnv() {
|
|
10
|
-
const scoped = activeEnvStorage.getStore();
|
|
11
|
-
return scoped ? scoped.env : globalThis.__env__;
|
|
12
|
-
}
|
|
13
|
-
//#endregion
|
|
14
8
|
//#region src/runtime/upstash.ts
|
|
15
9
|
function isMaskedValue(value) {
|
|
16
10
|
return !value || /^\*+$/.test(value);
|
|
17
11
|
}
|
|
18
|
-
function assertRuntimeValue(value,
|
|
19
|
-
if (isMaskedValue(value))
|
|
12
|
+
function assertRuntimeValue(value, envNames) {
|
|
13
|
+
if (isMaskedValue(value)) {
|
|
14
|
+
const names = envNames.map((name) => `\`${name}\``).join(" or ");
|
|
15
|
+
throw kvErrorDiagnostics.KV_MISSING_ENV({ names });
|
|
16
|
+
}
|
|
20
17
|
}
|
|
21
18
|
function resolveRuntimeUpstashStore(config, env) {
|
|
22
|
-
const
|
|
23
|
-
const
|
|
19
|
+
const completeEnvPair = upstashUrlEnvNames.map((urlName, index) => [readEnv(env, urlName), readEnv(env, upstashTokenEnvNames[index])]).find(([url, token]) => url && token);
|
|
20
|
+
const envUrl = readEnv(env, ...upstashUrlEnvNames);
|
|
21
|
+
const envToken = readEnv(env, ...upstashTokenEnvNames);
|
|
22
|
+
const hasMaskedPair = isMaskedValue(config.url) && isMaskedValue(config.token);
|
|
23
|
+
const resolveMaskedValue = (value, pairValue, fallback) => isMaskedValue(value) ? pairValue || fallback || value || "********" : value ?? "********";
|
|
24
24
|
const resolved = {
|
|
25
25
|
...config,
|
|
26
|
-
token:
|
|
27
|
-
url:
|
|
26
|
+
token: resolveMaskedValue(config.token, completeEnvPair?.[1], hasMaskedPair ? void 0 : envToken),
|
|
27
|
+
url: resolveMaskedValue(config.url, completeEnvPair?.[0], hasMaskedPair ? void 0 : envUrl)
|
|
28
28
|
};
|
|
29
|
-
assertRuntimeValue(resolved.url,
|
|
30
|
-
assertRuntimeValue(resolved.token,
|
|
29
|
+
assertRuntimeValue(resolved.url, upstashUrlEnvNames);
|
|
30
|
+
assertRuntimeValue(resolved.token, upstashTokenEnvNames);
|
|
31
31
|
return resolved;
|
|
32
32
|
}
|
|
33
33
|
function resolveRuntimeKVOptions(config, env = process.env) {
|
|
@@ -39,7 +39,7 @@ function resolveRuntimeKVOptions(config, env = process.env) {
|
|
|
39
39
|
}
|
|
40
40
|
//#endregion
|
|
41
41
|
//#region src/runtime/driver.ts
|
|
42
|
-
const lazyDriverMethods = new Set([
|
|
42
|
+
const lazyDriverMethods = /* @__PURE__ */ new Set([
|
|
43
43
|
"clear",
|
|
44
44
|
"getItem",
|
|
45
45
|
"getKeys",
|
|
@@ -51,12 +51,12 @@ const lazyDriverMethods = new Set([
|
|
|
51
51
|
const lazyOptionalDriverMethods = {
|
|
52
52
|
"cloudflare-kv-binding": /* @__PURE__ */ new Set(),
|
|
53
53
|
"deno-kv": /* @__PURE__ */ new Set(),
|
|
54
|
-
"fs-lite": new Set([
|
|
54
|
+
"fs-lite": /* @__PURE__ */ new Set([
|
|
55
55
|
"getItemRaw",
|
|
56
56
|
"getMeta",
|
|
57
57
|
"setItemRaw"
|
|
58
58
|
]),
|
|
59
|
-
"upstash": new Set([
|
|
59
|
+
"upstash": /* @__PURE__ */ new Set([
|
|
60
60
|
"getAndDeleteItem",
|
|
61
61
|
"getItems",
|
|
62
62
|
"incrementItem"
|
|
@@ -69,11 +69,11 @@ async function createRuntimeDriver(store) {
|
|
|
69
69
|
return factory(store);
|
|
70
70
|
}
|
|
71
71
|
case "deno-kv": {
|
|
72
|
-
const { default: factory } = await import("./deno-kv-
|
|
72
|
+
const { default: factory } = await import("./deno-kv-CSYGvRx4.js");
|
|
73
73
|
return factory(store);
|
|
74
74
|
}
|
|
75
75
|
case "fs-lite": {
|
|
76
|
-
const { default: factory } = await import("./fs-lite-
|
|
76
|
+
const { default: factory } = await import("./fs-lite-CbLKxLut.js");
|
|
77
77
|
return factory(store);
|
|
78
78
|
}
|
|
79
79
|
case "upstash": {
|
|
@@ -84,11 +84,21 @@ async function createRuntimeDriver(store) {
|
|
|
84
84
|
}
|
|
85
85
|
function createLazyKVRuntimeDriver(config) {
|
|
86
86
|
let driverPromise;
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
if (
|
|
90
|
-
|
|
91
|
-
|
|
87
|
+
let disposed = false;
|
|
88
|
+
function resolve() {
|
|
89
|
+
if (disposed) return Promise.reject(/* @__PURE__ */ new Error("KV storage has been disposed."));
|
|
90
|
+
if (driverPromise) return driverPromise;
|
|
91
|
+
const pending = (async () => {
|
|
92
|
+
const runtime = resolveRuntimeKVOptions(config);
|
|
93
|
+
if (!runtime) throw kvErrorDiagnostics.KV_DISABLED();
|
|
94
|
+
return createRuntimeDriver(runtime.store);
|
|
95
|
+
})();
|
|
96
|
+
driverPromise = pending;
|
|
97
|
+
pending.catch(() => {
|
|
98
|
+
if (driverPromise === pending) driverPromise = void 0;
|
|
99
|
+
});
|
|
100
|
+
return pending;
|
|
101
|
+
}
|
|
92
102
|
const target = {
|
|
93
103
|
name: `lazy:${config.store.driver}`,
|
|
94
104
|
options: config.store
|
|
@@ -97,6 +107,7 @@ function createLazyKVRuntimeDriver(config) {
|
|
|
97
107
|
const own = t[prop];
|
|
98
108
|
if (own !== void 0) return own;
|
|
99
109
|
if (prop === "dispose") return async () => {
|
|
110
|
+
disposed = true;
|
|
100
111
|
if (!driverPromise) return;
|
|
101
112
|
const driver = await driverPromise;
|
|
102
113
|
return driver.dispose?.call(driver);
|
|
@@ -111,27 +122,19 @@ function createLazyKVRuntimeDriver(config) {
|
|
|
111
122
|
}
|
|
112
123
|
//#endregion
|
|
113
124
|
//#region src/runtime/hosted-storage.ts
|
|
114
|
-
var KVStoreConfigurationError = class extends Error {};
|
|
115
|
-
var KVAtomicOperationUnsupportedError = class extends KVStoreConfigurationError {
|
|
116
|
-
constructor(store) {
|
|
117
|
-
super(`[vitehub] KV store "${store}" does not support atomic operations. Use Upstash.`);
|
|
118
|
-
}
|
|
119
|
-
};
|
|
120
125
|
function assertHostedConfig(config) {
|
|
121
|
-
if (!config) throw
|
|
126
|
+
if (!config) throw kvErrorDiagnostics.KV_DISABLED();
|
|
122
127
|
return config;
|
|
123
128
|
}
|
|
124
129
|
function deserializeValue(value) {
|
|
125
130
|
if (typeof value !== "string") return value;
|
|
126
131
|
return destr(value);
|
|
127
132
|
}
|
|
128
|
-
function createHostedKVStorage(config) {
|
|
129
|
-
return createNamedHostedKVStorage(config, "default");
|
|
130
|
-
}
|
|
131
|
-
function createNamedHostedKVStorage(config, name) {
|
|
133
|
+
function createHostedKVStorage(config, name = "default") {
|
|
132
134
|
const resolved = assertHostedConfig(config);
|
|
133
|
-
const
|
|
134
|
-
|
|
135
|
+
const stores = resolved.stores || { default: resolved.store };
|
|
136
|
+
const store = Object.hasOwn(stores, name) ? stores[name] : void 0;
|
|
137
|
+
if (!store) throw kvErrorDiagnostics.KV_STORE_NOT_FOUND({ store: name });
|
|
135
138
|
const driver = createLazyKVRuntimeDriver({
|
|
136
139
|
store,
|
|
137
140
|
stores: {
|
|
@@ -139,8 +142,11 @@ function createNamedHostedKVStorage(config, name) {
|
|
|
139
142
|
[name]: store
|
|
140
143
|
}
|
|
141
144
|
});
|
|
142
|
-
const storage =
|
|
143
|
-
storage.listKeys = (options) => driver.listKeys(
|
|
145
|
+
const storage = createKVStorage$1(driver);
|
|
146
|
+
storage.listKeys = (options) => driver.listKeys({
|
|
147
|
+
...options,
|
|
148
|
+
prefix: normalizeKVListPrefix(options.prefix)
|
|
149
|
+
});
|
|
144
150
|
const getAndDeleteItem = driver.getAndDeleteItem;
|
|
145
151
|
const incrementItem = driver.incrementItem;
|
|
146
152
|
if (getAndDeleteItem) storage.getAndDeleteItem = async (key) => {
|
|
@@ -152,6 +158,23 @@ function createNamedHostedKVStorage(config, name) {
|
|
|
152
158
|
//#endregion
|
|
153
159
|
//#region src/runtime/storage.ts
|
|
154
160
|
const storagePromises = /* @__PURE__ */ new Map();
|
|
161
|
+
let disposal;
|
|
162
|
+
/** Releases every cached store. Call from the application's shutdown hook after stopping new work. */
|
|
163
|
+
function disposeKVStores() {
|
|
164
|
+
if (disposal) return disposal;
|
|
165
|
+
const stores = [...storagePromises.values()];
|
|
166
|
+
storagePromises.clear();
|
|
167
|
+
const pending = Promise.allSettled(stores.map(async (promise) => {
|
|
168
|
+
await (await promise.catch(() => void 0))?.dispose();
|
|
169
|
+
})).then((results) => {
|
|
170
|
+
const failures = results.flatMap((result) => result.status === "rejected" ? [result.reason] : []);
|
|
171
|
+
if (failures.length) throw new AggregateError(failures, "Failed to dispose KV stores.");
|
|
172
|
+
}).finally(() => {
|
|
173
|
+
disposal = void 0;
|
|
174
|
+
});
|
|
175
|
+
disposal = pending;
|
|
176
|
+
return pending;
|
|
177
|
+
}
|
|
155
178
|
function inferHosting(env) {
|
|
156
179
|
if (getActiveCloudflareEnv()) return "cloudflare";
|
|
157
180
|
const explicit = readEnv(env, "VITEHUB_HOSTING");
|
|
@@ -176,21 +199,19 @@ async function resolveHostedConfig() {
|
|
|
176
199
|
}
|
|
177
200
|
}
|
|
178
201
|
async function resolveStorage(name = "default") {
|
|
202
|
+
if (disposal) await disposal;
|
|
179
203
|
const existing = storagePromises.get(name);
|
|
180
204
|
if (existing) return existing;
|
|
181
|
-
|
|
182
|
-
const promise = resolveHostedConfig().then((config) => name === "default" ? createHostedKVStorage(config) : createNamedHostedKVStorage(config, name));
|
|
183
|
-
storagePromises.set(name, promise);
|
|
184
|
-
return promise;
|
|
185
|
-
}
|
|
186
|
-
const promise = resolveHostedConfig().then((config) => name === "default" ? createHostedKVStorage(config) : createNamedHostedKVStorage(config, name));
|
|
205
|
+
const promise = resolveHostedConfig().then((config) => createHostedKVStorage(config, name));
|
|
187
206
|
storagePromises.set(name, promise);
|
|
207
|
+
promise.catch(() => {
|
|
208
|
+
if (storagePromises.get(name) === promise) storagePromises.delete(name);
|
|
209
|
+
});
|
|
188
210
|
return promise;
|
|
189
211
|
}
|
|
190
|
-
async function runKVOperation(operation, name, run) {
|
|
191
|
-
const
|
|
192
|
-
|
|
193
|
-
return result;
|
|
212
|
+
async function runKVOperation(operation, name, run, codes = []) {
|
|
213
|
+
const storage = await resolveStorage(name);
|
|
214
|
+
return kvResult(operation, name, () => run(storage), codes);
|
|
194
215
|
}
|
|
195
216
|
function createKVStorage(name = "default") {
|
|
196
217
|
return {
|
|
@@ -204,26 +225,33 @@ function createKVStorage(name = "default") {
|
|
|
204
225
|
return runKVOperation("get", name, (storage) => storage.getItem(key, options));
|
|
205
226
|
},
|
|
206
227
|
async getAndDelete(key) {
|
|
207
|
-
return runKVOperation("getAndDelete", name, (storage) => {
|
|
208
|
-
if (!storage.getAndDeleteItem) throw
|
|
228
|
+
return runKVOperation("getAndDelete", name, async (storage) => {
|
|
229
|
+
if (!storage.getAndDeleteItem) throw kvErrorDiagnostics.KV_UNSUPPORTED_OPERATION({
|
|
230
|
+
operation: "getAndDelete",
|
|
231
|
+
store: name
|
|
232
|
+
});
|
|
209
233
|
return storage.getAndDeleteItem(key);
|
|
210
|
-
});
|
|
234
|
+
}, ["KV_UNSUPPORTED_OPERATION"]);
|
|
211
235
|
},
|
|
212
236
|
async has(key, options) {
|
|
213
237
|
return runKVOperation("has", name, (storage) => storage.hasItem(key, options));
|
|
214
238
|
},
|
|
215
239
|
async increment(key, ttl) {
|
|
216
|
-
return runKVOperation("increment", name, (storage) => {
|
|
217
|
-
if (!storage.incrementItem) throw
|
|
240
|
+
return runKVOperation("increment", name, async (storage) => {
|
|
241
|
+
if (!storage.incrementItem) throw kvErrorDiagnostics.KV_UNSUPPORTED_OPERATION({
|
|
242
|
+
operation: "increment",
|
|
243
|
+
store: name
|
|
244
|
+
});
|
|
245
|
+
if (!Number.isFinite(ttl) || ttl <= 0 || !Number.isSafeInteger(Math.ceil(ttl))) throw kvErrorDiagnostics.KV_INVALID_TTL();
|
|
218
246
|
return storage.incrementItem(key, ttl);
|
|
219
|
-
});
|
|
247
|
+
}, ["KV_INVALID_TTL", "KV_UNSUPPORTED_OPERATION"]);
|
|
220
248
|
},
|
|
221
249
|
async keys(base, options) {
|
|
222
250
|
return runKVOperation("keys", name, (storage) => storage.getKeys(base, options));
|
|
223
251
|
},
|
|
224
252
|
async list(options) {
|
|
225
|
-
if (!Number.isInteger(options.limit) || options.limit <= 0)
|
|
226
|
-
return runKVOperation("list", name, (storage) => storage.listKeys(options));
|
|
253
|
+
if (!Number.isInteger(options.limit) || options.limit <= 0) return [kvErrorDiagnostics.KV_INVALID_LIMIT(), void 0];
|
|
254
|
+
return runKVOperation("list", name, (storage) => storage.listKeys(options), ["KV_INVALID_CURSOR"]);
|
|
227
255
|
},
|
|
228
256
|
async set(key, value, options) {
|
|
229
257
|
return runKVOperation("set", name, async (storage) => storage.setItem(key, value, options));
|
|
@@ -235,4 +263,4 @@ function createKVStorage(name = "default") {
|
|
|
235
263
|
}
|
|
236
264
|
const kv = createKVStorage();
|
|
237
265
|
//#endregion
|
|
238
|
-
export { kv };
|
|
266
|
+
export { disposeKVStores, kv };
|