@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 129 lines 4.54 KB
Raw
1 import { AlchemyContext } from "alchemy/AlchemyContext";
2 import type { InputProps } from "alchemy/Input";
3 import * as Namespace from "alchemy/Namespace";
4 import * as Output from "alchemy/Output";
5 import { ProviderModePolicy } from "alchemy/ProviderMode";
6 import * as Effect from "effect/Effect";
7 import type { PolicyStatement } from "../IAM/Policy.ts";
8 import { Bucket } from "../S3/Bucket.ts";
9 import { makeFrameworkSite, type FrameworkSiteProps } from "./FrameworkSite.ts";
10
11 /**
12 * The vinext-on-AWS framework module (it is its own deploy target — not
13 * OpenNext, not the Cloudflare Worker source).
14 */
15 export const VINEXT_AWS_FRAMEWORK_SPECIFIER = "@alchemy.run/frontend-frameworks/vinext/aws";
16
17 /** The AWS Lambda deploy target for the vinext build. */
18 export const VINEXT_AWS_TARGET_SPECIFIER = "@alchemy.run/frontend-frameworks/vinext/aws";
19
20 /** Env var the S3 data-cache adapter reads for the ISR/fetch cache bucket. */
21 export const VINEXT_CACHE_BUCKET_ENV = "CACHE_BUCKET_NAME";
22
23 export type VinextProps = FrameworkSiteProps;
24
25 /**
26 * Deploy a [vinext](https://vinext.dev) App Router application to AWS: the RSC
27 * server on a Lambda Web Function, static assets in S3, and a CloudFront
28 * distribution whose edge router serves uploaded files from S3 and
29 * forwards everything else to the server. Set `server: "function"` to run
30 * it on a streaming Lambda Function URL instead, for Regions without Web
31 * Functions.
32 *
33 * ISR / `"use cache"` persistence is an S3 bucket (not Cloudflare KV).
34 * This resource provisions the bucket, sets `CACHE_BUCKET_NAME`, and
35 * grants the server Get/Put/Delete/List. Alchemy injects the S3 data-cache
36 * adapter during the build; no Alchemy plugin is needed in `vite.config.ts`.
37 * A dedicated cache bucket keeps the graph acyclic (the asset bucket
38 * is bound to CloudFront).
39 *
40 * Requires vinext 1.0.0-beta.10 or newer. The Lambda build uses vinext's
41 * standalone packaging to include external runtime dependencies. Pages Router
42 * applications require a Node deployment target instead of Lambda.
43 *
44 * This is **not** `AWS.Website.Nextjs` (OpenNext) and **not**
45 * `Cloudflare.Website.Vinext`. During `alchemy dev` the site is
46 * `vinext dev` and no cloud resources are declared.
47 *
48 * ### Creating vinext Sites
49 * **Example:** Basic vinext App
50 * ```typescript
51 * const site = yield* AWS.Website.Vinext("Web", {
52 * rootDir: "./app",
53 * });
54 * ```
55 *
56 * **Example:** vite.config.ts (platform-agnostic)
57 * ```typescript
58 * import { defineConfig } from "vite";
59 * import vinext from "vinext";
60 *
61 * export default defineConfig({
62 * plugins: [vinext({ prerender: true })],
63 * });
64 * ```
65 *
66 * @resource
67 * @product Website
68 * @category Frontend
69 */
70 export const Vinext = (id: string, propsIn: InputProps<VinextProps, "server"> = {}) =>
71 Effect.gen(function* () {
72 const props = propsIn as VinextProps;
73 const ctx = yield* AlchemyContext;
74 const remoted = yield* ProviderModePolicy;
75 const isLocal = ctx.dev && remoted !== true;
76
77 if (isLocal) {
78 return {
79 ...(yield* makeFrameworkSite(id, props, {
80 name: "Vinext",
81 framework: VINEXT_AWS_FRAMEWORK_SPECIFIER,
82 target: VINEXT_AWS_TARGET_SPECIFIER,
83 })),
84 cacheBucket: undefined,
85 };
86 }
87
88 const cacheBucket = yield* Bucket("CacheBucket", {
89 forceDestroy: props.forceDestroy,
90 tags: props.tags,
91 });
92 const site = yield* makeFrameworkSite(
93 id,
94 {
95 ...props,
96 env: {
97 [VINEXT_CACHE_BUCKET_ENV]: cacheBucket.bucketName,
98 ...props.env,
99 },
100 },
101 {
102 name: "Vinext",
103 framework: VINEXT_AWS_FRAMEWORK_SPECIFIER,
104 target: VINEXT_AWS_TARGET_SPECIFIER,
105 // The same self-contained `dist/server` (S3 data cache included),
106 // served by a Node HTTP server instead of a Lambda handler.
107 webTarget: VINEXT_AWS_TARGET_SPECIFIER,
108 webOptions: { server: "web-function" },
109 webPrebuilt: true,
110 },
111 );
112 if (site.server !== undefined) {
113 yield* site.server.bind`Allow(${site.server}, AWS.Website.Vinext.Cache(${cacheBucket}))`({
114 policyStatements: [
115 {
116 Effect: "Allow",
117 Action: ["s3:GetObject", "s3:PutObject", "s3:DeleteObject"],
118 Resource: [Output.interpolate`${cacheBucket.bucketArn}/*` as never],
119 },
120 {
121 Effect: "Allow",
122 Action: ["s3:ListBucket"],
123 Resource: [cacheBucket.bucketArn as never],
124 },
125 ] satisfies PolicyStatement[],
126 });
127 }
128 return { ...site, cacheBucket };
129 }).pipe(Namespace.push(id));