@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 248 lines 8.99 KB
Raw
1 import type { MemoOptions } from "alchemy/Command/Memo";
2 import type { InputProps } from "alchemy/Input";
3 import { effectClass } from "alchemy/Util/effect";
4 import * as Effect from "effect/Effect";
5 import { Namespace } from "../KV/Namespace.ts";
6 import type { Providers } from "../Providers.ts";
7 import type { AssetsConfig } from "../Workers/Assets.ts";
8 import { VersionMetadata } from "../Workers/VersionMetadata.ts";
9 import {
10 Worker,
11 type NormalizedBindings,
12 type WorkerAssetsConfig,
13 type WorkerBindingProps,
14 type WorkerProps,
15 } from "../Workers/Worker.ts";
16
17 /**
18 * The default compatibility date when none is provided. Matches other
19 * Cloudflare Website resources so deploy and local dev agree.
20 */
21 const DEFAULT_COMPATIBILITY_DATE = "2026-05-12";
22
23 /**
24 * Default Worker entry for a vinext Cloudflare app. Official vinext
25 * examples wrap `vinext/server/fetch-handler` at this path.
26 */
27 const DEFAULT_WORKER_ENTRY = "worker/index.ts";
28
29 export interface VinextProps<Bindings extends WorkerBindingProps = {}> extends Omit<
30 WorkerProps<Bindings>,
31 "vite" | "main" | "assets" | "script" | "bundle" | "source" | "rules"
32 > {
33 /**
34 * Worker entry that delegates to vinext. Defaults to
35 * `worker/index.ts` — the official vinext Cloudflare layout:
36 *
37 * ```typescript
38 * import handler from "vinext/server/fetch-handler";
39 * export default {
40 * fetch(request: Request, env: Env, ctx: ExecutionContext) {
41 * return handler.fetch(request, env, ctx);
42 * },
43 * };
44 * ```
45 */
46 main?: string;
47 /**
48 * The vinext project root (the directory containing `app/` or `pages/`
49 * and `vite.config.ts`). Defaults to the process working directory.
50 */
51 rootDir?: string;
52 /**
53 * Controls which files are content-hashed to decide whether the Vite
54 * build needs to re-run. By default every project file outside build
55 * outputs and `node_modules` is hashed, plus the nearest lockfile.
56 */
57 memo?: MemoOptions;
58 /**
59 * Which Vite environments make up the deployed Worker.
60 *
61 * vinext's App Router build emits `rsc` + `ssr` + `client`. The
62 * default points the Worker entry at `rsc` and bundles `ssr`
63 * alongside it — the same shape vinext's official
64 * `@cloudflare/vite-plugin` config uses (`viteEnvironment: { name:
65 * "rsc", childEnvironments: ["ssr"] }`).
66 *
67 * @default { entry: "rsc", children: ["ssr"] }
68 */
69 viteEnvironments?: { entry?: string; children?: string[] };
70 /**
71 * Optional configuration for static asset routing behavior.
72 * Defaults to assets-first (`runWorkerFirst` unset) with
73 * `htmlHandling` / `notFoundHandling` set to `"none"` — the same
74 * shape official vinext writes into `wrangler.jsonc`. Hashed
75 * `/_next/static/*` chunks are served by the asset layer; the
76 * Worker only `ASSETS.fetch`es files the RSC handler marks with
77 * its static-file signal (e.g. `public/`). `runWorkerFirst: true`
78 * would 404 those chunks: vinext does not fetch them itself.
79 */
80 assets?: AssetsConfig;
81 }
82
83 // These options are inspected while constructing the Worker. Resolve them in
84 // the outer props Effect; pass-through properties can remain deferred Inputs.
85 type VinextInput<Bindings extends WorkerBindingProps> = InputProps<
86 VinextProps<Bindings>,
87 "env" | "cache" | "compatibility" | "assets"
88 >;
89
90 /**
91 * A Cloudflare Worker deployed from a [vinext](https://vinext.dev) app.
92 *
93 * vinext reimplements the Next.js API surface on Vite. `Vinext` is the
94 * Alchemy-shaped deploy path: it runs the project's own `vite.config.ts`
95 * (which must register `vinext()`) through Alchemy's wrangler-free
96 * Cloudflare Vite plugin, then deploys the RSC Worker plus client
97 * assets. There is no `wrangler.jsonc` and no
98 * `@vinext/cloudflare deploy`.
99 *
100 * This is **not** `Cloudflare.Website.Nextjs`. That resource runs
101 * `next build` through OpenNext. vinext never consumes `next build`
102 * output — mixing the two stacks will fail.
103 *
104 * Install `vinext`, `@vitejs/plugin-rsc`,
105 * `react-server-dom-webpack`, and `@alchemy.run/frontend-frameworks`
106 * in the deploying project. Do **not** also register
107 * `@cloudflare/vite-plugin` in `vite.config.ts` — Alchemy injects its
108 * own Cloudflare plugin (`vite-plugin-cloudflare:alchemy`; vinext
109 * matches the `vite-plugin-cloudflare:` prefix) and no-ops an official
110 * plugin if one is still present. The Worker source is
111 * `@alchemy.run/frontend-frameworks/vinext/source` (loaded with a
112 * dynamic `import()`, like the other Website framework resources).
113 *
114 * Bindings are declared on this resource (`env`) and read from
115 * `import { env } from "cloudflare:workers"` in server components,
116 * route handlers, and server actions.
117 *
118 * The resource injects Alchemy's KV data-cache adapter into the Vite build
119 * and local server. No Alchemy plugin or cache configuration is needed in
120 * `vite.config.ts` (the same ISR codec is used by Redis / S3).
121 * `Website.Vinext` provisions `VINEXT_KV_CACHE` (do not bind it in
122 * `env`) and `alchemy deploy` seeds prerender pairs into it. Workers
123 * Cache is enabled (`cache.enabled`) and `CF_VERSION_METADATA` is
124 * bound. There is no `@vinext/cloudflare` data-cache adapter.
125 *
126 * ### Deploying a vinext App
127 * **Example:** Basic vinext site
128 * ```typescript
129 * const site = yield* Cloudflare.Website.Vinext("Site");
130 * ```
131 *
132 * **Example:** Bindings on the Worker
133 * ```typescript
134 * const site = yield* Cloudflare.Website.Vinext("Site", {
135 * env: {
136 * GREETING: "Hello from vinext on Cloudflare!",
137 * },
138 * });
139 * ```
140 *
141 * In a server component:
142 * ```typescript
143 * import { env } from "cloudflare:workers";
144 *
145 * export default function Page() {
146 * return <h1>{env.GREETING}</h1>;
147 * }
148 * ```
149 *
150 * **Example:** Custom Worker entry
151 * ```typescript
152 * const site = yield* Cloudflare.Website.Vinext("Site", {
153 * main: "worker/index.ts",
154 * });
155 * ```
156 *
157 * ### Class Form
158 * **Example:** Declaring a Worker class
159 * ```typescript
160 * class Site extends Cloudflare.Website.Vinext<Site>()("Site") {}
161 *
162 * const site = yield* Site;
163 * ```
164 *
165 * @resource
166 * @product Website
167 * @category Workers & Compute
168 */
169 export const Vinext: {
170 <Self>(): {
171 <const Bindings extends WorkerBindingProps = {}, Req = never>(
172 id: string,
173 propsEff?: VinextInput<Bindings> | Effect.Effect<VinextInput<Bindings>, never, Req>,
174 ): Effect.Effect<Self, never, Req | Providers> & {
175 new (): Worker<{
176 [binding in keyof NormalizedBindings<Bindings, WorkerAssetsConfig>]: NormalizedBindings<
177 Bindings,
178 WorkerAssetsConfig
179 >[binding];
180 }>;
181 };
182 };
183 <const Bindings extends WorkerBindingProps = {}, Req = never>(
184 id: string,
185 propsEff?: VinextInput<Bindings> | Effect.Effect<VinextInput<Bindings>, never, Req>,
186 ): Effect.Effect<
187 Worker<{
188 [binding in keyof NormalizedBindings<Bindings, WorkerAssetsConfig>]: NormalizedBindings<
189 Bindings,
190 WorkerAssetsConfig
191 >[binding];
192 }>,
193 never,
194 Req | Providers
195 >;
196 } = (<const Bindings extends WorkerBindingProps = {}, Req = never>(
197 id?: string,
198 propsEff?: VinextInput<Bindings> | Effect.Effect<VinextInput<Bindings>, never, Req>,
199 ) =>
200 id === undefined
201 ? <const Bindings extends WorkerBindingProps = {}, Req = never>(
202 id: string,
203 propsEff?: VinextInput<Bindings> | Effect.Effect<VinextInput<Bindings>, never, Req>,
204 ) => effectClass(Vinext(id, propsEff))
205 : Worker(
206 id,
207 Effect.gen(function* () {
208 const props = (Effect.isEffect(propsEff) ? yield* propsEff : propsEff) ?? {};
209 // `main` is the source provider's entry; the Worker rejects its own
210 // `main` beside `source`.
211 const { main, ...workerProps } = props;
212 // Auto-provision the ISR/TPR data-cache KV. Official vinext
213 // leaves a wrangler placeholder; Alchemy owns the namespace.
214 // Do not bind VINEXT_KV_CACHE in user `env`.
215 const cache = yield* Namespace(`${id}IsrCache`);
216 const env = {
217 ...props.env,
218 VINEXT_KV_CACHE: cache,
219 CF_VERSION_METADATA: props.env?.CF_VERSION_METADATA ?? VersionMetadata(),
220 };
221 return {
222 ...workerProps,
223 env,
224 cache: { enabled: true, ...props.cache },
225 compatibility: {
226 date: props?.compatibility?.date ?? DEFAULT_COMPATIBILITY_DATE,
227 flags: Array.from(new Set(["nodejs_compat", ...(props?.compatibility?.flags ?? [])])),
228 },
229 assets: {
230 htmlHandling: "none" as const,
231 notFoundHandling: "none" as const,
232 ...props?.assets,
233 },
234 source: {
235 provider: "@alchemy.run/frontend-frameworks/vinext/source",
236 devMode: "server",
237 runtime: "node",
238 rootDir: props.rootDir,
239 options: {
240 main: main ?? DEFAULT_WORKER_ENTRY,
241 rootDir: props.rootDir,
242 memo: props.memo,
243 viteEnvironments: props.viteEnvironments ?? { entry: "rsc", children: ["ssr"] },
244 },
245 },
246 };
247 }),
248 )) as any;