Repository navigation
Expand file tree
/
Copy pathDaemonLogOutput.ts
More file actions
179 lines (164 loc) · 6.1 KB
/
Copy pathDaemonLogOutput.ts
File metadata and controls
179 lines (164 loc) · 6.1 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import { spawn, type ChildProcess } from 'node:child_process';
import * as path from 'node:path';
import { MAX_LOG_OUTPUT_BYTES, type LogOutputRequest } from './DaemonLogOutputProtocol';
interface IPendingWrite {
readonly byteLength: number;
readonly resolve: () => void;
readonly reject: (reason: unknown) => void;
}
/**
* Keeps blocking Windows stdout writes out of the CLI's event loop and thread pool.
* One acknowledged chunk is in flight; every exit joins the output-only child.
*/
export class DaemonLogOutput {
readonly #signal: AbortSignal | undefined;
readonly #outputFd: number;
readonly #failureController: AbortController = new AbortController();
#child: ChildProcess | undefined;
#ready: Promise<void> | undefined;
#resolveReady: (() => void) | undefined;
#rejectReady: ((reason: unknown) => void) | undefined;
#pending: IPendingWrite | undefined;
#failure: Error | undefined;
#closed: Promise<void> = Promise.resolve();
#closePromise: Promise<void> | undefined;
#stopRequested: boolean = false;
#cancelled: boolean = false;
public constructor(signal?: AbortSignal, outputFd: number = 1) {
this.#signal = signal;
this.#outputFd = outputFd;
signal?.addEventListener('abort', this.#onAbort, { once: true });
}
public get failureSignal(): AbortSignal {
return this.#failureController.signal;
}
public get workerPid(): number | undefined {
return this.#child?.pid;
}
public async writeAsync(bytes: Uint8Array): Promise<void> {
if (bytes.byteLength > MAX_LOG_OUTPUT_BYTES)
throw new RangeError('Daemon log output chunk is too large.');
this.#signal?.throwIfAborted();
this.failureSignal.throwIfAborted();
if (this.#closePromise) throw new Error('Daemon log output is closed.');
await this.#startAsync();
this.#signal?.throwIfAborted();
this.failureSignal.throwIfAborted();
if (this.#pending) throw new Error('Daemon log output already has a pending write.');
await new Promise<void>((resolve, reject) => {
this.#pending = { byteLength: bytes.byteLength, resolve, reject };
this.#send({ kind: 'write', bytes });
});
}
public closeAsync(): Promise<void> {
this.#closePromise ??= this.#closeOnceAsync();
return this.#closePromise;
}
async #closeOnceAsync(): Promise<void> {
try {
if (this.#child && !this.#stopRequested) {
this.#stopRequested = true;
if (this.#signal?.aborted || this.#failure || this.#pending) {
this.#cancelled = true;
this.#child.kill('SIGKILL');
} else if (this.#child.connected) {
this.#send({ kind: 'end' });
}
}
await this.#closed;
this.failureSignal.throwIfAborted();
} finally {
this.#signal?.removeEventListener('abort', this.#onAbort);
}
}
#startAsync(): Promise<void> {
if (this.#ready) return this.#ready;
this.#ready = new Promise<void>((resolve, reject) => {
this.#resolveReady = resolve;
this.#rejectReady = reject;
});
const child: ChildProcess = spawn(process.execPath, [path.join(__dirname, 'DaemonLogOutputWorker.js')], {
stdio: ['ignore', 'ignore', 'ignore', this.#outputFd, 'ipc'],
serialization: 'advanced',
windowsHide: true,
// This private I/O worker must not run user preload hooks or arbitrary user work.
env: { ...process.env, NODE_OPTIONS: undefined }
});
this.#child = child;
this.#closed = new Promise<void>((resolve) => {
child.once('close', (code, signal) => {
if (!this.#stopRequested || (!this.#cancelled && code !== 0)) {
this.#fail(new Error(`Daemon log output worker exited unexpectedly (${code ?? signal}).`));
}
this.#pending?.reject(
this.#failure ?? this.#signal?.reason ?? new Error('Daemon log output closed.')
);
this.#pending = undefined;
resolve();
});
});
child.once('error', (error) => this.#fail(error));
child.once('spawn', () => {
if (this.#signal?.aborted) this.#onAbort();
});
child.on('message', (message: unknown) => this.#onMessage(message));
return this.#ready;
}
#send(message: LogOutputRequest): void {
this.#child!.send(message, (error: Error | null) => {
if (error && !this.#cancelled) this.#fail(error);
});
}
#onMessage(message: unknown): void {
if (this.#cancelled) return;
if (typeof message === 'object' && message !== null && 'kind' in message) {
if (message.kind === 'ready' && this.#resolveReady) {
this.#resolveReady();
this.#resolveReady = undefined;
return;
}
const pending: IPendingWrite | undefined = this.#pending;
if (
message.kind === 'written' &&
'byteLength' in message &&
pending &&
pending.byteLength === message.byteLength
) {
pending.resolve();
this.#pending = undefined;
return;
}
if (message.kind === 'error' && 'message' in message && typeof message.message === 'string') {
const error: NodeJS.ErrnoException = new Error(`Daemon log output failed: ${message.message}`);
if ('code' in message && typeof message.code === 'string') error.code = message.code;
this.#fail(error);
return;
}
}
this.#fail(new Error('Invalid daemon log output acknowledgement.'));
}
#fail(error: Error): void {
this.#failure ??= error;
this.#failureController.abort(this.#failure);
this.#rejectReady?.(this.#failure);
this.#pending?.reject(this.#failure);
this.#pending = undefined;
if (this.#child && !this.#stopRequested) {
this.#stopRequested = true;
this.#cancelled = true;
this.#child.kill('SIGKILL');
}
}
readonly #onAbort = (): void => {
this.#cancelled = true;
this.#rejectReady?.(this.#signal?.reason);
this.#pending?.reject(this.#signal?.reason);
this.#pending = undefined;
if (this.#child) {
this.#stopRequested = true;
this.#child.kill('SIGKILL');
}
};
}