Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

Process.ts

104 lines3.8 KB

1import * as Context from "effect/Context";
2import * as Effect from "effect/Effect";
3import { FileSystem } from "effect/FileSystem";
4import type { Path } from "effect/Path";
5import type { Stdio } from "effect/Stdio";
6import type { Terminal } from "effect/Terminal";
7import type { ChildProcessSpawner } from "effect/unstable/process/ChildProcessSpawner";
8import type { HttpEffect } from "../Http.ts";
9import * as Http from "../Http.ts";
10import * as Output from "../Output.ts";
11import {
12 packEnvValue,
13 unpackEnvValue,
14 type BaseRuntimeContext,
15} from "../RuntimeContext.ts";
16​
17export type ProcessServices =
18 | ChildProcessSpawner
19 | FileSystem
20 | Path
21 | Stdio
22 | Terminal;
23​
24export interface ProcessContext extends BaseRuntimeContext {
25 run: <Req = never, RunReq = never>(
26 effect: Effect.Effect<void, never, RunReq>,
27 ) => Effect.Effect<void, never, Req | RunReq>;
29​
30/**
31 * Long-running host loop registration (`run`). Provided by `Platform` when the
32 * execution context implements {@link ProcessContext} (i.e. carries `run`).
33 *
34 * `Platform` wires this automatically for every host runtime context that
35 * implements `run` (EC2 instances, ECS tasks, processes), so an inline program
36 * can `yield* ServerHost` and call `host.run(...)` during plan/deploy without
37 * the caller providing the layer itself.
38 */
39export class ServerHost extends Context.Service<
40 ServerHost,
41 Pick<ProcessContext, "run">
42>()("Alchemy::ServerHost") {}
43​
44/**
45 * Deploy-time / plan-time host context for platforms that bundle a long-lived
46 * program. It collects background work registered via `run` and HTTP handlers
47 * registered via `serve` into a single `exports.program` effect that the
48 * generated container/instance entrypoint runs.
49 */
50export interface HostRuntimeContext extends ProcessContext {
51 serve: <Req = never>(
52 handler: HttpEffect<Req> | Effect.Effect<HttpEffect<Req>>,
53 options?: { shape?: Record<string, unknown> },
54 ) => Effect.Effect<void, never, Req>;
55 exports: Effect.Effect<{
56 readonly program: Effect.Effect<void, never, any>;
57 }>;
59​
60/**
61 * Build a {@link HostRuntimeContext} for a hosted platform of the given
62 * resource `type`. Both `run` (background loops) and `serve` (HTTP handlers)
63 * append to a single list of runners; `exports.program` runs them all
64 * concurrently. This is the shared host context used by `AWS.EC2.Instance` and
65 * `AWS.ECS.Task`.
66 */
67export const createHostRuntimeContext =
68 (type: string) =>
69 (id: string): HostRuntimeContext => {
70 const runners: Effect.Effect<void, never, any>[] = [];
71 const env: Record<string, any> = {};
72​
73 return {
74 Type: type,
75 id,
76 env,
77 set: (bindingId: string, output: Output.Output) =>
78 Effect.sync(() => {
79 const key = bindingId.replaceAll(/[^a-zA-Z0-9]/g, "_");
80 // `packEnvValue` marker-packs Redacted values so they survive the
81 // Output → env round-trip.
82 env[key] = output.pipe(Output.map(packEnvValue));
83 return key;
84 }),
85 get: <T>(key: string) =>
86 // Read straight from `process.env` — see `unpackEnvValue` for why
87 // this must never resolve through `Config.string`.
88 Effect.sync(() => unpackEnvValue<T>(process.env[key]) as T),
89 run: (effect: Effect.Effect<void, never, any>) =>
90 Effect.sync(() => {
91 runners.push(effect);
92 }),
93 serve: ((handler) =>
94 Effect.sync(() => {
95 // Register the HTTP handler as a runner. At container runtime the
96 // ambient `HttpServer` (if provided) serves it; `Http.serve` is a
97 // no-op when no server is bound, so this never crashes plan/deploy.
98 runners.push(Http.serve(handler as HttpEffect<any>));
99 })) as HostRuntimeContext["serve"],
100 exports: Effect.sync(() => ({
101 program: Effect.all(runners, { concurrency: "unbounded" }),
102 })),
103 } satisfies HostRuntimeContext;
104 };