Skip to content

Commit

Permalink
Handle output streams more robustly (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh authored Oct 1, 2024
1 parent 632859e commit edc4505
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 12 deletions.
2 changes: 2 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export default [
"@typescript-eslint/require-await": "off", // Temporary.
"@typescript-eslint/restrict-plus-operands": "off", // Temporary.
"@typescript-eslint/restrict-template-expressions": "off", // Temporary.
eqeqeq: "warn",
"no-duplicate-imports": "warn",
"no-multi-spaces": "warn",
"no-multiple-empty-lines": [
Expand All @@ -42,6 +43,7 @@ export default [
],
semi: "warn",
"sort-imports": "warn",
"no-useless-assignment": "warn",
},
},
];
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "graphviz-preview",
"version": "1.7.3",
"version": "1.7.4",
"publisher": "EFanZh",
"engines": {
"vscode": "^1.93.0"
Expand Down
34 changes: 28 additions & 6 deletions projects/extension/src/engines/dot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,46 @@ export function render(
const dotExtraArgs = configuration.dotExtraArgs;

return new Promise((resolve, reject) => {
// Configure output buffers.

const stdoutBuffers: Buffer[] = [];
const stderrBuffers: Buffer[] = [];

const getError = () => new Error(concatBuffers(stderrBuffers));
const resolveWithStdout = () => resolve(parseDocuments(concatBuffers(stdoutBuffers)));
const rejectWithStderr = () => reject(getError());
const rejectWithError = (error: Error) => reject(stderrBuffers.length === 0 ? error : getError());

// Configure process event handlers.

const process = child_process.spawn(dotPath, ["-T", "svg", ...dotExtraArgs], { cwd });

onCancel(process.kill.bind(process));

process.stdout.on("data", stdoutBuffers.push.bind(stdoutBuffers));
process.stderr.on("data", stderrBuffers.push.bind(stderrBuffers));
process.on("error", reject);
process.on("error", rejectWithError);

process.on("exit", (code) => {
if (code === 0) {
resolve(parseDocuments(concatBuffers(stdoutBuffers)));
const [output, handler] = code === 0 ? [stdout, resolveWithStdout] : [stderr, rejectWithStderr];

if (output.closed) {
handler();
} else {
reject(new Error(concatBuffers(stderrBuffers)));
output.on("close", handler);
}
});

// Configure stream event handlers.

const { stderr, stdout } = process;

stdout.on("data", stdoutBuffers.push.bind(stdoutBuffers));
stdout.on("error", rejectWithError);

stderr.on("data", stderrBuffers.push.bind(stderrBuffers));
stderr.on("error", rejectWithError);

// Begin to feed data to the process.

process.stdin.end(source);
});
}
Expand Down

0 comments on commit edc4505

Please sign in to comment.