-
-
Notifications
You must be signed in to change notification settings - Fork 260
Expand file tree
/
Copy pathoutput-error-handler.ts
More file actions
38 lines (32 loc) · 1.19 KB
/
output-error-handler.ts
File metadata and controls
38 lines (32 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import type { Writable } from 'node:stream';
import type { Command } from '../command.js';
import { fromSharedEvent } from '../observables.js';
import type { FlowController } from './flow-controller.js';
/**
* Kills processes and aborts further command spawning on output stream error (namely, SIGPIPE).
*/
export class OutputErrorHandler implements FlowController {
private readonly outputStream: Writable;
private readonly abortController: AbortController;
constructor({
abortController,
outputStream,
}: {
abortController: AbortController;
outputStream: Writable;
}) {
this.abortController = abortController;
this.outputStream = outputStream;
}
handle(commands: Command[]): { commands: Command[]; onFinish: () => void } {
const subscription = fromSharedEvent(this.outputStream, 'error').subscribe(() => {
commands.forEach((command) => command.kill());
// Avoid further commands from spawning, e.g. if `RestartProcess` is used.
this.abortController.abort();
});
return {
commands,
onFinish: () => subscription.unsubscribe(),
};
}
}