Skip to content

Commit 0040149

Browse files
fix(Viewer): crash if log too big
1 parent 7ea9a53 commit 0040149

1 file changed

Lines changed: 29 additions & 7 deletions

File tree

app/utils/local/scripts.js

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,42 @@ function commandExistsSync(execName) {
3030
});
3131
}
3232

33+
34+
const encoder = new TextEncoder();
35+
36+
function byteLength(str) {
37+
return encoder.encode(str).byteLength;
38+
}
39+
3340
function waitForReady(child, expectedResponse, signal) {
3441
// oxlint-disable-next-line promise/avoid-new
3542
return new Promise((resolve, reject) => {
3643
const readlineStdout = readline.createInterface({ input: child.stdout });
3744
const readlineStderr = readline.createInterface({ input: child.stderr });
3845

3946
let recentOutput = "";
47+
4048
function recordOutput(line) {
41-
recentOutput = `${recentOutput} ${line} \n`.slice(-MAX_ERROR_BUFFER_BYTES);
49+
const safeLine =
50+
byteLength(line) > MAX_ERROR_BUFFER_BYTES / 2
51+
? line.slice(0, MAX_ERROR_BUFFER_BYTES / 2) + "…[truncated]"
52+
: line;
53+
54+
recentOutput = `${recentOutput} ${safeLine}\n`;
55+
56+
while (byteLength(recentOutput) > MAX_ERROR_BUFFER_BYTES) {
57+
const newline = recentOutput.indexOf("\n");
58+
if (newline === -1) {
59+
recentOutput = "";
60+
break;
61+
}
62+
recentOutput = recentOutput.slice(newline + 1);
63+
}
4264
}
4365

4466
function cleanup() {
45-
readlineStdout.removeAllListeners();
46-
readlineStdout.close();
47-
readlineStderr.removeAllListeners();
48-
readlineStderr.close();
67+
readlineStdout.removeListener("line", onLine);
68+
readlineStderr.removeListener("line", onErrLine);
4969
child.removeListener("error", onError);
5070
child.removeListener("close", onClose);
5171
if (signal) {
@@ -58,6 +78,9 @@ function waitForReady(child, expectedResponse, signal) {
5878
recordOutput(line);
5979
if (line.includes(expectedResponse)) {
6080
cleanup();
81+
readlineStdout.on("line", (l) => console.log(`[${child.name}] ${l}`));
82+
readlineStderr.on("line", (l) => console.log(`[${child.name}] ${l}`));
83+
child.once("close", (code) => console.log(`[${child.name}] exited with code ${code}`));
6184
resolve(child);
6285
}
6386
}
@@ -77,8 +100,7 @@ function waitForReady(child, expectedResponse, signal) {
77100
cleanup();
78101
reject(
79102
new Error(
80-
`[${child.name}] exited with code ${code} before becoming ready.${
81-
recentOutput ? `\nRecent output:\n${recentOutput}` : ""
103+
`[${child.name}] exited with code ${code} before becoming ready.${recentOutput ? `\nRecent output:\n${recentOutput}` : ""
82104
}`,
83105
),
84106
);

0 commit comments

Comments
 (0)