@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, 04:45 UTC

main
ts 138 lines 5.51 KB
Raw
1 /**
2 * `@alchemy.run/frontend-frameworks/solidstart/aws` — the AWS Lambda deploy
3 * target for the SolidStart integration.
4 *
5 * SolidStart's server half is nitro (`@solidjs/vite-plugin-nitro-2`), so its
6 * AWS story is nitro's `aws-lambda` preset: `.output/server` is a
7 * self-contained Node ESM deployment unit whose `index.mjs` exports a Lambda
8 * `handler`, and `.output/public` holds the client assets (prerendered pages
9 * included) for the CDN. What this target owns:
10 *
11 * - **`nitroPreset`** — `"aws-lambda"`, enforced as the last word on the
12 * nitro config the integration hands to the appended nitro plugin.
13 * - **`configureNitro`** — enables response streaming by default
14 * (`awsLambda.streaming: true`): the emitted handler wraps
15 * `awslambda.streamifyResponse` and expects a Lambda Function URL with
16 * `invokeMode: RESPONSE_STREAM`. Set `streaming: false` on the target
17 * config for the buffered APIGW-style handler.
18 * - **`bundle`** — Node resolve conditions and the `@aws-sdk/` externals
19 * (the Lambda Node.js runtime ships SDK v3) for callers that post-process
20 * server code.
21 *
22 * No `finish`: nitro's aws-lambda output already ships its own
23 * `package.json` (`type: "module"`) and copied externals, so the server
24 * directory deploys as-is.
25 *
26 * No `devPlatform` seam: `dev` runs SolidStart's own Vite dev server, which
27 * is already the AWS Lambda programming model (plain Node).
28 */
29 import * as Effect from "effect/Effect";
30 import { runBuildChild } from "../core/BuildChild.ts";
31 import { DeployTargetError, makeDeployTarget } from "../core/index.ts";
32 import { make, type SolidStartTarget, type SolidStartTargetConfig } from "./SolidStart.ts";
33
34 /** The nitro deployment preset this target builds with. */
35 export const NITRO_PRESET = "aws-lambda";
36
37 /**
38 * The importable specifier of nitro's aws-lambda streaming runtime handler —
39 * the module a custom entry would re-export to wrap the framework's handler.
40 */
41 export const NITRO_HANDLER_SPECIFIER = "nitropack/presets/aws-lambda/runtime/aws-lambda-streaming";
42
43 /** AWS-specific knobs carried on the shared {@link SolidStartTargetConfig}. */
44 export interface SolidStartAwsTargetConfig extends SolidStartTargetConfig {
45 /**
46 * Whether the emitted Lambda handler streams its response
47 * (`awslambda.streamifyResponse`, requires a Function URL with
48 * `invokeMode: RESPONSE_STREAM`). The buffered handler answers
49 * APIGW/Function-URL events with a complete response object.
50 * @default true
51 */
52 readonly streaming?: boolean | undefined;
53 }
54
55 const fail = (message: string, cause?: unknown) =>
56 new DeployTargetError({ platform: "aws", message, cause });
57
58 /**
59 * The adapter-driven target — the shape the framework's regular vite/nitro
60 * build pipeline consumes. Used directly in the build child (where
61 * `cwd === root` holds); {@link makeAwsTarget} wraps it with the wholesale
62 * `build` hook that spawns the child.
63 */
64 const makeAwsAdapterTarget = (config: SolidStartAwsTargetConfig = {}): SolidStartTarget =>
65 makeDeployTarget({
66 platform: "aws",
67 config,
68 bundle: {
69 conditions: ["node", "import", "module"],
70 external: ["@aws-sdk/"],
71 },
72 nitroPreset: NITRO_PRESET,
73 configureNitro: (nitroConfig) => {
74 const awsLambda =
75 nitroConfig.awsLambda !== null && typeof nitroConfig.awsLambda === "object"
76 ? (nitroConfig.awsLambda as Record<string, unknown>)
77 : {};
78 nitroConfig.awsLambda = {
79 ...awsLambda,
80 streaming: config.streaming ?? true,
81 };
82 },
83 });
84
85 /**
86 * A SolidStart build loads and executes the project's `vite.config.*` (its
87 * `solidStart()` plugin scans the route directory from `process.cwd()`, and
88 * user plugins may mutate `process.env` or `process.chdir`) — none of which
89 * may touch the engine process, where many deploys share one event loop.
90 * This target's wholesale `build` therefore runs the framework in a
91 * disposable child process whose working directory IS the project root (see
92 * `core/BuildChild.ts`). The shared `core/BuildChildRunner` entry imports
93 * this module in the child and calls the exported {@link buildInChild}.
94 */
95 export interface SolidStartAwsBuildChildConfig {
96 readonly rootDir: string;
97 /** The (JSON-serializable) target config the parent was created with. */
98 readonly config: SolidStartAwsTargetConfig;
99 }
100
101 export const buildInChild = (config: SolidStartAwsBuildChildConfig) =>
102 Effect.gen(function* () {
103 const framework = yield* make({
104 root: config.rootDir,
105 // The adapter-only target: no wholesale `build` hook, so the child
106 // runs the regular vite/nitro build pipeline (no recursion).
107 target: makeAwsAdapterTarget(config.config),
108 nitro: config.config.nitro,
109 });
110 return yield* framework.build({ root: config.rootDir });
111 });
112
113 /**
114 * Create the AWS Lambda {@link SolidStartTarget}. See the module doc for the
115 * seams.
116 */
117 export const makeAwsTarget = (config: SolidStartAwsTargetConfig = {}): SolidStartTarget => ({
118 ...makeAwsAdapterTarget(config),
119 build: (context) =>
120 runBuildChild({
121 module: import.meta.url,
122 rootDir: context.root,
123 env: context.env,
124 framework: "solidstart",
125 config: {
126 rootDir: context.root,
127 config,
128 } satisfies SolidStartAwsBuildChildConfig,
129 }).pipe(Effect.mapError((error) => fail(error.message, error.cause))),
130 });
131
132 /**
133 * The deploy-target module contract (`resolveDeployTarget` accepts the
134 * default export — or the named `target` export — as a value or factory).
135 */
136 export const target = makeAwsTarget;
137
138 export default makeAwsTarget;