-
Notifications
You must be signed in to change notification settings - Fork 433
Expand file tree
/
Copy pathprocess.ts
More file actions
259 lines (234 loc) · 7.74 KB
/
process.ts
File metadata and controls
259 lines (234 loc) · 7.74 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
* process.ts
*
* Copyright (C) 2020-2022 Posit Software, PBC
*/
import { MuxAsyncIterator, pooledMap } from "async";
import { debug, info } from "../deno_ral/log.ts";
import { onCleanup } from "./cleanup.ts";
import { ProcessResult } from "./process-types.ts";
const processList = new Map<number, Deno.ChildProcess>();
let processCount = 0;
let cleanupRegistered = false;
export function registerForExitCleanup(process: Deno.ChildProcess) {
const thisProcessId = ++processCount; // don't risk repeated PIDs
processList.set(thisProcessId, process);
return thisProcessId;
}
export function unregisterForExitCleanup(processId: number) {
processList.delete(processId);
}
function ensureCleanup() {
if (!cleanupRegistered) {
cleanupRegistered = true;
onCleanup(() => {
for (const process of processList.values()) {
try {
process.kill();
// process.close();
} catch (error) {
info("Error occurred during cleanup: " + error);
}
}
});
}
}
export type ExecProcessOptions = Deno.CommandOptions & {
cmd: string;
};
export async function execProcess(
options: ExecProcessOptions,
stdin?: string,
mergeOutput?: "stderr>stdout" | "stdout>stderr",
stderrFilter?: (output: string) => string,
respectStreams?: boolean,
timeout?: number,
): Promise<ProcessResult> {
const withTimeout = <T>(promise: Promise<T>): Promise<T> => {
return timeout
? Promise.race([
promise,
new Promise((_, reject) =>
setTimeout(() => reject(new Error("Process timed out")), timeout)
),
]) as Promise<T>
: promise;
};
ensureCleanup();
// define process
try {
// If the caller asked for stdout/stderr to be directed to the rid of an open
// file, just allow that to happen. Otherwise, specify piped and we will implement
// the proper behavior for inherit, etc....
debug(`[execProcess] ${[options.cmd, ...(options.args || [])].join(" ")}`);
const denoCmd = new Deno.Command(options.cmd, {
...options,
stdin: stdin !== undefined ? "piped" : options.stdin,
stdout: typeof (options.stdout) === "number" ? options.stdout : "piped",
stderr: typeof (options.stderr) === "number" ? options.stderr : "piped",
});
const process = denoCmd.spawn();
const thisProcessId = registerForExitCleanup(process);
if (stdin !== undefined) {
const stdinWriter = process.stdin.getWriter();
if (!process.stdin) {
unregisterForExitCleanup(thisProcessId);
throw new Error("Process stdin not available");
}
// write in 4k chunks (deno observed to overflow at > 64k)
const kWindowSize = 4096;
const buffer = new TextEncoder().encode(stdin);
let offset = 0;
while (offset < buffer.length) {
const end = Math.min(offset + kWindowSize, buffer.length);
const window = buffer.subarray(offset, end);
await stdinWriter.write(window);
offset += window.byteLength;
}
stdinWriter.releaseLock();
try {
await process.stdin.close();
} catch (e) {
// The child may have closed its read end of the pipe before our
// close() completed (e.g. exited fast, failed to spawn). The
// resulting "Writable stream is closed or errored." is not a
// failure of execProcess — the child's exit status reflects any
// real problem. Swallow it so it doesn't escape as an unhandled
// rejection that aborts the process. See #14445.
debug(`[execProcess] stdin.close() rejected: ${e}`);
}
}
let stdoutText = "";
let stderrText = "";
// If the caller requests, merge the output into a single stream. This single stream will
// follow the runoption for that stream (e.g. inherit, pipe, etc...)
if (mergeOutput) {
// This multiplexer that holds the async streams and merges their results
const multiplexIterator = new MuxAsyncIterator<
Uint8Array
>();
// Add streams to the multiplexer
const addStream = (
iterator: AsyncIterableIterator<Uint8Array<ArrayBuffer>>,
filter?: (output: string) => string,
) => {
const streamIter = filter
? filteredAsyncIterator(iterator, filter)
: iterator;
multiplexIterator.add(streamIter);
};
addStream(process.stdout.values());
addStream(process.stderr.values(), stderrFilter);
// Process the output
const allOutput = await processOutput(
multiplexIterator,
mergeOutput === "stderr>stdout" ? options.stdout : options.stderr,
);
// Provide the output in whichever result the user requested
if (mergeOutput === "stderr>stdout") {
stdoutText = allOutput;
} else {
stderrText = allOutput;
}
// Close the streams
// FIXME: In Deno 2 we get ReadableStreams which do not have a close method?
//
// const closeStream = (stream: ReadableStream<Uint8Array<ArrayBuffer>> | null) => {
// if (stream) {
// stream.close();
// }
// };
// closeStream(process.stdout);
// closeStream(process.stderr);
} else {
// Process the streams independently
const promises: Promise<void>[] = [];
if (process.stdout !== null) {
promises.push(
processOutput(
process.stdout.values(),
options.stdout,
respectStreams ? "stdout" : undefined,
).then((text) => {
stdoutText = text;
// process.stdout!.close();
}),
);
}
if (process.stderr != null) {
const iterator = stderrFilter
? filteredAsyncIterator(process.stderr.values(), stderrFilter)
: process.stderr.values();
promises.push(
processOutput(
iterator,
options.stderr,
respectStreams ? "stderr" : undefined,
).then((text) => {
stderrText = text;
// process.stderr!.close();
}),
);
}
await withTimeout(Promise.all(promises));
}
// await result
const status = await withTimeout(process.output());
// close the process
// process.close();
unregisterForExitCleanup(thisProcessId);
debug(`[execProcess] Success: ${status.success}, code: ${status.code}`);
return {
success: status.success,
code: status.code,
stdout: stdoutText,
stderr: stderrText,
};
} catch (e) {
if (!(e instanceof Error)) {
throw e;
}
throw new Error(`Error executing '${options.cmd}': ${e.message}`);
}
}
export function processSuccessResult(): ProcessResult {
return {
success: true,
code: 0,
};
}
function filteredAsyncIterator(
iterator: AsyncIterableIterator<Uint8Array>,
filter: (output: string) => string,
): AsyncIterableIterator<Uint8Array> {
const encoder = new TextEncoder();
const decoder = new TextDecoder();
return pooledMap(1, iterator, (data: Uint8Array) => {
return Promise.resolve(
encoder.encode(filter(decoder.decode(data))),
);
});
}
// Processes ouptut from an interator (stderr, stdout, etc...)
async function processOutput(
iterator: AsyncIterable<Uint8Array>,
output?: "piped" | "inherit" | "null" | number,
which?: "stdout" | "stderr",
): Promise<string> {
const decoder = new TextDecoder();
let outputText = "";
for await (const chunk of iterator) {
if (output === "inherit" || output === undefined) {
if (which === "stdout") {
Deno.stdout.writeSync(chunk);
} else if (which === "stderr") {
Deno.stderr.writeSync(chunk);
} else {
info(decoder.decode(chunk), { newline: false });
}
}
const text = decoder.decode(chunk);
outputText += text;
}
return outputText;
}