1/**
2 * vinext data-cache adapter builder for Cloudflare KV.
3 *
4 * Cloudflare.Website.Vinext injects this adapter automatically. The runtime factory reads `env.VINEXT_KV_CACHE` (Worker
5 * binding) and uses the same ISR codec as Redis / S3.
6 *
7 * `Website.Vinext` provisions the namespace and seeds prerender pairs
8 * at deploy. Missing binding (local prerender) makes vinext log and
9 * fall back to the in-memory handler.
10 */
11import { fileURLToPath } from "node:url";
12import type { KvAdapterOptions } from "./kv-runtime.ts";
13
14export type { KvAdapterOptions } from "./kv-runtime.ts";
15
16export const DEFAULT_KV_BINDING = "VINEXT_KV_CACHE";
17
18export const kvAdapter = (options?: KvAdapterOptions) => {
19 if (options?.binding !== undefined && typeof options.binding !== "string") {
20 throw new TypeError(
21 "[vinext] kvAdapter({ binding }) must be a string Worker binding name.",
22 );
23 }
24 return {
25 adapter: fileURLToPath(new URL("./kv-runtime.js", import.meta.url)),
26 options,
27 };
28};