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

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