| 1 | /** |
| 2 | * The contract shared between the two halves of the Cloudflare dev |
| 3 | * transport: |
| 4 | * |
| 5 | * - the HOST half (`./host.ts`) runs in the alchemy/e2e process: it opens |
| 6 | * `cloudflare-runtime`'s platform proxy (a workerd instance hosting the |
| 7 | * configured bindings) and injects a {@link DevConnectInfo} into nitro's |
| 8 | * `runtimeConfig`; |
| 9 | * - the WORKER half (`./plugin.ts`) is a nitro plugin bundled into nitro's |
| 10 | * dev SSR worker THREAD: it reads the {@link DevConnectInfo} back out of |
| 11 | * `useRuntimeConfig()` and reconstructs the proxied platform over HTTP |
| 12 | * with `cloudflare-runtime`'s runtime-free client |
| 13 | * (`platform-proxy/connect`). |
| 14 | * |
| 15 | * The entire client state of the platform proxy is `{ url, token }` — two |
| 16 | * plain strings — which is what makes the worker-thread hop trivial: |
| 17 | * `runtimeConfig` strings survive nitro's bundling and worker replacement, |
| 18 | * so the plugin reconnects on its own after every dev reload while binding |
| 19 | * state (KV, DO storage, ...) stays live in the host's workerd instance. |
| 20 | */ |
| 21 | |
| 22 | /** |
| 23 | * The `runtimeConfig` key the host writes the {@link DevConnectInfo} under |
| 24 | * (private runtime config — server-side only, never exposed to the client). |
| 25 | */ |
| 26 | export const RUNTIME_CONFIG_KEY = "distilledCloudflareDev"; |
| 27 | |
| 28 | /** What the dev-only nitro plugin needs to reconstruct the platform env. */ |
| 29 | export interface DevConnectInfo { |
| 30 | /** Base URL of the platform-proxy workerd instance. */ |
| 31 | readonly url: string; |
| 32 | /** The proxy instance's auth token (all proxy endpoints 401 without it). */ |
| 33 | readonly token: string; |
| 34 | /** |
| 35 | * Absolute path of `cloudflare-runtime`'s runtime-free platform-proxy |
| 36 | * client module (`platform-proxy/connect`), resolved by the HOST from its |
| 37 | * own dependency tree. The plugin dynamically imports it by path — the |
| 38 | * dev worker cannot resolve the bare specifier itself when |
| 39 | * `cloudflare-runtime` is only a transitive dependency of the project |
| 40 | * (isolated installs expose direct dependencies only). |
| 41 | */ |
| 42 | readonly clientModule: string; |
| 43 | /** |
| 44 | * Literal env values laid over the proxied bindings — a same-named |
| 45 | * literal always wins (mirrors the SvelteKit dev platform's contract). |
| 46 | */ |
| 47 | readonly env?: Record<string, string> | undefined; |
| 48 | } |