@effected/engine 0.10.0 → 0.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/guard.d.ts DELETED
@@ -1,205 +0,0 @@
1
- /**
2
- * The slice of the host process {@link ProcessGuard.run} uses. Node's
3
- * `process` satisfies it.
4
- *
5
- * @remarks
6
- * Structural on purpose: the guard never reads a global, so a test passes a
7
- * double and a front end passes `process` from its own `main`.
8
- *
9
- * @public
10
- */
11
- interface ProcessGuardHost {
12
- /** Register the `uncaughtException` listener. */
13
- on(event: "uncaughtException", listener: (error: Error, origin: string) => void): unknown;
14
- /** Register the `unhandledRejection` listener. */
15
- on(event: "unhandledRejection", listener: (reason: unknown) => void): unknown;
16
- /** Raise an `uncaughtException` to the registered listeners. Used only by {@link ProcessGuardOptions.injectCrash}. */
17
- emit(event: "uncaughtException", error: Error, origin: "uncaughtException" | "unhandledRejection"): unknown;
18
- /** Raise an `unhandledRejection` to the registered listeners. Used only by {@link ProcessGuardOptions.injectCrash}. */
19
- emit(event: "unhandledRejection", reason: unknown, promise: Promise<unknown>): unknown;
20
- /** Where every guard report is written. For a stdio server, never stdout: that is the protocol wire. */
21
- readonly stderr: {
22
- write(chunk: string): unknown;
23
- };
24
- /** End the process at once. */
25
- exit(code?: number): never;
26
- }
27
- /**
28
- * When a stray exception or rejection ends the process.
29
- *
30
- * @remarks
31
- * `"exit"` exits 1 whenever it happens. `"exitBeforeConnect"` exits 1 until
32
- * the caller reports the server connected with
33
- * {@link ProcessGuardControl.markConnected}, then logs and keeps serving: a
34
- * server that dies mid-session drops every client attached to it. `"log"`
35
- * never exits.
36
- *
37
- * @public
38
- */
39
- interface ProcessGuardPolicy {
40
- /** For `uncaughtException`. */
41
- readonly onUncaught: "exit" | "exitBeforeConnect";
42
- /** For `unhandledRejection`. Defaults to `"exit"`. */
43
- readonly onRejection?: "exit" | "exitBeforeConnect" | "log" | undefined;
44
- }
45
- /**
46
- * One crash {@link ProcessGuardOptions.injectCrash} raises: which event, and
47
- * when.
48
- *
49
- * @public
50
- */
51
- interface ProcessGuardInjection {
52
- /** `"load"`: before `load` is called. `"connected"`: after the first {@link ProcessGuardControl.markConnected}. */
53
- readonly at: "load" | "connected";
54
- /** Which event to raise. */
55
- readonly kind: "uncaughtException" | "unhandledRejection";
56
- }
57
- /**
58
- * What {@link ProcessGuardOptions.load} is handed: the two moments only the
59
- * caller can see.
60
- *
61
- * @remarks
62
- * Plain callbacks, so a caller with no Effect runtime (a
63
- * `vscode-languageserver` server, a hand-written socket loop) uses them the
64
- * same way an Effect launcher does: an MCP launcher passes `markConnected`
65
- * to `McpStdio.launch`'s `onReady`; an LSP calls it once its connection is
66
- * listening.
67
- *
68
- * @public
69
- */
70
- interface ProcessGuardControl {
71
- /**
72
- * Report the server connected: from here on `"exitBeforeConnect"` logs and
73
- * keeps going. Call it once the server is serving. Later calls do nothing.
74
- * Safe to call after `load` has resolved, which is the usual case.
75
- *
76
- * @remarks
77
- * For a server framed over stdio (MCP, LSP), "serving" is the moment its
78
- * transport is built and reading stdin, before the first request arrives:
79
- * from then on a client is attached, and a stray error should be logged,
80
- * not end the session.
81
- */
82
- readonly markConnected: () => void;
83
- /**
84
- * Format every report from here on with `format`, in place of the guard's
85
- * own dependency-free formatter. A `format` that throws falls back to the
86
- * guard's own, so a broken formatter never hides a crash.
87
- */
88
- readonly useFormat: (format: (error: unknown) => string) => void;
89
- }
90
- /**
91
- * Options for {@link ProcessGuard.run}.
92
- *
93
- * @public
94
- */
95
- interface ProcessGuardOptions {
96
- /** Prefixes every report the guard writes, as in `my-server: uncaughtException (…): …`. */
97
- readonly label: string;
98
- /** The process: pass `process`. */
99
- readonly host: ProcessGuardHost;
100
- /** Defaults to `{ onUncaught: "exit", onRejection: "exit" }`. */
101
- readonly policy?: ProcessGuardPolicy | undefined;
102
- /**
103
- * Import, assemble and start the server. Everything that can throw while
104
- * loading belongs here, behind a dynamic `import()`, so the guards are
105
- * already listening when it runs. A rejection is reported as
106
- * `startup failed` and exits 1, whatever the policy.
107
- */
108
- readonly load: (guard: ProcessGuardControl) => Promise<unknown>;
109
- /**
110
- * For a test of the guards themselves: raise one stray `kind` at `at`, so
111
- * both halves of a policy can be driven end to end in a real process.
112
- *
113
- * @remarks
114
- * - `"load"` raises it once both listeners are installed and before
115
- * `load` is called, then waits for the guard to handle it: `load` is
116
- * not called until the listener has run. If a test double's `exit`
117
- * throws instead of ending the process, `run` rejects with what it
118
- * threw and never calls `load`. The pre-connect half of the policy
119
- * applies, so `"exitBeforeConnect"` exits 1 here, and only an
120
- * `onRejection` of `"log"` lets the process go on to load and serve.
121
- * The report uses the guard's own formatter, since no `useFormat` has
122
- * run yet.
123
- * - `"connected"` raises it after the first `markConnected`, where
124
- * `"exitBeforeConnect"` logs and keeps serving. It is never raised if
125
- * `markConnected` is never called. A throw from a test double's `exit`
126
- * there is dropped.
127
- *
128
- * **The `"connected"` report is asynchronous.** It is raised on a later
129
- * tick, a `setTimeout(0)` after `markConnected`, so it can land after the
130
- * first responses the server sends: a test that reads stderr once, right
131
- * after its first response, can see nothing. Wait for the report (with
132
- * `McpProcess.stderrUntil` from `@effected/mcp/testing`, for an MCP
133
- * server) rather than reading it once.
134
- *
135
- * Either is raised only through `host.emit`, on a `setTimeout(0)` tick,
136
- * never as a real throw or rejection, so the guard's listeners handle it
137
- * the same way under the real `process` and under a test double. Under
138
- * `process`, every listener registered for the event sees it, not only
139
- * the guard's. It carries an `[injected]` message; a rejection is emitted
140
- * with an already-handled rejected promise. `undefined`, or an `at` or
141
- * `kind` outside these values, does nothing at all. Wire it to an
142
- * environment variable only a test sets.
143
- */
144
- readonly injectCrash?: ProcessGuardInjection | undefined;
145
- }
146
- /**
147
- * Transport-neutral crash guards for a server process, installed before the
148
- * server's module graph loads. Imported from `@effected/engine/guard`.
149
- *
150
- * @remarks
151
- * {@link ProcessGuard.run} registers `uncaughtException` and
152
- * `unhandledRejection` listeners on `host`, then awaits `load`. It launches
153
- * nothing itself: `load` starts the server, over whatever transport, and
154
- * calls `markConnected` once it is serving. `@effected/mcp/guard`'s
155
- * `McpGuard.run` is this guard with an MCP stdio launch in its `load`.
156
- *
157
- * - This entrypoint has no runtime import at all, so a throw while the
158
- * server graph evaluates is still reported on stderr.
159
- * - A `load` that rejects is reported as `startup failed` and exits 1
160
- * whatever the policy. Left to a log-only rejection listener, it would let
161
- * the event loop drain and exit 0 with no server.
162
- * - Installing an `unhandledRejection` listener switches off Node's default
163
- * of throwing on one, so `"log"` really does keep the process running.
164
- * - An exit from a guard skips every finalizer the server registered.
165
- * - Every report is one line: `<label>: uncaughtException (<origin>): …`,
166
- * `<label>: unhandledRejection: …` or `<label>: startup failed: …`.
167
- *
168
- * @example
169
- * ```ts
170
- * import { ProcessGuard } from "@effected/engine/guard";
171
- *
172
- * await ProcessGuard.run({
173
- * label: "my-lsp",
174
- * host: process,
175
- * policy: { onUncaught: "exitBeforeConnect", onRejection: "log" },
176
- * load: async (guard) => {
177
- * const { startServer } = await import("./server.js");
178
- * await startServer();
179
- * guard.markConnected();
180
- * },
181
- * });
182
- * ```
183
- *
184
- * @public
185
- */
186
- export declare class ProcessGuard {
187
- private constructor();
188
- /**
189
- * Parse a test-only crash-injection setting into {@link ProcessGuardOptions.injectCrash}: `<at>:<kind>`, where `at`
190
- * is `"load"` or `"connected"` and `kind` is `"uncaughtException"` or `"unhandledRejection"`. Anything else, or no
191
- * value, is `undefined` (no injection), so a launcher can pass an environment variable straight through and every
192
- * launcher shares one grammar.
193
- *
194
- * @example
195
- * ```ts
196
- * ProcessGuard.parseInjectCrash("connected:unhandledRejection"); // => { at: "connected", kind: "unhandledRejection" }
197
- * ProcessGuard.parseInjectCrash("later:boom"); // => undefined
198
- * ```
199
- */
200
- static readonly parseInjectCrash: (value: string | undefined) => ProcessGuardInjection | undefined;
201
- /** Install the guards, then run `load`. Resolves once `load` has resolved. */
202
- static readonly run: (options: ProcessGuardOptions) => Promise<void>;
203
- }
204
- export type { ProcessGuardControl, ProcessGuardHost, ProcessGuardInjection, ProcessGuardOptions, ProcessGuardPolicy };
205
- //# sourceMappingURL=guard.d.ts.map
package/guard.js DELETED
@@ -1,3 +0,0 @@
1
- import { ProcessGuard } from "./ProcessGuard.js";
2
-
3
- export { ProcessGuard };
@@ -1,57 +0,0 @@
1
- import { Predicate } from "effect";
2
-
3
- // src/internal/causeMessage.ts
4
- /** How many nodes of a cause chain the walk reads before stopping, so a cyclic chain cannot spin. */
5
- const CAUSE_DEPTH = 8;
6
- /**
7
- * The message a single node carries: a thrown string, or a non-empty string `message` property. A getter or
8
- * proxy trap that throws is a node with no message.
9
- */
10
- const messageAt = (node) => {
11
- try {
12
- const text = Predicate.isString(node) ? node : Predicate.hasProperty(node, "message") && Predicate.isString(node.message) ? node.message : void 0;
13
- return text === void 0 || text.trim() === "" ? void 0 : text;
14
- } catch {
15
- return;
16
- }
17
- };
18
- /** The next link of the chain: a node's `cause`, or `undefined` when it has none or reading it throws. */
19
- const causeAt = (node) => {
20
- try {
21
- return Predicate.hasProperty(node, "cause") ? node.cause : void 0;
22
- } catch {
23
- return;
24
- }
25
- };
26
- /**
27
- * Every distinct non-empty message a preserved cause chain carries, nearest
28
- * first and joined with `: `, or `undefined` when nothing along it has one.
29
- *
30
- * A `Schema.Defect()` field holds whatever was thrown, so the read is guarded
31
- * rather than assumed: `Predicate.hasProperty` + `Predicate.isString` accepts
32
- * an `Error`, a cross-realm `Error` that fails `instanceof`, a plain
33
- * `{ message }` object and a thrown string alike. An empty or whitespace-only
34
- * message counts as none. The walk follows `cause` links because a wrapper's
35
- * text is often the generic summary and the driver's text underneath is the
36
- * actionable detail, yet a wrapper that composes its own context must not
37
- * lose it either; so nothing is dropped, and a deeper text the nearer one
38
- * already contains is not repeated. The walk is bounded, so a cycle terminates.
39
- *
40
- * It never throws: every property read is guarded, so a `message` getter that throws leaves that link
41
- * without text, and a `cause` getter that throws ends the walk with what was found so far.
42
- */
43
- const causeMessage = (cause) => {
44
- let message;
45
- let node = cause;
46
- for (let depth = 0; depth < CAUSE_DEPTH && node !== void 0 && node !== null; depth++) {
47
- const text = messageAt(node);
48
- if (text !== void 0) {
49
- if (message === void 0 || text.includes(message)) message = text;
50
- else if (!message.includes(text)) message = `${message}: ${text}`;
51
- }
52
- node = causeAt(node);
53
- }
54
- return message;
55
- };
56
-
57
- export { causeMessage };