The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { fileURLToPath } from "node:url"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as FileSystem from "effect/FileSystem"; |
| 4 | import * as Path from "effect/Path"; |
| 5 | import { runBuildChild } from "../core/BuildChild.ts"; |
| 6 | import { |
| 7 | DeployTargetError, |
| 8 | makeDeployTarget, |
| 9 | readServerModulesFromDisk, |
| 10 | sortServerModules, |
| 11 | } from "../core/index.ts"; |
| 12 | import { make, type FoldkitTarget, type FoldkitTargetConfig } from "./Foldkit.ts"; |
| 13 | |
| 14 | export interface FoldkitAwsTargetConfig extends FoldkitTargetConfig { |
| 15 | /** Stream Lambda responses. @default true */ |
| 16 | readonly streaming?: boolean | undefined; |
| 17 | } |
| 18 | const fail = (message: string, cause?: unknown) => |
| 19 | new DeployTargetError({ platform: "aws", message, cause }); |
| 20 | |
| 21 | /** Use the same web Request/Response adapter as the other Lambda framework targets. */ |
| 22 | const makeAwsFinishTarget = (config: FoldkitAwsTargetConfig = {}): FoldkitTarget => |
| 23 | makeDeployTarget({ |
| 24 | platform: "aws", |
| 25 | config, |
| 26 | bundle: { |
| 27 | conditions: ["node", "import", "module"], |
| 28 | external: ["@aws-sdk/"], |
| 29 | }, |
| 30 | finish: (output, context) => |
| 31 | Effect.gen(function* () { |
| 32 | if (!context.entry) return output; |
| 33 | const fs = yield* FileSystem.FileSystem; |
| 34 | const path = yield* Path.Path; |
| 35 | if (!output.distDirectory) return yield* fail("Foldkit produced no deployment directory."); |
| 36 | const server = path.join(output.distDirectory, "server"); |
| 37 | const adapter = yield* Effect.try({ |
| 38 | try: () => |
| 39 | fileURLToPath(import.meta.resolve("@alchemy.run/frontend-frameworks/aws-lambda")), |
| 40 | catch: (cause) => fail("Failed to resolve the Lambda adapter", cause), |
| 41 | }); |
| 42 | const source = yield* fs.readFileString(adapter); |
| 43 | yield* fs.writeFileString( |
| 44 | path.join(server, "foldkit-aws-lambda.mjs"), |
| 45 | source.replace(/^\/\/# sourceMappingURL=.*$/m, ""), |
| 46 | ); |
| 47 | yield* fs.writeFileString(path.join(server, "package.json"), '{"type":"module"}\n'); |
| 48 | const wrap = config.streaming === false ? "toBufferedLambdaHandler" : "toLambdaHandler"; |
| 49 | yield* fs.writeFileString( |
| 50 | path.join(server, "index.mjs"), |
| 51 | `import entry from ${JSON.stringify("./" + path.relative(server, context.entry).replaceAll("\\", "/"))}; |
| 52 | import { ${wrap} } from "./foldkit-aws-lambda.mjs"; |
| 53 | export const handler = ${wrap}((request) => entry.fetch(request)); |
| 54 | `, |
| 55 | ); |
| 56 | const modules = yield* readServerModulesFromDisk({ |
| 57 | directory: server, |
| 58 | prefix: "server", |
| 59 | }).pipe(Effect.mapError((error) => fail(error.message, error.cause))); |
| 60 | return { |
| 61 | ...output, |
| 62 | serverModules: sortServerModules(modules, "server/index.mjs"), |
| 63 | }; |
| 64 | }).pipe( |
| 65 | Effect.catchTag("PlatformError", (cause) => |
| 66 | Effect.fail(fail("Failed to emit the Foldkit Lambda entry", cause)), |
| 67 | ), |
| 68 | ), |
| 69 | }); |
| 70 | |
| 71 | export const buildInChild = (input: { |
| 72 | readonly rootDir: string; |
| 73 | readonly config: FoldkitAwsTargetConfig; |
| 74 | }) => |
| 75 | Effect.gen(function* () { |
| 76 | const framework = yield* make({ |
| 77 | ...input.config, |
| 78 | root: input.rootDir, |
| 79 | target: makeAwsFinishTarget(input.config), |
| 80 | }); |
| 81 | return yield* framework.build({ root: input.rootDir }); |
| 82 | }); |
| 83 | export const makeAwsTarget = (config: FoldkitAwsTargetConfig = {}): FoldkitTarget => ({ |
| 84 | ...makeAwsFinishTarget(config), |
| 85 | build: (context) => |
| 86 | runBuildChild({ |
| 87 | module: import.meta.url, |
| 88 | rootDir: context.root, |
| 89 | env: context.env, |
| 90 | framework: "foldkit", |
| 91 | config: { rootDir: context.root, config }, |
| 92 | }).pipe(Effect.mapError((error) => fail(error.message, error.cause))), |
| 93 | }); |
| 94 | export const target = makeAwsTarget; |
| 95 | export default makeAwsTarget; |