Use stream.finished for detecting end of streams

There are some corner cases with streams that cause them to not emit
finish close or end events which in turn means that code liseting for
these events directly may end up hanging forever.  Use stream.finished
to detect end of streams which will handle these cases correctly.
This commit is contained in:
Hornwitser committed 2021-12-19 03:21:00 +01:00
1 parent 601ebba919
commit 170a3d3cf7
6 files changed
+51 -25

No files matched your search

+12 -2
View File
@@ -8,6 +8,8 @@
const events = require("events");
const fs = require("fs-extra");
const path = require("path");
const stream = require("stream");
const util = require("util");
const winston = require("winston");
const Transport = require("winston-transport");
const { LEVEL, MESSAGE } = require("triple-beam");
@@ -19,6 +21,8 @@ const libFileOps = require("./file_ops");
const { levels, logger } = require("./logging");
const libStream = require("./stream");
const finished = util.promisify(stream.finished);
/**
* Format a parsed Factorio message with colors
@@ -435,7 +439,13 @@ async function queryLog(logDirectory, filter, index) {
}
});
fileStream.pipe(lineStream);
await events.once(lineStream, "close");
try {
await finished(lineStream);
} catch (err) {
if (log.length < filter.limit || err.code !== "ERR_STREAM_PREMATURE_CLOSE") {
throw err;
}
}
if (log.length >= filter.limit) {
break;
@@ -456,7 +466,7 @@ async function migrateLogs(log, directory, pattern) {
async function switchOutput(newOutputPath) {
if (output) {
output.end();
await events.once(output, "close");
await finished(output);
}
output = fs.createWriteStream(newOutputPath, { flags: "ax" });