Repository navigation
Expand file tree
/
Copy pathrender-process.ts
More file actions
61 lines (60 loc) · 1.88 KB
/
Copy pathrender-process.ts
File metadata and controls
61 lines (60 loc) · 1.88 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
import { spawn } from "node:child_process";
/** A disposable renderer: bounded input/output, hard deadline, no shell. */
export function renderProcess(
command: string,
args: string[],
input: string,
options: {
signal?: AbortSignal;
timeout: number;
maxBytes?: number;
},
): Promise<Buffer> {
options.signal?.throwIfAborted();
return new Promise((resolve, reject) => {
const child = spawn(command, args, { stdio: ["pipe", "pipe", "pipe"] });
const chunks: Buffer[] = [];
let length = 0,
errors = "",
failure: Error | undefined;
const stop = (error: Error) => {
failure ??= error;
child.kill("SIGKILL");
};
const abort = () =>
stop(
options.signal?.reason instanceof Error
? options.signal.reason
: new Error("render_cancelled"),
);
const timer = setTimeout(
() => stop(new Error("render_timeout")),
options.timeout,
);
options.signal?.addEventListener("abort", abort, { once: true });
if (options.signal?.aborted) abort();
child.stdout.on("data", (chunk: Buffer) => {
length += chunk.length;
if (length > (options.maxBytes ?? 64 * 1024 * 1024))
stop(new Error("render_output_too_large"));
else chunks.push(chunk);
});
child.stderr.on("data", (chunk: Buffer) => {
errors = (errors + chunk.toString()).slice(-8192);
});
child.stdin.on("error", (error) => {
if ((error as NodeJS.ErrnoException).code !== "EPIPE") stop(error);
});
child.on("error", (error) => {
failure ??= error;
});
child.on("close", (code) => {
clearTimeout(timer);
options.signal?.removeEventListener("abort", abort);
if (failure) reject(failure);
else if (code !== 0) reject(new Error(`render_failed: ${errors.trim()}`));
else resolve(Buffer.concat(chunks));
});
child.stdin.end(input);
});
}