| 1 | /** |
| 2 | * Process bootstrap for `Prisma.Compute` apps. The generated entry imports |
| 3 | * this module and the user's `main`, nothing else — see |
| 4 | * {@link ./Process.ts} for why. |
| 5 | */ |
| 6 | import { BunServices } from "@effect/platform-bun"; |
| 7 | import * as ConfigProvider from "effect/ConfigProvider"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as Layer from "effect/Layer"; |
| 10 | import * as Logger from "effect/Logger"; |
| 11 | import { MinimumLogLevel } from "effect/References"; |
| 12 | import * as FetchHttpClient from "effect/http/FetchHttpClient"; |
| 13 | import { BunHttpServer } from "../../Http.ts"; |
| 14 | import { Stage } from "../../Stage.ts"; |
| 15 | import { |
| 16 | entrypointLayer, |
| 17 | resolveProgram, |
| 18 | runProcess, |
| 19 | stackConstant, |
| 20 | } from "./Process.ts"; |
| 21 | |
| 22 | export interface PrismaBootstrapOptions { |
| 23 | /** Port to serve on when the platform does not inject `PORT`. */ |
| 24 | readonly port: number; |
| 25 | /** Stack identity baked in at deploy time. */ |
| 26 | readonly stack: { readonly name: string; readonly stage: string }; |
| 27 | } |
| 28 | |
| 29 | /** Serve the bundled app with a Bun HTTP server on `PORT` (all interfaces). */ |
| 30 | export const bootstrap = ( |
| 31 | entrypoint: unknown, |
| 32 | options: PrismaBootstrapOptions, |
| 33 | ): Promise<void> => { |
| 34 | process.env.PORT ??= String(options.port); |
| 35 | |
| 36 | const platform = Layer.mergeAll( |
| 37 | BunServices.layer, |
| 38 | FetchHttpClient.layer, |
| 39 | Logger.layer([Logger.consolePretty()]), |
| 40 | ); |
| 41 | |
| 42 | const stack = Layer.mergeAll( |
| 43 | stackConstant(options.stack.name, options.stack.stage), |
| 44 | Layer.succeed(Stage, options.stack.stage), |
| 45 | ); |
| 46 | |
| 47 | const program = resolveProgram("default").pipe( |
| 48 | Effect.provide( |
| 49 | entrypointLayer(entrypoint).pipe( |
| 50 | Layer.provideMerge(stack), |
| 51 | Layer.provideMerge(BunHttpServer({ hostname: "0.0.0.0" })), |
| 52 | Layer.provideMerge(platform), |
| 53 | Layer.provideMerge( |
| 54 | Layer.succeed( |
| 55 | ConfigProvider.ConfigProvider, |
| 56 | ConfigProvider.orElse( |
| 57 | ConfigProvider.fromUnknown({ ALCHEMY_PHASE: "runtime" }), |
| 58 | ConfigProvider.fromEnv(), |
| 59 | ), |
| 60 | ), |
| 61 | ), |
| 62 | Layer.provideMerge( |
| 63 | Layer.succeed(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"), |
| 64 | ), |
| 65 | ), |
| 66 | ), |
| 67 | Effect.scoped, |
| 68 | ); |
| 69 | |
| 70 | return runProcess("Prisma Compute", program); |
| 71 | }; |