| 1 | /** |
| 2 | * Cloudflare KV access via the OpenNext binding helper. |
| 3 | * Reads bindings from the process environment for `next dev` outside of `wrangler dev`. |
| 4 | */ |
| 5 | import { readBoundedBody } from "./bounded-body"; |
| 6 | import type { DraftClaimLockNamespace } from "./draft-claim-lock"; |
| 7 | import type { MerchEnv } from "./merch/types"; |
| 8 | |
| 9 | export interface KVNamespace { |
| 10 | get(key: string): Promise<string | null>; |
| 11 | put(key: string, value: string, opts?: { expirationTtl?: number }): Promise<void>; |
| 12 | list(opts?: { prefix?: string; limit?: number; cursor?: string }): Promise<{ keys: { name: string }[]; cursor?: string; list_complete?: boolean }>; |
| 13 | delete(key: string): Promise<void>; |
| 14 | } |
| 15 | |
| 16 | /** Native KV streaming reads keep signed facts bounded before allocation. */ |
| 17 | export interface KVStreamNamespace { |
| 18 | get(key: string, type: "stream"): Promise<ReadableStream<Uint8Array> | null>; |
| 19 | put: KVNamespace["put"]; |
| 20 | } |
| 21 | |
| 22 | export async function readBoundedKvJson(kv: KVStreamNamespace | undefined, key: string, maxBytes: number): Promise<unknown> { |
| 23 | if (!kv) return null; |
| 24 | try { |
| 25 | const body = await kv.get(key, "stream"); |
| 26 | if (!body) return null; |
| 27 | const bytes = await readBoundedBody({ body, headers: new Headers() }, maxBytes); |
| 28 | return JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(bytes)); |
| 29 | } catch { return null; } |
| 30 | } |
| 31 | |
| 32 | interface CloudflareEnv extends MerchEnv { |
| 33 | DRAFT_CLAIM_LOCK?: DraftClaimLockNamespace; |
| 34 | CURATED_KV?: KVNamespace & KVStreamNamespace; |
| 35 | ADMIN_LOGIN_LIMITER?: { limit(options: { key: string }): Promise<{ success: boolean }> }; |
| 36 | MERCH_INTEREST_LIMITER?: { limit(options: { key: string }): Promise<{ success: boolean }> }; |
| 37 | MERCH_INTEREST_SITE_ORIGIN?: string; |
| 38 | DEEPSEEK_API_KEY?: string; |
| 39 | DEEPSEEK_BASE_URL?: string; |
| 40 | DEEPSEEK_MODEL?: string; |
| 41 | GITHUB_TOKEN?: string; |
| 42 | CRON_SECRET?: string; |
| 43 | GITHUB_REPO?: string; |
| 44 | /** Cloud facts (facts/v1): Supabase Data API URL + publishable (anon) key. Never a service key. */ |
| 45 | SUPABASE_URL?: string; |
| 46 | SUPABASE_PUBLISHABLE_KEY?: string; |
| 47 | } |
| 48 | |
| 49 | function envFromProcess(): CloudflareEnv { |
| 50 | return { |
| 51 | MERCH_CONFIG_JSON: process.env.MERCH_CONFIG_JSON, |
| 52 | MERCH_INTEREST_SITE_ORIGIN: process.env.MERCH_INTEREST_SITE_ORIGIN, |
| 53 | MERCH_STRIPE_KEY: process.env.MERCH_STRIPE_KEY, |
| 54 | MERCH_STRIPE_WEBHOOK_SECRET: process.env.MERCH_STRIPE_WEBHOOK_SECRET, |
| 55 | MERCH_PAYMENT_CONFIGURATION: process.env.MERCH_PAYMENT_CONFIGURATION, |
| 56 | YOYCOL_ACCESS_KEY: process.env.YOYCOL_ACCESS_KEY, |
| 57 | YOYCOL_SECRET_KEY: process.env.YOYCOL_SECRET_KEY, |
| 58 | SUPABASE_URL: process.env.SUPABASE_URL, |
| 59 | SUPABASE_PUBLISHABLE_KEY: process.env.SUPABASE_PUBLISHABLE_KEY, |
| 60 | DEEPSEEK_API_KEY: process.env.DEEPSEEK_API_KEY, |
| 61 | DEEPSEEK_BASE_URL: process.env.DEEPSEEK_BASE_URL, |
| 62 | DEEPSEEK_MODEL: process.env.DEEPSEEK_MODEL, |
| 63 | GITHUB_TOKEN: process.env.GITHUB_TOKEN, |
| 64 | CRON_SECRET: process.env.CRON_SECRET, |
| 65 | GITHUB_REPO: process.env.GITHUB_REPO, |
| 66 | }; |
| 67 | } |
| 68 | |
| 69 | export async function getEnv(): Promise<CloudflareEnv> { |
| 70 | if (process.env.NEXT_PHASE === "phase-production-build") { |
| 71 | return envFromProcess(); |
| 72 | } |
| 73 | |
| 74 | try { |
| 75 | const mod = await import("@opennextjs/cloudflare"); |
| 76 | const ctx = await mod.getCloudflareContext({ async: true }); |
| 77 | return ctx.env as CloudflareEnv; |
| 78 | } catch { |
| 79 | return envFromProcess(); |
| 80 | } |
| 81 | } |
| 82 |