@theholocron/dagobah 5.0.0-alpha.342 → 5.0.0-alpha.343

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/README.md CHANGED
@@ -26,3 +26,6 @@ await serveHandler({
26
26
  what is missing. An unquoted multi-line value (a PEM key, which `op` prints that way) is read whole.
27
27
  - `startDevServer({ handle, env, port })`: loopback only; one log line per request (method, path, status).
28
28
  - `describeLogging(env, { tokenVar, datasetVar })`: one line, the dataset and never the token.
29
+ - `superviseProcesses({ primary, companion })`: runs two processes in the foreground, each with its own extra
30
+ environment; stops one when the other ends; Ctrl-C and a termination signal go to both and are not a failure. Resolves
31
+ `{ exitCode, endedFirst, interrupted }`; rejects naming a program that is missing or cannot start.
package/dist/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { spawnSync } from "node:child_process";
1
+ import { spawn, spawnSync } from "node:child_process";
2
2
  /** What {@link loadDevEnv} needs: which 1Password Environment holds the secrets, and which variables must be set. */
3
3
  interface LoadDevEnvInput<K extends string = string> {
4
4
  /** The name of the 1Password Environment, e.g. `Sentinel - dev`. */
@@ -90,4 +90,37 @@ interface ServeHandlerInput<K extends string> {
90
90
  * @returns The running server.
91
91
  */
92
92
  export declare function serveHandler<K extends string>(input: ServeHandlerInput<K>): Promise<DevServer>;
93
- export type { DescribeLoggingOptions, DevEnv, DevServer, DevServerInput, LoadDevEnvInput, ServeHandlerInput };
93
+ /** One process to run in the foreground. */
94
+ interface SupervisedProcess {
95
+ /** The program. */
96
+ command: string;
97
+ /** Its arguments. */
98
+ args: string[];
99
+ /** Variables added to the environment this process is started with, and to it alone. */
100
+ env?: Record<string, string> | undefined;
101
+ }
102
+ /** Input for {@link superviseProcesses}. */
103
+ interface SuperviseProcessesInput {
104
+ /** The process that matters, e.g. the application being developed. */
105
+ primary: SupervisedProcess;
106
+ /** The process that runs beside it, e.g. a tunnel's connector. */
107
+ companion: SupervisedProcess;
108
+ /** Starts a process. Defaults to `spawn`. */
109
+ spawn?: typeof spawn | undefined;
110
+ }
111
+ /** How the two processes ended. */
112
+ interface SuperviseProcessesResult {
113
+ /** `0` when it was stopped with Ctrl-C or a termination signal; otherwise the status of the one that ended first (`1` for one killed by a signal). */
114
+ exitCode: number;
115
+ /** Which one ended first. */
116
+ endedFirst: "primary" | "companion";
117
+ /** Whether this process was told to stop (Ctrl-C, a termination signal), as opposed to one of them ending on its own. */
118
+ interrupted: boolean;
119
+ }
120
+ /**
121
+ * Runs two processes side by side in the foreground, with their output on the terminal,
122
+ * and stops the other when either ends. Ctrl-C and a termination signal go to both and are not a failure. A process
123
+ * that cannot be started stops the other and rejects, saying which program it was.
124
+ */
125
+ export declare function superviseProcesses(input: SuperviseProcessesInput): Promise<SuperviseProcessesResult>;
126
+ export type { DescribeLoggingOptions, DevEnv, DevServer, DevServerInput, LoadDevEnvInput, ServeHandlerInput, SuperviseProcessesInput, SuperviseProcessesResult, SupervisedProcess };
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { spawnSync } from "node:child_process";
1
+ import { spawn, spawnSync } from "node:child_process";
2
2
  import { parse } from "dotenv";
3
3
  import { createServer } from "node:http";
4
4
  // src/quote-pem/quote-pem.ts
@@ -152,4 +152,74 @@ async function serveHandler(input) {
152
152
  for (const signal of ["SIGINT", "SIGTERM"]) process.once(signal, () => stop(server.close));
153
153
  return server;
154
154
  }
155
- export { describeLogging, loadDevEnv, serveHandler, startDevServer };
155
+ // src/supervise-processes/supervise-processes.ts
156
+ /**
157
+ * Runs two processes side by side in the foreground, with their output on the terminal,
158
+ * and stops the other when either ends. Ctrl-C and a termination signal go to both and are not a failure. A process
159
+ * that cannot be started stops the other and rejects, saying which program it was.
160
+ */
161
+ function superviseProcesses(input) {
162
+ return new Promise((resolve, reject) => {
163
+ const spawnImpl = input.spawn ?? spawn;
164
+ const start = (p) => spawnImpl(p.command, p.args, {
165
+ stdio: "inherit",
166
+ env: {
167
+ ...process.env,
168
+ ...p.env
169
+ }
170
+ });
171
+ const children = {
172
+ primary: start(input.primary),
173
+ companion: start(input.companion)
174
+ };
175
+ const names = ["primary", "companion"];
176
+ let endedFirst;
177
+ let status = 0;
178
+ let interrupted = false;
179
+ let done = false;
180
+ const ended = /* @__PURE__ */ new Set();
181
+ const signals = ["SIGINT", "SIGTERM"].map((signal) => {
182
+ const handler = () => {
183
+ interrupted = true;
184
+ for (const child of Object.values(children)) child.kill(signal);
185
+ };
186
+ process.on(signal, handler);
187
+ return [signal, handler];
188
+ });
189
+ const cleanUp = () => {
190
+ for (const [signal, handler] of signals) process.off(signal, handler);
191
+ };
192
+ const stopAll = () => {
193
+ for (const child of Object.values(children)) child.kill("SIGTERM");
194
+ };
195
+ for (const name of names) {
196
+ const other = name === "primary" ? "companion" : "primary";
197
+ children[name].once("error", (err) => {
198
+ if (done) return;
199
+ done = true;
200
+ cleanUp();
201
+ stopAll();
202
+ const program = input[name].command;
203
+ reject(/* @__PURE__ */ new Error(err.code === "ENOENT" ? `${program} is not installed or not on the PATH` : `could not start ${program}: ${err.message}`));
204
+ });
205
+ children[name].once("exit", (code) => {
206
+ ended.add(name);
207
+ if (endedFirst === void 0) {
208
+ endedFirst = name;
209
+ status = code ?? 1;
210
+ children[other].kill("SIGTERM");
211
+ }
212
+ if (ended.size === names.length && !done) {
213
+ done = true;
214
+ cleanUp();
215
+ resolve({
216
+ exitCode: interrupted ? 0 : status,
217
+ endedFirst,
218
+ interrupted
219
+ });
220
+ }
221
+ });
222
+ }
223
+ });
224
+ }
225
+ export { describeLogging, loadDevEnv, serveHandler, startDevServer, superviseProcesses };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@theholocron/dagobah",
3
- "version": "5.0.0-alpha.342",
3
+ "version": "5.0.0-alpha.343",
4
4
  "description": "Local development server for a web-standard handler: loads its secrets from a 1Password Environment and serves it over HTTP behind a tunnel.",
5
5
  "keywords": [
6
6
  "dev-server",