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

NodeRuntime.ts

74 lines2.7 KB

1/**
2 * Node.js process runner for starting an Effect program at the application
3 * edge.
4 *
5 * This module exposes `runMain`, the launcher used by Node CLIs, scripts,
6 * servers, and workers when a single Effect should become the process root. It
7 * handles runtime error reporting, Node process signals, and teardown so the
8 * rest of the program can stay inside Effect.
9 *
10 * **Mental model**
11 *
12 * `runMain` is the last call in `main.ts`: build the effect, provide the layers
13 * it needs, then hand the self-contained program to this module. The function
14 * does not provide Node services itself; use `NodeServices.layer` or narrower
15 * platform layers before launching.
16 *
17 * **Common tasks**
18 *
19 * - Run a CLI command or script and let failures become process failures.
20 * - Keep a server or worker fiber alive as the process main program.
21 * - Override teardown or disable automatic error reporting at the boundary.
22 *
23 * **Gotchas**
24 *
25 * `SIGINT` and `SIGTERM` interrupt the main fiber so scoped finalizers can run.
26 * Clean success lets the event loop drain naturally, while signal-triggered
27 * interruption or a non-zero teardown code exits the process. Keep long-lived
28 * resources in Effect scopes and avoid finalizers that never complete.
29 *
30 * @since 4.0.0
31 */
32import * as NodeRuntime from "@effect/platform-node-shared/NodeRuntime"
33import type { Effect } from "effect/Effect"
34import type * as Runtime from "effect/Runtime"
35​
36/**
37 * Helps you run a main effect with built-in error handling, logging, and signal management.
38 *
39 * **When to use**
40 *
41 * Use to run an Effect as your application's main program, especially
42 * when you need structured error handling, log management, interrupt support,
43 * or advanced teardown capabilities.
44 *
45 * **Details**
46 *
47 * This function launches an Effect as the main entry point, setting exit codes
48 * based on success or failure, handling interrupts (e.g., Ctrl+C), and optionally
49 * logging errors. By default, it logs errors and uses a "pretty" format, but both
50 * behaviors can be turned off. You can also provide custom teardown logic to
51 * finalize resources or produce different exit codes.
52 *
53 * The optional configuration object can include:
54 * - `disableErrorReporting`: Turn off automatic error logging.
55 * - `teardown`: Provide custom finalization logic.
56 *
57 * @category running
58 * @since 4.0.0
59 */
60export const runMain: {
61 (
62 options?: {
63 readonly disableErrorReporting?: boolean | undefined
64 readonly teardown?: Runtime.Teardown | undefined
65 }
66 ): <E, A>(effect: Effect<A, E>) => void
67 <E, A>(
68 effect: Effect<A, E>,
69 options?: {
70 readonly disableErrorReporting?: boolean | undefined
71 readonly teardown?: Runtime.Teardown | undefined
72 }
73 ): void
74} = NodeRuntime.runMain